Skip to content

Instantly share code, notes, and snippets.

@thibaultmol
Created January 4, 2023 20:23
Show Gist options
  • Save thibaultmol/5d6b159dc5d95909f4190f4a94b78bac to your computer and use it in GitHub Desktop.
Save thibaultmol/5d6b159dc5d95909f4190f4a94b78bac to your computer and use it in GitHub Desktop.
Python script to split output from geojson-grid to seperate files
import json
from shapely.geometry import Polygon
# Generate geojson grid https://cityofaustin.github.io/geojson-grid/ and save it as grid.json
# Load the input file
with open("grid.json", "r") as f:
data = json.load(f)
# Iterate through the features
for i, feature in enumerate(data["features"]):
# Get the geometry of the feature
geometry = feature["geometry"]
if geometry["type"] == "Polygon":
# Create a Shapely Polygon object
poly = Polygon(geometry["coordinates"][0])
# Create a GeoJSON feature with the polygon as the geometry
geojson_feature = {
"type": "Feature",
"geometry": poly.__geo_interface__,
"properties": {}
}
# Save the feature to a separate file
with open(f"polygon_{i}.geojson", "w") as out:
json.dump(geojson_feature, out)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment