API conventions¶
gometry follows a consistent set of naming, typing, and protocol rules across every geometry, CRS, grid, and index operation.
Naming and call shape¶
- Properties, methods, and free functions — facts are properties
(
geom.area,geom.bounds), unary operations are methods (geom.buffer(1),geom.to_space(target)), and binary relationships are free functions (gm.intersects(a, b),gm.distance(a, b)). Measurement overrides use free functions (gm.area,gm.length) withmetric=andunit=. - Immutable updates and conversions —
with_<field>returns a modified copy without moving coordinates (with_space);to_<format>serializes or transforms (to_space,to_wkb);from_<format>parses.force_2dandforce_3dchange coordinate dimensions. - Whole-collection
_allfunctions — array methods run row by row (arr.polygonize()), while_allfree functions pool an entire sequence (union_all,polygonize_all). - Constructors and grids — classes construct scalars (
Point,LineString); plural functions build packed arrays (points,line_strings). Grid functions useh3_,s2_,geohash_, andtile_prefixes with domain depth names (resolution,level,precision,zoom). - Typed output options — keywords such as
CRS.select alternate representations under one name and narrow the static return type.to_projjson( as_dict= True)
Scalar, array, and ragged outputs¶
Operations accept one value or a sequence under one name (for example,
from_wkt). Binary operations broadcast a scalar against an
array or pair equal-length arrays row by row; unequal array lengths raise
GeometryError.
- Dense numeric results are read-only NumPy
ndarrays. - Geometry batches are
GeometryArray. - Variable-length per-row results are
ListArray(a missing row isNone; a present row with no matches is an empty group). - Text and diagnostics return Python
lists when no dense shape fits.
Type annotations preserve geometry kind, nullability, and scalar vs. array
outputs (for example, Point.buffer returns Polygon; buffering a nullable
array preserves None rows). See Arrays & performance.
Python protocols¶
Geometries and arrays implement standard Python protocols: a & b / a | b /
a - b / a ^ b overlays, str(geom) / f"{geom:.2f}" / f"{geom:x}"
formatting, bool(geom) (falsy when empty), structural match/case patterns,
hashing, and sequence indexing/slicing/iteration.
import gometry as gm
geom = gm.from_wkt("POINT (1.20517 5)")
print(str(geom), "|", f"{geom:.2f}", "|", bool(gm.from_wkt("POINT EMPTY")))
match geom:
case gm.Point(x, y):
print("matched point:", x, y)
arr = gm.GeometryArray([gm.Point(0, 0), gm.Point(3, 4)])
print(gm.Point(3, 4) in arr, arr.index(gm.Point(3, 4)))
POINT (1.20517 5) | POINT (1.21 5.00) | False
matched point: 1.20517 5.0
True 1
Equality and comparison¶
Choose the comparison that matches your intent:
equals: topologically identical point sets (ignores vertex order and Z/M).equals_exact: same coordinate sequence within a tolerance, in compatible frames.equals_identical: exact coordinates, Z/M, and completeSpace. Scalar==andhash()use this value equality.
GeometryArray == GeometryArray compares whole arrays and returns one bool;
use equals_identical(left, right) for an elementwise boolean mask. For
CRS, == compares stored definitions; use
CRS.same_as for operational equivalence.
Error hierarchy¶
Invalid geometry, parameter, CRS, and parse errors inherit from
GeometryError (a ValueError subclass):
InvalidGeometryError,
CRSError (with
CRSMismatchError and
TransformError), and
ParseError.
Three domain exceptions inherit directly from standard built-ins instead of
GeometryError:
GeometryTypeErrorsubclassesTypeError(wrong geometry kind for the operation).RepresentabilityErrorsubclassesOverflowError(exact geometric output cannot be represented in binary64).ResourceLimitErrorsubclassesRuntimeError(configured output or computation budget exhausted).
Ordinary Python protocol errors keep their built-in types (TypeError,
IndexError, StopIteration). See Errors & exceptions.
Z/M, overlay linework, and interchange¶
- Z and M layouts — XY, XYZ, XYM, and XYZM are supported; per-operation propagation is summarized in Geometry & dimensions.
- Overlay linework — overlays merge degree-two nodes and split only at true
junctions (matching
line_merge), so clipping a line with a covering polygon leaves one continuous line. - Interchange boundaries — homogeneous arrays export native GeoArrow coordinate buffers, while mixed types fall back to WKB. See Arrow & storage, Text & binary formats, and Security & untrusted input.