cboettig commited on
Commit
1b85eb1
1 Parent(s): c2b1848

difference

Browse files
Files changed (1) hide show
  1. difference.ipynb +159 -0
difference.ipynb ADDED
@@ -0,0 +1,159 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": null,
6
+ "id": "00982ea4-b630-4233-b6df-354af3498ed2",
7
+ "metadata": {},
8
+ "outputs": [],
9
+ "source": []
10
+ },
11
+ {
12
+ "cell_type": "code",
13
+ "execution_count": 2,
14
+ "id": "e84709ab-1b47-49ee-8cbd-8aa69744b6c3",
15
+ "metadata": {},
16
+ "outputs": [
17
+ {
18
+ "data": {
19
+ "application/vnd.jupyter.widget-view+json": {
20
+ "model_id": "a4103c257b7b4691a0a86d817390e9ca",
21
+ "version_major": 2,
22
+ "version_minor": 0
23
+ },
24
+ "text/plain": [
25
+ "FloatProgress(value=0.0, layout=Layout(width='auto'), style=ProgressStyle(bar_color='black'))"
26
+ ]
27
+ },
28
+ "metadata": {},
29
+ "output_type": "display_data"
30
+ },
31
+ {
32
+ "data": {
33
+ "application/vnd.jupyter.widget-view+json": {
34
+ "model_id": "31245f0a73634185b297464775ba4c13",
35
+ "version_major": 2,
36
+ "version_minor": 0
37
+ },
38
+ "text/plain": [
39
+ "FloatProgress(value=0.0, layout=Layout(width='auto'), style=ProgressStyle(bar_color='black'))"
40
+ ]
41
+ },
42
+ "metadata": {},
43
+ "output_type": "display_data"
44
+ }
45
+ ],
46
+ "source": [
47
+ "import ibis\n",
48
+ "from ibis import _\n",
49
+ "conn = ibis.duckdb.connect(\"tmp2\", extensions=[\"spatial\"])\n",
50
+ "\n",
51
+ "tbl = (\n",
52
+ " conn.read_parquet(\"https://data.source.coop/cboettig/ca30x30/ca_areas.parquet\")\n",
53
+ " .cast({\"SHAPE\": \"geometry\"})\n",
54
+ " .rename(geom = \"SHAPE\", gid = \"OBJECTID\")\n",
55
+ " # .filter(_.UNIT_NAME == \"Angeles National Forest\")\n",
56
+ " .filter(_.reGAP < 3) \n",
57
+ " #.mutate(geom = _.geom.convert(\"epsg:3310\",\"epsg:4326\"))\n",
58
+ ")\n",
59
+ "\n",
60
+ "conn.create_table(\"t1\", tbl.filter(_.Release_Year == 2024))\n",
61
+ "conn.create_table(\"t2\", tbl.filter(_.Release_Year == 2023))\n",
62
+ "\n",
63
+ "conn.disconnect()"
64
+ ]
65
+ },
66
+ {
67
+ "cell_type": "code",
68
+ "execution_count": null,
69
+ "id": "121c3cad-680c-4f3a-9075-638711ea1634",
70
+ "metadata": {},
71
+ "outputs": [],
72
+ "source": [
73
+ "%%time\n",
74
+ "import duckdb\n",
75
+ "db = duckdb.connect(\"tmp2\")\n",
76
+ "db.install_extension(\"spatial\")\n",
77
+ "db.load_extension(\"spatial\")\n",
78
+ "\n",
79
+ "db.sql('''\n",
80
+ "CREATE OR REPLACE TABLE diff AS (\n",
81
+ "with temp as \n",
82
+ "(\n",
83
+ " select b.gid, st_union_agg(a.geom) as geom\n",
84
+ " from t1 b join t2 a on st_intersects(a.geom, b.geom)\n",
85
+ " group by b.gid\n",
86
+ ") \n",
87
+ "select st_difference(b.geom,coalesce(t.geom, 'GEOMETRYCOLLECTION EMPTY'::geometry)) as geom\n",
88
+ "from t1 b left join temp t on b.gid = t.gid\n",
89
+ ")\n",
90
+ "''')\n"
91
+ ]
92
+ },
93
+ {
94
+ "cell_type": "code",
95
+ "execution_count": null,
96
+ "id": "e0a31bd7-4b67-46ad-88c4-7c17d820d66b",
97
+ "metadata": {},
98
+ "outputs": [],
99
+ "source": [
100
+ "#db.table(\"diff\").to_parquet(\"diff.parquet\")"
101
+ ]
102
+ },
103
+ {
104
+ "cell_type": "code",
105
+ "execution_count": null,
106
+ "id": "28a66c8b-89fb-4972-9ff5-ee9bfdf148e9",
107
+ "metadata": {},
108
+ "outputs": [],
109
+ "source": [
110
+ "conn = ibis.duckdb.connect(\"tmp\", extensions=[\"spatial\"])\n",
111
+ "gdf = conn.table(\"diff\").mutate(geom = _.geom.convert(\"epsg:3310\",\"epsg:4326\")).execute()"
112
+ ]
113
+ },
114
+ {
115
+ "cell_type": "code",
116
+ "execution_count": null,
117
+ "id": "308dc665-1323-4e9b-bc2d-69201c325c4b",
118
+ "metadata": {},
119
+ "outputs": [],
120
+ "source": [
121
+ "import leafmap.maplibregl as leafmap\n",
122
+ "m = leafmap.Map()\n",
123
+ "m.add_gdf(gdf)\n",
124
+ "m\n"
125
+ ]
126
+ },
127
+ {
128
+ "cell_type": "code",
129
+ "execution_count": null,
130
+ "id": "775abaca-f21a-4cd8-ad7d-0310cc6b33b6",
131
+ "metadata": {},
132
+ "outputs": [],
133
+ "source": [
134
+ "\"TO 'new2024.geojson' WITH (FORMAT GDAL, DRIVER 'GeoJSON', LAYER_CREATION_OPTIONS 'WRITE_BBOX=YES')\""
135
+ ]
136
+ }
137
+ ],
138
+ "metadata": {
139
+ "kernelspec": {
140
+ "display_name": "Python 3 (ipykernel)",
141
+ "language": "python",
142
+ "name": "python3"
143
+ },
144
+ "language_info": {
145
+ "codemirror_mode": {
146
+ "name": "ipython",
147
+ "version": 3
148
+ },
149
+ "file_extension": ".py",
150
+ "mimetype": "text/x-python",
151
+ "name": "python",
152
+ "nbconvert_exporter": "python",
153
+ "pygments_lexer": "ipython3",
154
+ "version": "3.11.10"
155
+ }
156
+ },
157
+ "nbformat": 4,
158
+ "nbformat_minor": 5
159
+ }