-
-
Notifications
You must be signed in to change notification settings - Fork 282
Add provider for MVT tile generation from Postgres #1979
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Add provider for MVT tile generation from Postgres #1979
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank You @ThorodanBrom ! Would you be able to add a Postgres Provider docker example at pygeoapi-examples? https://github.com/geopython/pygeoapi-examples/tree/main/docker
pygeoapi/provider/mvt_postgres.py
Outdated
query = text(""" | ||
WITH | ||
bounds AS ( | ||
SELECT ST_TileEnvelope(:z, :y, :x, ST_MakeEnvelope(-180, -90, 180, 90, 4326)) AS boundgeom |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why the tile envelope here is based on the extent of the crs, instead of the extent of the geometry (like in the case of WebMercator)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ST_TileEnvelope by default is configured to EPSG:3857. In order to use it for other CRSs, the docs mention that you can add an extra parameter of a geometry that has an extent of the 0th zoom level. There's an example of setting it for EPSG:4326, we just used that here. (hope that this answered your question, if not, please let me know)
We can probably use the WKB representation there so that MakeEnvelope
isn't calculated for every tile request.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, I think the WKB representation would make sense here!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed in 97ba024. I used EWKT representation instead of WKB as the the WKB hex string was too long
pygeoapi/provider/mvt_postgres.py
Outdated
SELECT ST_TileEnvelope(:z, :y, :x) AS boundgeom | ||
), | ||
mvtgeom AS ( | ||
SELECT ST_AsMVTGeom(ST_Transform({geom}, 3857), bounds.boundgeom) AS geom {fields} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think converting curves to lines (ST_CurveToLine), could speed up queries in the case of more complex geometries.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, I will try this out, it looks like Martin also uses it.
So by using ST_CurveToLine
, will this provider also be able to support CIRCULARSTRING
-like data types (that is what I understood from reading the documentation)? Or is it just going to be used for query performance?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think simplification, in general, yields performance; and in the case of tiles, we do not need the geometries to be so detailed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed in 97ba024
|
Thanks for the review,
|
Thank You @ThorodanBrom ! I think it is ok to rebase, but I am pinging @tomkralidis here, just in case. |
+1 to rebase, given it will start CI again fresh. |
- `/tiles` and `/tiles/{TMS}` APIs are working
- Supports properties - Supports both WebMercatorQuad, WorldCRS84Quad Co-authored-by: PRAJWAL S <praju18299@gmail.com> Co-authored-by: Tanvi Prasad <tanvi.prasad@datakaveri.org>
- Fixed flake8 issues - Corrected tile indices in `ST_TileEnvelope` - Previous indices worked when tile URL was `/z/x/y`. Current ones work for `/z/y/x`, which is OGC-compliant - Used `ST_CurveToLine` when querying for features during tile generation to potentially speed up queries - Martin also uses it (https://maplibre.org/martin/sources-pg-functions.html#simple-function) - Used EWKT representation of `ST_MakeEnvelope(-180,-90,180,90,4326)` for WorldCRS84Quad tiles
d9c05c6
to
97ba024
Compare
table: hotosm_bdi_waterways | ||
geom_field: foo_geom | ||
options: | ||
zoom: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ThorodanBrom I think that the zoom
should be placed under options
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please fix the format of the example in the documentation, and I think we are good to go.
You can also click on the check-boxes that say "Updates to public demo", as this provider is not used in the configuration file of the demo.
Thank You!
Thanks for the review @doublebyte1. I'm working on the example in
Do let me know if this sounds OK. |
You are right in that there are no tests for other MVT providers. Let's keep it like this, for now. Thank You for the availability! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work here! Minor change requests on naming consistency.
@@ -22,6 +22,7 @@ pygeoapi core tile providers are listed below, along with supported features. | |||
`MVT-elastic`_,✅,✅,✅,❌,❌,✅ | |||
`MVT-proxy`_,❓,❓,❓,❓,❌,✅ | |||
`WMTSFacade`_,✅,❌,✅,✅,✅,❌ | |||
`MVT-postgres`_,✅,✅,✅,✅,❌,✅ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rename to MVT-postgresql
.. note:: | ||
Requires Python packages sqlalchemy, geoalchemy2 and psycopg2-binary | ||
|
||
Must have PostGIS installed with protobuf-c support. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Turn into an additional note
.. note:: | ||
Geometry must be using EPSG:4326 | ||
|
||
This provider gives support to serving tiles generated using `Postgres <https://www.postgresql.org/>`_ with `PostGIS <https://postgis.net/>`_. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/Postgres/PostgreSQL/g
|
||
providers: | ||
- type: tile | ||
name: MVT-postgres |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MVT-postgresql
pygeoapi/plugin.py
Outdated
@@ -53,6 +53,7 @@ | |||
'MVT-tippecanoe': 'pygeoapi.provider.mvt_tippecanoe.MVTTippecanoeProvider', # noqa: E501 | |||
'MVT-elastic': 'pygeoapi.provider.mvt_elastic.MVTElasticProvider', | |||
'MVT-proxy': 'pygeoapi.provider.mvt_proxy.MVTProxyProvider', | |||
'MVT-postgres': 'pygeoapi.provider.mvt_postgres.MVTPostgresProvider', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'MVT-postgresql': 'pygeoapi.provider.mvt_postgresql.MVTPostgreSQLProvider',
pygeoapi/provider/mvt_postgres.py
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rename to pygeoapi/provider/mvt_postgresql.py
pygeoapi/provider/mvt_postgres.py
Outdated
@@ -0,0 +1,246 @@ | |||
# ================================================================= | |||
# | |||
# Authors: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add yourself as author. Example in https://github.com/geopython/pygeoapi/blob/master/setup.py#L3
pygeoapi/provider/mvt_postgres.py
Outdated
# | ||
# Authors: | ||
# | ||
# Copyright (c) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add copyright. Example in https://github.com/geopython/pygeoapi/blob/master/setup.py#L5
pygeoapi/provider/mvt_postgres.py
Outdated
# | ||
# ================================================================= | ||
|
||
import logging |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Order imports by standard library, 3rd party packages, then local imports, alphabetically, separated by blank line.
from copy import deepcopy
import logging
from sqlalchemy.sql import text
from pygeoapi.models.provider.base import (
TileSetMetadata, TileMatrixSetEnum, LinkType)
from pygeoapi.provider.base import ProviderConnectionError
from pygeoapi.provider.base_mvt import BaseMVTProvider
from pygeoapi.provider.postgresql import PostgreSQLProvider
from pygeoapi.util import url_join
pygeoapi/provider/mvt_postgres.py
Outdated
LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
class MVTPostgresProvider(BaseMVTProvider): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rename to MVTPostgreSQLProvider
- Updated all necessary files to change postgres -> postgresql related to the plugin - Updated documentation - Added authors to plugin file and organized imports
…S, return ProviderTileNotFound error - Otherwise error was thrown from PostgreSQL `ST_MakeTileEnvelope` function
19b9fbb
to
9f596a1
Compare
Hello @tomkralidis, thanks for the review. I think I've addressed all your comments in 9a320cc. (I added a small bug fix in a034d15 as well). Please let me know if there are any other required changes. |
Must have PostGIS installed with protobuf-c support | ||
|
||
.. note:: | ||
Geometry must be using EPSG:4326 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand that this note is outdated for the postgresql provider. Is the note valid for this case?
Overview
This PR adds MVT tile generation from Postgres/PostGIS tables natively.
Related Issue / discussion
This is related to issue #1978
Additional information
Dependency policy (RFC2)
Updates to public demo
Contributions and licensing
(as per https://github.com/geopython/pygeoapi/blob/master/CONTRIBUTING.md#contributions-and-licensing)