write_geometry

geodetic_engine.geodesy.geometry.write_geometry(geometry, format, *, crs=None, decimals=None, grid_size=None, axis_order='xy', byte_order='little', validate='warn')[source]

Write a geometry in an encoding from FORMATS.

WKT is written at full precision, trimmed, so 1.50 is 1.5 and nothing is cut. "wkb" is ISO WKB, the OGC standard; "ewkb" is PostGIS’s extended WKB, the only flavour with an SRID.

Parameters:
  • geometry (Geometry | None) – The geometry, xy, or None, which is written as None.

  • format (Literal['wkt', 'wkb', 'wkb_hex', 'ewkt', 'ewkb', 'ewkb_hex', 'geojson', 'esrijson']) – The encoding: "wkt", "wkb" (bytes), "wkb_hex", "ewkt", "ewkb", "ewkb_hex", "geojson" (a GeoJSON geometry object) or "esrijson" (an Esri JSON geometry).

  • crs (object, default: None) – The CRS the geometry is in. Needed for the SRID of EWKT and EWKB, for axis_order="crs", and for Esri’s spatialReference.

  • decimals (int | Sequence[int | None] | None, default: None) – Round to this many decimals first, half up: one number for every axis, or one per axis in the order x, y, z, m (None keeps that axis).

  • grid_size (float | None, default: None) – Snap to a grid of this size first instead, keeping polygons valid.

  • axis_order (Literal['xy', 'crs'], default: "xy") – "xy", or "crs" to write the CRS’s declared order.

  • byte_order (Literal['little', 'big'], default: "little") – "little" or "big", for WKB.

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

Return type:

Any

Returns:

The encoding – text, bytes or a mapping, or None for None.

Raises:
  • GeometryError – If the geometry cannot be written in format: an SRID or spatialReference without a code to write, M values in GeoJSON, a geometry collection in Esri JSON; or if validate="raise" and rounding made it invalid.

  • ValueError – If an option is not one of those above.

Example

>>> point = shapely.Point(10.123456789, 60.987654321)
>>> write_geometry(point, "ewkb_hex", crs="EPSG:4326")
'0101000020E6100000C7726EBA353F24405B39F0746B7E4E40'
>>> write_geometry(point, "wkt", decimals=3)
'POINT (10.123 60.988)'