From d7f3aaf3c67e47e79f287a6afa629686114e033a Mon Sep 17 00:00:00 2001 From: vincentsarago Date: Tue, 28 Mar 2023 18:03:39 +0200 Subject: [PATCH] cast geography to geometry --- tests/fixtures/my_data.sql | 3 +++ tests/routes/test_collections.py | 42 ++++++++++++++++++++++---------- tests/routes/test_geography.py | 39 +++++++++++++++++++++++++++++ tipg/dbmodel.py | 3 ++- 4 files changed, 73 insertions(+), 14 deletions(-) create mode 100644 tests/routes/test_geography.py diff --git a/tests/fixtures/my_data.sql b/tests/fixtures/my_data.sql index 9b97529b..2d782403 100644 --- a/tests/fixtures/my_data.sql +++ b/tests/fixtures/my_data.sql @@ -17,4 +17,7 @@ ALTER TABLE public.my_data ADD COLUMN otherdt timestamptz; ALTER TABLE public.my_data ADD COLUMN othergeom geometry; UPDATE my_data SET otherdt=datetime+'1 year'::interval, othergeom=st_pointonsurface(geom); CREATE VIEW public.my_data_alt AS SELECT * FROM my_data; +-- Create a copy of my_data but with geography instead of Geometry +CREATE TABLE public.my_data_geo AS SELECT * FROM my_data; +ALTER TABLE public.my_data_geo ALTER COLUMN geom TYPE geography(Polygon,4326) USING ST_Transform(geom,4326)::geography; COMMIT; diff --git a/tests/routes/test_collections.py b/tests/routes/test_collections.py index c30d8872..65502da1 100644 --- a/tests/routes/test_collections.py +++ b/tests/routes/test_collections.py @@ -1,5 +1,7 @@ """Test /collections endpoints.""" +collection_number = 14 + def test_collections(app): """Test /collections endpoint.""" @@ -8,8 +10,8 @@ def test_collections(app): assert response.headers["content-type"] == "application/json" body = response.json() assert ["links", "numberMatched", "numberReturned", "collections"] == list(body) - assert body["numberMatched"] == 13 - assert body["numberReturned"] == 13 + assert body["numberMatched"] == collection_number + assert body["numberReturned"] == collection_number ids = [x["id"] for x in body["collections"]] assert "public.landsat_wrs" in ids @@ -27,7 +29,7 @@ def test_collections_search(app): """Test /collections endpoint.""" response = app.get("/collections", params={"limit": 1}) body = response.json() - assert body["numberMatched"] == 13 + assert body["numberMatched"] == collection_number assert body["numberReturned"] == 1 rels = [x["rel"] for x in body["links"]] assert "next" in rels @@ -35,15 +37,17 @@ def test_collections_search(app): response = app.get("/collections", params={"limit": 1, "offset": 1}) body = response.json() - assert body["numberMatched"] == 13 + assert body["numberMatched"] == collection_number assert body["numberReturned"] == 1 rels = [x["rel"] for x in body["links"]] assert "next" in rels assert "prev" in rels - response = app.get("/collections", params={"limit": 1, "offset": 12}) + response = app.get( + "/collections", params={"limit": 1, "offset": collection_number - 1} + ) body = response.json() - assert body["numberMatched"] == 13 + assert body["numberMatched"] == collection_number assert body["numberReturned"] == 1 rels = [x["rel"] for x in body["links"]] assert "next" not in rels @@ -51,16 +55,23 @@ def test_collections_search(app): response = app.get("/collections", params={"bbox": "-180,81,180,87"}) body = response.json() - assert body["numberMatched"] == 10 + assert ( + body["numberMatched"] == collection_number - 3 + ) # 2 collections are not within the bbox ids = [x["id"] for x in body["collections"]] assert "public.nongeo_data" not in ids assert "public.canada" not in ids response = app.get("/collections", params={"datetime": "../2022-12-31T23:59:59Z"}) body = response.json() - assert body["numberMatched"] == 3 + assert body["numberMatched"] == 4 ids = [x["id"] for x in body["collections"]] - assert ["public.my_data", "public.my_data_alt", "public.nongeo_data"] == ids + assert [ + "public.my_data", + "public.my_data_alt", + "public.my_data_geo", + "public.nongeo_data", + ] == ids response = app.get("/collections", params={"datetime": "2022-12-31T23:59:59Z/.."}) body = response.json() @@ -68,15 +79,20 @@ def test_collections_search(app): response = app.get("/collections", params={"datetime": "2003-12-31T23:59:59Z/.."}) body = response.json() - assert body["numberMatched"] == 3 + assert body["numberMatched"] == 4 ids = [x["id"] for x in body["collections"]] - assert ["public.my_data", "public.my_data_alt", "public.nongeo_data"] == ids + assert [ + "public.my_data", + "public.my_data_alt", + "public.my_data_geo", + "public.nongeo_data", + ] == ids response = app.get("/collections", params={"datetime": "2004-12-31T23:59:59Z/.."}) body = response.json() - assert body["numberMatched"] == 2 + assert body["numberMatched"] == 3 ids = [x["id"] for x in body["collections"]] - assert ["public.my_data", "public.my_data_alt"] == ids + assert ["public.my_data", "public.my_data_alt", "public.my_data_geo"] == ids response = app.get( "/collections", params={"datetime": "2004-01-01T00:00:00Z/2004-12-31T23:59:59Z"} diff --git a/tests/routes/test_geography.py b/tests/routes/test_geography.py new file mode 100644 index 00000000..ad183e69 --- /dev/null +++ b/tests/routes/test_geography.py @@ -0,0 +1,39 @@ +"""test tipg endpoint with table having a geography column.""" + +import mapbox_vector_tile +import numpy + + +def test_geography_column(app): + response = app.get("/collections/public.my_data_geo") + assert response.status_code == 200 + assert response.headers["content-type"] == "application/json" + body = response.json() + assert body["id"] == "public.my_data_geo" + + response = app.get("/collections/public.my_data_geo/items") + assert response.status_code == 200 + assert response.headers["content-type"] == "application/geo+json" + body = response.json() + assert body["type"] == "FeatureCollection" + assert body["id"] == "public.my_data_geo" + assert body["title"] == "public.my_data_geo" + assert body["links"] + assert body["numberMatched"] == 6 + assert body["numberReturned"] == 6 + assert body["features"][0]["geometry"]["type"] == "Polygon" + + response = app.get("/collections/public.my_data_geo/tilejson.json") + assert response.status_code == 200 + resp_json = response.json() + assert resp_json["name"] == "public.my_data_geo" + assert resp_json["minzoom"] == 5 + assert resp_json["maxzoom"] == 12 + numpy.testing.assert_almost_equal( + resp_json["bounds"], [-47.5356, 74.8049, -8.97407, 81.8555] + ) + + response = app.get("/collections/public.my_data_geo/tiles/5/11/5") + assert response.status_code == 200 + decoded = mapbox_vector_tile.decode(response.content) + assert len(decoded["default"]["features"]) diff --git a/tipg/dbmodel.py b/tipg/dbmodel.py index 274d1cf4..b330a35b 100644 --- a/tipg/dbmodel.py +++ b/tipg/dbmodel.py @@ -356,7 +356,8 @@ def _select_mvt( tile: Tile, ): """Create MVT from intersecting geometries.""" - geom = logic.V(geometry_column.name) + print(geometry_column.type) + geom = pg_funcs.cast(logic.V(geometry_column.name), "geometry") # make sure the geometries do not overflow the TMS bbox if not tms.is_valid(tile):