Let's assume I have a georeferenced data set seed_layer.geojson
and a mask layer leech_layer.geojson
.
test_leech.geojson:
{
"type": "FeatureCollection",
"features": [
{ "type": "Feature", "properties": { "content_leech": 1000 }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 0.0, 1.0 ], [ 0.0, 3.0 ], [ 3.0, 3.0 ], [ 3.0, 1.0 ] ] ] } },
{ "type": "Feature", "properties": { "content_leech": 2000 }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 4.0, 1.0 ], [ 4.0, 3.0 ], [ 6.0, 3.0 ], [ 6.0, 1.0 ]] ] } }
]
}
test_seed.geojson:
{
"type": "FeatureCollection",
"features": [
{ "type": "Feature", "properties": { "content_seed": 10 }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 0.0, 0.0 ], [ 0.0, 2.0 ], [ 2.0, 2.0 ], [ 2.0, 0.0 ] ] ] } },
{ "type": "Feature", "properties": { "content_seed": 10 }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 0.0, 2.0 ], [ 0.0, 4.0 ], [ 2.0, 4.0 ], [ 2.0, 2.0 ]] ] } },
{ "type": "Feature", "properties": { "content_seed": 10 }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 2.0, 0.0 ], [ 2.0, 2.0 ], [ 4.0, 2.0 ], [ 4.0, 0.0 ] ] ] } },
{ "type": "Feature", "properties": { "content_seed": 200 }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 2.0, 4.0 ], [ 4.0, 4.0 ], [ 4.0, 2.0 ], [ 2.0, 2.0 ] ] ] } },
{ "type": "Feature", "properties": { "content_seed": 10 }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 4.0, 0.0 ], [ 6.0, 0.0 ], [ 6.0, 2.0 ], [ 4.0, 2.0 ] ] ] } },
{ "type": "Feature", "properties": { "content_seed": 10 }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 6.0, 2.0 ], [ 6.0, 4 ], [ 4.0, 4.0 ], [ 4.0, 2.0 ] ] ] } }
]
}
I plot both layers ...
import geopandas as gpd
leech_layer = gpd.read_file('../temp/test_leech.geojson')
seed_layer = gpd.read_file('../temp/test_seed.geojson')
base = seed_layer.plot(figsize=(6,3), column='content_seed', legend=True, cmap='Accent', edgecolor='black', alpha=0.75)
base.text(1,1, '0 (10)')
base.text(1,3, '1 (10)')
base.text(3,1, '2 (10)')
base.text(3,3, '3 (200)')
base.text(5,1, '4 (10)')
base.text(5,3, '5 (10)')
leech_layer.plot(ax=base, color='red', alpha=0.5)
... and receive the following figure:
What's the best practice in GeoPandas to aggregate values in a column seed_layer.geojson
(e.g. content_seed
) to the geometries in leech_layer.geojson
as the sumproduct of overlapping area and the value in the content_seed
column?
Example: The red shape on the left side would be assigned the following value:
(2*20+2*20+1*10+1*200)/6 = 41.67