File size: 27,046 Bytes
5ee179c
 
 
 
 
 
 
 
 
 
 
 
430f057
5ee179c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
430f057
5ee179c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
430f057
5ee179c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
430f057
5ee179c
 
 
 
10e8f52
5ee179c
 
 
 
 
 
 
 
 
 
 
 
 
10e8f52
 
 
 
 
 
 
 
5ee179c
 
430f057
5ee179c
 
430f057
5ee179c
 
 
 
 
 
 
 
 
430f057
5ee179c
 
430f057
5ee179c
 
 
 
 
 
 
 
10e8f52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5ee179c
 
430f057
5ee179c
 
430f057
5ee179c
 
 
 
 
 
 
 
10e8f52
 
 
 
 
 
 
 
5ee179c
 
430f057
5ee179c
 
430f057
5ee179c
 
 
 
 
 
 
 
 
 
430f057
5ee179c
 
430f057
5ee179c
 
 
 
 
 
 
 
 
 
 
430f057
5ee179c
 
430f057
5ee179c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
430f057
5ee179c
 
430f057
5ee179c
 
 
 
 
 
 
 
 
 
 
 
10e8f52
5ee179c
 
 
 
430f057
5ee179c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10e8f52
 
 
 
 
 
 
 
5ee179c
 
430f057
5ee179c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
430f057
5ee179c
 
430f057
5ee179c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
430f057
5ee179c
 
430f057
5ee179c
 
 
 
 
 
 
 
 
 
430f057
5ee179c
 
430f057
5ee179c
 
 
 
 
 
 
 
 
 
430f057
5ee179c
 
430f057
5ee179c
 
 
 
 
 
 
 
 
 
430f057
5ee179c
 
430f057
5ee179c
 
 
 
 
 
 
 
 
430f057
5ee179c
 
430f057
5ee179c
 
 
 
 
 
 
10e8f52
 
 
 
 
 
 
 
 
 
430f057
10e8f52
 
430f057
10e8f52
 
 
 
 
 
 
 
 
ae1519e
10e8f52
 
 
 
430f057
10e8f52
 
 
 
ae1519e
10e8f52
ae1519e
 
10e8f52
ae1519e
 
 
10e8f52
 
ae1519e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10e8f52
ae1519e
 
 
 
10e8f52
ae1519e
 
 
 
 
 
 
10e8f52
ae1519e
 
10e8f52
 
ae1519e
10e8f52
 
5ee179c
 
430f057
ae1519e
10e8f52
430f057
10e8f52
 
 
ae1519e
 
10e8f52
 
 
 
 
 
 
 
 
 
 
 
 
430f057
5ee179c
 
430f057
5ee179c
 
 
 
 
 
10e8f52
 
ae1519e
 
 
 
 
10e8f52
 
 
ae1519e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10e8f52
5ee179c
 
 
 
 
 
 
 
 
 
 
 
 
 
430f057
5ee179c
 
430f057
5ee179c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
430f057
5ee179c
 
