{ "cells": [ { "cell_type": "markdown", "id": "4b4adc2a-bf0c-4ace-87be-dbaf90be0125", "metadata": {}, "source": [ "# Pre-processing" ] }, { "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": 1, "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": "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": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
managermanager_typearea
0California Department of Fish and WildlifeState42086.259379
1California Department of Parks and RecreationState17931.321473
2California Tahoe ConservancyState4803.250929
3California Department of Water ResourcesState2248.610289
4University of CaliforniaState1860.854444
5California Department of Forestry and Fire Pro...State1089.029581
6Coachella Valley Mountains ConservancyState119.907070
7California State Lands CommissionState109.016475
8California State Coastal ConservancyState97.314705
9California State University SonomaState38.760956
10Other StateState32.927882
11San Joaquin River ConservancyState1.531470
12California Department of TransportationState1.261433
13UnknownState0.255531
14California State UniversityState0.021589
\n", "
" ], "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 }