File size: 6,937 Bytes
08ae6d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "2a04e077-1297-406a-a257-fb0f98566036",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import ibis\n",
    "from ibis import _\n",
    "import ibis.selectors as s\n",
    "from cng.utils import *\n",
    "from cng.h3 import *\n",
    "from minio import Minio\n",
    "import streamlit \n",
    "from datetime import timedelta\n",
    "import re\n",
    "duckdb_install_h3()\n",
    "\n",
    "# con = ibis.duckdb.connect(\"duck.db\",extensions = [\"spatial\", \"h3\"])\n",
    "con = ibis.duckdb.connect(extensions = [\"spatial\", \"h3\"])\n",
    "set_secrets(con)\n",
    "\n",
    "# Get signed URLs to access license-controlled layers\n",
    "key = st.secrets[\"MINIO_KEY\"]\n",
    "secret = st.secrets[\"MINIO_SECRET\"]\n",
    "client = Minio(\"minio.carlboettiger.info\", key, secret)\n",
    "\n",
    "mobi_z8 = con.read_parquet(\"https://minio.carlboettiger.info/public-mobi/hex/all-richness-h8.parquet\").select(\"h8\", \"Z\").rename(richness = \"Z\")\n",
    "svi_z8 = con.read_parquet(\"https://minio.carlboettiger.info/public-social-vulnerability/2022/SVI2022_US_tract_h3_z8.parquet\").select(\"h8\", \"svi\").filter(_.svi > 0)\n",
    "carbon_z8 = con.read_parquet(\"https://minio.carlboettiger.info/public-carbon/hex/us-tracts-vuln-total-carbon-2018-h8.parquet\").select('carbon','h8')\n",
    "county_bounds = con.read_parquet(\"https://minio.carlboettiger.info/public-census/2024/county/2024_us_county.parquet\")\n",
    "tpl_z8 = con.read_parquet(\"s3://shared-tpl/conservation_almanac/z8/tpl_h3_z8.parquet\")\n",
    "landvote_z8 = con.read_parquet(\"s3://shared-tpl/landvote/z8/landvote_h3_z8.parquet\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "919e66ee-5e41-4e86-a9b9-867dc6bc3d87",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "31265c6efc164d5082af3ed5f855ef2e",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "FloatProgress(value=0.0, layout=Layout(width='auto'), style=ProgressStyle(bar_color='black'))"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "current_tables = con.list_tables()\n",
    "\n",
    "if \"conservation_almanac\" not in set(current_tables):\n",
    "    con.create_table(\"conservation_almanac\", tpl_z8)\n",
    "\n",
    "if \"landvote\" not in set(current_tables):\n",
    "    con.create_table(\"landvote\", landvote_z8)\n",
    "\n",
    "if \"carbon\" not in set(current_tables):\n",
    "    con.create_table(\"carbon\", carbon_z8)\n",
    "\n",
    "if \"mobi\" not in set(current_tables):\n",
    "    con.create_table(\"mobi\", mobi_z8)\n",
    "\n",
    "if \"svi\" not in set(current_tables):\n",
    "    con.create_table(\"svi\", svi_z8)\n",
    "\n",
    "conservation_almanac = con.table(\"conservation_almanac\")\n",
    "landvote = con.table(\"landvote\")\n",
    "carbon = con.table(\"carbon\")\n",
    "mobi = con.table(\"mobi\")\n",
    "svi = con.table(\"svi\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "78a7680e-a2ba-4e4a-ab90-c703ebda74b6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>fid</th>\n",
       "      <th>mean_carbon</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>12347</td>\n",
       "      <td>484.333333</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>21514</td>\n",
       "      <td>440.160714</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>14997</td>\n",
       "      <td>440.160714</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>29331</td>\n",
       "      <td>430.705128</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>29333</td>\n",
       "      <td>388.551515</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>29245</td>\n",
       "      <td>366.432727</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>25881</td>\n",
       "      <td>339.250000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>24728</td>\n",
       "      <td>339.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>24727</td>\n",
       "      <td>339.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>29326</td>\n",
       "      <td>333.180000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     fid  mean_carbon\n",
       "0  12347   484.333333\n",
       "1  21514   440.160714\n",
       "2  14997   440.160714\n",
       "3  29331   430.705128\n",
       "4  29333   388.551515\n",
       "5  29245   366.432727\n",
       "6  25881   339.250000\n",
       "7  24728   339.000000\n",
       "8  24727   339.000000\n",
       "9  29326   333.180000"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "con.sql('''\n",
    "SELECT fid, AVG(carbon) as mean_carbon\n",
    "    FROM conservation_almanac\n",
    "    LEFT JOIN carbon\n",
    "    USING (h8)\n",
    "    GROUP BY fid\n",
    "    ORDER BY mean_carbon DESC LIMIT 10;\n",
    "    ''').execute()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "93796006-8203-496d-8288-2c0156ea22bc",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}