9

I have two square polygons df1 and df2. Both polygons are assigned a value of 10 (df1) and 20 (df2) in the "content" column. Let's assume, I have a circle with the midpoint 1.5,1.5 and a radius of 1.

How can I assign to this circle a new value "content", based on the overlap with the other polygons?

For example, one third of the circle overlaps with df1 and one third of the circle overlaps with df2. In this case, the circle would then obtain 10 * 1/3 + 20 * 1/3 as a value for "content".

from shapely.geometry import Polygon
import geopandas

polys1 = geopandas.GeoSeries([Polygon([(0,0), (2,0), (2,2), (0,2)])])

polys2 = geopandas.GeoSeries([Polygon([(1,1), (3,1), (3,3), (1,3)])])

df1 = geopandas.GeoDataFrame({'geometry': polys1, 'df1':[1], 'content':[10]})

df2 = geopandas.GeoDataFrame({'geometry': polys2, 'df2':[1], 'content':[20]})

ax = df1.plot(color='red');
df2.plot(ax=ax, color='green', alpha=0.5);

enter image description here

1 Answer 1

9

enter image description here

from shapely.geometry import Polygon, Point
import geopandas

polys1 = geopandas.GeoSeries([Polygon([(0,0), (2,0), (2,2), (0,2)])])
polys2 = geopandas.GeoSeries([Polygon([(1,1), (3,1), (3,3), (1,3)])])

df1 = geopandas.GeoDataFrame({'geometry': polys1, 'df1':[1], 'content':[10]})
df2 = geopandas.GeoDataFrame({'geometry': polys2, 'df2':[1], 'content':[20]})

#Geometries
geom1 = df1.iloc[0].geometry
geom2 = df2.iloc[0].geometry
circle = Point(1.5, 1.5).buffer(1)

#Areas
area_common = geom1.intersection(geom2).area
area1 = circle.intersection(geom1).area
area2 = circle.intersection(geom2).area

#Ratio excluding brown area
ratio1 = (area1 - area_common) / circle.area #ratio of blue area
ratio2 = (area2 - area_common) / circle.area #ratio of yellow area

#Ratio including brown area
#ratio1 = area1 / circle.area
#ratio2 = area2 / circle.area

content = df1.iloc[0].content * ratio1 + df2.iloc[0].content * ratio2    
df3 = geopandas.GeoDataFrame({'geometry': circle, 'df3':[1], 'content':content})

ax = df1.plot(color='red')
df2.plot(ax=ax, color='green', alpha=0.5);
df3.plot(ax=ax, color='yellow', alpha=0.5);
0

Your Answer

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

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