shprewind
Overview
shprewind (code/shapelib-1.3.0a2/shprewind.c) is a command-line geometry repair utility authored by Frank Warmerdam that validates and resets the vertex winding order of rings in polygon shapefiles (SHPT_POLYGON, SHPT_POLYGONZ, SHPT_POLYGONM) to conform strictly to the ESRI Shapefile Technical Description.
According to the ESRI Shapefile specification, multi-ring polygons distinguish exterior boundaries from interior holes (βislandsβ vs. βlakesβ) solely by vertex winding orientation:
- Outer (Exterior) Rings must be wound clockwise (when viewed in a standard Cartesian coordinate system where $+X$ is East and $+Y$ is North).
- Inner (Hole) Rings must be wound counter-clockwise.
When third-party converters or hand-digitized scripts produce polygons with reversed ring orientations, GIS engines (such as ArcGIS, QGIS, or CARIS) may misinterpret outer boundaries as negative-area holes or fail during spatial intersection operations. shprewind reads every shape from <in_shp_file>, invokes SHPRewindObject() to test and reverse any improperly wound rings, and writes the corrected geometries to <out_shp_file>.
Usage
shprewind <in_shp_file> <out_shp_file>
[!NOTE]
shprewindonly copies and rewinds the.shpand.shxgeometry files. If<in_shp_file>has an associated.dbfattribute table (and.prjprojection file), copy<in_basename>.dbfto<out_basename>.dbfalongside runningshprewindso the output shapefile retains its attributes.
Positional Arguments & Exit Codes
| Argument / Exit Code | Description |
|---|---|
<in_shp_file> | Required. Source shapefile path/basename (opened read-only via SHPOpen(argv[1], "rb")). |
<out_shp_file> | Required. Destination shapefile path/basename (created via SHPCreate(argv[2], nShapeType)). |
Exit 0 | Completed successfully and printed <nInvalidCount> objects rewound.. |
Exit 1 | Missing arguments (argc != 3), or SHPOpen / SHPCreate failed. |
Internal Ring-Winding Algorithm (SHPRewindObject in shpopen.c)
For each entity i in 0 .. nEntities - 1:
shprewindreads the feature viapsShape = SHPReadObject(hSHP, i).- Calls
SHPRewindObject(hSHP, psShape)(code/shapelib-1.3.0a2/shpopen.c, lines 2036β2205):- If
psShape->nSHPTypeis notSHPT_POLYGON,SHPT_POLYGONZ, orSHPT_POLYGONM(or hasnVertices == 0ornParts == 0), returns0immediately. - Step A (Inner vs. Outer Ring Classification): For each ring
iOpRing(0 .. nParts - 1), picks a test point strictly on an edge segment ofiOpRingand runs a ray-casting point-in-ring test against all other ringsiCheckRingin the sameSHPObject. If the point is enclosed by an odd number of other rings,iOpRingis classified as an Inner Ring (bInner = TRUE); otherwise it is an Outer Ring (bInner = FALSE). - Step B (Signed Area & Winding Check): Computes the shoelace signed sum $\sum (X_i Y_{i+1} - X_{i+1} Y_i)$ around
iOpRing. - Step C (In-Place Reversal): If an Outer Ring is counter-clockwise (
dfSum > 0) or an Inner Ring is clockwise (dfSum < 0), reverses the ringβspadfX,padfY,padfZ, andpadfMarrays in-place and incrementsnAltered.
- If
- Writes the corrected object to
hSHPOutviaSHPWriteObject(hSHPOut, -1, psShape)and freespsShape. - Closes both handles and prints
<nInvalidCount> objects rewound..