File size: 10,405 Bytes
1b85eb1 d48b10c 1b85eb1 5dee49f d48b10c 5dee49f d48b10c 5dee49f d48b10c 5dee49f 1b85eb1 d48b10c 1b85eb1 d48b10c 1b85eb1 4590b2a d48b10c 4590b2a 5dee49f d48b10c 5dee49f 4590b2a 1b85eb1 d48b10c 1b85eb1 d48b10c 1b85eb1 d48b10c 1b85eb1 d48b10c 1b85eb1 d48b10c 5dee49f d48b10c 1b85eb1 d48b10c 1b85eb1 5dee49f d48b10c 5dee49f d48b10c 5dee49f 1b85eb1 d48b10c 1b85eb1 d48b10c 1b85eb1 d48b10c 1b85eb1 d48b10c 1b85eb1 |
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 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 21,
"id": "e84709ab-1b47-49ee-8cbd-8aa69744b6c3",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "c4382edb57b643e6907b0314c79387bd",
"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": "b4ae3a5cd5f84c5b86fdcd767d330adf",
"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": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">DatabaseTable: tmp2.main.t2\n",
" gid int64\n",
" cpad_ACCESS_TYP string\n",
" cpad_PARK_NAME string\n",
" cpad_MNG_AGENCY string\n",
" cpad_MNG_AG_LEV string\n",
" reGAP int16\n",
" Easement int16\n",
" TYPE string\n",
" CA_County_Name string\n",
" CA_Region_Name string\n",
" TerrMar string\n",
" CA_Ecoregion_Name string\n",
" ACCESS_TYP string\n",
" MNG_AGNCY string\n",
" MNG_AG_LEV string\n",
" UNIT_NAME string\n",
" DefaultSelection string\n",
" CA_Ecoregion_Acres float32\n",
" CA_Region_Acres float32\n",
" CA_County_Acres float32\n",
" Acres float32\n",
" CA_Marine_Acres float32\n",
" Release_Year int16\n",
" mgmt_stack string\n",
" geom geospatial:geometry\n",
" SHAPE_bbox xmin: float32\n",
" ymin: float32\n",
" xmax: float32\n",
" ymax: float32\n",
"</pre>\n"
],
"text/plain": [
"DatabaseTable: tmp2.main.t2\n",
" gid int64\n",
" cpad_ACCESS_TYP string\n",
" cpad_PARK_NAME string\n",
" cpad_MNG_AGENCY string\n",
" cpad_MNG_AG_LEV string\n",
" reGAP int16\n",
" Easement int16\n",
" TYPE string\n",
" CA_County_Name string\n",
" CA_Region_Name string\n",
" TerrMar string\n",
" CA_Ecoregion_Name string\n",
" ACCESS_TYP string\n",
" MNG_AGNCY string\n",
" MNG_AG_LEV string\n",
" UNIT_NAME string\n",
" DefaultSelection string\n",
" CA_Ecoregion_Acres float32\n",
" CA_Region_Acres float32\n",
" CA_County_Acres float32\n",
" Acres float32\n",
" CA_Marine_Acres float32\n",
" Release_Year int16\n",
" mgmt_stack string\n",
" geom geospatial:geometry\n",
" SHAPE_bbox xmin: float32\n",
" ymin: float32\n",
" xmax: float32\n",
" ymax: float32"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"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",
")\n",
"conn.create_table(\"t1\", tbl.filter(_.Release_Year == 2024), overwrite = True)\n",
"conn.create_table(\"t2\", tbl.filter(_.Release_Year == 2023), overwrite = True)\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "303792ac-9b1d-41b2-a17b-5cf855d70633",
"metadata": {},
"outputs": [],
"source": [
"ca2024 = conn.table(\"t1\").execute()\n",
"ca2023 = conn.table(\"t2\").execute()\n",
"\n",
"import leafmap.maplibregl as leafmap\n",
"m = leafmap.Map()\n",
"m.add_gdf(ca2024, name = \"2024\")\n",
"m.add_gdf(ca2023, name =\"2023\")\n",
"\n",
"m"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9fdd2ed2-623f-479c-b0b7-7c723f3f6728",
"metadata": {},
"outputs": [],
"source": [
"\n",
"conn.disconnect()"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "121c3cad-680c-4f3a-9075-638711ea1634",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "c69a096d24974e9ea8ad3d5b937b723a",
"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"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 22min 10s, sys: 43 s, total: 22min 53s\n",
"Wall time: 11min 47s\n"
]
}
],
"source": [
"%%time\n",
"\n",
"## RUN this on a machine with a whole lot of RAM. consider filtering federal/non-federal first.\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": 7,
"id": "cf3d3e5b-2ff1-4ef9-a147-01f15e970e49",
"metadata": {},
"outputs": [],
"source": [
"## Cannot go straight to geoparquet due to M geometries!\n",
"#db.table(\"diff\").to_parquet(\"diff.parquet\")\n",
"\n",
"## This doesn't work either: \n",
"#db.sql('''CREATE OR REPLACE TABLE diff2024 AS SELECT *, st_force2d(geom) AS geom FROM diff''')\n",
"\n",
"## We could cast geom as blob...."
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "308dc665-1323-4e9b-bc2d-69201c325c4b",
"metadata": {},
"outputs": [],
"source": [
"# F*ck it. Let's do it all in RAM via geopandas, which drops M geoms due to a fortunate bug! \n",
"conn = ibis.duckdb.connect(\"tmp2\", extensions=[\"spatial\"])\n",
"gdf = conn.table(\"diff\").mutate(geom = _.geom.convert(\"epsg:3310\",\"epsg:4326\")).execute()\n",
"gdf.to_parquet(\"ca2024_diffs.parquet\")\n"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "063a11d8-15d7-4b91-b67c-3ccae3edcc8d",
"metadata": {},
"outputs": [],
"source": [
"# stash in our team S3 storage \n",
"\n",
"import streamlit as st\n",
"from minio import Minio\n",
"import os\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, secure=True)\n",
"\n",
"size = os.path.getsize(\"ca2024_diffs.parquet\")\n",
"with open(\"ca2024_diffs.parquet\", \"rb\") as file_data:\n",
" client.put_object(\"public-biodiversity\", \"ca30x30/ca2024_diffs.parquet\", file_data, length = size)\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 26,
"id": "34425101-0592-42fd-9d62-22c9e7a6d6ac",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "c5cc696c15374d519fb940e054a902f0",
"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": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# can read from S3 and plot the whole thing. Note gdf has no metadata.\n",
"\n",
"import leafmap.maplibregl as leafmap\n",
"import ibis\n",
"conn = ibis.duckdb.connect(extensions=[\"spatial\"])\n",
"gdf = conn.read_parquet(\"https://minio.carlboettiger.info/public-biodiversity/ca30x30/ca2024_diffs.parquet\").execute()\n",
"m = leafmap.Map()\n",
"m.add_gdf(gdf)\n",
"#m.to_html(\"ca2024.html\")\n",
"m"
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "17af39a0-9a69-4bfa-9084-d7b26adf74fc",
"metadata": {},
"outputs": [],
"source": [
"path = \"ca2024.html\"\n",
"size = os.path.getsize(path)\n",
"with open(path, \"rb\") as file_data:\n",
" client.put_object(\"public-biodiversity\", \"ca30x30/\"+path, file_data, length = size)\n",
"\n",
"\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
}
|