-
Notifications
You must be signed in to change notification settings - Fork 5
/
utilities.py
200 lines (157 loc) · 5.89 KB
/
utilities.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
"""FastVector App - Utilities"""
import os
import json
from fastapi import FastAPI
from pygeofilter.backends.sql import to_sql_where
from pygeofilter.parsers.ecql import parse
import config
async def get_tile(database: str, scheme: str, table: str, z: int,
x: int, y: int, fields: str, cql_filter: str, db_settings: object, app: FastAPI) -> bytes:
"""
Method to return vector tile from database.
"""
cachefile = f'{os.getcwd()}/cache/{database}_{scheme}_{table}/{z}/{x}/{y}'
if os.path.exists(cachefile):
return '', True
pool = app.state.databases[f'{database}_pool']
async with pool.acquire() as con:
sql_field_query = f"""
SELECT column_name
FROM information_schema.columns
WHERE table_name = '{table}'
AND column_name != 'geom';
"""
field_mapping = {}
db_fields = await con.fetch(sql_field_query)
for field in db_fields:
field_mapping[field['column_name']] = field['column_name']
if fields is None:
field_list = ""
for field in db_fields:
field_list += f", {field['column_name']}"
else:
field_list = f",{fields}"
sql_vector_query = f"""
SELECT ST_AsMVT(tile, '{scheme}.{table}', 4096)
FROM (
WITH
bounds AS (
SELECT ST_TileEnvelope({z}, {x}, {y}) as geom
)
SELECT
st_asmvtgeom(
ST_Transform(t.geom, 3857)
,bounds.geom
) AS mvtgeom {field_list}
FROM {scheme}.{table} as t, bounds
WHERE ST_Intersects(
ST_Transform(t.geom, 4326),
ST_Transform(bounds.geom, 4326)
)
"""
if cql_filter:
ast = parse(cql_filter)
where_statement = to_sql_where(ast, field_mapping)
sql_vector_query += f" AND {where_statement}"
sql_vector_query += f"LIMIT {db_settings['max_features_per_tile']}) as tile"
tile = await con.fetchval(sql_vector_query)
if fields is None and cql_filter is None and db_settings['cache_age_in_seconds'] > 0:
cachefile_dir = f'{os.getcwd()}/cache/{database}_{scheme}_{table}/{z}/{x}'
if not os.path.exists(cachefile_dir):
try:
os.makedirs(cachefile_dir)
except OSError:
pass
with open(cachefile, "wb") as file:
file.write(tile)
file.close()
return tile, False
async def get_tables_metadata(app: FastAPI) -> list:
"""
Method used to get tables metadata.
"""
tables_metadata = []
for database in config.DATABASES.items():
pool = app.state.databases[f'{database[0]}_pool']
async with pool.acquire() as con:
tables_query = """
SELECT schemaname, tablename
FROM pg_catalog.pg_tables
WHERE schemaname not in ('pg_catalog','information_schema', 'topology')
AND tablename != 'spatial_ref_sys';
"""
tables = await con.fetch(tables_query)
for table in tables:
tables_metadata.append(
{
"name" : table['tablename'],
"schema" : table['schemaname'],
"type" : "table",
"id" : f"{table['schemaname']}.{table['tablename']}",
"database" : config.DATABASES[database[0]]['database']
}
)
return tables_metadata
async def get_table_columns(database: str, scheme: str, table: str, app: FastAPI) -> list:
"""
Method used to retrieve columns for a given table.
"""
pool = app.state.databases[f'{database}_pool']
async with pool.acquire() as con:
column_query = f"""
SELECT
jsonb_agg(
jsonb_build_object(
'name', attname,
'type', format_type(atttypid, null),
'description', col_description(attrelid, attnum)
)
)
FROM pg_attribute
WHERE attnum>0
AND attrelid=format('%I.%I', '{scheme}', '{table}')::regclass
"""
columns = await con.fetchval(column_query)
return json.loads(columns)
async def get_table_geometry_type(database: str, scheme: str, table: str, app: FastAPI) -> list:
"""
Method used to retrieve the geometry type for a given table.
"""
pool = app.state.databases[f'{database}_pool']
async with pool.acquire() as con:
geometry_query = f"""
SELECT ST_GeometryType(geom) as geom_type
FROM {scheme}.{table}
"""
geometry_type = await con.fetchval(geometry_query)
return geometry_type
async def get_table_center(database: str, scheme: str, table: str, app: FastAPI) -> list:
"""
Method used to retrieve the table center for a given table.
"""
pool = app.state.databases[f'{database}_pool']
async with pool.acquire() as con:
query = f"""
SELECT ST_X(ST_Centroid(ST_Union(geom))) as x,
ST_Y(ST_Centroid(ST_Union(geom))) as y
FROM {scheme}.{table}
"""
center = await con.fetch(query)
return [center[0][0],center[0][1]]
async def get_table_bounds(database: str, scheme: str, table: str, app: FastAPI) -> list:
"""
Method used to retrieve the bounds for a given table.
"""
pool = app.state.databases[f'{database}_pool']
async with pool.acquire() as con:
query = f"""
SELECT ARRAY[
ST_XMin(ST_Union(geom)),
ST_YMin(ST_Union(geom)),
ST_XMax(ST_Union(geom)),
ST_YMax(ST_Union(geom))
]
FROM {scheme}.{table}
"""
extent = await con.fetchval(query)
return extent