Created
November 6, 2023 20:34
-
-
Save meyer1994/7ae8df22b2a1ac4d04ef17f05fab3e4f to your computer and use it in GitHub Desktop.
Example of intersection calculation using fiona and shapely
This file contains 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 json | |
import fiona | |
import shapely | |
import shapely.geometry | |
with fiona.open('data/test.geojson', 'r') as f: | |
geom = next(iter(f)) | |
geom = shapely.geometry.shape(geom.geometry) | |
with fiona.open('data/dashboard_indigenous-lands-static-layer.shp', 'r') as f: | |
geometries = [shapely.geometry.shape(i.geometry) for i in f] | |
polys = [i for i in geometries if i.geom_type == 'Polygon'] | |
multi = [i for i in geometries if i.geom_type == 'MultiPolygon'] | |
for i in multi: | |
for g in i.geoms: | |
polys.append(g) | |
multi = shapely.MultiPolygon(polys) | |
intersec = shapely.intersection(geom, multi) | |
area_geom = shapely.area(geom) | |
area_intersec = shapely.area(intersec) | |
with open('out.geojson', 'wt') as f: | |
json.dump(intersec.__geo_interface__, f) | |
print(area_intersec / area_geom) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment