Closed zyune closed 2 years ago
I will explain how above code works.
๐๐ปFirst, read file from directory, it's edible, we can put the file waiting to be read at any directory. It give us two geodataframe which is Maine_County
and Street_level
Maine_County = gpd.read_file(
'Maine_County_Boundary_Polygons_Dissolved_Feature.geojson')
Street_level = gpd.read_file(
'eligible-areas-2-22-layer-G8qemhB46k7dh-m1XZ2JM.zip')
Second, put the data we read from Maine_County_Boundary_Polygons_Dissolved_Feature.geojson
in to a dictionary. The key is county name, the value is each county's geometry.
maine_county_dict = {}
for i, row in Maine_County.iterrows():
maine_county_dict[row['COUNTY']] = row.geometry
maine_county_dict
๐๐ป This is how the dictionary look like
Third, find the centroid of polygon of Street_level, and set centroid as the geometry of Street_level. This is the soul of my function.
Street_level['centroid_column'] = Street_level.centroid
Street_level = Street_level.set_geometry('centroid_column')
Fourth, check if the centroid of street level grid is with in the area of county polygon. and split street level geodataframes into the specified sub geojson
The input of this function is county name, which should be a string
def store_county_street_to_file_v2(county_name):
gdf_list = []
for index, i in Street_level.iterrows():
if i.centroid_column.within(maine_county_dict[county_name]):
gdf_list.append(i)
gdf = gpd.GeoDataFrame(gdf_list)
del gdf['centroid_column']
gdf.set_geometry('geometry')
gdf.to_file('county/'+county_name+'.geojson')
Fifth, Run function store_county_street_to_file_v2
for every county in Maine.
for i in maine_county_dict:
store_county_street_to_file_v2(i)
That's all
This change fix a bug , it delete an unused variable.
Instruction on how to run my code.
county
in current directory,mkdir county
python split_by_maine_county.py