Source code for geodetic_engine.geojson._osdu

"""The OSDU CRS conversion service's ``convertGeoJson``, request in, response out.

Private to :mod:`geodetic_engine.geojson`, which exports
:func:`convert_geojson_request`.
"""

from __future__ import annotations

from collections.abc import Mapping
from typing import Any, Final

from geodetic_engine.geodesy import CoordinateReferenceSystem
from geodetic_engine.geojson._convert import _convert
from geodetic_engine.geojson._scope import is_wgs84_lonlat

_COLLECTIONS: Final = ("FeatureCollection", "AnyCrsFeatureCollection")


[docs] def convert_geojson_request(body: Mapping[str, Any]) -> dict[str, Any]: """Answer a ``convertGeoJson`` request as the OSDU service would. The request and response are those of ``POST /v4/convertGeoJson``, so results can be compared with the service's, or used in their place. The output follows the service's rule: plain GeoJSON when ``toCRS`` is WGS 84 longitude and latitude, with heights in metres, AnyCrs otherwise. A position that cannot be transformed is NaN, as there, and counted out of ``successCount``. Unlike the service, the package's rules hold: a datum change needs an operation, named by ``transformation`` or bound in an early-bound CRS; a missing grid is an error; heights keep ``persistableReferenceUnitZ``. Args: body: The request: ``featureCollection``, ``toCRS``, and optionally ``toUnitZ`` and ``transformation``. Returns: The response: ``featureCollection``, ``successCount``, ``totalCount`` and ``operationsApplied``. Raises: TypeError: If ``body`` is not a mapping. ValueError: If a member is missing, or the collection is not one. GeoJsonError, GeodesyError: As :func:`convert_geojson` raises them. """ if not isinstance(body, Mapping): raise TypeError(f"a request is a JSON object, not {type(body).__name__}") collection = body.get("featureCollection") to_crs = body.get("toCRS") if ( not isinstance(collection, Mapping) or collection.get("type") not in _COLLECTIONS ): raise ValueError( "featureCollection is a FeatureCollection or AnyCrsFeatureCollection" ) if not isinstance(to_crs, str) or not to_crs.strip(): raise ValueError("toCRS is a CRS record id or persistableReference") target = CoordinateReferenceSystem.from_user_input(to_crs) plain = is_wgs84_lonlat(target) # The response has no reasons for failed positions, which cost a call each. result = _convert( collection, to_crs, source_crs=None, operation=body.get("transformation") or None, # Plain GeoJSON heights are in metres, whatever toUnitZ says. target_unit_z=None if plain else body.get("toUnitZ") or None, coordinate_epoch=None, output="geojson" if plain else "anycrs", on_error="nan", decimals=None, densify=None, segments="straight", # As the service writes positions: as transformed, never cut. antimeridian=None, orient=None, fallback_operation=None, reasons=False, ) return { "featureCollection": result.document, "successCount": result.success_count, "totalCount": result.total_count, "operationsApplied": list(result.operations_applied), }