简体   繁体   English

在 class 内的多边形内创建随机点

[英]Create random points within a polygon within a class

I am trying to create a single point within a polygon using a class for use in an agent based model.我正在尝试使用 class 在多边形内创建一个点,以用于基于代理的 model。

Currently I am able to create random points constrained to the bounds of the polygon, but not the polygon itself.目前,我能够创建受限于多边形边界的随机点,但不能创建多边形本身。 My code at present appears to ignore the if statement within the while loop.我目前的代码似乎忽略了 while 循环中的 if 语句。 I am very new to python so this could be a limitation I am missing.我对 python 很陌生,所以这可能是我缺少的限制。

Here is my current code:这是我当前的代码:

import geopandas as gpd
import matplotlib.pyplot as plt
import random
import pandas as pd

bounds = gpd.read_file("./data/liverpool_bounds.gpkg")


class Agent():
    def __init__(self, bounds):
        x_min, y_min, x_max, y_max = bounds.total_bounds

        counter = 0
        while counter != 1:
            x = random.uniform(x_min, x_max)
            y = random.uniform(y_min, y_max)
            df = pd.DataFrame({'x': [x], 'y': [y]})
            self.agent = gpd.GeoDataFrame(
                df, geometry=gpd.points_from_xy(df.x, df.y))

            if self.agent.within(bounds) is True:
                counter = 1

            # counter does not increase
            print(counter)
            # gives both True and False
            print(self.agent.within(bounds))


Agent(bounds).agent

This code gives an infinite loop.这段代码给出了一个无限循环。 Expected behavior would be to stop given a Boolean True value, and to continue with False, until a True value.预期的行为将是停止给定 Boolean True 值,并继续 False,直到 True 值。

Don't use the counter variable, but a break statement when the point is sampled within the polygon.不要使用 counter 变量,而是在多边形内对点进行采样时使用 break 语句。 The counter variable will always be one on exit so this does not carry new information.计数器变量在退出时将始终为 1,因此它不携带新信息。 I'm not really familiar with the Geopandas library, but you can achieve a solution with Shapely , which is a very nice library imo.我对 Geopandas 库不是很熟悉,但是您可以使用Shapely实现解决方案,这是一个非常好的库 imo。 With this program structure your object becomes more generally useable.使用此程序结构,您的 object 变得更普遍可用。

from shapely.geometry import Point, Polygon
import random


bounds = [(0, 0), (1, 0), (1, 1), (0, 1)]


class Agent():
    def __init__(self, bounds):
        self.polygon = Polygon(bounds)

        # implement your object wide dataframe here to which you can append

    def add_random_point(self):
        xmin, ymin, xmax, ymax = self.polygon.bounds
        while True:
            x = random.uniform(xmin, xmax)
            y = random.uniform(ymin, ymax)

            if Point(x, y).within(self.polygon):
                # if this condition is true, add to a dataframe here

                print(x, y)
                break


obj = Agent(bounds)
obj.add_random_point()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM