round_geometry

geodetic_engine.geodesy.geometry.round_geometry(geometry, decimals=None, *, grid_size=None, validate='warn')[source]

Round a geometry’s coordinates, or those of an array of geometries.

decimals rounds every coordinate half up, by its shortest decimal form: with 2 decimals, 2.675 is 2.68 and -0.125 is -0.13. -0.0 becomes 0.0. Every vertex is kept, which can make a polygon invalid. grid_size snaps to a grid instead, with shapely.set_precision, which keeps polygons valid but can remove vertices. Rounding twice gives what rounding once does.

Parameters:
  • geometry (Any) – A Shapely geometry or None, or a sequence or array of them.

  • decimals (int | Sequence[int | None] | None, default: None) – One number for every axis, or one per axis in the order x, y, z, m, None keeping that axis.

  • grid_size (float | None, default: None) – The grid to snap to, instead of decimals.

  • validate (Literal['off', 'warn', 'raise'], default: "warn") – What to do when rounding to decimals makes a valid geometry invalid: "off", "warn" (a GeometryValidityWarning) or "raise".

Return type:

Any

Returns:

The rounded geometry, or a numpy array of them.

Raises:
  • GeometryError – If validate="raise" and a geometry became invalid, or grid_size is asked for geometries with M values.

  • ValueError – If both decimals and grid_size are given, or either is not valid.

Example

>>> round_geometry(shapely.Point(2.675, -0.125), 2).wkt
'POINT (2.68 -0.13)'