1

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:

enter image description here

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

enter image description here

0

One possible way is as follows. I am sure though, there are nicer ways!

def seedtoleech(seed_layer,leech_layer,seed_column):      

    # First, the index is reset.
    # Both indices will be transfered to the gpd.overlay() layer
    # to allow identifying overlaps.

    seed_layer = seed_layer.reset_index()
    leech_layer = leech_layer.reset_index()

    # Add area 
    leech_layer = add_area(leech_layer)
    seed_layer = add_area(seed_layer)

    # Create overlay layer
    overlay_layer  = gpd.overlay(seed_layer,leech_layer)

    # Add area of the overlay area
    overlay_layer = add_area(overlay_layer)

    # Add new columns to the overlay layer
    # The relative overlay
    # The relative_overlay multiplied with the seed value
    overlay_layer['area_rel'] = 0.0
    overlay_layer['seed_value'] = 0.0

    for i in range(len(overlay_layer)):
        overlay_layer.at[i,'area_rel'] = overlay_layer.at[i,'area']/overlay_layer.at[i,'area_2']
        overlay_layer.at[i,'seed_value'] = overlay_layer.at[i,'area_rel']*overlay_layer.at[i,seed_column]

    # seed_value is the area-weighted seed value

    overlay_layer = overlay_layer.drop(['index_1', 'area_1','area_2','area','area_rel','content_seed'], axis=1)

    # Dissolve the overlay layer by index_2 (the geometries of the leech layer) and some the leeched/seeded value
    merged_layer = overlay_layer.dissolve(by='index_2', aggfunc='sum')
    return(merged_layer)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.