-
-
Save DakaiZhou/e5ecfae4e5ee993ce4387ba8a7c172f5 to your computer and use it in GitHub Desktop.
medium random location
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def vis_random_location_process(num_pt, geo_df): | |
""" | |
Generate num_pt random location coordinates . | |
:param num_pt INT number of random location coordinates | |
:param geo_df geopandas.geodataframe.GeoDataFrame contains geo data | |
""" | |
polygon = geo_df["geometry"] | |
# 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_1 = [] | |
y_1 = [] | |
x_2 = [] | |
y_2 = [] | |
while i < num_pt: | |
# generate random location coordinates | |
x_t = np.random.uniform(minx, maxx) | |
y_t = np.random.uniform(miny, maxy) | |
x_1.append(x_t) | |
y_1.append(y_t) | |
# further check whether it is in the city area | |
for p in polygon: | |
if Point(x_t, y_t).within(p): | |
x_2.append(x_t) | |
y_2.append(y_t) | |
del x_1[-1] | |
del y_1[-1] | |
i = i + 1 | |
break | |
rect1 = patches.Rectangle((minx, miny), maxx-minx, maxy-miny, linewidth=1, | |
edgecolor='r', facecolor='none') | |
rect2 = copy(rect1) | |
rect3 = copy(rect1) | |
rect4 = copy(rect1) | |
ax1 = geo_df.plot() | |
ax2 = geo_df.plot() | |
ax3 = geo_df.plot() | |
ax4 = geo_df.plot() | |
ax1.add_patch(rect1) | |
ax2.add_patch(rect2) | |
ax3.add_patch(rect3) | |
ax4.add_patch(rect4) | |
ax2.plot(x_1, y_1, "ro") | |
ax2.plot(x_2, y_2, "ro") | |
ax3.plot(x_1, y_1, "bo") | |
ax3.plot(x_2, y_2, "ro") | |
ax4.plot(x_2, y_2, "ro") | |
ax1.set_title("Step 1: Define boundaries") | |
ax2.set_title("Step 2: Generate rondom locations") | |
ax3.set_title("Step 3: Validate locations") | |
ax4.set_title("Step 4: Remove invalid locations") | |
plt.tight_layout() | |
ax1.figure.savefig("step1.png", dpi=150) | |
ax2.figure.savefig("step2.png", dpi=150) | |
ax3.figure.savefig("step3.png", dpi=150) | |
ax4.figure.savefig("step4.png", dpi=150) | |
plt.show() | |
vis_random_location_process(30, düsseldorf_df) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment