0

I am having trouble calculating the area of an intersection of two shapely GeometryCollections which have been created from two GeoJSON multipolygons that represent drivetimes.

I want to check for two possible scenarios and have set up test data.

In the first scenario, one drivetime is wholly within the other drivetime and I have confirmed this is the case through rendering the drivetimes on a folium map in Jupyter. I assume that in this case I can just use ".within" and, if it returns "True", I can apportion 100% of the smaller catchment to the area shared?

But in the second scenario, visually far more than 95% of the smaller catchment is clearly within the larger one when the GeoJSON shapes are rendered on the folium map but when I use the following code, it only registers that 49% is within the area. Is the conversion from GeoJSON multipolygon to Shapely that I am using, losing the relative scale of the actual catchments?

My code and outputs, along with photos of the catchments are below, if someone could tell me where I am going wrong in calculating the size of the intersection, that would be great.

from shapely.geometry import GeometryCollection, shape

catchment1 = GeometryCollection([shape(value[0][1]['data']['features'][0]['geometry']).buffer(0) for feature in features])
catchment2 = GeometryCollection([shape(value[1][1]['data']['features'][0]['geometry']).buffer(0) for feature in features])

if catchment2.within(catchment1):
    print("Within")
else:
    print("Not Within")
    if catchment1.intersects(catchment2):
        print("intersects")
        print(f"Shape 1 Area: {catchment1.area}")
        print(f"Shape 2 Area: {catchment2.area}")
        print(f"Shape 2 Area as a percentage of Shape1 Area {(catchment2.area/catchment1.area) * 100}")


        print(f"Intersection {catchment1.intersects(catchment2)}")
        print(f"Intersection Area1 : {catchment1.intersection(catchment2).area}")
        print(f"Intersection Area1 : {catchment2.intersection(catchment1).area}")

        print(f"Percentage of area 2 in intersection {(catchment1.intersection(catchment2).area/catchment2.area)*100}")
    else:
        print("does not intersect")

And the output I am getting is:

Not Within
intersects
Shape 1 Area: 0.026273957817
Shape 2 Area: 0.015491099132000195
Shape 2 Area as a percentage of Shape1 Area 58.959899532064455
Intersection True
Intersection Area1 : 0.0076861602514691355
Intersection Area1 : 0.0076861602514691225
Percentage of area 2 in intersection 49.616622977976554

Catchment 1 enter image description here

Catchment 2 enter image description here

1 Answer 1

0

When I remove ".buffer(0) for feature in features]" from the shape command, the relative size of the areas matches the map accurately.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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