获取多边形角内的随机点



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

我得到了这个链接

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

但它是一个JavaScript代码,但如何进入Java代码。

在 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)

最新更新