问题
I want to intersect polygons within a GeoDataFrame based on if conditions on column values. I made up the GeoDataFrame with "id", "yr" (year), "wk" (week).
# import libraries
import pandas as pd
import geopandas as gpd
import matplotlib.pyplot as plt
# txt 2 polygon
d = {'id': {0: 'a', 1: 'a', 2: 'a', 3: 'a', 4: 'a', 5: 'a', 6: 'a', 7: 'a', 8: 'a', 9: 'a', 10: 'b', 11: 'b', 12: 'b', 13: 'b', 14: 'b', 15: 'b', 16: 'b', 17: 'b', 18: 'b', 19: 'b', 20: 'c', 21: 'c', 22: 'c', 23: 'c', 24: 'c'}, 'yr': {0: 2017, 1: 2017, 2: 2017, 3: 2017, 4: 2017, 5: 2018, 6: 2018, 7: 2018, 8: 2018, 9: 2018, 10: 2017, 11: 2017, 12: 2017, 13: 2017, 14: 2018, 15: 2018, 16: 2018, 17: 2018, 18: 2018, 19: 2018, 20: 2017, 21: 2017, 22: 2017, 23: 2018, 24: 2018}, 'wk': {0: 3, 1: 4, 2: 5, 3: 6, 4: 7, 5: 5, 6: 7, 7: 10, 8: 11, 9: 15, 10: 4, 11: 5, 12: 6, 13: 8, 14: 5, 15: 11, 16: 13, 17: 14, 18: 15, 19: 18, 20: 5, 21: 7, 22: 9, 23: 11, 24: 15}, 'lat': {0: 41, 1: 46, 2: 39, 3: 43, 4: 51, 5: 50, 6: 45, 7: 37, 8: 43, 9: 41, 10: 43, 11: 45, 12: 48, 13: 58, 14: 49, 15: 60, 16: 41, 17: 43, 18: 42, 19: 58, 20: 40, 21: 42, 22: 48, 23: 50, 24: 46}, 'lon': {0: -78, 1: -73, 2: -66, 3: -75, 4: -68, 5: -81, 6: -80, 7: -69, 8: -71, 9: -76, 10: -78, 11: -70, 12: -69, 13: -76, 14: -77, 15: -68, 16: -72, 17: -76, 18: -80, 19: -82, 20: -72, 21: -60, 22: -62, 23: -74, 24: -72}}
df = pd.DataFrame.from_dict(d)
df['id_yr_wk'] = df['id'].map(str) + '_' + df['yr'].map(str) + '_' + df['wk'].map(str)
points = gpd.GeoDataFrame(df, geometry=gpd.points_from_xy(df.lon, df.lat))
buff = points.copy()
buff['geometry'] = buff.geometry.buffer(5)
print(buff.head())
# plot
fig, ax = plt.subplots()
buff.boundary.plot(ax=ax)
points.plot(column='id', ax=ax)
Here, thanks to @Matthew Borish's iteration for doing this. But this iteration intersects every polygon with the rest of the polygons in the GeoDataFrame.
In this example, if I want to save time and just want to intersect polygons for different "id" across the same year and the same week, how could I do that?
The final DataFrame should like this:
+--------------+--------------+---------------+
| id_yr_wk_1 | id_yr_wk_2 | intsct_area |
+--------------+--------------+---------------+
| a_2017_3 | b_2017_3 | some_values |
+--------------+--------------+---------------+
| a_2017_3 | c_2017_3 | some_values |
+--------------+--------------+---------------+
| b_2017_3 | c_2017_3 | some_values |
+--------------+--------------+---------------+
| ... | ... | ... |
来源:https://stackoverflow.com/questions/61801681/intersect-polygons-based-on-if-conditions-on-column-values