Skip to content

Commit bbdc599

Browse files
committed
building tile vector using ST_ASMVT
1 parent dcfee9c commit bbdc599

File tree

1 file changed

+38
-13
lines changed

1 file changed

+38
-13
lines changed

application/routers/tiles.py

+38-13
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,49 @@ def build_db_query(tile, session: Session):
2626
lon_min, lat_min, lon_max, lat_max = tile_bounds(z, x, y)
2727

2828
geometry_column = "geometry"
29-
if dataset == "conservation-area":
30-
geometry_column = "point"
3129

3230
tile_width = 256
3331

3432
mvt_geom_query = text(
35-
f"""
36-
SELECT ST_AsMVT(q, 'entities_layer', {tile_width}, 'geom') AS mvt
37-
FROM (
38-
SELECT ST_AsMVTGeom(
39-
{geometry_column},
40-
ST_MakeEnvelope(:lon_min, :lat_min, :lon_max, :lat_max, 4326),
41-
{tile_width}, 4096, true) AS geom
42-
FROM entity
43-
WHERE dataset = :dataset
44-
AND ST_Intersects({geometry_column}, ST_MakeEnvelope(:lon_min, :lat_min, :lon_max, :lat_max, 4326))
33+
f"""SELECT ST_AsMVT(q, :dataset, :tile_width, 'geom') FROM
34+
(SELECT
35+
ST_AsMVTGeom(
36+
{geometry_column},
37+
ST_MakeEnvelope(:lon_min, :lat_min, :lon_max, :lat_max, 4326),
38+
:tile_width,
39+
4096,
40+
true
41+
) as geom,
42+
jsonb_build_object(
43+
'name', entity.name,
44+
'dataset', entity.dataset,
45+
'organisation-entity', entity.organisation_entity,
46+
'entity', entity.entity,
47+
'entry-date', entity.entry_date,
48+
'start-date', entity.start_date,
49+
'end-date', entity.end_date,
50+
'prefix', entity.prefix,
51+
'reference', entity.reference
52+
) AS properties
53+
FROM entity
54+
WHERE dataset = :dataset
55+
AND ST_Intersects({geometry_column}, ST_MakeEnvelope(:lon_min, :lat_min, :lon_max, :lat_max, 4326))
4556
) AS q
46-
"""
57+
"""
4758
)
59+
# f"""
60+
# SELECT ST_AsMVT(q, 'entities_layer', {tile_width}, 'geom') AS mvt
61+
# FROM (
62+
# SELECT ST_AsMVTGeom(
63+
# {geometry_column},
64+
# ST_MakeEnvelope(:lon_min, :lat_min, :lon_max, :lat_max, 4326),
65+
# {tile_width}, 4096, true) AS geom
66+
# FROM entity
67+
# WHERE dataset = :dataset
68+
# AND ST_Intersects({geometry_column}, ST_MakeEnvelope(:lon_min, :lat_min, :lon_max, :lat_max, 4326))
69+
# ) AS q
70+
# """
71+
# )
4872

4973
result = session.execute(
5074
mvt_geom_query,
@@ -54,6 +78,7 @@ def build_db_query(tile, session: Session):
5478
"lon_max": lon_max,
5579
"lat_max": lat_max,
5680
"dataset": dataset,
81+
"tile_width": tile_width,
5782
},
5883
).scalar()
5984
return result

0 commit comments

Comments
 (0)