File size: 12,139 Bytes
d2c79b3 c2b1848 d2c79b3 c2b1848 d2c79b3 c2b1848 d2c79b3 c2b1848 d2c79b3 c2b1848 d2c79b3 c2b1848 d2c79b3 c2b1848 d2c79b3 c2b1848 d2c79b3 c2b1848 d2c79b3 c2b1848 d2c79b3 c2b1848 d2c79b3 c2b1848 d2c79b3 c2b1848 d2c79b3 c2b1848 d2c79b3 c2b1848 d2c79b3 c2b1848 d2c79b3 |
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 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 |
{
"cells": [
{
"cell_type": "markdown",
"id": "4b4adc2a-bf0c-4ace-87be-dbaf90be0125",
"metadata": {},
"source": [
"# Preporcessing"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "f7e6298c-d886-432a-a1b7-c3fee914c24f",
"metadata": {},
"outputs": [],
"source": [
"import ibis\n",
"from ibis import _\n",
"\n",
"conn = ibis.duckdb.connect(\"tmp\", extensions=[\"spatial\"])\n",
"ca_parquet = \"https://data.source.coop/cboettig/ca30x30/ca_areas.parquet\"\n",
"# or use local copy:\n",
"ca_parquet = \"/home/rstudio/source.coop/cboettig/ca30x30/ca_areas.parquet\"\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "a0cb34b1-8d70-49bf-80c6-244ecc8ddf84",
"metadata": {},
"outputs": [],
"source": [
"buffer = -0.00001\n",
"\n",
"tbl = (\n",
" conn.read_parquet(ca_parquet)\n",
" .cast({\"SHAPE\": \"geometry\"})\n",
" .rename(geom = \"SHAPE\")\n",
" # .filter(_.UNIT_NAME == \"Angeles National Forest\")\n",
" .filter(_.reGAP < 3) \n",
")\n",
"tbl_2023 = tbl.filter(_.Release_Year == 2023).mutate(geom=_.geom.buffer(buffer))\n",
"tbl_2024 = tbl.filter(_.Release_Year == 2024)\n",
"intersects = tbl_2024.anti_join(tbl_2023, _.geom.intersects(tbl_2023.geom))\n",
"\n",
"new2024 = intersects.select(\"OBJECTID\").mutate(established = 2024)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "275c171a-f82f-4ee8-991c-1e34eb83a33d",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "dd86bb91838d45aa87197fc49a3b2362",
"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": [
"ca = (conn\n",
" .read_parquet(ca_parquet)\n",
" .cast({\"SHAPE\": \"geometry\"})\n",
" .mutate(area = _.SHAPE.area())\n",
" .filter(_.Release_Year == 2024)\n",
" .filter(_.reGAP < 3)\n",
" .left_join(new2024, \"OBJECTID\")\n",
" .mutate(established=_.established.fill_null(2023))\n",
" .mutate(geom = _.SHAPE.convert(\"epsg:3310\",\"epsg:4326\"))\n",
" .rename(name = \"UNIT_NAME\", access_type = \"ACCESS_TYP\", manager = \"MNG_AGNCY\",\n",
" manager_type = \"MNG_AG_LEV\", id = \"OBJECTID\", type = \"TYPE\")\n",
" .select(_.established, _.reGAP, _.name, _.access_type, _.manager, _.manager_type,\n",
" _.Easement, _.Acres, _.id, _.type, _.geom)\n",
" )\n",
"ca.execute().to_parquet(\"ca2024.parquet\")"
]
},
{
"cell_type": "markdown",
"id": "cebd0ff5-8353-4b84-b9ee-182b74613554",
"metadata": {},
"source": [
"# Testing & visualization\n",
"\n",
"`ca2024.parquet()` now contains all we need. The code below illustrates some quick examples of the kinds of visualizations and summaries we might want to compute with this data. \n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "55afe07c-8681-4308-bbb9-e460f7380f86",
"metadata": {},
"outputs": [],
"source": [
"import leafmap.maplibregl as leafmap\n",
"import ibis\n",
"from ibis import _\n",
"conn = ibis.duckdb.connect(extensions=[\"spatial\"])\n",
"\n",
"ca2024 = conn.read_parquet(\"ca2024.parquet\")"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "9d4cd1c4-288b-4d1c-907c-ca76ccbdb1d6",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"geopandas.geodataframe.GeoDataFrame"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"gdf = ca2024.execute()\n",
"gdf.__class__"
]
},
{
"cell_type": "markdown",
"id": "ebbb2650-4442-4e54-8467-5e681d6fab9e",
"metadata": {},
"source": [
"Using difference (overlap) instead:"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "6f3df8c1-a603-4dd5-be84-8deaae928d0a",
"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>manager</th>\n",
" <th>manager_type</th>\n",
" <th>area</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>California Department of Fish and Wildlife</td>\n",
" <td>State</td>\n",
" <td>42086.259379</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>California Department of Parks and Recreation</td>\n",
" <td>State</td>\n",
" <td>17931.321473</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>California Tahoe Conservancy</td>\n",
" <td>State</td>\n",
" <td>4803.250929</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>California Department of Water Resources</td>\n",
" <td>State</td>\n",
" <td>2248.610289</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>University of California</td>\n",
" <td>State</td>\n",
" <td>1860.854444</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>California Department of Forestry and Fire Pro...</td>\n",
" <td>State</td>\n",
" <td>1089.029581</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>Coachella Valley Mountains Conservancy</td>\n",
" <td>State</td>\n",
" <td>119.907070</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>California State Lands Commission</td>\n",
" <td>State</td>\n",
" <td>109.016475</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
" <td>California State Coastal Conservancy</td>\n",
" <td>State</td>\n",
" <td>97.314705</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>California State University Sonoma</td>\n",
" <td>State</td>\n",
" <td>38.760956</td>\n",
" </tr>\n",
" <tr>\n",
" <th>10</th>\n",
" <td>Other State</td>\n",
" <td>State</td>\n",
" <td>32.927882</td>\n",
" </tr>\n",
" <tr>\n",
" <th>11</th>\n",
" <td>San Joaquin River Conservancy</td>\n",
" <td>State</td>\n",
" <td>1.531470</td>\n",
" </tr>\n",
" <tr>\n",
" <th>12</th>\n",
" <td>California Department of Transportation</td>\n",
" <td>State</td>\n",
" <td>1.261433</td>\n",
" </tr>\n",
" <tr>\n",
" <th>13</th>\n",
" <td>Unknown</td>\n",
" <td>State</td>\n",
" <td>0.255531</td>\n",
" </tr>\n",
" <tr>\n",
" <th>14</th>\n",
" <td>California State University</td>\n",
" <td>State</td>\n",
" <td>0.021589</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" manager manager_type \\\n",
"0 California Department of Fish and Wildlife State \n",
"1 California Department of Parks and Recreation State \n",
"2 California Tahoe Conservancy State \n",
"3 California Department of Water Resources State \n",
"4 University of California State \n",
"5 California Department of Forestry and Fire Pro... State \n",
"6 Coachella Valley Mountains Conservancy State \n",
"7 California State Lands Commission State \n",
"8 California State Coastal Conservancy State \n",
"9 California State University Sonoma State \n",
"10 Other State State \n",
"11 San Joaquin River Conservancy State \n",
"12 California Department of Transportation State \n",
"13 Unknown State \n",
"14 California State University State \n",
"\n",
" area \n",
"0 42086.259379 \n",
"1 17931.321473 \n",
"2 4803.250929 \n",
"3 2248.610289 \n",
"4 1860.854444 \n",
"5 1089.029581 \n",
"6 119.907070 \n",
"7 109.016475 \n",
"8 97.314705 \n",
"9 38.760956 \n",
"10 32.927882 \n",
"11 1.531470 \n",
"12 1.261433 \n",
"13 0.255531 \n",
"14 0.021589 "
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# compute some summary tables:\n",
"\n",
"(ca2024\n",
" .filter(_.established == 2024)\n",
" .filter(_.manager_type == \"State\")\n",
" .group_by(_.manager, _.manager_type)\n",
" .agg(area = _.Acres.sum())\n",
" .order_by(_.area.desc())\n",
" .execute()\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "c62854f6-1456-4207-8c69-53af17970102",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "10329a95c7b84de4b598f0ccf4c6af20",
"version_major": 2,
"version_minor": 1
},
"text/plain": [
"Map(height='600px', map_options={'bearing': 0, 'center': (0, 20), 'pitch': 0, 'style': 'https://basemaps.carto…"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"gdf = ca2024.filter(_.manager == \"California Department of Parks and Recreation\", _.established== 2024).execute()\n",
"\n",
"established = {'property': 'established',\n",
" 'type': 'categorical',\n",
" 'stops': [\n",
" [2023, \"#26542C80\"], \n",
" [2024, \"#F3AB3D80\"]]\n",
" }\n",
"paint = {\"fill-color\": established}\n",
"\n",
"m = leafmap.Map(style=\"positron\")\n",
"m.add_gdf(gdf,layer_type=\"fill\", name = \"CA 30x30\", paint = paint)\n",
"m.add_layer_control()\n",
"m"
]
}
],
"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.11.10"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|