Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:

services:
postgres:
image: postgis/postgis:14-3.2
image: postgis/postgis:17-3.5
ports:
- 5432:5432
env:
Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,14 @@ providers:

### PostgreSQL GeoJSON

The PostgreSQL GeoJSON Provider extends the PostgreSQL Provider in pygeoapi core by rendering geometries as GeoJSON in the database, using PostGIS `ST_AsGeoJSON`.
The PostgreSQL GeoJSON Provider extends the PostgreSQL Provider in pygeoapi core by rendering features as GeoJSON in the database, using PostGIS `ST_AsGeoJSON`.
It requires PostGIS 3.5 or later, for the `id_column` argument of `ST_AsGeoJSON(record)`.
The core provider loads each geometry and converts it to GeoJSON in Python, which is slow for large or complex geometries.
Rendering in the database skips that conversion, so feature responses build faster.
Pair it with the [JSON formatter](#json) to also skip re-encoding the GeoJSON for `f=json`.

Geometries are returned in the table's own coordinate reference system: `ST_AsGeoJSON` does not reproject, so the `crs` query parameter is not supported.
Store geometries in WGS 84 (EPSG:4326) to serve standard GeoJSON.
The `crs` query parameter is supported: PostGIS reprojects geometries with `ST_Transform`, following the same axis order as the core provider (including `always_xy`).
`skipGeometry` writes each feature's geometry as `null`.

To use the PostgreSQL GeoJSON Provider, you need to specify `pygeoapi_plugins.provider.postgresql.PostgreSQLGeoJSONProvider` as the provider's name.
The rest of the configuration is the same as the [PostgreSQL Provider](https://docs.pygeoapi.io/en/latest/data-publishing/ogcapi-features.html#postgresql) in pygeoapi core.
Expand Down
84 changes: 56 additions & 28 deletions pygeoapi_plugins/formatter/json_.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,23 +109,44 @@ def __repr__(self):
return f'<JSONFormatter> {self.name}'


class GeoJSONGeometry(dict):
class _LazyJSON(dict):
"""
A GeoJSON geometry that keeps the JSON it came from in `raw`, and only
parses it the first time its contents are read.
A dict that keeps the JSON it came from in `raw`, and only parses it
the first time its contents are read.

Until then the dict holds just `type`, so a geometry that `JSONFormatter`
writes out from `raw` never creates its coordinate lists. Every read
method parses first, so pygeoapi (HTML, JSON-LD, other formatters) sees
an ordinary dict. `raw` is not updated if the dict is changed.
Until then the dict holds just `type`, so an object that `JSONFormatter`
writes out from `raw` never creates its members. Every read method
parses first, so pygeoapi (HTML, JSON-LD, other formatters) sees an
ordinary dict. `raw` is not updated if the dict is changed.

`orjson.dumps` reads dict storage directly, so it must be given `raw`
(as `JSONFormatter` does) rather than an unread `GeoJSONGeometry`.
(as `JSONFormatter` does) rather than an unread object.
"""

__slots__ = ('_loaded', 'raw')

def _load(self):
"""Parse `raw` into the dict, once"""
if not self._loaded:
self._loaded = True
dict.update(self, orjson.loads(self.raw))

def __reduce__(self):
self._load()
return (_rebuild, (type(self), self.raw, dict(self)))

__hash__ = None


class GeoJSONGeometry(_LazyJSON):
"""
A GeoJSON geometry, as written by `ST_AsGeoJSON(geom)`, parsed on
first read (see `_LazyJSON`).

`GeoJSONGeometry(None)` returns `None`, for features without geometry.
"""

__slots__ = ('_loaded', 'raw')
__slots__ = ()

@overload
def __new__(cls, raw: None) -> None: ...
Expand All @@ -152,21 +173,24 @@ def __init__(self, raw: str | bytes):
super().__init__(orjson.loads(raw))
self._loaded = True

def _load(self):
"""Parse `raw` into the dict, once"""
if not self._loaded:
self._loaded = True
dict.update(self, orjson.loads(self.raw))

def __reduce__(self):
self._load()
return (_rebuild_geometry, (self.raw, dict(self)))
class GeoJSONFeature(_LazyJSON):
"""
A whole GeoJSON feature, as written by `ST_AsGeoJSON(record, ...)`,
parsed on first read (see `_LazyJSON`).
"""

__hash__ = None
__slots__ = ()

def __init__(self, raw: str | bytes):
self.raw = raw
# Non-empty for the same reason as GeoJSONGeometry
super().__init__(type='Feature')
self._loaded = False


def _parse_first(name: str):
"""Wrap a dict method so it parses the geometry before running"""
"""Wrap a dict method so it parses `raw` before running"""
method = getattr(dict, name)

def wrapper(self, *args, **kwargs):
Expand Down Expand Up @@ -203,17 +227,17 @@ def wrapper(self, *args, **kwargs):
'update',
'clear',
):
setattr(GeoJSONGeometry, _name, _parse_first(_name))
setattr(_LazyJSON, _name, _parse_first(_name))
del _name


def _rebuild_geometry(raw: str | bytes, data: dict) -> GeoJSONGeometry:
"""Unpickle a `GeoJSONGeometry` with its (possibly changed) contents"""
geometry = GeoJSONGeometry(raw)
geometry._loaded = True
dict.clear(geometry)
dict.update(geometry, data)
return geometry
def _rebuild(cls: type, raw: str | bytes, data: dict) -> _LazyJSON:
"""Unpickle a `_LazyJSON` subclass with its (possibly changed) contents"""
obj = cls(raw)
obj._loaded = True
dict.clear(obj)
dict.update(obj, data)
return obj


# Feature members that can hold a geometry: GeoJSON geometry, JSON-FG place
Expand All @@ -227,9 +251,13 @@ def _embed_geometry(feature: dict) -> dict:

:param feature: `dict` of GeoJSON or JSON-FG feature

:returns: `dict` of feature
:returns: `dict` of feature, or `orjson.Fragment` of an unread
`GeoJSONFeature`
"""

if isinstance(feature, GeoJSONFeature) and not feature._loaded:
return orjson.Fragment(feature.raw)

embedded = {}
for key in _GEOMETRY_KEYS:
geometry = feature.get(key)
Expand Down
Loading
Loading