Skip to content

Commit a435d30

Browse files
Merge pull request #47 from developmentseed/castGeographyToGeometry
cast geography to geometry
2 parents 99c7de7 + d7f3aaf commit a435d30

File tree

4 files changed

+73
-14
lines changed

4 files changed

+73
-14
lines changed

tests/fixtures/my_data.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,7 @@ ALTER TABLE public.my_data ADD COLUMN otherdt timestamptz;
1717
ALTER TABLE public.my_data ADD COLUMN othergeom geometry;
1818
UPDATE my_data SET otherdt=datetime+'1 year'::interval, othergeom=st_pointonsurface(geom);
1919
CREATE VIEW public.my_data_alt AS SELECT * FROM my_data;
20+
-- Create a copy of my_data but with geography instead of Geometry
21+
CREATE TABLE public.my_data_geo AS SELECT * FROM my_data;
22+
ALTER TABLE public.my_data_geo ALTER COLUMN geom TYPE geography(Polygon,4326) USING ST_Transform(geom,4326)::geography;
2023
COMMIT;

tests/routes/test_collections.py

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""Test /collections endpoints."""
22

3+
collection_number = 14
4+
35

46
def test_collections(app):
57
"""Test /collections endpoint."""
@@ -8,8 +10,8 @@ def test_collections(app):
810
assert response.headers["content-type"] == "application/json"
911
body = response.json()
1012
assert ["links", "numberMatched", "numberReturned", "collections"] == list(body)
11-
assert body["numberMatched"] == 13
12-
assert body["numberReturned"] == 13
13+
assert body["numberMatched"] == collection_number
14+
assert body["numberReturned"] == collection_number
1315

1416
ids = [x["id"] for x in body["collections"]]
1517
assert "public.landsat_wrs" in ids
@@ -27,56 +29,70 @@ def test_collections_search(app):
2729
"""Test /collections endpoint."""
2830
response = app.get("/collections", params={"limit": 1})
2931
body = response.json()
30-
assert body["numberMatched"] == 13
32+
assert body["numberMatched"] == collection_number
3133
assert body["numberReturned"] == 1
3234
rels = [x["rel"] for x in body["links"]]
3335
assert "next" in rels
3436
assert "prev" not in rels
3537

3638
response = app.get("/collections", params={"limit": 1, "offset": 1})
3739
body = response.json()
38-
assert body["numberMatched"] == 13
40+
assert body["numberMatched"] == collection_number
3941
assert body["numberReturned"] == 1
4042
rels = [x["rel"] for x in body["links"]]
4143
assert "next" in rels
4244
assert "prev" in rels
4345

44-
response = app.get("/collections", params={"limit": 1, "offset": 12})
46+
response = app.get(
47+
"/collections", params={"limit": 1, "offset": collection_number - 1}
48+
)
4549
body = response.json()
46-
assert body["numberMatched"] == 13
50+
assert body["numberMatched"] == collection_number
4751
assert body["numberReturned"] == 1
4852
rels = [x["rel"] for x in body["links"]]
4953
assert "next" not in rels
5054
assert "prev" in rels
5155

5256
response = app.get("/collections", params={"bbox": "-180,81,180,87"})
5357
body = response.json()
54-
assert body["numberMatched"] == 10
58+
assert (
59+
body["numberMatched"] == collection_number - 3
60+
) # 2 collections are not within the bbox
5561
ids = [x["id"] for x in body["collections"]]
5662
assert "public.nongeo_data" not in ids
5763
assert "public.canada" not in ids
5864

5965
response = app.get("/collections", params={"datetime": "../2022-12-31T23:59:59Z"})
6066
body = response.json()
61-
assert body["numberMatched"] == 3
67+
assert body["numberMatched"] == 4
6268
ids = [x["id"] for x in body["collections"]]
63-
assert ["public.my_data", "public.my_data_alt", "public.nongeo_data"] == ids
69+
assert [
70+
"public.my_data",
71+
"public.my_data_alt",
72+
"public.my_data_geo",
73+
"public.nongeo_data",
74+
] == ids
6475

6576
response = app.get("/collections", params={"datetime": "2022-12-31T23:59:59Z/.."})
6677
body = response.json()
6778
assert body["numberMatched"] == 0
6879

6980
response = app.get("/collections", params={"datetime": "2003-12-31T23:59:59Z/.."})
7081
body = response.json()
71-
assert body["numberMatched"] == 3
82+
assert body["numberMatched"] == 4
7283
ids = [x["id"] for x in body["collections"]]
73-
assert ["public.my_data", "public.my_data_alt", "public.nongeo_data"] == ids
84+
assert [
85+
"public.my_data",
86+
"public.my_data_alt",
87+
"public.my_data_geo",
88+
"public.nongeo_data",
89+
] == ids
7490

7591
response = app.get("/collections", params={"datetime": "2004-12-31T23:59:59Z/.."})
7692
body = response.json()
77-
assert body["numberMatched"] == 2
93+
assert body["numberMatched"] == 3
7894
ids = [x["id"] for x in body["collections"]]
79-
assert ["public.my_data", "public.my_data_alt"] == ids
95+
assert ["public.my_data", "public.my_data_alt", "public.my_data_geo"] == ids
8096

8197
response = app.get(
8298
"/collections", params={"datetime": "2004-01-01T00:00:00Z/2004-12-31T23:59:59Z"}

tests/routes/test_geography.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"""test tipg endpoint with table having a geography column."""
2+
3+
import mapbox_vector_tile
4+
import numpy
5+
6+
7+
def test_geography_column(app):
8+
response = app.get("/collections/public.my_data_geo")
9+
assert response.status_code == 200
10+
assert response.headers["content-type"] == "application/json"
11+
body = response.json()
12+
assert body["id"] == "public.my_data_geo"
13+
14+
response = app.get("/collections/public.my_data_geo/items")
15+
assert response.status_code == 200
16+
assert response.headers["content-type"] == "application/geo+json"
17+
body = response.json()
18+
assert body["type"] == "FeatureCollection"
19+
assert body["id"] == "public.my_data_geo"
20+
assert body["title"] == "public.my_data_geo"
21+
assert body["links"]
22+
assert body["numberMatched"] == 6
23+
assert body["numberReturned"] == 6
24+
assert body["features"][0]["geometry"]["type"] == "Polygon"
25+
26+
response = app.get("/collections/public.my_data_geo/tilejson.json")
27+
assert response.status_code == 200
28+
resp_json = response.json()
29+
assert resp_json["name"] == "public.my_data_geo"
30+
assert resp_json["minzoom"] == 5
31+
assert resp_json["maxzoom"] == 12
32+
numpy.testing.assert_almost_equal(
33+
resp_json["bounds"], [-47.5356, 74.8049, -8.97407, 81.8555]
34+
)
35+
36+
response = app.get("/collections/public.my_data_geo/tiles/5/11/5")
37+
assert response.status_code == 200
38+
decoded = mapbox_vector_tile.decode(response.content)
39+
assert len(decoded["default"]["features"])

tipg/dbmodel.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,8 @@ def _select_mvt(
356356
tile: Tile,
357357
):
358358
"""Create MVT from intersecting geometries."""
359-
geom = logic.V(geometry_column.name)
359+
print(geometry_column.type)
360+
geom = pg_funcs.cast(logic.V(geometry_column.name), "geometry")
360361

361362
# make sure the geometries do not overflow the TMS bbox
362363
if not tms.is_valid(tile):

0 commit comments

Comments
 (0)