4

I don't know how to add two polygons in Python using Shapely.

By adding I mean for instance if I was adding two squares with height 4 and width 2, and they had the same coordinates, it should return a square that has height 8 and width 2.

I have tried using MultiPolygons and also using union between the two polygons but I'm not able to get the desired accumulative height result.

Does anybody know how to do what I described? Or are there any other Python modules out there that will allow me to do the same?

RGS2014
  • 71
  • 1
  • 4
  • You mean like staking blocks, like Tetris? – Mike T Oct 15 '14 at 21:56
  • Not really. When the two polygons are added the new polygon should have the combined height of the two polygons put together. I'm not sure if that clears it up, but I'm not sure how to effectively describe my issue. – RGS2014 Oct 16 '14 at 20:28

1 Answers1

5

Have you tried the union function? Beware, if the polygons do not intersect, it will return a MultiPolygon. An example:

from shapely.geometry import Polygon

p1 = Polygon([(0,0),(1,0),(1,1),(0,1)])
p2 = Polygon([(0,1),(1,1),(2,1),(2,2)])
newp = p1.union(p2)
eguaio
  • 3,754
  • 1
  • 24
  • 38