【发布时间】:2020-02-13 08:53:18
【问题描述】:
我正在尝试使用类在基于代理的模型中创建一个多边形内的单个点。
目前,我能够创建限制在多边形边界内的随机点,但不能创建多边形本身。我目前的代码似乎忽略了 while 循环中的 if 语句。我对 python 很陌生,所以这可能是我缺少的限制。
这是我当前的代码:
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
这段代码给出了一个无限循环。预期的行为是在给定布尔 True 值的情况下停止,并以 False 继续,直到出现 True 值。
【问题讨论】:
标签: python class gis polygon point