Python: Polygon does not close (shapely
<p>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).</p>

<p>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.</p>

<p>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:</p>

<pre><code>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)]))
</code></pre>

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

<pre><code>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))
</code></pre>

<p>Any solution? </p>

<p>I think it is related with the third coordinate. In the documentation (<a href="http://toblerity.org/shapely/manual.html" rel="nofollow">shapely doc</a>), it tells: </p>

<blockquote>
  <p>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.</p>
</blockquote>

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

<pre><code>[(620, 420), (620, 420), (620, 40), (620, 40)]
[(620, 40), (620, 420), (620, 420), (620, 40)]
</code></pre>

<p>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.</p>

python shapely