Repository navigation
Have PostGIS write each feature in PostgreSQLGeoJSONProvider - #84
Closed
dblodgett-usgs wants to merge 2 commits into
Closed
dblodgett-usgs wants to merge 2 commits into
dblodgett-usgs wants to merge 2 commits into
Conversation
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
Member
|
Superseded by #85, which includes these commits plus the PostGIS 3.5 migration. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Assisted by Claude Opus 5.5 (implementation and testing) and Fable 5.1 (planning)
PostgreSQLGeoJSONProvideralready has PostGIS write the geometry, but it still loads each row through the ORM andbuilds a feature dict per row. This change has PostGIS write the whole feature, and the JSON formatter puts those
strings into the response without parsing them.
What changes
PostgreSQLGeoJSONProvider.queryrunsST_AsGeoJSON(t.*, geom, max_decimal_digits, false, id)over a subquery thatapplies the core provider's filters (property, CQL, bbox, datetime), sort, offset and limit. Each row comes back as
one string, wrapped in a
GeoJSONFeature.ST_AsGeoJSONmoves to the featureid, soidalsostays in
propertiesas it does with the core provider.GeoJSONGeometry's lazy parsing moves to a base class,_LazyJSON, shared with the newGeoJSONFeature. A featureis parsed only when something reads it (HTML, JSON-LD, JSON-FG with a time field), and then serializes from its
contents.
JSONFormatterwrites an unreadGeoJSONFeatureinto the collection as anorjson.Fragment.skipGeometryfall back to the core ORM path, so they still work but don't get thespeedup.
get()is unchanged.numberMatchedcomes from an exactcount(*)with the same filters, run only whencountis set or forresulttype=hits, as in the core provider.Results
100,000 flowlines from a national hydrofabric table, served by uvicorn with one worker. Median of three runs on a
fresh server:
Peak server memory at 100,000 features: 1,035 MB for the core provider, 555 MB before this change, 490 MB after it.
The direct fetch runs the same
ST_AsGeoJSONquery with psycopg2 and no pygeoapi, so it is the floor for thisapproach.
Against the core provider's response at 100,000 features: same ids in the same order, equal properties, coordinates
within 5e-10 (rounded to 9 decimals by
max_decimal_digits). Offset, sortby, property filter, bbox, CQL,properties=,countandhitsgive the same results as the core provider on the same table.Tests
tests/test_postgresql_provider.py,tests/test_json_formatter.pyandtests/test_jsonfg_formatter.pypass. Oneformatter test is added: a
GeoJSONFeatureis written out unchanged and unparsed, and parses to the same feature whenread as a dict.
Notes
Types like
real,numeric, timestamps with time zone and NaN may come out differently than from the core provider.provider, which adds
ORDER BY idonly when no sortby is given.provider.
query()copies the core signature, so it adds four ruff B006 warnings (bbox=[]and similar), matchingPseudoPostgreSQLProviderin the same file.