430f057
5ee179c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "39bf1de3-cba6-475a-a988-ad48e5af4a04",
   "metadata": {},
   "source": [
    "# Get zonal stats "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ba047a55-642d-4c27-a367-5f35f4406218",
   "metadata": {},
   "outputs": [],
   "source": [
    "import ibis\n",
    "import ibis.selectors as s\n",
    "from ibis import _\n",
    "import fiona\n",
    "import geopandas as gpd\n",
    "import rioxarray\n",
    "from shapely.geometry import box\n",
    "\n",
    "import rasterio\n",
    "from rasterio.mask import mask\n",
    "from rasterstats import zonal_stats\n",
    "import pandas as pd\n",
    "from joblib import Parallel, delayed\n",
    "\n",
    "con = ibis.duckdb.connect()\n",
    "con.load_extension(\"spatial\")\n",
    "threads = -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8b5656db-2d1d-4ca8-826d-7588126e52e8",
   "metadata": {},
   "outputs": [],
   "source": [
    "# cropping US data to only CA \n",
    "def crop_raster_to_bounds(tif_file, vector_gdf):\n",
    "    with rasterio.open(tif_file) as src:\n",
    "        # Get California's bounding box in the same CRS as the raster\n",
    "        california_bounds = vector_gdf.total_bounds\n",
    "        california_bounds = rasterio.coords.BoundingBox(\n",
    "            *california_bounds\n",
    "        )\n",
    "        # Crop the raster to the California bounding box\n",
    "        out_image, out_transform = mask(src, [california_bounds], crop=True)\n",
    "        out_meta = src.meta.copy()\n",
    "        out_meta.update({\n",
    "            \"driver\": \"GTiff\",\n",
    "            \"height\": out_image.shape[1],\n",
    "            \"width\": out_image.shape[2],\n",
    "            \"transform\": out_transform\n",
    "        })\n",
    "        print(\"Unique values in cropped raster:\", np.unique(out_image))\n",
    "\n",
    "    return out_image, out_meta\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9a0e3446-16ac-40b0-9e34-db0157038c5a",
   "metadata": {},
   "outputs": [],
   "source": [
    "def big_zonal_stats(vec_file, tif_file, stats, col_name, n_jobs, verbose=10, timeout=10000):\n",
    "    gdf = gpd.read_parquet(vec_file)\n",
    "    if gdf.crs is None:\n",
    "        gdf = gdf.set_crs(\"EPSG:4326\")\n",
    "    gdf = gdf.rename(columns={\"geom\": \"geometry\"})\n",
    "    gdf = gdf.set_geometry(\"geometry\")\n",
    "    gdf = gdf[gdf[\"geometry\"].notna()].copy()\n",
    "\n",
    "    with rasterio.open(tif_file) as src:\n",
    "        raster_crs = src.crs\n",
    "        gdf = gdf.to_crs(raster_crs)  # Transform vector to raster CRS\n",
    "        \n",
    "        # CA bounding box + convert it to a polygon in raster CRS\n",
    "        california_polygon = box(*gdf.total_bounds)\n",
    "        \n",
    "        out_image, out_transform = mask(src, [california_polygon], crop=True, nodata=src.nodata)\n",
    "\n",
    "        # If raster is 3D, select the first band\n",
    "        if out_image.ndim == 3:\n",
    "            out_image = out_image[0]\n",
    "\n",
    "    # compute zonal statistics for each geometry slice\n",
    "    def get_stats(geom_slice):\n",
    "        geom = [geom_slice.geometry]\n",
    "        stats_result = zonal_stats(\n",
    "            geom, out_image, stats=stats, affine=out_transform, all_touched=True, nodata=src.nodata\n",
    "        )\n",
    "        return stats_result[0] if stats_result and stats_result[0].get(\"mean\") is not None else {'mean': None}\n",
    "\n",
    "    output = [get_stats(row) for row in gdf.itertuples()]\n",
    "    gdf[col_name] = [res['mean'] for res in output]\n",
    "\n",
    "    return gdf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ce66bae6-bac5-4837-9b01-fde16a00c303",
   "metadata": {},
   "outputs": [],
   "source": [
    "# getting local copies of data \n",
    "# aws s3 cp s3://vizzuality/hfp-100/hfp_2021_100m_v1-2_cog.tif . --endpoint-url=https://data.source.coop\n",
    "# aws s3 cp s3://vizzuality/lg-land-carbon-data/natcrop_bii_100m_cog.tif . --endpoint-url=https://data.source.coop\n",
    "# aws s3 cp s3://vizzuality/lg-land-carbon-data/natcrop_fii_100m_cog.tif . --endpoint-url=https://data.source.coop\n",
    "# aws s3 cp s3://vizzuality/lg-land-carbon-data/natcrop_expansion_100m_cog.tif . --endpoint-url=https://data.source.coop\n",
    "# aws s3 cp s3://vizzuality/lg-land-carbon-data/natcrop_reduction_100m_cog.tif . --endpoint-url=https://data.source.coop\n",
    "# aws s3 cp s3://cboettig/carbon/cogs/irrecoverable_c_total_2018.tif . --endpoint-url=https://data.source.coop\n",
    "# aws s3 cp s3://cboettig/carbon/cogs/manageable_c_total_2018.tif . --endpoint-url=https://data.source.coop\n",
    "# ! aws s3 cp s3://cboettig/justice40/disadvantaged-communities.parquet . --endpoint-url=https://data.source.coop\n",
    "# minio/shared-biodiversity/redlist/cog/combined_sr_2022.tif\n",
    "# /home/rstudio/minio/shared-biodiversity/redlist/cog/combined_rwr_2022.tif\n",
    "# ! aws s3 cp s3://cboettig/social-vulnerability/svi2020_us_tract.parquet . --endpoint-url=https://data.source.coop\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "531e7f88-1ce1-4027-b0ab-aab597e9a2b2",
   "metadata": {},
   "source": [
    "# Biodiversity Data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "66dec912-ad8a-41cf-a5c2-6ec9cc350984",
   "metadata": {},
   "outputs": [],
   "source": [
    "%%time\n",
    "tif_file = 'SpeciesRichness_All.tif'\n",
    "vec_file = \"/home/rstudio/github/ca-30x30/ca2024-30m.parquet\"\n",
    "df = big_zonal_stats(vec_file, tif_file, stats = ['mean'], col_name = \"richness\", n_jobs=threads, verbose=0).to_parquet(\"cpad-stats-temp.parquet\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b081ec1a-ea91-485e-95f9-12cd06c2002a",
   "metadata": {},
   "outputs": [],
   "source": [
    "%%time\n",
    "tif_file = 'RSR_All.tif'\n",
    "vec_file = './cpad-stats-temp.parquet'\n",
    "df = big_zonal_stats(vec_file, tif_file, stats = ['mean'],\n",
    "                      col_name = \"rsr\", n_jobs=threads, verbose=0).to_parquet(\"cpad-stats-temp.parquet\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d5133f36-404e-4f6a-a90b-eb5f098e6f06",
   "metadata": {},
   "outputs": [],
   "source": [
    "%%time\n",
    "tif_file = 'combined_sr_2022.tif'\n",
    "vec_file = './cpad-stats-temp.parquet'\n",
    "df = big_zonal_stats(vec_file, tif_file, stats = ['mean'],  col_name = \"all_species_richness\", n_jobs=threads, verbose=0).to_parquet(\"cpad-stats-temp.parquet\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2ce56a66-34e3-4f61-95ae-65d1f06bc468",
   "metadata": {},
   "outputs": [],
   "source": [
    "%%time\n",
    "tif_file = 'combined_rwr_2022.tif'\n",
    "vec_file = './cpad-stats-temp.parquet'\n",
    "df = big_zonal_stats(vec_file, tif_file, stats = ['mean'],  col_name = \"all_species_rwr\", n_jobs=threads, verbose=0).to_parquet(\"cpad-stats-temp.parquet\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6c129894-3775-4842-8767-f81a8f626d2c",
   "metadata": {},
   "source": [
    "# Carbon Data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "19c3e402-8712-450f-b3dd-af9d0c01689c",
   "metadata": {},
   "outputs": [],
   "source": [
    "%%time\n",
    "tif_file = 'irrecoverable_c_total_2018.tif'\n",
    "vec_file = './cpad-stats-temp.parquet'\n",
    "df = big_zonal_stats(vec_file, tif_file, stats = ['mean'],  col_name = \"irrecoverable_carbon\", n_jobs=threads, verbose=0).to_parquet(\"cpad-stats-temp.parquet\")\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c55c777a-48ce-4403-a171-cfc0d2351df6",
   "metadata": {},
   "outputs": [],
   "source": [
    "%%time\n",
    "tif_file = 'manageable_c_total_2018.tif'\n",
    "vec_file = './cpad-stats-temp.parquet'\n",
    "df = big_zonal_stats(vec_file, tif_file, stats = ['mean'],  col_name = \"manageable_carbon\", n_jobs=threads, verbose=0).to_parquet(\"cpad-stats-temp.parquet\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "33ac0fb7-2cde-448d-a634-1973e34ac14f",
   "metadata": {},
   "outputs": [],
   "source": [
    "%%time\n",
    "tif_file = 'deforest_carbon_100m_cog.tif'\n",
    "vec_file = './cpad-stats-temp.parquet'\n",
    "df = big_zonal_stats(vec_file, tif_file, stats = ['mean'], \n",
    "                     col_name = \"deforest_carbon\", n_jobs=threads, verbose=0).to_parquet(\"cpad-stats-temp.parquet\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "096c00a8-57af-41d7-93cc-85d85414aa4f",
   "metadata": {},
   "source": [
    "# Human Impact Data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d2a8c10f-e94b-4eef-940f-2af9599edee1",
   "metadata": {},
   "outputs": [],
   "source": [
    "%%time\n",
    "tif_file = 'natcrop_bii_100m_cog.tif'\n",
    "vec_file = './cpad-stats-temp.parquet'\n",
    "df = big_zonal_stats(vec_file, tif_file, stats = ['mean'], \n",
    "                     col_name = \"biodiversity_intactness_loss\", n_jobs=threads, verbose=0).to_parquet(\"cpad-stats-temp.parquet\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1c318f39-7ca0-4f3c-80fb-73f72202e4e0",
   "metadata": {},
   "outputs": [],
   "source": [
    "%%time\n",
    "tif_file = 'natcrop_fii_100m_cog.tif'\n",
    "vec_file = './cpad-stats-temp.parquet'\n",
    "df = big_zonal_stats(vec_file, tif_file, stats = ['mean'],\n",
    "                     col_name = \"forest_integrity_loss\", n_jobs=threads, verbose=0).to_parquet(\"cpad-stats-temp.parquet\")\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "aef9070a-c87a-463e-81b8-3cc6c5c9d484",
   "metadata": {},
   "outputs": [],
   "source": [
    "%%time\n",
    "tif_file = 'natcrop_expansion_100m_cog.tif'\n",
    "vec_file = './cpad-stats-temp.parquet'\n",
    "df = big_zonal_stats(vec_file, tif_file, stats = ['mean'],  col_name = \"crop_expansion\", n_jobs=threads, verbose=0)\n",
    "gpd.GeoDataFrame(df, geometry=\"geometry\").to_parquet(\"cpad-stats-temp.parquet\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d94f937b-b32c-4de1-b4ac-93ce33f0919f",
   "metadata": {},
   "outputs": [],
   "source": [
    "%%time\n",
    "tif_file = 'natcrop_reduction_100m_cog.tif'\n",
    "vec_file = './cpad-stats-temp.parquet'\n",
    "df = big_zonal_stats(vec_file, tif_file, stats = ['mean'],  col_name = \"crop_reduction\", n_jobs=threads, verbose=0).to_parquet(\"cpad-stats-temp.parquet\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6bdaba61-30c1-49d6-a4e6-db68f1daafa3",
   "metadata": {},
   "outputs": [],
   "source": [
    "%%time\n",
    "tif_file = 'hfp_2021_100m_v1-2_cog.tif'\n",
    "vec_file = './cpad-stats-temp.parquet'\n",
    "df = big_zonal_stats(vec_file, tif_file, stats = ['mean'],  col_name = \"human_impact\", n_jobs=threads, verbose=0).to_parquet(\"cpad-stats-temp.parquet\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f8e037d4-7a34-42bc-941f-0c09ee80ef3b",
   "metadata": {},
   "source": [
    "# Need to convert SVI & Justice40 files to tif"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c4a19013-65f1-4eef-be2d-0cf1be3d0f7f",
   "metadata": {},
   "outputs": [],
   "source": [
    "import geopandas as gpd\n",
    "import numpy as np\n",
    "import rasterio\n",
    "from rasterio.features import rasterize\n",
    "from rasterio.transform import from_bounds\n",
    "\n",
    "def get_geotiff(gdf, output_file,col):\n",
    "    gdf = gdf.set_geometry(\"geometry\")\n",
    "    gdf = gdf.set_crs(\"EPSG:4326\")\n",
    "    print(gdf.crs)\n",
    "\n",
    "    # Set raster properties\n",
    "    minx, miny, maxx, maxy = gdf.total_bounds  # Get the bounds of the geometry\n",
    "    pixel_size = 0.01  # Define the pixel size in units of the CRS\n",
    "    width = int((maxx - minx) / pixel_size)\n",
    "    height = int((maxy - miny) / pixel_size)\n",
    "    transform = from_bounds(minx, miny, maxx, maxy, width, height)\n",
    "    \n",
    "    # Define rasterization with continuous values\n",
    "    shapes = ((geom, value) for geom, value in zip(gdf.geometry, gdf[col]))\n",
    "    raster = rasterize(\n",
    "        shapes,\n",
    "        out_shape=(height, width),\n",
    "        transform=transform,\n",
    "        fill=0.0,  # Background value for areas outside the geometry\n",
    "        dtype=\"float32\"  # Set data type to handle continuous values\n",
    "    )\n",
    "    print(\"Unique values in raster:\", np.unique(raster))\n",
    "\n",
    "    # Define GeoTIFF metadata\n",
    "    out_meta = {\n",
    "        \"driver\": \"GTiff\",\n",
    "        \"height\": height,\n",
    "        \"width\": width,\n",
    "        \"count\": 1,\n",
    "        \"dtype\": raster.dtype,\n",
    "        \"crs\": gdf.crs,\n",
    "        \"transform\": transform,\n",
    "        \"compress\": \"deflate\"  # Use compression to reduce file size\n",
    "    }\n",
    "    \n",
    "    # Write to a GeoTIFF file with COG options\n",
    "    with rasterio.open(output_file, \"w\", **out_meta) as dest:\n",
    "        dest.write(raster, 1)\n",
    "        dest.build_overviews([2, 4, 8, 16], rasterio.enums.Resampling.average)\n",
    "        dest.update_tags(1, TIFFTAG_RESOLUTION_UNIT=\"Meter\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f4925a74-5ed2-49a4-845b-6a0f0398a43e",
   "metadata": {},
   "source": [
    "# SVI"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4e678f01-73af-4f99-a565-e9b7f04d9547",
   "metadata": {},
   "outputs": [],
   "source": [
    "# clean up SVI data\n",
    "svi_df =  (con\n",
    " .read_parquet(\"svi2020_us_tract.parquet\")\n",
    " .select(\"RPL_THEMES\",\"RPL_THEME1\",\"RPL_THEME2\",\"RPL_THEME3\",\"RPL_THEME4\",\"Shape\")\n",
    " .rename(SVI = \"RPL_THEMES\", socioeconomic = \"RPL_THEME1\", \n",
    "         household_char = \"RPL_THEME2\", racial_ethnic_minority =  \"RPL_THEME3\",\n",
    "         housing_transit = \"RPL_THEME4\", geometry = \"Shape\")\n",
    ".cast({\"geometry\":\"geometry\"})\n",
    ")\n",
    "svi_df.execute().to_parquet(\"svi2020_us_tract_clean.parquet\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c5046d6b-9798-46d3-a1bc-548e29414007",
   "metadata": {},
   "outputs": [],
   "source": [
    "gdf = gpd.read_parquet(\"svi2020_us_tract_clean.parquet\")\n",
    "svi = gdf[['SVI','geometry']]\n",
    "socio = gdf[['socioeconomic','geometry']]\n",
    "house = gdf[['household_char','geometry']]\n",
    "minority = gdf[['racial_ethnic_minority','geometry']]\n",
    "transit = gdf[['housing_transit','geometry']]\n",
    "\n",
    "#convert SVI parquet to tif\n",
    "get_geotiff(svi,\"svi.tif\",\"SVI\")\n",
    "get_geotiff(socio,\"svi_socioeconomic.tif\",\"socioeconomic\")\n",
    "get_geotiff(house,\"svi_household.tif\",\"household_char\")\n",
    "get_geotiff(minority,\"svi_minority.tif\",\"racial_ethnic_minority\")\n",
    "get_geotiff(transit,\"svi_transit.tif\",\"housing_transit\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6a36b77f-d0be-45bd-9318-da4b57eaf353",
   "metadata": {},
   "outputs": [],
   "source": [
    "%%time\n",
    "tif_file = 'svi.tif'\n",
    "vec_file = './cpad-stats-temp.parquet'\n",
    "df = big_zonal_stats(vec_file, tif_file, stats = ['mean'],  col_name = \"SVI\", n_jobs=threads, verbose=0).to_parquet(\"cpad-stats-temp.parquet\")\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "05ef74e2-3f23-4f69-8cd3-8862cb73a259",
   "metadata": {},
   "outputs": [],
   "source": [
    "%%time\n",
    "vec_file = './cpad-stats-temp.parquet'\n",
    "tif_file = 'svi_socioeconomic.tif'\n",
    "df = big_zonal_stats(vec_file, tif_file, stats = ['mean'],  col_name = \"socioeconomic_status\", n_jobs=threads, verbose=0).to_parquet(\"cpad-stats-temp.parquet\")\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "23417a03-38c2-4b31-8340-f08ec8a11631",
   "metadata": {},
   "outputs": [],
   "source": [
    "%%time\n",
    "vec_file = './cpad-stats-temp.parquet'\n",
    "tif_file = 'svi_household.tif'\n",
    "df = big_zonal_stats(vec_file, tif_file, stats = ['mean'],  col_name = \"household_char\", n_jobs=threads, verbose=0).to_parquet(\"cpad-stats-temp.parquet\")\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "de86d7f0-6cdc-4d05-bdee-d9803cbd83bd",
   "metadata": {},
   "outputs": [],
   "source": [
    "%%time\n",
    "vec_file = './cpad-stats-temp.parquet'\n",
    "tif_file = 'svi_minority.tif'\n",
    "df = big_zonal_stats(vec_file, tif_file, stats = ['mean'],  col_name = \"racial_ethnic_minority\", n_jobs=threads, verbose=0).to_parquet(\"cpad-stats-temp.parquet\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0c49dd50-7dd3-4240-9af8-3e32ec656bc0",
   "metadata": {},
   "outputs": [],
   "source": [
    "%%time\n",
    "vec_file = './cpad-stats-temp.parquet'\n",
    "tif_file = 'svi_transit.tif'\n",
    "df = big_zonal_stats(vec_file, tif_file, stats = ['mean'],  col_name = \"housing_transit\", n_jobs=threads, verbose=0).to_parquet(\"cpad-stats-temp.parquet\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ff4b6604-9828-4882-90bd-554c21f5c6e6",
   "metadata": {},
   "source": [
    "# Justice40 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3678a91f-72f7-4339-a409-a97776cba043",
   "metadata": {},
   "outputs": [],
   "source": [
    "#clean up\n",
    "justice40 =  (con\n",
    "        .read_parquet(\"disadvantaged-communities.parquet\")\n",
    "        .rename(geometry = \"SHAPE\",justice40=\"Disadvan\")\n",
    "        .filter(_.StateName == \"California\")\n",
    "        .mutate(geometry = _.geometry.convert(\"ESRI:102039\",\"EPSG:4326\"))\n",
    "        .select(\"justice40\",\"geometry\")\n",
    "        )\n",
    "justice40.execute().to_parquet(\"ca_justice40.parquet\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8faa425f-6f9c-4189-a53a-24dd0250c539",
   "metadata": {},
   "outputs": [],
   "source": [
    "# #justice40 is either 0 or 1, so we want to get the percentage of polygon where justice40 = 1.  \n",
    "\n",
    "def big_zonal_stats_binary(vec_file, justice40_file, col_name,projected_crs=\"EPSG:3310\"):\n",
    "    # Read both vector files as GeoDataFrames\n",
    "    gdf = gpd.read_parquet(vec_file)\n",
    "    justice40_gdf = gpd.read_parquet(justice40_file)\n",
    "    \n",
    "    # Set CRS if not already set (assuming both should be in EPSG:4326, modify if needed)\n",
    "    if gdf.crs is None:\n",
    "        gdf = gdf.set_crs(\"EPSG:4326\")\n",
    "    if justice40_gdf.crs is None:\n",
    "        justice40_gdf = justice40_gdf.set_crs(\"EPSG:4326\")\n",
    "     # Ensure both GeoDataFrames are in the same CRS and reproject to a projected CRS for area calculations\n",
    "    gdf = gdf.to_crs(projected_crs)\n",
    "    justice40_gdf = justice40_gdf.to_crs(projected_crs)\n",
    "    \n",
    "    # Ensure both GeoDataFrames are in the same CRS\n",
    "    gdf = gdf.to_crs(justice40_gdf.crs)\n",
    "    \n",
    "    # Filter justice40 polygons where justice40 == 1\n",
    "    justice40_gdf = justice40_gdf[justice40_gdf['justice40'] == 1].copy()\n",
    "    \n",
    "    # Prepare a list to hold percentage of justice40 == 1 for each polygon\n",
    "    percentages = []\n",
    "    \n",
    "    # Iterate over each polygon in the main GeoDataFrame\n",
    "    for geom in gdf.geometry:\n",
    "        # Find intersecting justice40 polygons\n",
    "        justice40_intersections = justice40_gdf[justice40_gdf.intersects(geom)].copy()\n",
    "        \n",
    "        # Calculate the intersection area\n",
    "        if not justice40_intersections.empty:\n",
    "            justice40_intersections['intersection'] = justice40_intersections.intersection(geom)\n",
    "            total_intersection_area = justice40_intersections['intersection'].area.sum()\n",
    "            \n",
    "            # Calculate percentage based on original polygon's area\n",
    "            percentage_1 = (total_intersection_area / geom.area) \n",
    "        else:\n",
    "            percentage_1 = 0.0  # No intersection with justice40 == 1 polygons\n",
    "        \n",
    "        # Append result\n",
    "        percentages.append(percentage_1)\n",
    "    \n",
    "    # Add results to the original GeoDataFrame\n",
    "    gdf[col_name] = percentages\n",
    "    return gdf\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fe80fc28-73ce-4a26-9925-851c2798e467",
   "metadata": {},
   "outputs": [],
   "source": [
    "%%time\n",
    "vec_file = './cpad-stats-temp.parquet'\n",
    "\n",
    "df = big_zonal_stats_binary(vec_file, \"ca_justice40.parquet\", col_name=\"percent_disadvantaged\")\n",
    "df.to_parquet(\"cpad-stats-temp.parquet\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e3083b85-1322-4188-ac08-e73c2570978c",
   "metadata": {},
   "source": [
    "# Cleaning up"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2e4de199-82d4-4e2b-8572-6fe19b57d1ee",
   "metadata": {},
   "outputs": [],
   "source": [
    "## clean up\n",
    "con = ibis.duckdb.connect(extensions=[\"spatial\"])\n",
    "ca_geom = con.read_parquet(\"ca2024-30m.parquet\").cast({\"geom\":\"geometry\"}).select(\"id\",\"geom\")\n",
    "\n",
    "ca = (con\n",
    "        .read_parquet(\"cpad-stats-temp.parquet\")\n",
    "        .cast({\n",
    "                \"crop_expansion\": \"int64\",\n",
    "                \"crop_reduction\": \"int64\",\n",
    "                \"manageable_carbon\": \"int64\",\n",
    "                \"irrecoverable_carbon\": \"int64\"\n",
    "            })\n",
    "        .rename(svi = \"SVI\")\n",
    "        .mutate(\n",
    "            richness=_.richness.round(3),\n",
    "                rsr=_.rsr.round(3),\n",
    "                all_species_rwr=_.all_species_rwr.round(3),\n",
    "                all_species_richness=_.all_species_richness.round(3),\n",
    "                percent_disadvantaged=(_.percent_disadvantaged).round(3),\n",
    "                svi=_.svi.round(3),\n",
    "                svi_socioeconomic_status=_.socioeconomic_status.round(3),\n",
    "                svi_household_char=_.household_char.round(3),\n",
    "                svi_racial_ethnic_minority=_.racial_ethnic_minority.round(3),\n",
    "                svi_housing_transit=_.housing_transit.round(3),\n",
    "                human_impact=_.human_impact.round(3),\n",
    "                deforest_carbon=_.deforest_carbon.round(3),\n",
    "                biodiversity_intactness_loss=_.biodiversity_intactness_loss.round(3),\n",
    "                forest_integrity_loss=_.forest_integrity_loss.round(3),\n",
    "            )\n",
    "          .select('established', 'reGAP', 'name', 'access_type', 'manager', 'manager_type', 'Easement', 'Acres', 'id', 'type','richness', \n",
    "                 'rsr', 'all_species_rwr', 'all_species_richness','deforest_carbon', 'irrecoverable_carbon', 'manageable_carbon', 'human_impact',\n",
    "                 'percent_disadvantaged','svi', 'svi_socioeconomic_status', 'svi_household_char', \n",
    "                 'svi_racial_ethnic_minority','svi_housing_transit')\n",
    "          .join(ca_geom, \"id\", how=\"inner\")\n",
    "        )\n",
    "\n",
    "ca.head(5).execute()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3780de2c-3a68-442c-bb3b-64c792418979",
   "metadata": {},
   "source": [
    "# Save as PMTiles + Upload data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "05c791c9-888a-483a-9dbb-a2ba7eb1bce2",
   "metadata": {},
   "outputs": [],
   "source": [
    "import subprocess\n",
    "import os\n",
    "from huggingface_hub import HfApi, login\n",
    "import streamlit as st\n",
    "\n",
    "login(st.secrets[\"HF_TOKEN\"])\n",
    "# api = HfApi(add_to_git_credential=False)\n",
    "api = HfApi()\n",
    "\n",
    "def hf_upload(file, repo_id):\n",
    "    info = api.upload_file(\n",
    "            path_or_fileobj=file,\n",
    "            path_in_repo=file,\n",
    "            repo_id=repo_id,\n",
    "            repo_type=\"dataset\",\n",
    "        )\n",
    "def generate_pmtiles(input_file, output_file, max_zoom=12):\n",
    "    # Ensure Tippecanoe is installed\n",
    "    if subprocess.call([\"which\", \"tippecanoe\"], stdout=subprocess.DEVNULL) != 0:\n",
    "        raise RuntimeError(\"Tippecanoe is not installed or not in PATH\")\n",
    "\n",
    "    # Construct the Tippecanoe command\n",
    "    command = [\n",
    "        \"tippecanoe\",\n",
    "        \"-o\", output_file,\n",
    "        \"-zg\",\n",
    "        \"--extend-zooms-if-still-dropping\",\n",
    "        \"--force\",\n",
    "        \"--projection\", \"EPSG:4326\",  \n",
    "        \"-L\",\"layer:\"+input_file,\n",
    "    ]\n",
    "    # Run Tippecanoe\n",
    "    try:\n",
    "        subprocess.run(command, check=True)\n",
    "        print(f\"Successfully generated PMTiles file: {output_file}\")\n",
    "    except subprocess.CalledProcessError as e:\n",
    "        print(f\"Error running Tippecanoe: {e}\")\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1f2d179d-6d47-4e84-83c6-7cb3d969fc00",
   "metadata": {},
   "outputs": [],
   "source": [
    "gdf = ca.execute().set_crs(\"EPSG:4326\")\n",
    "gdf.to_file(\"cpad-stats.geojson\")\n",
    "\n",
    "generate_pmtiles(\"cpad-stats.geojson\", \"cpad-stats.pmtiles\")\n",
    "hf_upload(\"cpad-stats.pmtiles\", \"boettiger-lab/ca-30x30\")\n",
    "\n",
    "gdf.to_parquet(\"cpad-stats.parquet\")\n",
    "hf_upload(\"cpad-stats.parquet\", \"boettiger-lab/ca-30x30\")\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bf53bfe6-2175-4aa8-b3cb-be2c1db86b1b",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}