Last active
July 2, 2024 02:02
-
-
Save robbibt/157dd72ec8485e27b46de73b43a786ff to your computer and use it in GitHub Desktop.
Generating Voronoi polygons from polygon inputs (not points)
This file contains hidden or 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
import momepy | |
import geopandas as gpd | |
# Read data and convert to projected CRS | |
gdf = gpd.read_file( | |
"/gdata1/projects/coastal/cem/sediment_compartments/compartments_checklist_final.geojson" | |
).to_crs("EPSG:3577") | |
col = "ID_Seconda" | |
# Create a limit for tessellation (join all polygons and buffer that) | |
limit = gdf.simplify(1000).buffer(50000).unary_union | |
# Run tesselation on specific column | |
# shrink: distance for negative buffer to generate space between adjacent polygons | |
# segment: maximum distance between points after discretization | |
tess = momepy.Tessellation( | |
gdf, unique_id=col, limit=limit, segment=500, shrink=500 | |
) | |
# Get the resulting GeoDataFrame | |
result = tess.tessellation | |
# Join tesselated data back into dataframe | |
joined_gdf = ( | |
gdf.drop("geometry", axis=1) | |
.set_index(col) | |
.join(result.set_index(col)) | |
) | |
gdf_voronoi = gpd.GeoDataFrame(joined_gdf, geometry="geometry") | |
# Export to GeoJSON | |
gdf_voronoi.to_crs("EPSG:4326").to_file( | |
"/gdata1/projects/coastal/cem/sediment_compartments/compartments_checklist_final_voronoi.geojson" | |
) | |
# Plot | |
ax = gdf_voronoi.reset_index().plot(column=col) | |
gdf.plot(ax=ax, column=col, alpha=0.5, linewidth=1, edgecolor="black") |
Author
robbibt
commented
Jul 2, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment