Polygon¶
Create, inspect, and serialize polygon geometries with optional holes (interior rings).
Constructor¶
proc createPolygon*(ctx: var GeosContext; shell: LinearRing; holes: openArray[LinearRing] = []): Polygon
Creates a Polygon from a shell LinearRing and zero or more hole LinearRings.
Ownership transfer. The shell and all holes transfer ownership to the new polygon. After calling createPolygon, the passed-in ring handles are neutralized (set to nil) — do not use those variables again.
var ctx = initGeosContext()
let shell = ctx.createLinearRing([(0.0, 0.0), (10.0, 0.0), (10.0, 10.0), (0.0, 10.0), (0.0, 0.0)])
let poly = ctx.createPolygon(shell)
# `shell` handle is now neutralized — cannot be used further
Polygon with holes¶
Pass one or more interior ring LinearRings as the holes parameter:
var ctx = initGeosContext()
let shell = ctx.createLinearRing([(0.0, 0.0), (10.0, 0.0), (10.0, 10.0), (0.0, 10.0), (0.0, 0.0)])
let hole = ctx.createLinearRing([(2.0, 2.0), (8.0, 2.0), (8.0, 8.0), (2.0, 8.0), (2.0, 2.0)])
let poly = ctx.createPolygon(shell, [hole])
# Both `shell` and `hole` handles are now neutralized
Accessors¶
exteriorRing¶
Returns a clone of the exterior ring (shell) as a LinearRing. The caller owns the result. Modifications to the returned ring do not affect the polygon.
numInteriorRings¶
Returns the number of interior rings (holes). Returns 0 for a polygon with no holes.
interiorRingN¶
Returns a clone of the interior ring at 0-based index n. Raises GeosGeomError if n is out of bounds.
if poly.numInteriorRings() > 0:
let firstHole = poly.interiorRingN(0)
echo firstHole.numCoordinates()
String representation¶
Returns a summary string showing the number of holes:
let simple = ctx.createPolygon(shell)
echo simple # "Polygon(0 holes)"
let withHole = ctx.createPolygon(shell, [hole])
echo withHole # "Polygon(1 holes)"
Full example¶
import nimgeos
var ctx = initGeosContext()
# Shell: 10×10 square
let shell = ctx.createLinearRing([(0.0, 0.0), (10.0, 0.0), (10.0, 10.0), (0.0, 10.0), (0.0, 0.0)])
# Hole: 6×6 inner square
let hole = ctx.createLinearRing([(2.0, 2.0), (8.0, 2.0), (8.0, 8.0), (2.0, 8.0), (2.0, 2.0)])
let poly = ctx.createPolygon(shell, [hole])
echo poly # "Polygon(1 holes)"
echo poly.exteriorRing() # LinearRing (5 coords)
echo poly.numInteriorRings() # 1
let inner = poly.interiorRingN(0)
echo inner.numCoordinates() # 5