convert_geojson

geodetic_engine.geojson.convert_geojson(document, target_crs, *, source_crs=None, operation=None, target_unit_z=None, coordinate_epoch=None, output='auto', on_error='raise', decimals=None, densify=None, segments='straight', antimeridian='split', orient=None, fallback_operation=None)[source]

Convert every position of a GeoJSON document to another CRS.

The document is RFC 7946 GeoJSON, OSDU AnyCrs GeoJSON or OGC JSON-FG, with a FeatureCollection, a Feature or a geometry at its root. Its structure, id, properties, foreign members and key order are kept; positions are converted, each bbox is computed again, and the members stating the CRS are written for the target.

Where the positions are comes from the document: RFC 7946 is WGS 84 (CRS84, or CRS84h with heights), AnyCrs states its CRS on its collection, a GeoJSON 2008 crs member names one, and a JSON-FG place is in the CRS of its nearest coordRefSys, in that CRS’s axis order.

Parameters:
  • document (object) – The document: a mapping, JSON text, or an object with a __geo_interface__.

  • target_crs (object) – The CRS to convert to, as anything from_user_input() accepts, an OSDU record id or persistableReference included. A compound CRS converts heights between vertical datums.

  • source_crs (object, default: None) – The CRS agreed with whoever wrote the document, for one that does not state its own (RFC 7946’s “prior arrangement”). One that does must state the same.

  • operation (Any, default: None) – The operation to apply, as for Transformation; required for a datum change. The OSDU service’s transformation.

  • target_unit_z (str | None, default: None) – The unit to write heights in, as length_unit() reads it: a symbol, an OSDU UnitOfMeasure id or a unit persistableReference. The OSDU service’s toUnitZ.

  • coordinate_epoch (float | None, default: None) – As for transform(); a JSON-FG coordRefSys may state it instead.

  • output (Literal['auto', 'geojson', 'anycrs', 'jsonfg', 'jsonfg-plus'], default: "auto") – "auto" keeps the document’s family: AnyCrs stays AnyCrs, and plain GeoJSON is written as JSON-FG unless the target is WGS 84 longitude and latitude. "geojson", "anycrs", "jsonfg" and "jsonfg-plus" choose one; "jsonfg-plus" also gives each feature a geometry in WGS 84.

  • on_error (Literal['raise', 'withdraw', 'nan'], default: "raise") – What a position that cannot be transformed does: "raise" the error, naming the position by its JSON path; "withdraw" its feature’s geometry, writing it as null; or write "nan" in its place, as the OSDU service does.

  • decimals (int | Sequence[int | None] | None, default: None) – Round converted values half up to this many decimals, one number for all, or one per value in xy order then height.

  • densify (float | None, default: None) – Insert vertices into each line and ring first, so that no segment is longer than this: RFC 7946 draws a segment straight in longitude and latitude, and it is not straight in another CRS. In source CRS units for straight segments, in metres along the ellipsoid for geodesics. The positions inserted are converted and counted with the others.

  • segments (Literal['straight', 'geodesic'], default: "straight") – What a segment is in the source CRS: "straight", as GeoJSON has it, or "geodesic", the shortest line on the source CRS’s ellipsoid, as boundaries are commonly defined. Geodesics need densify.

  • antimeridian (Literal['split', 'unwrap'] | None, default: "split") – Positions written in a geographic CRS whose part crosses its antimeridian: "split" cuts the part there, into parts of a multipart geometry, as RFC 7946 (3.1.9) has it, closing a ring round a pole through the pole, and a bbox crosses it, its west bound east of its east one, as RFC 7946 (5.2) has it, where that box is the narrower and no wider than half a turn of longitude; "unwrap" carries the longitudes on past the antimeridian; and None keeps them as transformed, a segment going the long way round.

  • orient (Literal['ccw', 'cw'] | None, default: None) – None keeps the vertex order as transformed; "ccw" winds exterior rings counterclockwise and holes clockwise (RFC 7946’s right-hand rule), and "cw" the other way.

  • fallback_operation (Any, default: None) – For "jsonfg-plus", the operation from the source CRS to WGS 84, if one is needed for the fallback geometry.

Return type:

GeoJsonResult

Returns:

The converted document, the counts of positions converted, and the provenance of every transformation applied.

Raises:
  • TypeError – If document is not a mapping, JSON text or an object with a __geo_interface__.

  • GeoJsonError – If the document is not GeoJSON this reads, or its CRS is not stated or contradicts source_crs.

  • ValueError – If an option is not valid for the document or the target, for example heights converted to a unit the output cannot state; if heights of no stated unit would become heights of a three-dimensional CRS; or if JSON-FG would state one epoch for positions at different epochs of a dynamic target.

  • GeodesyError – If the transformation is refused: an operation must be named, a grid is missing, an epoch is needed.

  • TransformationFailedError – With on_error="raise", if a position cannot be transformed, naming it by its JSON path.

Example

>>> result = convert_geojson(
...     {"type": "Point", "coordinates": [2.5, 63.5]},
...     "EPSG:32631",
...     decimals=2,
... )
>>> result.document["coordinates"], result.document["coordRefSys"]
([475108.05, 7041396.93], 'http://www.opengis.net/def/crs/EPSG/0/32631')