hub/crs_conversion.py

30 lines
808 B
Python

import geopandas as gpd
from shapely.geometry import Polygon, MultiPolygon
from shapely.geometry.polygon import orient
input_file = 'data/selected_buildings.geojson'
output_file = 'data/selected_buildings_corrected.geojson'
gdf = gpd.read_file(input_file)
if gdf.crs is None:
gdf.set_crs(epsg=3857, inplace=True)
gdf = gdf.to_crs(epsg=4326)
def reorient_geometry(geom):
if geom.is_empty:
return geom
elif geom.geom_type == 'Polygon':
return orient(geom, sign=1.0)
elif geom.geom_type == 'MultiPolygon':
reoriented_polygons = [orient(polygon, sign=1.0) for polygon in geom.geoms]
return MultiPolygon(reoriented_polygons)
else:
return geom
gdf['geometry'] = gdf['geometry'].apply(reorient_geometry)
gdf.to_file(output_file, driver='GeoJSON')