Skip to content

Commit

Permalink
Created a skeletal layer for map boundaries
Browse files Browse the repository at this point in the history
  • Loading branch information
davenquinn committed Jul 10, 2024
1 parent 03a747a commit 91afcd7
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.env
.venv
__pycache__
.pytest_cache
.pytest_cache
.idea
3 changes: 3 additions & 0 deletions macrostrat_tileserver/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ async def shutdown_event():

app.include_router(filterable_router, tags=["Filterable"], prefix="/v2")

from .map_bounds import router as map_bounds_router
app.include_router(map_bounds_router, tags=["Map Bounds"], prefix="/v2")


@app.get("/carto/rotation-models")
async def rotation_models():
Expand Down
62 changes: 62 additions & 0 deletions macrostrat_tileserver/map_bounds/__init__.py
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"
15 changes: 15 additions & 0 deletions macrostrat_tileserver/map_bounds/queries/rgeom.sql
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

0 comments on commit 91afcd7

Please sign in to comment.