3

When creating a polygon using the Shapely, I push 4 vertices in the the polygon function. The output should be a tuple with 5 elements (the first vertex is doubled, and described as the last one too).

It seems, however, that the order of the input vertices I pass to the function has impact the result: sometime the polygon is described with 5 vertices (as it should) and sometimes with 4 - meaning, it`s not a closed polygon (or in other words - it is not a polygon at all) It must be some bug.

In the following example, the only difference between poly1 and poly 2 is the order of the vertices I pass. The direction is exactly the same though:

from shapely.geometry import Polygon

print ('poly1 = ', Polygon([(620, 420, 500), (620, 420, 0), (620, 40, 0),(620, 40, 500)]))
print ('poly2 = ',Polygon([(620, 40, 500), (620, 420, 500), (620, 420, 0), (620, 40, 0)]))

However, the result is different - one is a closed polygon, the other is open. The type of both, btw, is still a shapely polygon.

poly1 =  POLYGON Z ((620 420 500, 620 420 0, 620 40 0, 620 40 500, 620 420 500))
poly2 =  POLYGON Z ((620 40 500, 620 420 500, 620 420 0, 620 40 0))

Any solution?

Georgy
  • 12,464
  • 7
  • 65
  • 73
Yair
  • 859
  • 2
  • 12
  • 27

1 Answers1

4

I think it is related with the third coordinate. In the documentation (shapely doc), it tells:

A third z coordinate value may be used when constructing instances, but has no effect on geometric analysis. All operations are performed in the x-y plane.

This means that shapely simply does not process the z coordinate. In your example, if you erase the z coordinate, you get:

[(620, 420), (620, 420), (620, 40), (620, 40)]
[(620, 40), (620, 420), (620, 420), (620, 40)]

When you pass a linear string to build a polygon, shapely Polygon constructor checks if the last point is equal to the first one. If not, the point is added to get a linear ring. In the second case, as far as shapely can see, the last coordinate is already repeated and there is no need to add any other point.

Kadir Şahbaz
  • 418
  • 7
  • 21
eguaio
  • 3,754
  • 1
  • 24
  • 38