Skip to content
This repository was archived by the owner on Dec 22, 2025. It is now read-only.

Commit 39a1093

Browse files
authored
Merge pull request #50 from UW-Macrostrat/single-map
Single map
2 parents 4ba4df9 + 6330ec4 commit 39a1093

File tree

5 files changed

+112
-0
lines changed

5 files changed

+112
-0
lines changed

.idea/sqldialects.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
from pathlib import Path
2+
from typing import List
3+
4+
from buildpg import V, render
5+
from fastapi import APIRouter, Request, Query
6+
from macrostrat.utils import get_logger
7+
8+
from ..utils import scales_for_zoom, MapCompilation, get_layer_sql, VectorTileResponse
9+
10+
log = get_logger(__name__)
11+
12+
router = APIRouter()
13+
14+
__here__ = Path(__file__).parent
15+
16+
17+
@router.get("/{slug}/{z}/{x}/{y}")
18+
async def get_tile(
19+
request: Request,
20+
slug: str,
21+
z: int,
22+
x: int,
23+
y: int,
24+
):
25+
"""Get a tile from the tileserver."""
26+
pool = request.app.state.pool
27+
28+
params = dict(
29+
z=z,
30+
x=x,
31+
y=y,
32+
slug=slug,
33+
)
34+
35+
async with pool.acquire() as con:
36+
units_ = await run_layer_query(
37+
con,
38+
"units",
39+
compilation=V(compilation_name + ".polygons"),
40+
lithology=lithology,
41+
**params,
42+
)
43+
lines_ = await run_layer_query(
44+
con, "lines", compilation=V(compilation_name + ".lines"), **params
45+
)
46+
return VectorTileResponse(units_, lines_)
47+
48+
49+
def build_lithology_clause(lithology: List[str]):
50+
"""Build a WHERE clause to filter by lithology."""
51+
if lithology is None or len(lithology) == 0:
52+
return "true"
53+
54+
LITHOLOGY_COLUMNS = [
55+
"lith_group",
56+
"lith_class",
57+
"lith_type",
58+
"lith",
59+
]
60+
61+
cols = [f"liths.{col}::text = ANY(:lithology)" for col in LITHOLOGY_COLUMNS]
62+
q = " OR ".join(cols)
63+
return f"({q})"
64+
65+
66+
async def run_layer_query(con, layer_name, **params):
67+
query = get_layer_sql(__here__ / "queries", layer_name)
68+
if ":where_lithology" in query:
69+
lith_clause = build_lithology_clause(params.get("lithology"))
70+
query = query.replace(":where_lithology", lith_clause)
71+
q, p = render(query, layer_name=layer_name, **params)
72+
return await con.fetchval(q, *p)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
SELECT
2+
l.line_id,
3+
l.source_id,
4+
coalesce(l.descrip, '') AS descrip,
5+
coalesce(l.name, '') AS name,
6+
coalesce(l.direction, '') AS direction,
7+
coalesce(l.type, '') AS "type",
8+
tile_layers.tile_geom(l.geom, :envelope) AS geom
9+
FROM maps.lines l
10+
JOIN maps.sources s ON l.source_id = s.source_id
11+
WHERE s.slug = :slug
12+
AND ST_Intersects(geom, ST_Transform(:envelope, 4326))
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
SELECT
3+
strike,
4+
dip,
5+
dip_dir,
6+
point_type,
7+
certainty,
8+
comments,
9+
tile_layers.tile_geom(geom, :envelope) AS geom
10+
FROM maps.points p
11+
JOIN maps.sources s
12+
WHERE p.source_id = s.source_id
13+
AND ST_Intersects(geom, ST_Transform(:envelope, 4326))
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
SELECT
2+
p.map_id,
3+
p.source_id,
4+
l.*, -- map legend info
5+
tile_layers.tile_geom(z.geom, :envelope) AS geom
6+
FROM maps.polygons p
7+
JOIN maps.sources s
8+
ON p.source_id = s.source_id
9+
LEFT JOIN maps.map_legend ml
10+
ON p.map_id = ml.map_id
11+
LEFT JOIN tile_layers.map_legend_info AS l
12+
ON l.legend_id = ml.legend_id
13+
WHERE s.slug = :slug
14+
AND ST_Intersects(geom, ST_Transform(:envelope, 4326))

0 commit comments

Comments
 (0)