GID_HuggingFace / modules /geojson_processor.py
giho905e's picture
Upload 30 files
84e78bb
raw
history blame
2.12 kB
import geopandas as gpd
def geojson_processor_to_csv(geojson_data, output_file):
"""
Procces and Convert GeoJSON data to a csv.
Args:
geojson_data (str): Parsed GeoJSON data path.
output_file (str): Name of the output CSV file.
Returns:
pd.DataFrame: Pandas DataFrames containing the GeoJSON features.
"""
# split geometries from the DataFrame to reduce processing time in later steps
geom_df = gpd.GeoDataFrame(geojson_data["geometry"], crs="EPSG:4326")
# get geometry bounds
df_bounds = geom_df['geometry'].bounds
# create bbox column from bounds
geom_df['bbox'] = list(zip(df_bounds['minx'], df_bounds['miny'], df_bounds['maxx'], df_bounds['maxy']))
print('bboxes added to df.')
# calculate geometry centroids
geom_df['centroid'] = geom_df['geometry'].centroid
print('centroids added to df.')
# find neighbors
geom_df['neighbors'] = None
# Iterate through the GeoDataFrame to find neighbors
for index, row in geom_df.iterrows():
neighbors = []
for other_index, other_row in geom_df.iterrows():
if index != other_index and row['geometry'].touches(other_row['geometry']):
neighbors.append(other_row['ID'])
geom_df.at[index, 'neighbors'] = neighbors
# save df as csv
geom_df.to_csv(output_file + '_geom', index=False)
print('geometry file saved')
# assign unique string identifiers to each row based on its position in the DataFrame
geojson_data["geometry"] = [output_file + '_geom;' + str(i) + ";0" for i in range(geojson_data.shape[0])]
geojson_data["bbox"] = [output_file + '_geom;' + str(i) + ";1" for i in range(geojson_data.shape[0])]
geojson_data["centroid"] = [output_file + '_geom;' + str(i) + ";2" for i in range(geojson_data.shape[0])]
geojson_data["neighbors"] = [output_file + '_geom;' + str(i) + ";3" for i in range(geojson_data.shape[0])]
# save df as csv
geojson_data.to_csv(output_file + '_main', index=False)
print('main file saved.')
return 'Processing done and saved'