Skip to content

Instantly share code, notes, and snippets.

@DakaiZhou
Last active November 1, 2021 09:56
Show Gist options
  • Save DakaiZhou/ad149a8e6553e7d30a940f56f64a0734 to your computer and use it in GitHub Desktop.
Save DakaiZhou/ad149a8e6553e7d30a940f56f64a0734 to your computer and use it in GitHub Desktop.
medium random location
def generate_random_location_within_ROI(num_pt, polygon):
"""
Generate num_pt random location coordinates .
:param num_pt INT number of random location coordinates
:param polygon geopandas.geoseries.GeoSeries the polygon of the region
:return x, y lists of location coordinates, longetude and latitude
"""
# define boundaries
bounds_all = polygon.bounds
minx = min(bounds_all.minx)
maxx = max(bounds_all.maxx)
miny = min(bounds_all.miny)
maxy = max(bounds_all.maxy)
i = 0
x = []
y = []
while i < num_pt:
# generate random location coordinates
x_t = np.random.uniform(minx, maxx)
y_t = np.random.uniform(miny, maxy)
# further check whether it is in the city area
for p in polygon:
if Point(x_t, y_t).within(p):
x.append(x_t)
y.append(y_t)
i = i + 1
break
return x, y
x, y = generate_random_location_within_ROI(30, düsseldorf_geo)
bounds_all = düsseldorf_df["geometry"].bounds
minx = min(bounds_all.minx)
maxx = max(bounds_all.maxx)
miny = min(bounds_all.miny)
maxy = max(bounds_all.maxy)
ax = düsseldorf_df.plot()
rect = patches.Rectangle((minx, miny), maxx-minx, maxy-miny, linewidth=1, edgecolor='r', facecolor='none')
ax.add_patch(rect)
ax.plot(x, y, "ro")
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment