[postgis-users] shp2pgsql inner/outer ring detection bug FIX

strk strk at freek.keybit.net
Thu Jan 2 23:36:51 PST 2003

strk wrote:
> [..] 
> I think the problem can be generally put in these words:
> 	- Shapefile file format requires shapefiles to contain
> 	  clockwise ordered vertex for outer rings and
> 	  counterclockwise ordered vertex for inner rings
> 	  (we are talking about POLYGON type shapes).
> 	- Postgis requires Polygons to have outer ring listed
> 	  first and inner rings (holes) listed after that.
> 	- shp2pgsql does its best to transform inner/outer 
> 	  information from one format to another.
> 	- *ASSUMING* shp2pgsql does not mess with vertex ordering
> 	  the resulting pgsql polygon records *SHOULD* have the
> 	  first ring defined by clockwise-ordered vertex and the
> 	  others by counterclockwise-ordered vertex.
> If the ASSUMPTION in the last statement is TRUE, then it will be
> easy to test wheater shp2pgsql is doing the right think just checking
> for the coherent consequence (first ring clockwise, other rings
> counterclockwie).
> [..] 

Ok, I made some checks by myself. The ASSUMPTION was true, shp2pgsql does not
mess with vertex orientation. It first finds all outer and inner rings checking
vertex orientation and then associates each inner ring to the outer ring it is
within using a PIP function (Point In Polygon). If an inner ring is found not 
to be inside any outer ring it is considered an outer ring itself. This is what
happened to me. In this case - I think - user should be noticed since this means
that the shapefile is "dirty" (as the Shapefile Format Specification states).

Anyway for my case shapefile was not "dirty". The PIP function was ...

Checking the same function in postgis_fn.c I've found the bug:
the edge loop check has to be i<(n-1) instead of i<n
Fixing this everything works fine.

Best regards,

More information about the postgis-users mailing list