Skip to content

Have PostGIS write each feature in PostgreSQLGeoJSONProvider - #85

Merged
webb-ben merged 3 commits into
masterfrom
bw-dev-84
Nov 16, 2026
Merged

webb-ben merged 3 commits into
masterfrom
bw-dev-84

Conversation

@webb-ben

@webb-ben webb-ben commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

Description

Have PostGIS write each whole feature as GeoJSON in PostgreSQLGeoJSONProvider, using ST_AsGeoJSON(record, ..., id_column), for every items request, including crs and skipGeometry.

This supersedes #84 and includes its two commits from @dblodgett-usgs (44578b0, a1028d2) unchanged, plus a follow-up commit (b995d6e) that:

  • fixes the tests, which failed in CI because the id_column argument of ST_AsGeoJSON(record) only exists from PostGIS 3.5, while CI ran PostGIS 3.2
  • handles crs and skipGeometry in PostGIS instead of falling back to the core provider. The fallback ignored the CRS transform, so crs= returned untransformed coordinates.

Changes Made

  • PostGIS 3.5 is now required. CI moves from postgis/postgis:14-3.2 to postgis/postgis:17-3.5, and the README says so.
  • crs: the geometry goes through ST_Transform, and through ST_FlipCoordinates wherever pyproj would read or write northing first (for example EPSG:4326 without always_xy). The output matches the core provider.
  • skipGeometry: a NULL geometry column, which PostGIS 3.5 writes as "geometry": null.
  • Older PostGIS: on versions before 3.5, items requests now raise ProviderQueryError with the message "PostgreSQLGeoJSONProvider requires PostGIS 3.5+", instead of failing with an unhandled SQL error.
  • Cleanup: removed the ORM helpers that only served the old fallback (_GEOJSON, _geojson, _select_properties_clause, and the _sqlalchemy_to_feature override). get() still uses the core provider.
  • Tests:
    • CRS transforms are compared with the core provider (EPSG:4326, 3857 and 32735, with and without always_xy).
    • A feature with no geometry and no properties.
    • The PostGIS 3.5 error message.
    • Two MVT tile-size expectations updated to PostGIS 3.5 output, about 13% larger than on 3.2. The checks that simplification makes tiles smaller are unchanged.

Benchmark

Run with a local pygeoapi server (uvicorn, one worker) on 118,208 features (the test waterways copied 8 times), with the plugin JSON formatter and PostGIS 3.5.

Request Core provider This PR
limit=1000 156 ms 60 ms
All features 6.17 s 1.58 s
All, skipGeometry=true 1.51 s 0.73 s
All, crs=EPSG:3857 13.6 s 3.83 s
All, crs=EPSG:4326 13.7 s 2.12 s
Peak server memory 1,729 MB 1,081 MB

The PostGIS container ran under amd64 emulation on Apple Silicon, so database-side times are inflated, and repeat runs varied by 10–15%.

The features match the core provider's ids, order and properties, with coordinates within 1e-8.

I also benchmarked approaches that work on PostGIS 3.2:

Approach All features
String concat of the parts 1.51 s
json_build_object 2.10 s

They were no faster, so this PR requires 3.5 instead.

Related Issues

Supersedes #84.

Additional Notes

Deployments on PostGIS older than 3.5 need to upgrade before using PostgreSQLGeoJSONProvider. PostGIS 3.5 supports PostgreSQL 12–17. Other providers are unaffected.

🤖 Generated with Claude Code

dblodgett-usgs and others added 3 commits November 7, 2026 07:57
PostgreSQLGeoJSONProvider.query now selects ST_AsGeoJSON(t.*, geom,
max_decimal_digits, false, id) over a subquery that applies the core
filters, sort, offset and limit, so PostGIS writes the whole feature as
text. The provider returns those strings as GeoJSONFeature objects
without parsing them, and JSONFormatter writes each one into the
collection verbatim as an orjson.Fragment. The id column is selected a
second time under an alias that ST_AsGeoJSON moves to the feature id,
so id stays in properties as with the core provider.

Requests with a CRS transform or skipGeometry take the core provider's
ORM path. get() is unchanged.

GeoJSONGeometry's lazy parsing moves to a base class, _LazyJSON, shared
with the new GeoJSONFeature. A feature that is read (HTML, JSON-LD,
JSON-FG with a time field) parses on first access and serializes from
its contents, as GeoJSONGeometry already did.

At 100,000 flowlines from a national hydrofabric table, uvicorn with
one worker, the response takes 1.04 s against 2.11 s before this change
and 7.28 s for the core PostgreSQL provider, 1.28 times a direct
psycopg2 fetch of the same ST_AsGeoJSON rows. Peak server memory is
490 MB against 555 MB and 1,035 MB. The response matches the core
provider's ids, order and properties, with coordinates rounded to 9
decimals.

Assisted-by: Claude Opus-5.5
@webb-ben
webb-ben changed the base branch from master to pr-84 November 16, 2026 15:39
@webb-ben
webb-ben changed the base branch from pr-84 to master November 16, 2026 15:42
@webb-ben
webb-ben merged commit 9fd1e90 into master Nov 16, 2026
6 checks passed
@webb-ben
webb-ben deleted the bw-dev-84 branch November 16, 2026 15:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants