Spaces:
Sleeping
Sleeping
test
Browse files- app.py +16 -10
- static-maps.ipynb +56 -135
app.py
CHANGED
@@ -5,16 +5,17 @@
|
|
5 |
|
6 |
import ibis
|
7 |
from ibis import _
|
8 |
-
|
9 |
|
|
|
10 |
|
11 |
state_boundaries = "https://data.source.coop/cboettig/us-boundaries/us-state-territory.parquet"
|
12 |
county_boundaries = "https://data.source.coop/cboettig/us-boundaries/us-county.parquet"
|
13 |
-
|
14 |
states = conn.read_parquet(state_boundaries).rename(state_id = "STUSPS", state = "NAME")
|
15 |
county = conn.read_parquet(county_boundaries).rename(county = "NAMELSAD", state = "STATE_NAME")
|
16 |
|
17 |
-
votes = conn.read_csv("
|
|
|
18 |
|
19 |
vote = (votes
|
20 |
.filter(_["Jurisdiction Type"] == "County")
|
@@ -22,22 +23,22 @@ vote = (votes
|
|
22 |
.mutate(key = _.county + ibis.literal('-') + _.state_id)
|
23 |
.rename(amount = 'Conservation Funds at Stake', yes = '% Yes')
|
24 |
.mutate(amount_n=_.amount.replace('$', '').replace(',', '').cast('float'))
|
25 |
-
.
|
|
|
|
|
26 |
)
|
27 |
df = (county
|
28 |
.join(states.select("state", "state_id"), "state")
|
29 |
.mutate(key = _.county + ibis.literal('-') + _.state_id)
|
30 |
.select('key', 'geometry')
|
31 |
.right_join(vote, "key")
|
32 |
-
|
33 |
)
|
34 |
-
# 20 of 23 counties have matches
|
35 |
-
gdf = df.execute()
|
36 |
|
|
|
|
|
37 |
|
38 |
|
39 |
-
import leafmap.maplibregl as leafmap
|
40 |
-
m = leafmap.Map(style="positron")
|
41 |
|
42 |
outcome = [
|
43 |
'match',
|
@@ -48,9 +49,14 @@ outcome = [
|
|
48 |
]
|
49 |
paint = {"fill-extrusion-color": outcome,
|
50 |
"fill-extrusion-opacity": 0.7,
|
51 |
-
"fill-extrusion-height": ["*", ["get", "
|
52 |
}
|
53 |
|
54 |
#m.add_geojson("vote.geojson")
|
|
|
|
|
55 |
m.add_gdf(gdf, "fill-extrusion", paint = paint)
|
56 |
m.to_streamlit()
|
|
|
|
|
|
|
|
5 |
|
6 |
import ibis
|
7 |
from ibis import _
|
8 |
+
import streamlit as st
|
9 |
|
10 |
+
conn = ibis.duckdb.connect(extensions=["spatial"])
|
11 |
|
12 |
state_boundaries = "https://data.source.coop/cboettig/us-boundaries/us-state-territory.parquet"
|
13 |
county_boundaries = "https://data.source.coop/cboettig/us-boundaries/us-county.parquet"
|
|
|
14 |
states = conn.read_parquet(state_boundaries).rename(state_id = "STUSPS", state = "NAME")
|
15 |
county = conn.read_parquet(county_boundaries).rename(county = "NAMELSAD", state = "STATE_NAME")
|
16 |
|
17 |
+
votes = conn.read_csv("landvote.csv")
|
18 |
+
votes.count().execute()
|
19 |
|
20 |
vote = (votes
|
21 |
.filter(_["Jurisdiction Type"] == "County")
|
|
|
23 |
.mutate(key = _.county + ibis.literal('-') + _.state_id)
|
24 |
.rename(amount = 'Conservation Funds at Stake', yes = '% Yes')
|
25 |
.mutate(amount_n=_.amount.replace('$', '').replace(',', '').cast('float'))
|
26 |
+
.mutate(log_amount=_.amount_n.log())
|
27 |
+
.mutate(year=_['Date'].year().cast('int32'))
|
28 |
+
.select('key', 'Status', 'yes', 'year', 'amount', 'log_amount', )
|
29 |
)
|
30 |
df = (county
|
31 |
.join(states.select("state", "state_id"), "state")
|
32 |
.mutate(key = _.county + ibis.literal('-') + _.state_id)
|
33 |
.select('key', 'geometry')
|
34 |
.right_join(vote, "key")
|
35 |
+
.drop('key_right')
|
36 |
)
|
|
|
|
|
37 |
|
38 |
+
year = st.slider("Select a year", min_value=1988, max_value=2024, value=2022, step=1)
|
39 |
+
gdf = df.filter(_.year==year).execute()
|
40 |
|
41 |
|
|
|
|
|
42 |
|
43 |
outcome = [
|
44 |
'match',
|
|
|
49 |
]
|
50 |
paint = {"fill-extrusion-color": outcome,
|
51 |
"fill-extrusion-opacity": 0.7,
|
52 |
+
"fill-extrusion-height": ["*", ["get", "log_amount"], 5000],
|
53 |
}
|
54 |
|
55 |
#m.add_geojson("vote.geojson")
|
56 |
+
import leafmap.maplibregl as leafmap
|
57 |
+
m = leafmap.Map(style="positron")
|
58 |
m.add_gdf(gdf, "fill-extrusion", paint = paint)
|
59 |
m.to_streamlit()
|
60 |
+
|
61 |
+
|
62 |
+
|
static-maps.ipynb
CHANGED
@@ -2,166 +2,48 @@
|
|
2 |
"cells": [
|
3 |
{
|
4 |
"cell_type": "code",
|
5 |
-
"execution_count":
|
6 |
"id": "b313a218-4778-4d5b-9036-f0370d4212a0",
|
7 |
"metadata": {},
|
8 |
"outputs": [],
|
9 |
"source": [
|
10 |
"import ibis\n",
|
11 |
"from ibis import _\n",
|
12 |
-
"
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
"cell_type": "code",
|
17 |
-
"execution_count": 73,
|
18 |
-
"id": "fbbd28ea-f1e3-4f3d-ab99-a2ae9b04f3d4",
|
19 |
-
"metadata": {
|
20 |
-
"scrolled": true
|
21 |
-
},
|
22 |
-
"outputs": [
|
23 |
-
{
|
24 |
-
"data": {
|
25 |
-
"text/plain": [
|
26 |
-
"74"
|
27 |
-
]
|
28 |
-
},
|
29 |
-
"execution_count": 73,
|
30 |
-
"metadata": {},
|
31 |
-
"output_type": "execute_result"
|
32 |
-
}
|
33 |
-
],
|
34 |
-
"source": [
|
35 |
"state_boundaries = \"https://data.source.coop/cboettig/us-boundaries/us-state-territory.parquet\"\n",
|
36 |
"county_boundaries = \"https://data.source.coop/cboettig/us-boundaries/us-county.parquet\"\n",
|
37 |
-
"\n",
|
38 |
"states = conn.read_parquet(state_boundaries).rename(state_id = \"STUSPS\", state = \"NAME\")\n",
|
39 |
"county = conn.read_parquet(county_boundaries).rename(county = \"NAMELSAD\", state = \"STATE_NAME\")\n",
|
40 |
"\n",
|
41 |
-
"votes = conn.read_csv(\"
|
42 |
-
"
|
43 |
-
"
|
44 |
-
]
|
45 |
-
},
|
46 |
-
{
|
47 |
-
"cell_type": "code",
|
48 |
-
"execution_count": 74,
|
49 |
-
"id": "f766d5ea-1a1b-4113-b985-f2014c8935c9",
|
50 |
-
"metadata": {},
|
51 |
-
"outputs": [
|
52 |
-
{
|
53 |
-
"data": {
|
54 |
-
"text/html": [
|
55 |
-
"<div>\n",
|
56 |
-
"<style scoped>\n",
|
57 |
-
" .dataframe tbody tr th:only-of-type {\n",
|
58 |
-
" vertical-align: middle;\n",
|
59 |
-
" }\n",
|
60 |
-
"\n",
|
61 |
-
" .dataframe tbody tr th {\n",
|
62 |
-
" vertical-align: top;\n",
|
63 |
-
" }\n",
|
64 |
-
"\n",
|
65 |
-
" .dataframe thead th {\n",
|
66 |
-
" text-align: right;\n",
|
67 |
-
" }\n",
|
68 |
-
"</style>\n",
|
69 |
-
"<table border=\"1\" class=\"dataframe\">\n",
|
70 |
-
" <thead>\n",
|
71 |
-
" <tr style=\"text-align: right;\">\n",
|
72 |
-
" <th></th>\n",
|
73 |
-
" <th>Jurisdiction Type</th>\n",
|
74 |
-
" <th>n</th>\n",
|
75 |
-
" </tr>\n",
|
76 |
-
" </thead>\n",
|
77 |
-
" <tbody>\n",
|
78 |
-
" <tr>\n",
|
79 |
-
" <th>0</th>\n",
|
80 |
-
" <td>County</td>\n",
|
81 |
-
" <td>23</td>\n",
|
82 |
-
" </tr>\n",
|
83 |
-
" <tr>\n",
|
84 |
-
" <th>1</th>\n",
|
85 |
-
" <td>Municipal</td>\n",
|
86 |
-
" <td>48</td>\n",
|
87 |
-
" </tr>\n",
|
88 |
-
" <tr>\n",
|
89 |
-
" <th>2</th>\n",
|
90 |
-
" <td>Special District</td>\n",
|
91 |
-
" <td>1</td>\n",
|
92 |
-
" </tr>\n",
|
93 |
-
" <tr>\n",
|
94 |
-
" <th>3</th>\n",
|
95 |
-
" <td>State</td>\n",
|
96 |
-
" <td>2</td>\n",
|
97 |
-
" </tr>\n",
|
98 |
-
" </tbody>\n",
|
99 |
-
"</table>\n",
|
100 |
-
"</div>"
|
101 |
-
],
|
102 |
-
"text/plain": [
|
103 |
-
" Jurisdiction Type n\n",
|
104 |
-
"0 County 23\n",
|
105 |
-
"1 Municipal 48\n",
|
106 |
-
"2 Special District 1\n",
|
107 |
-
"3 State 2"
|
108 |
-
]
|
109 |
-
},
|
110 |
-
"execution_count": 74,
|
111 |
-
"metadata": {},
|
112 |
-
"output_type": "execute_result"
|
113 |
-
}
|
114 |
-
],
|
115 |
-
"source": [
|
116 |
-
"#votes.group_by(_.State).agg(n= _.count()).execute()\n",
|
117 |
-
"votes.group_by(_[\"Jurisdiction Type\"]).agg(n= _.count()).execute()\n",
|
118 |
-
"#states.head().execute()"
|
119 |
-
]
|
120 |
-
},
|
121 |
-
{
|
122 |
-
"cell_type": "code",
|
123 |
-
"execution_count": 95,
|
124 |
-
"id": "295b0b78-9c2a-436d-aa9c-34899858e637",
|
125 |
-
"metadata": {},
|
126 |
-
"outputs": [
|
127 |
-
{
|
128 |
-
"data": {
|
129 |
-
"application/vnd.jupyter.widget-view+json": {
|
130 |
-
"model_id": "f05ac36b0a714711bf4dfaffa490e77a",
|
131 |
-
"version_major": 2,
|
132 |
-
"version_minor": 0
|
133 |
-
},
|
134 |
-
"text/plain": [
|
135 |
-
"FloatProgress(value=0.0, layout=Layout(width='auto'), style=ProgressStyle(bar_color='black'))"
|
136 |
-
]
|
137 |
-
},
|
138 |
-
"metadata": {},
|
139 |
-
"output_type": "display_data"
|
140 |
-
}
|
141 |
-
],
|
142 |
-
"source": [
|
143 |
"vote = (votes\n",
|
144 |
" .filter(_[\"Jurisdiction Type\"] == \"County\")\n",
|
145 |
" .rename(county = \"Jurisdiction Name\", state_id = \"State\")\n",
|
146 |
" .mutate(key = _.county + ibis.literal('-') + _.state_id)\n",
|
147 |
" .rename(amount = 'Conservation Funds at Stake', yes = '% Yes')\n",
|
148 |
" .mutate(amount_n=_.amount.replace('$', '').replace(',', '').cast('float'))\n",
|
149 |
-
" .
|
|
|
|
|
150 |
" )\n",
|
151 |
"df = (county\n",
|
152 |
" .join(states.select(\"state\", \"state_id\"), \"state\")\n",
|
153 |
" .mutate(key = _.county + ibis.literal('-') + _.state_id)\n",
|
154 |
" .select('key', 'geometry')\n",
|
155 |
" .right_join(vote, \"key\")\n",
|
156 |
-
"
|
157 |
" )\n",
|
158 |
-
"
|
159 |
-
"gdf = df.execute()"
|
160 |
]
|
161 |
},
|
162 |
{
|
163 |
"cell_type": "code",
|
164 |
-
"execution_count":
|
165 |
"id": "fa397626-6e94-4ab9-a3bb-2bcbd14e8d40",
|
166 |
"metadata": {
|
167 |
"scrolled": true
|
@@ -170,7 +52,21 @@
|
|
170 |
{
|
171 |
"data": {
|
172 |
"application/vnd.jupyter.widget-view+json": {
|
173 |
-
"model_id": "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
174 |
"version_major": 2,
|
175 |
"version_minor": 1
|
176 |
},
|
@@ -178,7 +74,7 @@
|
|
178 |
"Map(height='600px', map_options={'bearing': 0, 'center': (0, 20), 'pitch': 0, 'style': 'https://tiles.openfree…"
|
179 |
]
|
180 |
},
|
181 |
-
"execution_count":
|
182 |
"metadata": {},
|
183 |
"output_type": "execute_result"
|
184 |
}
|
@@ -187,6 +83,9 @@
|
|
187 |
"import leafmap.maplibregl as leafmap\n",
|
188 |
"m = leafmap.Map(style=\"positron\")\n",
|
189 |
"\n",
|
|
|
|
|
|
|
190 |
"outcome = [\n",
|
191 |
" 'match',\n",
|
192 |
" ['get', 'Status'], \n",
|
@@ -196,13 +95,35 @@
|
|
196 |
" ]\n",
|
197 |
"paint = {\"fill-extrusion-color\": outcome, \n",
|
198 |
" \"fill-extrusion-opacity\": 0.7,\n",
|
199 |
-
" \"fill-extrusion-height\": [\"*\", [\"get\", \"
|
200 |
" }\n",
|
201 |
"\n",
|
202 |
"#m.add_geojson(\"vote.geojson\")\n",
|
203 |
"m.add_gdf(gdf, \"fill-extrusion\", paint = paint)\n",
|
204 |
"m"
|
205 |
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
206 |
}
|
207 |
],
|
208 |
"metadata": {
|
|
|
2 |
"cells": [
|
3 |
{
|
4 |
"cell_type": "code",
|
5 |
+
"execution_count": 124,
|
6 |
"id": "b313a218-4778-4d5b-9036-f0370d4212a0",
|
7 |
"metadata": {},
|
8 |
"outputs": [],
|
9 |
"source": [
|
10 |
"import ibis\n",
|
11 |
"from ibis import _\n",
|
12 |
+
"import streamlit as st\n",
|
13 |
+
"\n",
|
14 |
+
"conn = ibis.duckdb.connect(extensions=[\"spatial\"])\n",
|
15 |
+
"\n",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
"state_boundaries = \"https://data.source.coop/cboettig/us-boundaries/us-state-territory.parquet\"\n",
|
17 |
"county_boundaries = \"https://data.source.coop/cboettig/us-boundaries/us-county.parquet\"\n",
|
|
|
18 |
"states = conn.read_parquet(state_boundaries).rename(state_id = \"STUSPS\", state = \"NAME\")\n",
|
19 |
"county = conn.read_parquet(county_boundaries).rename(county = \"NAMELSAD\", state = \"STATE_NAME\")\n",
|
20 |
"\n",
|
21 |
+
"votes = conn.read_csv(\"landvote.csv\")\n",
|
22 |
+
"votes.count().execute()\n",
|
23 |
+
"\n",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
"vote = (votes\n",
|
25 |
" .filter(_[\"Jurisdiction Type\"] == \"County\")\n",
|
26 |
" .rename(county = \"Jurisdiction Name\", state_id = \"State\")\n",
|
27 |
" .mutate(key = _.county + ibis.literal('-') + _.state_id)\n",
|
28 |
" .rename(amount = 'Conservation Funds at Stake', yes = '% Yes')\n",
|
29 |
" .mutate(amount_n=_.amount.replace('$', '').replace(',', '').cast('float'))\n",
|
30 |
+
" .mutate(log_amount=_.amount_n.log())\n",
|
31 |
+
" .mutate(year=_['Date'].year().cast('int32'))\n",
|
32 |
+
" .select('key', 'Status', 'yes', 'year', 'amount', 'log_amount', )\n",
|
33 |
" )\n",
|
34 |
"df = (county\n",
|
35 |
" .join(states.select(\"state\", \"state_id\"), \"state\")\n",
|
36 |
" .mutate(key = _.county + ibis.literal('-') + _.state_id)\n",
|
37 |
" .select('key', 'geometry')\n",
|
38 |
" .right_join(vote, \"key\")\n",
|
39 |
+
" .drop('key_right')\n",
|
40 |
" )\n",
|
41 |
+
"\n"
|
|
|
42 |
]
|
43 |
},
|
44 |
{
|
45 |
"cell_type": "code",
|
46 |
+
"execution_count": 133,
|
47 |
"id": "fa397626-6e94-4ab9-a3bb-2bcbd14e8d40",
|
48 |
"metadata": {
|
49 |
"scrolled": true
|
|
|
52 |
{
|
53 |
"data": {
|
54 |
"application/vnd.jupyter.widget-view+json": {
|
55 |
+
"model_id": "e447c30d35374eb4a1d0a28b83b78159",
|
56 |
+
"version_major": 2,
|
57 |
+
"version_minor": 0
|
58 |
+
},
|
59 |
+
"text/plain": [
|
60 |
+
"FloatProgress(value=0.0, layout=Layout(width='auto'), style=ProgressStyle(bar_color='black'))"
|
61 |
+
]
|
62 |
+
},
|
63 |
+
"metadata": {},
|
64 |
+
"output_type": "display_data"
|
65 |
+
},
|
66 |
+
{
|
67 |
+
"data": {
|
68 |
+
"application/vnd.jupyter.widget-view+json": {
|
69 |
+
"model_id": "b325fe101b644a36a48fb1ec7bc0fcd2",
|
70 |
"version_major": 2,
|
71 |
"version_minor": 1
|
72 |
},
|
|
|
74 |
"Map(height='600px', map_options={'bearing': 0, 'center': (0, 20), 'pitch': 0, 'style': 'https://tiles.openfree…"
|
75 |
]
|
76 |
},
|
77 |
+
"execution_count": 133,
|
78 |
"metadata": {},
|
79 |
"output_type": "execute_result"
|
80 |
}
|
|
|
83 |
"import leafmap.maplibregl as leafmap\n",
|
84 |
"m = leafmap.Map(style=\"positron\")\n",
|
85 |
"\n",
|
86 |
+
"\n",
|
87 |
+
"gdf = df.filter(_.year==2022).execute()\n",
|
88 |
+
"\n",
|
89 |
"outcome = [\n",
|
90 |
" 'match',\n",
|
91 |
" ['get', 'Status'], \n",
|
|
|
95 |
" ]\n",
|
96 |
"paint = {\"fill-extrusion-color\": outcome, \n",
|
97 |
" \"fill-extrusion-opacity\": 0.7,\n",
|
98 |
+
" \"fill-extrusion-height\": [\"*\", [\"get\", \"log_amount\"], 5000],\n",
|
99 |
" }\n",
|
100 |
"\n",
|
101 |
"#m.add_geojson(\"vote.geojson\")\n",
|
102 |
"m.add_gdf(gdf, \"fill-extrusion\", paint = paint)\n",
|
103 |
"m"
|
104 |
]
|
105 |
+
},
|
106 |
+
{
|
107 |
+
"cell_type": "code",
|
108 |
+
"execution_count": 129,
|
109 |
+
"id": "4f46a96e-e8ca-4c38-9164-08ab159e3832",
|
110 |
+
"metadata": {},
|
111 |
+
"outputs": [
|
112 |
+
{
|
113 |
+
"data": {
|
114 |
+
"text/plain": [
|
115 |
+
"19.90228528487658"
|
116 |
+
]
|
117 |
+
},
|
118 |
+
"execution_count": 129,
|
119 |
+
"metadata": {},
|
120 |
+
"output_type": "execute_result"
|
121 |
+
}
|
122 |
+
],
|
123 |
+
"source": [
|
124 |
+
"import numpy as np \n",
|
125 |
+
"np.log(440000000)"
|
126 |
+
]
|
127 |
}
|
128 |
],
|
129 |
"metadata": {
|