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
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
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "707b1a14",
   "metadata": {},
   "source": [
    "Pre-process SVI Data from [CDC portal](https://www.atsdr.cdc.gov/place-health/php/svi/svi-data-documentation-download.html)\n",
    "\n",
    "- Tract data for United States from 2022, 2020, 2010, 2000.  \n",
    "- Data documentation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "803df305",
   "metadata": {},
   "outputs": [],
   "source": [
    "import ibis\n",
    "from ibis import _\n",
    "import streamlit as st\n",
    "from utilities import generate_pmtiles\n",
    "\n",
    "con = ibis.duckdb.connect(\"duck.db\", extensions=['httpfs', 'spatial', 'h3'])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "7ac648e6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "781a57e6e9004c5b8b7ae644aea77dbe",
       "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": "9de6547cfe7e4b32af6852eadf27e53e",
       "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": "stderr",
     "output_type": "stream",
     "text": [
      "For layer 0, using name \"svi\"\n",
      "84120 features, 34922477 bytes of geometry, 5150225 bytes of string pool\n",
      "tile 1/0/0 size is 673414 with detail 12, >500000    \n",
      "Going to try keeping the sparsest 66.82% of the features to make it fit\n",
      "tile 1/0/0 size is 654918 with detail 12, >500000    \n",
      "Going to try keeping the sparsest 45.92% of the features to make it fit\n",
      "tile 1/0/0 size is 627082 with detail 12, >500000    \n",
      "Going to try keeping the sparsest 32.95% of the features to make it fit\n",
      "tile 1/0/0 size is 571221 with detail 12, >500000    \n",
      "Going to try keeping the sparsest 25.96% of the features to make it fit\n",
      "tile 1/0/0 size is 515026 with detail 12, >500000    \n",
      "Going to try keeping the sparsest 22.68% of the features to make it fit\n",
      "tile 2/0/1 size is 556184 with detail 12, >500000    \n",
      "Going to try keeping the sparsest 80.91% of the features to make it fit\n",
      "tile 2/1/1 size is 680483 with detail 12, >500000    \n",
      "Going to try keeping the sparsest 66.13% of the features to make it fit\n",
      "tile 2/0/1 size is 544973 with detail 12, >500000    \n",
      "Going to try keeping the sparsest 66.81% of the features to make it fit\n",
      "tile 2/1/1 size is 633636 with detail 12, >500000    \n",
      "Going to try keeping the sparsest 46.96% of the features to make it fit\n",
      "tile 2/0/1 size is 529976 with detail 12, >500000    \n",
      "Going to try keeping the sparsest 56.73% of the features to make it fit\n",
      "tile 2/1/1 size is 562278 with detail 12, >500000    \n",
      "Going to try keeping the sparsest 37.59% of the features to make it fit\n",
      "tile 2/0/1 size is 509845 with detail 12, >500000    \n",
      "Going to try keeping the sparsest 50.07% of the features to make it fit\n",
      "tile 3/1/3 size is 614365 with detail 12, >500000    \n",
      "Going to try keeping the sparsest 73.25% of the features to make it fit\n",
      "tile 3/2/3 size is 828844 with detail 12, >500000    \n",
      "Going to try keeping the sparsest 54.29% of the features to make it fit\n",
      "tile 3/1/3 size is 557346 with detail 12, >500000    \n",
      "Going to try keeping the sparsest 59.14% of the features to make it fit\n",
      "tile 3/2/3 size is 622365 with detail 12, >500000    \n",
      "Going to try keeping the sparsest 39.26% of the features to make it fit\n",
      "tile 3/1/3 size is 507698 with detail 12, >500000    \n",
      "Going to try keeping the sparsest 52.42% of the features to make it fit\n",
      "tile 4/4/5 size is 513228 with detail 12, >500000    \n",
      "Going to try keeping the sparsest 87.68% of the features to make it fit\n",
      "tile 4/3/6 size is 635333 with detail 12, >500000    \n",
      "Going to try keeping the sparsest 70.83% of the features to make it fit\n",
      "tile 4/3/6 size is 515357 with detail 12, >500000    \n",
      "Going to try keeping the sparsest 61.85% of the features to make it fit\n",
      "tile 4/4/6 size is 1080604 with detail 12, >500000    \n",
      "Going to try keeping the sparsest 41.64% of the features to make it fit\n",
      "tile 4/4/6 size is 614947 with detail 12, >500000    \n",
      "Going to try keeping the sparsest 30.47% of the features to make it fit\n",
      "tile 5/8/12 size is 784796 with detail 12, >500000    \n",
      "Going to try keeping the sparsest 57.34% of the features to make it fit\n",
      "tile 5/8/12 size is 540488 with detail 12, >500000    \n",
      "Going to try keeping the sparsest 47.74% of the features to make it fit\n",
      "  99.9%  12/973/1656   \n",
      "  100.0%  12/4092/1352  \r"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Successfully generated PMTiles file: svi-data/2022/SVI2022_US_tract.pmtiles\n"
     ]
    }
   ],
   "source": [
    "expr = con.read_geo(\"svi-data/2022/SVI2022_US_tract.gdb\")\n",
    "expr.to_parquet(\"svi-data/2022/SVI2022_US_tract.parquet\")\n",
    "\n",
    "# tippecanoe requires geojson input to create PMTiles.  Drop most additional variables in PMTiles creation.\n",
    "query = ibis.to_sql(expr.select('STATE', 'COUNTY', 'LOCATION', 'FIPS', 'RPL_THEMES', 'Shape'))\n",
    "con.raw_sql(f\"COPY ({query}) TO '/tmp/svi.json' WITH (FORMAT GDAL, DRIVER 'GeoJSON', LAYER_CREATION_OPTIONS 'WRITE_BBOX=YES');\")\n",
    "\n",
    "generate_pmtiles(\"/tmp/svi.json\", \"svi-data/2022/SVI2022_US_tract.pmtiles\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "2e29cc6e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<minio.helpers.ObjectWriteResult at 0x77886893f050>"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import minio\n",
    "import re\n",
    "\n",
    "minio_key = st.secrets[\"MINIO_KEY\"]\n",
    "minio_secret = st.secrets[\"MINIO_SECRET\"]\n",
    "mc = minio.Minio(\"minio.carlboettiger.info\", minio_key, minio_secret)\n",
    "\n",
    "mc.fput_object(\"public-data\", \"social-vulnerability/2022/SVI2022_US_tract.pmtiles\", \"svi-data/2022/SVI2022_US_tract.pmtiles\")\n",
    "mc.fput_object(\"public-data\", \"social-vulnerability/2022/SVI2022_US_tract.parquet\", \"svi-data/2022/SVI2022_US_tract.parquet\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "5fcd59bc-72a4-4de7-9cdb-1b6eca9407fb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<duckdb.duckdb.DuckDBPyConnection at 0x7edb2419f330>"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "\n",
    "\n",
    "# Local cloud\n",
    "minio_key = st.secrets[\"MINIO_KEY\"]\n",
    "minio_secret = st.secrets[\"MINIO_SECRET\"]\n",
    "query1 =   f'''\n",
    "CREATE OR REPLACE SECRET secret1 (\n",
    "    TYPE S3,\n",
    "    KEY_ID '{minio_key}',\n",
    "    SECRET '{minio_secret}',\n",
    "    ENDPOINT 'minio.carlboettiger.info',\n",
    "    URL_STYLE 'path',\n",
    "    SCOPE \"s3://public-gbif\"\n",
    "\n",
    ");\n",
    "'''\n",
    "query2 =   f'''\n",
    "CREATE OR REPLACE SECRET secret2 (\n",
    "    TYPE S3,\n",
    "    KEY_ID '{minio_key}',\n",
    "    SECRET '{minio_secret}',\n",
    "    ENDPOINT 'minio.carlboettiger.info',\n",
    "    URL_STYLE 'path',\n",
    "    SCOPE \"s3://public-data\"\n",
    "\n",
    ");\n",
    "'''\n",
    "# don't scope to a single bucket\n",
    "#     SCOPE 's3://public-gbif'\n",
    "\n",
    "con.raw_sql(query1)\n",
    "con.raw_sql(query2)\n",
    "## Limits are sometimes good \n",
    "con.raw_sql(\"SET memory_limit = '20GB';\")\n",
    "con.raw_sql(\"set threads=40;\")\n",
    "\n",
    "# can/should we add explicit spatial index to gbif first?  using RTree takes too much memory"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "dcf50375-75ee-4208-87b2-6ffef6361742",
   "metadata": {},
   "outputs": [],
   "source": [
    "overture = (\n",
    "    con.read_parquet('s3://overturemaps-us-west-2/release/2024-11-13.0/theme=divisions/type=division_area/*', \n",
    "                     filename=True, hive_partitioning=1))\n",
    "usa = overture.filter(_.subtype==\"country\").filter(_.country == \"US\").select(_.geometry).execute()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "ce86081b-a46f-426b-9432-9bce588156ee",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "gbif = con.read_parquet(\"s3://public-gbif/2024-10-01/**\")\n",
    "svi = con.read_parquet(\"s3://public-data/social-vulnerability/2022/SVI2022_US_tract.parquet\").rename(geom = \"Shape\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3891abb6-3652-4217-8615-106d354ff131",
   "metadata": {},
   "source": [
    "We iterate through the city list to do this efficiently.  (Should we filter gbif down to US boundary as a one-off first?  We will assume it is efficient to filter the full globe state by state)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "69bf6dc6-4a13-4830-8c1a-87bb5899eb32",
   "metadata": {},
   "outputs": [],
   "source": [
    "all_states = svi.select(_.ST_ABBR).distinct().order_by(_.ST_ABBR).execute()[\"ST_ABBR\"]\n",
    "#all_states"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "32a2b4c1-e08b-4fbb-b891-ac19053a4585",
   "metadata": {},
   "outputs": [],
   "source": [
    "## select from the list we haven't yet written (allows resume).\n",
    "import minio\n",
    "import re\n",
    "\n",
    "minio_key = st.secrets[\"MINIO_KEY\"]\n",
    "minio_secret = st.secrets[\"MINIO_SECRET\"]\n",
    "mc = minio.Minio(\"minio.carlboettiger.info\", minio_key, minio_secret)\n",
    "obj = mc.list_objects(\"public-gbif\", \"social-vulnerability\", recursive=True)\n",
    "pattern = r\"social-vulnerability/|\\.parquet$\"\n",
    "finished = [re.sub(pattern, \"\",  i.object_name) for i in obj if not i.is_dir]\n",
    "remaining = set(all_states) - set(finished)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "4ecc58a3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'AK',\n",
       " 'AL',\n",
       " 'AR',\n",
       " 'AZ',\n",
       " 'CA',\n",
       " 'CO',\n",
       " 'CT',\n",
       " 'DC',\n",
       " 'DE',\n",
       " 'FL',\n",
       " 'GA',\n",
       " 'HI',\n",
       " 'IA',\n",
       " 'ID',\n",
       " 'IL',\n",
       " 'IN',\n",
       " 'KS',\n",
       " 'KY',\n",
       " 'LA',\n",
       " 'MA',\n",
       " 'MD',\n",
       " 'ME',\n",
       " 'MI',\n",
       " 'MN',\n",
       " 'MO',\n",
       " 'MS',\n",
       " 'MT',\n",
       " 'NC',\n",
       " 'ND',\n",
       " 'NE',\n",
       " 'NH',\n",
       " 'NJ',\n",
       " 'NM',\n",
       " 'NV',\n",
       " 'NY',\n",
       " 'OH',\n",
       " 'OK',\n",
       " 'OR',\n",
       " 'PA',\n",
       " 'RI',\n",
       " 'SC',\n",
       " 'SD',\n",
       " 'TN',\n",
       " 'TX',\n",
       " 'UT',\n",
       " 'VA',\n",
       " 'VT',\n",
       " 'WA',\n",
       " 'WI',\n",
       " 'WV',\n",
       " 'WY'}"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "remaining"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c3a4005c-1e8c-4f2a-a93c-1c158c9c26ab",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "NV/Eureka County\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "6fd7b6e6fe1a4e3b9c8d476e0e757644",
       "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": [
      "NV/Lander County\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "80f08f9cc267481996667dd1e383a3fb",
       "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": [
      "NV/Clark County\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "872f806c983d4804b399881eac7d3bd9",
       "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": [
      "NV/Storey County\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "9927ce307cc8413f973891b388c89288",
       "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": [
      "NV/Churchill County\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "a5d573d2766c41fbbfde5af9ed19ab76",
       "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": [
      "NV/Esmeralda County\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "bda8ee87bd1340f1b666c61b5d6bc716",
       "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": [
      "NV/Lyon County\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "460041e4c2a745d5b0ff6dff64f26343",
       "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": [
      "NV/Nye County\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "7690f211b2e2418e876898369d3b04ef",
       "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": [
      "NV/Douglas County\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "fb3235545ac04933ae82d95b5783657e",
       "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": [
      "NV/Elko County\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "9c72bf43115f4c65b9b7bdfb44c2fc67",
       "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": [
      "NV/Pershing County\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "2e7d1ff419fe4bf4ae07c1d4288d0259",
       "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": [
      "NV/Washoe County\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "c57a17eaf70941a9968ad2db89e2c98d",
       "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": [
      "NV/Humboldt County\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "958d00b68de94acaa96d63871daa356b",
       "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": [
      "NV/Carson City\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "a162c535a70f4dc89b7e708f2fc8633b",
       "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": [
      "NV/Lincoln County\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "e4ca31b6f6b64667bc8c1e5dfac7ab03",
       "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": [
      "NV/White Pine County\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "b89cd3441e714d5983daffa6c93cc0ee",
       "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": [
      "NV/Mineral County\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "c05bb3bbd3434373a068a489410beb4a",
       "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": [
      "NE/Blaine County\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "f88979e7186546f3be239f901393ab8a",
       "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": [
      "NE/Butler County\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "c4bfcd9ba81944b29907ccf3c6d3783e",
       "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": [
      "NE/Custer County\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "6cd52ae08b7b4dea931ff2ffa5d6c7f6",
       "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": [
      "NE/Dakota County\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "b3a2e7411b69407ba76c06b0d083a961",
       "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": [
      "NE/Kearney County\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "e0ccf52489a3467ba172afef3e36f2f0",
       "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": [
      "NE/Keith County\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "e6f8cd9d59284e7aaa9eabe117a69079",
       "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": [
    "## And here we go, long-running loop over each city\n",
    "for i in remaining:\n",
    "    counties = svi.filter(_.ST_ABBR == i).select(_.COUNTY).distinct().execute()[\"COUNTY\"].to_numpy()\n",
    "    for county in counties:\n",
    "       gdf = (svi\n",
    "              .filter(_.ST_ABBR == i, _.COUNTY== county)\n",
    "              .mutate(area = _.geom.area())\n",
    "       )\n",
    "\n",
    "       print(i + \"/\" + county)\n",
    "       \n",
    "       bounds =  gdf.execute().total_bounds\n",
    "       points = (gbif\n",
    "              .filter(_.decimallongitude >= bounds[0], \n",
    "                     _.decimallongitude < bounds[2], \n",
    "                     _.decimallatitude >= bounds[1], \n",
    "                     _.decimallatitude < bounds[3])\n",
    "              )\n",
    "                     \n",
    "       (gdf\n",
    "       .join(points, gdf.geom.intersects(points.geom))\n",
    "       .to_parquet(f\"s3://public-gbif/social-vulnerability/state={i}/{county}.parquet\")\n",
    "       )\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "050a358f-e2de-49bd-a80d-4f8c47e36bab",
   "metadata": {},
   "source": [
    "gbif_usa = con.read_parquet(\"s3://cboettig/gbif/svi/**\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "9bd1299b-af6b-4d85-97fb-ba83a5c26c70",
   "metadata": {},
   "outputs": [
    {
     "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: ibis_read_parquet_msislo4d7fcgdfh2pyoxvxjkdu\n",
       "  OBJECTID                         int64\n",
       "  ST                               string\n",
       "  STATE                            string\n",
       "  ST_ABBR                          string\n",
       "  STCNTY                           string\n",
       "  COUNTY                           string\n",
       "  FIPS                             string\n",
       "  LOCATION                         string\n",
       "  AREA_SQMI                        float64\n",
       "  E_TOTPOP                         int32\n",
       "  M_TOTPOP                         int32\n",
       "  E_HU                             int32\n",
       "  M_HU                             int32\n",
       "  E_HH                             int32\n",
       "  M_HH                             int32\n",
       "  E_POV150                         int32\n",
       "  M_POV150                         int32\n",
       "  E_UNEMP                          int32\n",
       "  M_UNEMP                          int32\n",
       "  E_HBURD                          int32\n",
       "  M_HBURD                          int32\n",
       "  E_NOHSDP                         int32\n",
       "  M_NOHSDP                         int32\n",
       "  E_UNINSUR                        int32\n",
       "  M_UNINSUR                        int32\n",
       "  E_AGE65                          int32\n",
       "  M_AGE65                          int32\n",
       "  E_AGE17                          int32\n",
       "  M_AGE17                          int32\n",
       "  E_DISABL                         int32\n",
       "  M_DISABL                         int32\n",
       "  E_SNGPNT                         int32\n",
       "  M_SNGPNT                         int32\n",
       "  E_LIMENG                         int32\n",
       "  M_LIMENG                         int32\n",
       "  E_MINRTY                         int32\n",
       "  M_MINRTY                         int32\n",
       "  E_MUNIT                          int32\n",
       "  M_MUNIT                          int32\n",
       "  E_MOBILE                         int32\n",
       "  M_MOBILE                         int32\n",
       "  E_CROWD                          int32\n",
       "  M_CROWD                          int32\n",
       "  E_NOVEH                          int32\n",
       "  M_NOVEH                          int32\n",
       "  E_GROUPQ                         int32\n",
       "  M_GROUPQ                         int32\n",
       "  EP_POV150                        float64\n",
       "  MP_POV150                        float64\n",
       "  EP_UNEMP                         float64\n",
       "  MP_UNEMP                         float64\n",
       "  EP_HBURD                         float64\n",
       "  MP_HBURD                         float64\n",
       "  EP_NOHSDP                        float64\n",
       "  MP_NOHSDP                        float64\n",
       "  EP_UNINSUR                       float64\n",
       "  MP_UNINSUR                       float64\n",
       "  EP_AGE65                         float64\n",
       "  MP_AGE65                         float64\n",
       "  EP_AGE17                         float64\n",
       "  MP_AGE17                         float64\n",
       "  EP_DISABL                        float64\n",
       "  MP_DISABL                        float64\n",
       "  EP_SNGPNT                        float64\n",
       "  MP_SNGPNT                        float64\n",
       "  EP_LIMENG                        float64\n",
       "  MP_LIMENG                        float64\n",
       "  EP_MINRTY                        float64\n",
       "  MP_MINRTY                        float64\n",
       "  EP_MUNIT                         float64\n",
       "  MP_MUNIT                         float64\n",
       "  EP_MOBILE                        float64\n",
       "  MP_MOBILE                        float64\n",
       "  EP_CROWD                         float64\n",
       "  MP_CROWD                         float64\n",
       "  EP_NOVEH                         float64\n",
       "  MP_NOVEH                         float64\n",
       "  EP_GROUPQ                        float64\n",
       "  MP_GROUPQ                        float64\n",
       "  EPL_POV150                       float64\n",
       "  EPL_UNEMP                        float64\n",
       "  EPL_HBURD                        float64\n",
       "  EPL_NOHSDP                       float64\n",
       "  EPL_UNINSUR                      float64\n",
       "  SPL_THEME1                       float64\n",
       "  RPL_THEME1                       float64\n",
       "  EPL_AGE65                        float64\n",
       "  EPL_AGE17                        float64\n",
       "  EPL_DISABL                       float64\n",
       "  EPL_SNGPNT                       float64\n",
       "  EPL_LIMENG                       float64\n",
       "  SPL_THEME2                       float64\n",
       "  RPL_THEME2                       float64\n",
       "  EPL_MINRTY                       float64\n",
       "  SPL_THEME3                       float64\n",
       "  RPL_THEME3                       float64\n",
       "  EPL_MUNIT                        float64\n",
       "  EPL_MOBILE                       float64\n",
       "  EPL_CROWD                        float64\n",
       "  EPL_NOVEH                        float64\n",
       "  EPL_GROUPQ                       float64\n",
       "  SPL_THEME4                       float64\n",
       "  RPL_THEME4                       float64\n",
       "  SPL_THEMES                       float64\n",
       "  RPL_THEMES                       float64\n",
       "  F_POV150                         int16\n",
       "  F_UNEMP                          int16\n",
       "  F_HBURD                          int16\n",
       "  F_NOHSDP                         int16\n",
       "  F_UNINSUR                        int16\n",
       "  F_THEME1                         int16\n",
       "  F_AGE65                          int16\n",
       "  F_AGE17                          int16\n",
       "  F_DISABL                         int16\n",
       "  F_SNGPNT                         int16\n",
       "  F_LIMENG                         int16\n",
       "  F_THEME2                         int16\n",
       "  F_MINRTY                         int16\n",
       "  F_THEME3                         int16\n",
       "  F_MUNIT                          int16\n",
       "  F_MOBILE                         int16\n",
       "  F_CROWD                          int16\n",
       "  F_NOVEH                          int16\n",
       "  F_GROUPQ                         int16\n",
       "  F_THEME4                         int16\n",
       "  F_TOTAL                          int16\n",
       "  E_DAYPOP                         int32\n",
       "  E_NOINT                          int32\n",
       "  M_NOINT                          int32\n",
       "  E_AFAM                           int32\n",
       "  M_AFAM                           int32\n",
       "  E_HISP                           int32\n",
       "  M_HISP                           int32\n",
       "  E_ASIAN                          int32\n",
       "  M_ASIAN                          int32\n",
       "  E_AIAN                           int32\n",
       "  M_AIAN                           int32\n",
       "  E_NHPI                           int32\n",
       "  M_NHPI                           int32\n",
       "  E_TWOMORE                        int32\n",
       "  M_TWOMORE                        int32\n",
       "  E_OTHERRACE                      int32\n",
       "  M_OTHERRACE                      int32\n",
       "  EP_NOINT                         float64\n",
       "  MP_NOINT                         float64\n",
       "  EP_AFAM                          float64\n",
       "  MP_AFAM                          float64\n",
       "  EP_HISP                          float64\n",
       "  MP_HISP                          float64\n",
       "  EP_ASIAN                         float64\n",
       "  MP_ASIAN                         float64\n",
       "  EP_AIAN                          float64\n",
       "  MP_AIAN                          float64\n",
       "  EP_NHPI                          float64\n",
       "  MP_NHPI                          float64\n",
       "  EP_TWOMORE                       float64\n",
       "  MP_TWOMORE                       float64\n",
       "  EP_OTHERRACE                     float64\n",
       "  MP_OTHERRACE                     float64\n",
       "  Shape_Length                     float64\n",
       "  Shape_Area                       float64\n",
       "  geom                             geospatial:geometry\n",
       "  area                             float64\n",
       "  gbifid                           string\n",
       "  datasetkey                       string\n",
       "  occurrenceid                     string\n",
       "  kingdom                          string\n",
       "  phylum                           string\n",
       "  class                            string\n",
       "  order                            string\n",
       "  family                           string\n",
       "  genus                            string\n",
       "  species                          string\n",
       "  infraspecificepithet             string\n",
       "  taxonrank                        string\n",
       "  scientificname                   string\n",
       "  verbatimscientificname           string\n",
       "  verbatimscientificnameauthorship string\n",
       "  countrycode                      string\n",
       "  locality                         string\n",
       "  stateprovince                    string\n",
       "  occurrencestatus                 string\n",
       "  individualcount                  int32\n",
       "  publishingorgkey                 string\n",
       "  decimallatitude                  float64\n",
       "  decimallongitude                 float64\n",
       "  coordinateuncertaintyinmeters    float64\n",
       "  coordinateprecision              float64\n",
       "  elevation                        float64\n",
       "  elevationaccuracy                float64\n",
       "  depth                            float64\n",
       "  depthaccuracy                    float64\n",
       "  eventdate                        timestamp(6)\n",
       "  day                              int32\n",
       "  month                            int32\n",
       "  year                             int32\n",
       "  taxonkey                         int32\n",
       "  specieskey                       int32\n",
       "  basisofrecord                    string\n",
       "  institutioncode                  string\n",
       "  collectioncode                   string\n",
       "  catalognumber                    string\n",
       "  recordnumber                     string\n",
       "  identifiedby                     array&lt;string&gt;\n",
       "  dateidentified                   timestamp(6)\n",
       "  license                          string\n",
       "  rightsholder                     string\n",
       "  recordedby                       array&lt;string&gt;\n",
       "  typestatus                       array&lt;string&gt;\n",
       "  establishmentmeans               string\n",
       "  lastinterpreted                  timestamp(6)\n",
       "  mediatype                        array&lt;string&gt;\n",
       "  issue                            array&lt;string&gt;\n",
       "  geom_right                       geospatial:geometry\n",
       "  h0                               string\n",
       "  h1                               string\n",
       "  h2                               string\n",
       "  h3                               string\n",
       "  h4                               string\n",
       "  h5                               string\n",
       "  h6                               string\n",
       "  h7                               string\n",
       "  h8                               string\n",
       "  h9                               string\n",
       "  h10                              string\n",
       "  h11                              string\n",
       "</pre>\n"
      ],
      "text/plain": [
       "DatabaseTable: ibis_read_parquet_msislo4d7fcgdfh2pyoxvxjkdu\n",
       "  OBJECTID                         int64\n",
       "  ST                               string\n",
       "  STATE                            string\n",
       "  ST_ABBR                          string\n",
       "  STCNTY                           string\n",
       "  COUNTY                           string\n",
       "  FIPS                             string\n",
       "  LOCATION                         string\n",
       "  AREA_SQMI                        float64\n",
       "  E_TOTPOP                         int32\n",
       "  M_TOTPOP                         int32\n",
       "  E_HU                             int32\n",
       "  M_HU                             int32\n",
       "  E_HH                             int32\n",
       "  M_HH                             int32\n",
       "  E_POV150                         int32\n",
       "  M_POV150                         int32\n",
       "  E_UNEMP                          int32\n",
       "  M_UNEMP                          int32\n",
       "  E_HBURD                          int32\n",
       "  M_HBURD                          int32\n",
       "  E_NOHSDP                         int32\n",
       "  M_NOHSDP                         int32\n",
       "  E_UNINSUR                        int32\n",
       "  M_UNINSUR                        int32\n",
       "  E_AGE65                          int32\n",
       "  M_AGE65                          int32\n",
       "  E_AGE17                          int32\n",
       "  M_AGE17                          int32\n",
       "  E_DISABL                         int32\n",
       "  M_DISABL                         int32\n",
       "  E_SNGPNT                         int32\n",
       "  M_SNGPNT                         int32\n",
       "  E_LIMENG                         int32\n",
       "  M_LIMENG                         int32\n",
       "  E_MINRTY                         int32\n",
       "  M_MINRTY                         int32\n",
       "  E_MUNIT                          int32\n",
       "  M_MUNIT                          int32\n",
       "  E_MOBILE                         int32\n",
       "  M_MOBILE                         int32\n",
       "  E_CROWD                          int32\n",
       "  M_CROWD                          int32\n",
       "  E_NOVEH                          int32\n",
       "  M_NOVEH                          int32\n",
       "  E_GROUPQ                         int32\n",
       "  M_GROUPQ                         int32\n",
       "  EP_POV150                        float64\n",
       "  MP_POV150                        float64\n",
       "  EP_UNEMP                         float64\n",
       "  MP_UNEMP                         float64\n",
       "  EP_HBURD                         float64\n",
       "  MP_HBURD                         float64\n",
       "  EP_NOHSDP                        float64\n",
       "  MP_NOHSDP                        float64\n",
       "  EP_UNINSUR                       float64\n",
       "  MP_UNINSUR                       float64\n",
       "  EP_AGE65                         float64\n",
       "  MP_AGE65                         float64\n",
       "  EP_AGE17                         float64\n",
       "  MP_AGE17                         float64\n",
       "  EP_DISABL                        float64\n",
       "  MP_DISABL                        float64\n",
       "  EP_SNGPNT                        float64\n",
       "  MP_SNGPNT                        float64\n",
       "  EP_LIMENG                        float64\n",
       "  MP_LIMENG                        float64\n",
       "  EP_MINRTY                        float64\n",
       "  MP_MINRTY                        float64\n",
       "  EP_MUNIT                         float64\n",
       "  MP_MUNIT                         float64\n",
       "  EP_MOBILE                        float64\n",
       "  MP_MOBILE                        float64\n",
       "  EP_CROWD                         float64\n",
       "  MP_CROWD                         float64\n",
       "  EP_NOVEH                         float64\n",
       "  MP_NOVEH                         float64\n",
       "  EP_GROUPQ                        float64\n",
       "  MP_GROUPQ                        float64\n",
       "  EPL_POV150                       float64\n",
       "  EPL_UNEMP                        float64\n",
       "  EPL_HBURD                        float64\n",
       "  EPL_NOHSDP                       float64\n",
       "  EPL_UNINSUR                      float64\n",
       "  SPL_THEME1                       float64\n",
       "  RPL_THEME1                       float64\n",
       "  EPL_AGE65                        float64\n",
       "  EPL_AGE17                        float64\n",
       "  EPL_DISABL                       float64\n",
       "  EPL_SNGPNT                       float64\n",
       "  EPL_LIMENG                       float64\n",
       "  SPL_THEME2                       float64\n",
       "  RPL_THEME2                       float64\n",
       "  EPL_MINRTY                       float64\n",
       "  SPL_THEME3                       float64\n",
       "  RPL_THEME3                       float64\n",
       "  EPL_MUNIT                        float64\n",
       "  EPL_MOBILE                       float64\n",
       "  EPL_CROWD                        float64\n",
       "  EPL_NOVEH                        float64\n",
       "  EPL_GROUPQ                       float64\n",
       "  SPL_THEME4                       float64\n",
       "  RPL_THEME4                       float64\n",
       "  SPL_THEMES                       float64\n",
       "  RPL_THEMES                       float64\n",
       "  F_POV150                         int16\n",
       "  F_UNEMP                          int16\n",
       "  F_HBURD                          int16\n",
       "  F_NOHSDP                         int16\n",
       "  F_UNINSUR                        int16\n",
       "  F_THEME1                         int16\n",
       "  F_AGE65                          int16\n",
       "  F_AGE17                          int16\n",
       "  F_DISABL                         int16\n",
       "  F_SNGPNT                         int16\n",
       "  F_LIMENG                         int16\n",
       "  F_THEME2                         int16\n",
       "  F_MINRTY                         int16\n",
       "  F_THEME3                         int16\n",
       "  F_MUNIT                          int16\n",
       "  F_MOBILE                         int16\n",
       "  F_CROWD                          int16\n",
       "  F_NOVEH                          int16\n",
       "  F_GROUPQ                         int16\n",
       "  F_THEME4                         int16\n",
       "  F_TOTAL                          int16\n",
       "  E_DAYPOP                         int32\n",
       "  E_NOINT                          int32\n",
       "  M_NOINT                          int32\n",
       "  E_AFAM                           int32\n",
       "  M_AFAM                           int32\n",
       "  E_HISP                           int32\n",
       "  M_HISP                           int32\n",
       "  E_ASIAN                          int32\n",
       "  M_ASIAN                          int32\n",
       "  E_AIAN                           int32\n",
       "  M_AIAN                           int32\n",
       "  E_NHPI                           int32\n",
       "  M_NHPI                           int32\n",
       "  E_TWOMORE                        int32\n",
       "  M_TWOMORE                        int32\n",
       "  E_OTHERRACE                      int32\n",
       "  M_OTHERRACE                      int32\n",
       "  EP_NOINT                         float64\n",
       "  MP_NOINT                         float64\n",
       "  EP_AFAM                          float64\n",
       "  MP_AFAM                          float64\n",
       "  EP_HISP                          float64\n",
       "  MP_HISP                          float64\n",
       "  EP_ASIAN                         float64\n",
       "  MP_ASIAN                         float64\n",
       "  EP_AIAN                          float64\n",
       "  MP_AIAN                          float64\n",
       "  EP_NHPI                          float64\n",
       "  MP_NHPI                          float64\n",
       "  EP_TWOMORE                       float64\n",
       "  MP_TWOMORE                       float64\n",
       "  EP_OTHERRACE                     float64\n",
       "  MP_OTHERRACE                     float64\n",
       "  Shape_Length                     float64\n",
       "  Shape_Area                       float64\n",
       "  geom                             geospatial:geometry\n",
       "  area                             float64\n",
       "  gbifid                           string\n",
       "  datasetkey                       string\n",
       "  occurrenceid                     string\n",
       "  kingdom                          string\n",
       "  phylum                           string\n",
       "  class                            string\n",
       "  order                            string\n",
       "  family                           string\n",
       "  genus                            string\n",
       "  species                          string\n",
       "  infraspecificepithet             string\n",
       "  taxonrank                        string\n",
       "  scientificname                   string\n",
       "  verbatimscientificname           string\n",
       "  verbatimscientificnameauthorship string\n",
       "  countrycode                      string\n",
       "  locality                         string\n",
       "  stateprovince                    string\n",
       "  occurrencestatus                 string\n",
       "  individualcount                  int32\n",
       "  publishingorgkey                 string\n",
       "  decimallatitude                  float64\n",
       "  decimallongitude                 float64\n",
       "  coordinateuncertaintyinmeters    float64\n",
       "  coordinateprecision              float64\n",
       "  elevation                        float64\n",
       "  elevationaccuracy                float64\n",
       "  depth                            float64\n",
       "  depthaccuracy                    float64\n",
       "  eventdate                        timestamp(6)\n",
       "  day                              int32\n",
       "  month                            int32\n",
       "  year                             int32\n",
       "  taxonkey                         int32\n",
       "  specieskey                       int32\n",
       "  basisofrecord                    string\n",
       "  institutioncode                  string\n",
       "  collectioncode                   string\n",
       "  catalognumber                    string\n",
       "  recordnumber                     string\n",
       "  identifiedby                     array<string>\n",
       "  dateidentified                   timestamp(6)\n",
       "  license                          string\n",
       "  rightsholder                     string\n",
       "  recordedby                       array<string>\n",
       "  typestatus                       array<string>\n",
       "  establishmentmeans               string\n",
       "  lastinterpreted                  timestamp(6)\n",
       "  mediatype                        array<string>\n",
       "  issue                            array<string>\n",
       "  geom_right                       geospatial:geometry\n",
       "  h0                               string\n",
       "  h1                               string\n",
       "  h2                               string\n",
       "  h3                               string\n",
       "  h4                               string\n",
       "  h5                               string\n",
       "  h6                               string\n",
       "  h7                               string\n",
       "  h8                               string\n",
       "  h9                               string\n",
       "  h10                              string\n",
       "  h11                              string"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gbif_usa"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a6ce4d65-6f93-4725-87fa-29bf413398ad",
   "metadata": {},
   "outputs": [],
   "source": [
    "The four summary theme ranking variables, detailed in the Data Dictionary below, are:\n",
    "• Socioeconomic Status - RPL_THEME1\n",
    "• Household Characteristics - RPL_THEME2\n",
    "• Racial & Ethnic Minority Status - RPL_THEME3\n",
    "• Housing Type & Transportation - RPL_THEME4 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d2e85529-348b-4f33-b09d-f8424299dc8d",
   "metadata": {},
   "outputs": [],
   "source": [
    "import seaborn.objects as so\n",
    "\n",
    "#df = gbif_usa.group_by(_.FIPS).agg(n = _.count().log(), svi = _.RPL_THEMES.mean()).execute()\n",
    "df = gbif_usa.group_by(_.STATE, _.COUNTY).agg(n = _.count() / _.Shape_Area.sum(), svi1 = _.RPL_THEME1.mean(), svi3 = _.RPL_THEME3.mean()).execute()\n",
    "\n",
    "so.Plot(df, x = \"svi1\", y=\"n\", color = \"svi3\").add(so.Dots()).scale(y=\"log\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9030d3dc-e2fb-41b7-8fe9-80ee76739b78",
   "metadata": {},
   "outputs": [],
   "source": [
    "import altair as alt\n",
    "\n",
    "alt.Chart(df).mark_point().encode(\n",
    "    x='svi1',\n",
    "    y='n',\n",
    "    color='svi3',\n",
    "    tooltip = ['STATE', 'COUNTY']\n",
    ")\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "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.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}