-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created a skeletal layer for map boundaries
- Loading branch information
1 parent
03a747a
commit 91afcd7
Showing
4 changed files
with
82 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
.env | ||
.venv | ||
__pycache__ | ||
.pytest_cache | ||
.pytest_cache | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
from pathlib import Path | ||
|
||
from buildpg import render | ||
from fastapi import APIRouter, Request, Response | ||
from timvt.resources.enums import MimeTypes | ||
|
||
router = APIRouter() | ||
|
||
__here__ = Path(__file__).parent | ||
|
||
|
||
@router.get("/rgeom/{z}/{x}/{y}") | ||
async def rgeom( | ||
request: Request, | ||
compilation: Compilation, | ||
z: int, | ||
x: int, | ||
y: int, | ||
): | ||
"""Get a tile from the tileserver.""" | ||
pool = request.app.state.pool | ||
|
||
async with pool.acquire() as con: | ||
data = await run_layer_query( | ||
con, | ||
"rgeom", | ||
z=z, | ||
x=x, | ||
y=y, | ||
) | ||
data = join_layers([units_, lines_]) | ||
kwargs = {} | ||
kwargs.setdefault("media_type", MimeTypes.pbf.value) | ||
return Response(data, **kwargs) | ||
|
||
|
||
async def run_layer_query(con, layer_name, **params): | ||
query = get_layer_sql(layer_name) | ||
q, p = render(query, layer_name=layer_name, **params) | ||
|
||
# Overcomes a shortcoming in buildpg that deems casting to an array as unsafe | ||
# https://github.com/samuelcolvin/buildpg/blob/e2a16abea5c7607b53c501dbae74a5765ba66e15/buildpg/components.py#L21 | ||
q = q.replace("textarray", "text[]") | ||
|
||
print(q,p) | ||
|
||
return await con.fetchval(q, *p) | ||
|
||
|
||
def get_layer_sql(layer: str): | ||
query = __here__ / "queries" / (layer + ".sql") | ||
|
||
q = query.read_text() | ||
q = q.strip() | ||
if q.endswith(";"): | ||
q = q[:-1] | ||
|
||
# Replace the envelope with the function call. Kind of awkward. | ||
q = q.replace(":envelope", "tile_utils.envelope(:x, :y, :z)") | ||
|
||
# Wrap with MVT creation | ||
return f"WITH feature_query AS ({q}) SELECT ST_AsMVT(feature_query, :layer_name) FROM feature_query" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
WITH mvt_features AS ( | ||
SELECT | ||
source_id, | ||
rgeom geom | ||
FROM maps.sources | ||
WHERE | ||
rgeom is NOT NULL | ||
AND status_code = 'active' | ||
AND ST_Intersects(geom, ST_Transform(:envelope, 4326)) | ||
|
||
) | ||
SELECT | ||
source_id, | ||
tile_layers.tile_geom(z.geom, :envelope) AS geom | ||
FROM mvt_features z |