-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
123 lines (98 loc) · 3.13 KB
/
utils.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
import cartopy.io.shapereader as shpreader
from shapely.geometry import Point
from sqlalchemy import text
def get_country(iso: str):
shp_countries = shpreader.natural_earth(
resolution="110m", category="cultural", name="admin_0_countries"
)
for record in shpreader.Reader(shp_countries).records():
if record.attributes["ISO_A2"].upper() == iso.upper():
return record.geometry
return None
def get_state(iso: str):
shp_states = shpreader.natural_earth(
resolution="110m", category="cultural", name="admin_1_states_provinces_lakes"
)
for record in shpreader.Reader(shp_states).records():
if record.attributes["iso_3166_2"].upper() == iso.upper():
return record.geometry
return None
def lat_lon_inside_geom(lat, lon, geometry):
"""test if lat lon is inside a WKT geometry
Parameters
----------
lat: float
latitude value
lon: float
longitude value
wkt: str
geometry in well-known-text format
Returns
-------
is_inside: bool
boolean value indicating whether lat, lon is inside the WKT
"""
point = Point(lon, lat)
return point.within(geometry)
def db_query_climatetrace(session, north, south, east, west):
query = text(
"""
SELECT DISTINCT lat, lon, filename, reference_number, locode
FROM asset
WHERE lat <= :north
AND lat >= :south
AND lon <= :east
AND lon >= :west;
"""
)
params = {"north": north, "south": south, "east": east, "west": west}
result = session.execute(query, params).fetchall()
return result
def db_query_edgar_by_iso(session, iso):
query = text(
"""
SELECT DISTINCT cc.locode
FROM "CityCellOverlapEdgar" AS cc
JOIN "GridCellEmissionsEdgar" AS gc ON gc.cell_id = cc.cell_id
WHERE cc.locode LIKE :iso || '%';
"""
)
params = {'iso': iso}
result = session.execute(query, params).fetchall()
return result
def db_query_edgar_by_range(session, north, south, east, west):
query = text(
"""
WITH "GridCells" AS (
SELECT DISTINCT id, lat_center, lon_center
FROM "GridCellEdgar"
WHERE lat_center <= :north
AND lat_center >= :south
AND lon_center <= :east
AND lon_center >= :west
)
SELECT
gc.lat_center AS lat,
gc.lon_center AS lon,
gce.reference_number,
cc.locode
FROM "GridCells" AS gc
JOIN "CityCellOverlapEdgar" AS cc
ON gc.id = cc.cell_id
JOIN "GridCellEmissionsEdgar" AS gce
ON gc.id = gce.cell_id
"""
)
params = {"north": north, "south": south, "east": east, "west": west}
result = session.execute(query, params).fetchall()
return result
def locode_data(session, locode):
query = text(
"""
SELECT geometry, bbox_north, bbox_south, bbox_east, bbox_west
FROM osm
WHERE locode = :locode
"""
)
results = session.execute(query, {"locode": locode}).fetchall()
return results