Skip to content

Commit

Permalink
Merge pull request #47 from developmentseed/castGeographyToGeometry
Browse files Browse the repository at this point in the history
cast geography to geometry
  • Loading branch information
vincentsarago authored Mar 28, 2023
2 parents 99c7de7 + d7f3aaf commit a435d30
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 14 deletions.
3 changes: 3 additions & 0 deletions tests/fixtures/my_data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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;
42 changes: 29 additions & 13 deletions tests/routes/test_collections.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Test /collections endpoints."""

collection_number = 14


def test_collections(app):
"""Test /collections endpoint."""
Expand All @@ -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
Expand All @@ -27,56 +29,70 @@ 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
assert "prev" not in rels

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
assert "prev" in rels

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()
assert body["numberMatched"] == 0

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"}
Expand Down
39 changes: 39 additions & 0 deletions tests/routes/test_geography.py
Original file line number Diff line number Diff line change
@@ -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"])
3 changes: 2 additions & 1 deletion tipg/dbmodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit a435d30

Please sign in to comment.