I am trying to create a single point within a polygon using a class for use in an agent based 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. I am very new to python so this could be a limitation I am missing.
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.