-
Notifications
You must be signed in to change notification settings - Fork 0
/
utilities.py
193 lines (161 loc) · 5.05 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
import os
base_url = "https://minio.carlboettiger.info"
# make sure h3 is installed.
import duckdb
db = duckdb.connect()
db.install_extension("h3", repository = "community")
db.close()
# enable ibis to use built-in function from the h3 extension
import ibis
from ibis import _
@ibis.udf.scalar.builtin
def h3_cell_to_boundary_wkt (array) -> str:
...
# Configure write-access to source.coop
import streamlit as st
def set_source_secrets(con):
secret = os.getenv("SOURCE_SECRET")
if secret is None:
secret = st.secrets["SOURCE_SECRET"]
key = os.getenv("SOURCE_KEY")
if key is None:
key = st.secrets["SOURCE_KEY"]
query= f'''
CREATE OR REPLACE SECRET source (
TYPE S3,
KEY_ID '{key}',
SECRET '{secret}',
ENDPOINT 'data.source.coop',
URL_STYLE 'path',
SCOPE 's3://cboettig'
);
set THREADS=100;
'''
con.raw_sql(query)
def set_aws_secrets(con):
query= f'''
CREATE OR REPLACE SECRET aws (
TYPE S3,
ENDPOINT 's3.us-west-2.amazonaws.com',
SCOPE 's3://overturemaps-us-west-2/release/'
);
'''
# ENDPOINT 'data.source.coop',
con.raw_sql(query)
# or write access to minio
def set_secrets(con):
secret = os.getenv("MINIO_SECRET")
if secret is None:
secret = st.secrets["MINIO_SECRET"]
key = os.getenv("MINIO_KEY")
if key is None:
key = st.secrets["MINIO_KEY"]
query= f'''
CREATE OR REPLACE SECRET secret2 (
TYPE S3,
KEY_ID '{key}',
SECRET '{secret}',
ENDPOINT 'minio.carlboettiger.info',
URL_STYLE 'path',
SCOPE 's3://public-gbif/'
);
'''
con.raw_sql(query)
import minio
def s3_client(type="minio"):
minio_key = st.secrets["MINIO_KEY"]
minio_secret = st.secrets["MINIO_SECRET"]
client = minio.Minio("minio.carlboettiger.info", minio_key, minio_secret)
if type == "minio":
return client
source_key = st.secrets["SOURCE_KEY"]
source_secret = st.secrets["SOURCE_SECRET"]
client = minio.Minio("data.source.coop", source_key, source_secret)
return client
import pydeck as pdk
def HexagonLayer(data, v_scale = 1):
return pdk.Layer(
"H3HexagonLayer",
id="gbif",
data=data,
extruded=True,
get_elevation="value",
get_hexagon="hex",
elevation_scale = 50 * v_scale,
elevation_range = [0,1],
pickable=True,
auto_highlight=True,
get_fill_color="[255 - value, 255, value]",
)
def terrain_styling():
maptiler_key = os.getenv("MAPTILER_KEY")
if maptiler_key is None:
maptiler_key = st.secrets["MAPTILER_KEY"]
terrain_style = {
"version": 8,
"sources": {
"osm": {
"type": "raster",
"tiles": ["https://server.arcgisonline.com/ArcGIS/rest/services/NatGeo_World_Map/MapServer/tile/{z}/{y}/{x}.png"],
"tileSize": 256,
"attribution": "© National Geographic",
"maxzoom": 19,
},
"terrainSource": {
"type": "raster-dem",
"url": f"https://api.maptiler.com/tiles/terrain-rgb-v2/tiles.json?key={maptiler_key}",
"tileSize": 256,
},
"hillshadeSource": {
"type": "raster-dem",
"url": f"https://api.maptiler.com/tiles/terrain-rgb-v2/tiles.json?key={maptiler_key}",
"tileSize": 256,
},
},
"layers": [
{"id": "osm", "type": "raster", "source": "osm"},
{
"id": "hills",
"type": "hillshade",
"source": "hillshadeSource",
"layout": {"visibility": "visible"},
"paint": {"hillshade-shadow-color": "#473B24"},
},
],
"terrain": {"source": "terrainSource", "exaggeration": .1},
}
return terrain_style
####
## grab polygon of a National park:
import ibis
from ibis import _
import geopandas as gpd
def get_city(name = "Oakland", con = ibis.duckdb.connect()):
gdf = (con
.read_geo("/vsicurl/https://data.source.coop/cboettig/us-boundaries/mappinginequality.json")
.filter(_.city == name)
.agg(geom = _.geom.unary_union())
).execute()
return gdf
def get_polygon(name = "New Haven",
source = "City",
_con = ibis.duckdb.connect()):
match source:
case 'City':
gdf = get_city(name, _con)
case 'Custom':
gdf = gpd.read_file(name)
case "All":
gdf = None
case _:
gdf = None
return gdf
import hashlib
import pandas as pd
def unique_path(gdf_name, rank, taxa, zoom, distinct_taxa):
#gdf_hash = str(pd.util.hash_pandas_object(gdf).sum())
text = [gdf_name, rank, taxa, str(zoom), distinct_taxa]
sig = "-".join(text)
sig = hashlib.sha1(sig.encode()).hexdigest()
dest = "cache/gbif_" + sig + ".json"
return dest