Sunday, 4 March 2018

Getting intersection of multiple polygons efficiently in Python


I would like to get the intersection of multiple polygons. Using Python's shapely package, I can find the intersection of two polygons using the intersection function. Is there a similar efficient function for obtaining the intersection of multiple polygons?


Here is a code snippet to understand what I mean:


from shapely.geometry import Point

coord1 = ( 0,0 )

point1 = Point(coord1)
circle1 = point1.buffer(1)

coord2 = ( 1,1 )
point2 = Point(coord2)
circle2 = point2.buffer(1)

coord3 = ( 1,0 )
point3 = Point(coord3)
circle3 = point3.buffer(1)


An intersection of two circles can be found by circle1.intersection(circle2). I can find the intersection of all three circles by circle1.intersection(circle2).intersection(circle3). However, this approach is not salable to a large number of polygons as it requires increasingly more code. I would like a function that takes an arbitrary number of polygons and returns their intersection.




No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...