【问题标题】:Get random points within polygon corners?在多边形角内获取随机点?
【发布时间】:2019-03-28 07:16:26
【问题描述】:

我在谷歌地图中画了一个多边形。我想在多边形边界内生成随机坐标以在多边形内添加一些标记。我该怎么做?

我有这个链接

https://gis.stackexchange.com/questions/163044/mapbox-how-to-generate-a-random-coordinate-inside-a-polygon

但它是一个javascript代码但是如何进入java代码。

【问题讨论】:

  • 我没听懂,你想在 Poligon 边界内生成一个点吗?
  • 是的,我想在多边形内添加标记?我有经纬度坐标

标签: android google-maps polygon


【解决方案1】:

在 Python 中,您可以在如下定义的多边形中生成随机坐标

import numpy as np
import random

from shapely.geometry import Polygon, Point


poly = Polygon([(23.789642, 90.354714), (23.789603, 90.403000), (23.767688, 90.403597),(23.766510, 90.355448)])

def random_points_within(poly, num_points):
    min_x, min_y, max_x, max_y = poly.bounds

    points = []

    while len(points) < num_points:
        random_point = Point([random.uniform(min_x, max_x), random.uniform(min_y, max_y)])
        if (random_point.within(poly)):
            points.append(random_point)

    return points


points = random_points_within(poly,1000)

for p in points:
    print(p.x,",",p.y)

【讨论】:

  • 很好的解决方案,但如果 n_points >= 1000 则效率低下。也许还有一些额外的限制?
猜你喜欢
  • 2011-03-21
  • 2020-02-13
  • 2016-01-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多