3

I have Shapely polygon geometries, A circle(c) and list of polygons within the circle. I want to find the union of the circle with the intersections. (circle + intersections) circle Polygons output

I was trying to make a MultiPolygon of difference and intersection:

poly_list = [poly_N, poly_E]

# finding the intersection of circle and polys
intersec_list = []
for poly in poly_list:
    intersec_list.append(circle.intersection(poly))

MP_intersec = MultiPolygon(intersec_list)

# finding the difference of circle and polygons
diff_list = []
for inter in intersec_list:
    diffi = c.difference(inter)

MP_diff = MultiPolygon(diff_list)

# find the union of circle and intersection
# output: a MultiPolygon or a geometric collection with 4 polygons and the difference of circle.  

MP_out = MultiPolygon([MP_diff, MP_intersec])

but MP_out gives invalid geometry probably because I have overlapping polygon as well.

9
  • 2
    Intersection isn't working because you want something else. Do you want a union or the union of the circle with the intersection? This is the sort of question that screams for a picture.
    – Vince
    May 31 '20 at 4:22
  • I agree with @Vince, a picture often says a thousand words! I too would say it is a Union you want to do.
    – Hornbydd
    May 31 '20 at 13:39
  • @Vince Yes I want to find the union of circle with the intersection
    – FJ_Abbasi
    May 31 '20 at 18:13
  • 1
    By definition, the union of a shape and any shape that it had previously been intersected is the shape itself. A multipolygon cannot be composed of exterior rings that overlap at more than a point.
    – Vince
    May 31 '20 at 19:46
  • Please add a picture which shows what exactly you need. I recommend you to read Shapely Manual. It's hard to tell what really you need just by reading your question text and comments. Jun 1 '20 at 0:50
4

As far as I know, there is no a specific method performing what you want. The following script creates a geometry collection including the circle and intersected areas.

from shapely.geometry import Point, Polygon, GeometryCollection

# sample geometries. c: circle, r*: rectangles
c = Point(5, 5).buffer(3)
r1 = Polygon([(1, 6), (1, 9), (9, 9), (9, 6)])
r2 = Polygon([(9, 4), (9, 1), (1, 1), (1, 4)])
rectangles = [r1, r2]

geoms = [c] # create a list including circle
for r in rectangles:
    # add intersection to the list
    geoms.append(c.intersection(r))

# create shapely GeometryCollection
geom_collection = GeometryCollection(geoms)

enter code here

Then, you can get the geometries in the collection.

geom_collection.is_valid
# OUT:
# True

geom_collection.geoms
# OUT:
# <shapely.geometry.base.HeterogeneousGeometrySequence at ...>

list(geom_collection.geoms)
# OUT:
# [<shapely.geometry.polygon.Polygon at ...>, 
#  <shapely.geometry.polygon.Polygon at ...>,
#  <shapely.geometry.polygon.Polygon at ...>]

# OR use index
geom_collection.geoms[0] # gives the circle, because it is the first element
3
  • I have overlapping polygons so MultiPolygon is not working.
    – FJ_Abbasi
    May 31 '20 at 22:42
  • Thank you it is very helpful. What if we want a geometry collection of intersected areas and difference of circle?
    – FJ_Abbasi
    Jun 2 '20 at 12:44
  • For differences of circle, change c.intersection(r) into r.difference(c). If you want the collection not to include the circle, set geoms = [] (empty list) instead of geoms = [c]. Jun 2 '20 at 13:09

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.