{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "00982ea4-b630-4233-b6df-354af3498ed2", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 2, "id": "e84709ab-1b47-49ee-8cbd-8aa69744b6c3", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "a4103c257b7b4691a0a86d817390e9ca", "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" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "31245f0a73634185b297464775ba4c13", "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": [ "import ibis\n", "from ibis import _\n", "conn = ibis.duckdb.connect(\"tmp2\", extensions=[\"spatial\"])\n", "\n", "tbl = (\n", " conn.read_parquet(\"https://data.source.coop/cboettig/ca30x30/ca_areas.parquet\")\n", " .cast({\"SHAPE\": \"geometry\"})\n", " .rename(geom = \"SHAPE\", gid = \"OBJECTID\")\n", " # .filter(_.UNIT_NAME == \"Angeles National Forest\")\n", " .filter(_.reGAP < 3) \n", " #.mutate(geom = _.geom.convert(\"epsg:3310\",\"epsg:4326\"))\n", ")\n", "\n", "conn.create_table(\"t1\", tbl.filter(_.Release_Year == 2024))\n", "conn.create_table(\"t2\", tbl.filter(_.Release_Year == 2023))\n", "\n", "conn.disconnect()" ] }, { "cell_type": "code", "execution_count": null, "id": "121c3cad-680c-4f3a-9075-638711ea1634", "metadata": {}, "outputs": [], "source": [ "%%time\n", "import duckdb\n", "db = duckdb.connect(\"tmp2\")\n", "db.install_extension(\"spatial\")\n", "db.load_extension(\"spatial\")\n", "\n", "db.sql('''\n", "CREATE OR REPLACE TABLE diff AS (\n", "with temp as \n", "(\n", " select b.gid, st_union_agg(a.geom) as geom\n", " from t1 b join t2 a on st_intersects(a.geom, b.geom)\n", " group by b.gid\n", ") \n", "select st_difference(b.geom,coalesce(t.geom, 'GEOMETRYCOLLECTION EMPTY'::geometry)) as geom\n", "from t1 b left join temp t on b.gid = t.gid\n", ")\n", "''')\n" ] }, { "cell_type": "code", "execution_count": null, "id": "e0a31bd7-4b67-46ad-88c4-7c17d820d66b", "metadata": {}, "outputs": [], "source": [ "#db.table(\"diff\").to_parquet(\"diff.parquet\")" ] }, { "cell_type": "code", "execution_count": null, "id": "28a66c8b-89fb-4972-9ff5-ee9bfdf148e9", "metadata": {}, "outputs": [], "source": [ "conn = ibis.duckdb.connect(\"tmp\", extensions=[\"spatial\"])\n", "gdf = conn.table(\"diff\").mutate(geom = _.geom.convert(\"epsg:3310\",\"epsg:4326\")).execute()" ] }, { "cell_type": "code", "execution_count": null, "id": "308dc665-1323-4e9b-bc2d-69201c325c4b", "metadata": {}, "outputs": [], "source": [ "import leafmap.maplibregl as leafmap\n", "m = leafmap.Map()\n", "m.add_gdf(gdf)\n", "m\n" ] }, { "cell_type": "code", "execution_count": null, "id": "775abaca-f21a-4cd8-ad7d-0310cc6b33b6", "metadata": {}, "outputs": [], "source": [ "\"TO 'new2024.geojson' WITH (FORMAT GDAL, DRIVER 'GeoJSON', LAYER_CREATION_OPTIONS 'WRITE_BBOX=YES')\"" ] } ], "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 }