Newer
Older
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
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
{
"cells": [
{
"cell_type": "markdown",
"id": "471a762b",
"metadata": {},
"source": [
"# Visualization 2"
]
},
{
"cell_type": "markdown",
"id": "2478daaa-cb6c-4d73-92af-01ae91e773fe",
"metadata": {},
"source": [
"### Geographic Data / Maps\n",
"\n",
"#### Installation\n",
"```python\n",
"pip3 install --upgrade pip\n",
"pip3 install geopandas shapely descartes geopy netaddr\n",
"sudo apt install -y python3-rtree\n",
"```\n",
"\n",
"- `import geopandas as gpd`\n",
"- `.shp` => Shapefile\n",
"- `gpd.datasets.get_path(<shp file path>)`:\n",
" - example: `gpd.datasets.get_path(\"naturalearth_lowres\")`\n",
"- `gpd.read_file(<path>)`"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e6f50cc3",
"metadata": {},
"outputs": [],
"source": [
"import matplotlib\n",
"import matplotlib.pyplot as plt\n",
"import pandas as pd\n",
"import math\n",
"import requests\n",
"import re\n",
"import os\n",
"\n",
"# new import statements\n",
"import geopandas as gpd\n",
"from shapely.geometry import Point, Polygon, box"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "257671bc-1e79-47c2-aa7a-1725562d23ef",
"metadata": {},
"outputs": [],
"source": [
"!ls /home/gurmail.singh/.local/lib/python3.8/site-packages/geopandas/datasets/naturalearth_lowres"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e7269706-188a-48e3-882a-ac068170b1af",
"metadata": {},
"outputs": [],
"source": [
"!ls /home/gurmail.singh/.local/lib/python3.8/site-packages/geopandas/datasets"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "273ed288",
"metadata": {},
"outputs": [],
"source": [
"# Find the path for \"naturalearth_lowres\"\n",
"path = \n",
"# Read the shapefile for \"naturalearth_lowres\" and\n",
"# set index using \"name\" column\n",
"gdf = "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cf4d871c-d6d6-4d99-be96-75faf804d4a7",
"metadata": {},
"outputs": [],
"source": [
"gdf.head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5c983208-7851-4d25-92e7-3f110039411a",
"metadata": {},
"outputs": [],
"source": [
"type(gdf).__mro__"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "653edec9-8b82-4684-ae82-a9fa399459ce",
"metadata": {},
"outputs": [],
"source": [
"# All shapefiles have a column called \"geometry\"\n",
"gdf[\"geometry\"]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e2eb071a-b29a-4edd-8747-50b02fd43108",
"metadata": {},
"outputs": [],
"source": [
"type(gdf[\"geometry\"]).__mro__"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "da5b5783-d78e-460f-bd22-b41e7f24f871",
"metadata": {},
"outputs": [],
"source": [
"# First country's name and geometry\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8f4da997-567d-47f6-8594-f0a37b92b9e6",
"metadata": {},
"outputs": [],
"source": [
"# Second country's name geometry\n",
"print(gdf.index[1])\n",
"gdf[\"geometry\"].iat[1]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d380b32f-c401-4601-b6d3-02ac344b3f08",
"metadata": {},
"outputs": [],
"source": [
"# Geometry for \"United States of America\"\n",
"# gdf.at[<row_index>, <column_name>]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3528f252-36b7-4ca5-9108-0951367837cc",
"metadata": {},
"outputs": [],
"source": [
"# Type of Tanzania's geometry\n",
"print(gdf.index[1], type(gdf[\"geometry\"].iat[1]))\n",
"\n",
"# Type of United States of America's geometry\n",
"print(\"United States of America\", type(gdf.at[\"United States of America\", \"geometry\"]))"
]
},
{
"cell_type": "markdown",
"id": "47711b72-a24c-4da8-a804-3faf88a5fe8b",
"metadata": {},
"source": [
"- `gdf.plot(figsize=(<width>, <height>), column=<column name>)`\n",
"- `ax.set_axis_off()`"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9874c7de-226e-4296-af60-45e091836a19",
"metadata": {},
"outputs": [],
"source": [
"ax = gdf.plot(figsize=(8,4))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f5547cc5-3404-421e-aeed-4b4cf8ee8382",
"metadata": {},
"outputs": [],
"source": [
"# Set facecolor=\"0.7\", edgecolor=\"black\"\n",
"ax = gdf.plot(figsize=(8,4))\n",
"# Turn off the axes\n",
"# ax.set_axis_off()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "58da716e",
"metadata": {},
"outputs": [],
"source": [
"# Color the map based on population column, column=\"pop_est\" and set cmap=\"cool\" and legend=True\n",
"ax = gdf.plot(figsize=(8,4))\n",
"# Turn off the axes\n",
"ax.set_axis_off()"
]
},
{
"cell_type": "markdown",
"id": "c7a85431-bdab-46ab-8e29-5f91d8a2e0bc",
"metadata": {},
"source": [
"#### Create a map where countries with >100M people are red, others are gray."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "962a552b-94a6-4690-8018-f82173dd6096",
"metadata": {},
"outputs": [],
"source": [
"# Create a map where countries with >100M people are red, others are gray\n",
"\n",
"# Add a new column called color to gdf and set default value to \"lightgray\"\n",
"\n",
"# Boolean indexing to set color to red for countries with \"pop_est\" > 1e8\n",
"\n",
"# Create the plot\n",
"# ax = gdf.plot(figsize=(8,4), color=gdf[\"color\"])\n",
"# ax.set_axis_off()"
]
},
{
"cell_type": "markdown",
"id": "5a32c52c-509f-4f7f-8dd6-bc7fe53c64fd",
"metadata": {},
"source": [
"### All shapefile geometries are shapely shapes. "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "33e92def-a7db-4d90-adc8-a3d01d472ac1",
"metadata": {},
"outputs": [],
"source": [
"type(gdf[\"geometry\"].iat[2])"
]
},
{
"cell_type": "markdown",
"id": "39c95c23",
"metadata": {},
"source": [
"### Shapely shapes\n",
"\n",
"- `from shapely.geometry import Point, Polygon, box`\n",
"- `Polygon([(<x1>, <y1>), (<x2>, <y2>), (<x3>, <y3>), ...])`\n",
"- `box(minx, miny, maxx, maxy)`\n",
"- `Point(<x>, <y>)`\n",
"- `<shapely object>.buffer(<size>)`\n",
" - example: `Point(5, 5).buffer(3)` creates a circle"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "61716db9",
"metadata": {},
"outputs": [],
"source": [
"triangle = # triangle\n",
"triangle"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6a36021a-8653-4698-a0ba-818c14091d79",
"metadata": {},
"outputs": [],
"source": [
"type(triangle)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bddd958d-6fde-42df-8ae3-459e25fa3f04",
"metadata": {},
"outputs": [],
"source": [
"box1 = # not a type; just a function that creates box\n",
"box1"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "eb7ade8d-96d2-4770-bce9-b3e35996b0b8",
"metadata": {},
"outputs": [],
"source": [
"type(box1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e5308c61-b1fa-433b-bb05-485ca7bd23da",
"metadata": {},
"outputs": [],
"source": [
"point = \n",
"point"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5c669619-af78-477d-b807-3e6d99278f2f",
"metadata": {},
"outputs": [],
"source": [
"type(point)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f015d27e-8fd8-446f-992f-4f7a88cc582d",
"metadata": {},
"outputs": [],
"source": [
"# use buffer to create a circle from a point\n",
"circle = \n",
"circle"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "39fe5752-8038-46cc-b4a6-320e6a78bbd1",
"metadata": {},
"outputs": [],
"source": [
"type(circle)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "800cc48d-c241-4439-9161-ff309e50373f",
"metadata": {},
"outputs": [],
"source": [
"triangle_buffer = triangle.buffer(3)\n",
"triangle_buffer"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6b9478d3-7cc9-4e80-ae84-e56d7bfd0b71",
"metadata": {},
"outputs": [],
"source": [
"type(triangle_buffer)"
]
},
{
"cell_type": "markdown",
"id": "1a340443-d750-43d5-99cd-28e7034ce898",
"metadata": {},
"source": [
"#### Shapely methods\n",
"\n",
"- Shapely methods:\n",
" - `union`: any point that is in either shape (OR)\n",
" - `intersection`: any point that is in both shapes (AND)\n",
" - `difference`: subtraction\n",
" - `intersects`: do they overlap? returns True / False"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d1c5f9f7",
"metadata": {},
"outputs": [],
"source": [
"# union triangle and box1\n",
"# it will give any point that is in either shape (OR)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8a2d3357",
"metadata": {},
"outputs": [],
"source": [
"# intersection triangle and box1\n",
"# any point that is in both shapes (AND)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f327bb99-be34-4a00-a216-a6f40df48268",
"metadata": {},
"outputs": [],
"source": [
"# difference of triangle and box1"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9082b54a",
"metadata": {},
"outputs": [],
"source": [
"# difference of box1 and triangle\n",
"box1.difference(triangle) # subtraction"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0def4359-78f6-4f14-80ae-05c25ff64bc5",
"metadata": {},
"outputs": [],
"source": [
"# check whether triangle intersects box1\n",
"# the is, check do they overlap?"
]
},
{
"cell_type": "markdown",
"id": "51a82cf3-fe28-46d2-a019-d87f6a0f41b6",
"metadata": {},
"source": [
"Is the point \"near\" (<6 units) the triangle?"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7a87b70f",
"metadata": {},
"outputs": [],
"source": [
"triangle.union(point.buffer(6))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e04daa60",
"metadata": {},
"outputs": [],
"source": [
"triangle.intersects(point.buffer(6))"
]
},
{
"cell_type": "markdown",
"id": "bf500303",
"metadata": {},
"source": [
"#### Extracting \"Europe\" data from \"naturalearth_lowres\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "410b08cf",
"metadata": {},
"outputs": [],
"source": [
"# Europe bounding box\n",
"eur_window = box(-10.67, 34.5, 31.55, 71.05)"
]
},
{
"cell_type": "markdown",
"id": "d45fdfcb-b244-4eff-a9d6-5cc4fefdfbd8",
"metadata": {},
"source": [
"Can we use `intersects` method?"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "24f4a32b-9ed4-468b-a194-ebe0395fcdb6",
"metadata": {},
"outputs": [],
"source": [
"gdf.intersects(eur_window)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ff455178-84da-45bf-b5b9-a71a7f9160f7",
"metadata": {},
"outputs": [],
"source": [
"# Incorrect v1\n",
"gdf[gdf.intersects(eur_window)].plot()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "03aa8da8-72f4-45fd-8931-e410d1204226",
"metadata": {},
"outputs": [],
"source": [
"# Incorrect v2\n",
"gdf[~gdf.intersects(eur_window)].plot()"
]
},
{
"cell_type": "markdown",
"id": "4173f3c5-017f-44ff-b713-158219fcf3e5",
"metadata": {},
"source": [
"Can we use `intersection` method?"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d7226fd9-3bb2-48c3-a5e9-5ce1ca0dd88f",
"metadata": {},
"outputs": [],
"source": [
"gdf.intersection(eur_window)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "108b8b8a",
"metadata": {},
"outputs": [],
"source": [
"gdf.intersection(eur_window).plot()"
]
},
{
"cell_type": "markdown",
"id": "f13e4e16-286a-4681-8bc5-9bd3fcf599a7",
"metadata": {},
"source": [
"How can we get rid of empty polygons (and remove the warning)?"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8c0dd051-a808-4fd4-b5ff-fee6ed580753",
"metadata": {},
"outputs": [],
"source": [
"eur = gdf.intersection(eur_window)\n",
"eur.is_empty"
]
},
{
"cell_type": "markdown",
"id": "acb1b886-f7c8-41d4-979a-2309c2b973bd",
"metadata": {},
"source": [
"Remove all the empty polygons using `is_empty`."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "55a76a00",
"metadata": {},
"outputs": [],
"source": [
"eur = eur[~eur.is_empty]\n",
"eur"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "08c59df7",
"metadata": {},
"outputs": [],
"source": [
"eur.plot()"
]
},
{
"cell_type": "markdown",
"id": "97a12d23",
"metadata": {},
"source": [
"#### Centroids of European countries"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "eb5c83b7",
"metadata": {},
"outputs": [],
"source": [
"# plot the centroids\n",
"ax = eur.plot(facecolor=\"lightgray\", edgecolor=\"k\")\n",
"eur.centroid.plot(ax=ax)"
]
},
{
"cell_type": "markdown",
"id": "dbfab5e7-5941-438a-baf2-e05369106004",
"metadata": {},
"source": [
"### Lat / Lon CRS\n",
"\n",
"- Lon is x-coord\n",
"- Lat is y-coord\n",
" - tells you where the point on Earth is\n",
"- **IMPORTANT**: degrees are not a unit of distance. 1 degree of longitute near the equator is a lot farther than moving 1 degree of longitute near the north pole\n",
"\n",
"Using `.crs` to access CRS of a gdf.\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "89251896",
"metadata": {},
"outputs": [],
"source": [
"eur.crs"
]
},
{
"cell_type": "markdown",
"id": "2a6ba447-6f1a-4d31-8c33-ec1eeb9e0deb",
"metadata": {},
"source": [
"#### Single CRS doesn't work for the whole earth\n",
"\n",
"- Setting a different CRS for Europe that is based on meters.\n",
"- https://spatialreference.org/ref/?search=europe"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "586038b1",
"metadata": {},
"outputs": [],
"source": [
"# Setting CRS to \"EPSG:3035\"\n",
"eur2 = eur.to_crs(\"EPSG:3035\")\n",
"eur2.crs"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d46c124c-9aff-4c2f-81fe-08885b606800",
"metadata": {},
"outputs": [],
"source": [
"ax = eur2.plot(facecolor=\"lightgray\", edgecolor=\"k\")\n",
"eur2.centroid.plot()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "045b9c33",
"metadata": {},
"outputs": [],
"source": [
"ax = eur2.plot(facecolor=\"lightgray\", edgecolor=\"k\")\n",
"eur2.centroid.plot(ax=ax)"
]
},
{
"cell_type": "markdown",
"id": "0634941f",
"metadata": {},
"source": [
"#### How much error does lat/long computation introduce?"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c0b72aff",
"metadata": {},
"outputs": [],
"source": [
"ax = eur2.plot(facecolor=\"lightgray\", edgecolor=\"k\")\n",
"eur2.centroid.plot(ax=ax, color=\"k\") # black => correct\n",
"eur.centroid.to_crs(\"EPSG:3035\").plot(ax=ax, color=\"r\") # red => miscalculated"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ca9e306e",
"metadata": {},
"outputs": [],
"source": [
"type(eur2.iloc[0])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f489c88d-5964-4358-b8c4-fc80d28b5491",
"metadata": {},
"outputs": [],
"source": [
"type(eur2).__mro__"
]
},
{
"cell_type": "markdown",
"id": "85880c73",
"metadata": {},
"source": [
"#### Area of European countries"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3e4874d9",
"metadata": {},
"outputs": [],
"source": [
"eur2.area # area in sq meters"
]
},
{
"cell_type": "markdown",
"id": "95f55824",
"metadata": {},
"source": [
"What is the area in **sq miles**?"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "85ee20c2",
"metadata": {},
"outputs": [],
"source": [
"# Conversion: / 1000 / 1000 / 2.59\n",
"(eur2.area / 1000 / 1000 / 2.59).sort_values(ascending=False)\n",
"# careful! some countries (e.g., Russia) were cropped when we did intersection"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cd600837",
"metadata": {},
"outputs": [],
"source": [
"# area on screen, not real area\n",
"eur.area"
]
},
{
"cell_type": "markdown",
"id": "daf1245f-9939-468d-9a5f-34225c2dbc51",
"metadata": {},
"source": [
"### CRS\n",
"\n",
"- `<GeoDataFrame object>.crs`: gives you information about current CRS.\n",
"- `<GeoDataFrame object>.to_crs(<TARGET CRS>)`: changes CRS to `<TARGET CRS>`."
]
},
{
"cell_type": "markdown",
"id": "9da3ee4c",
"metadata": {},
"source": [
"### Madison area emergency services\n",
"\n",
"- Data source: https://data-cityofmadison.opendata.arcgis.com/\n",
" - Search for:\n",
" - \"City limit\"\n",
" - \"Lakes and rivers\"\n",
" - \"Fire stations\"\n",
" - \"Police stations\"\n",
"\n",
"- CRS for Madison area: https://en.wikipedia.org/wiki/Universal_Transverse_Mercator_coordinate_system#/media/File:Universal_Transverse_Mercator_zones.svg"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a6f80847",
"metadata": {},
"outputs": [],
"source": [
"city = gpd.read_file(\"City_Limit.zip\").to_crs(\"epsg:32616\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7f8595be",
"metadata": {},
"outputs": [],
"source": [
"city.crs"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9ebd361f",
"metadata": {},
"outputs": [],
"source": [
"water = gpd.read_file(\"Lakes_and_Rivers.zip\").to_crs(city.crs)\n",
"fire = gpd.read_file(\"Fire_Stations.zip\").to_crs(city.crs)\n",
"police = gpd.read_file(\"Police_Stations.zip\").to_crs(city.crs)"
]
},
{
"cell_type": "markdown",
"id": "723e2bff-545f-4a8c-9f72-8a9906a99b1b",
"metadata": {},
"source": [
"#### Run this on your virtual machine\n",
"\n",
"`sudo sh -c \"echo 'Options = UnsafeLegacyRenegotiation' >> /etc/ssl/openssl.cnf\"`\n",
"\n",
"then restart notebook!"
]
},
{
"cell_type": "markdown",
"id": "b6069860-7dd9-43f2-970e-fd15980135ef",
"metadata": {},
"source": [
"#### GeoJSON\n",
"\n",
"How to find the below URL?\n",
"\n",
"- Go to info page of a dataset, for example: https://data-cityofmadison.opendata.arcgis.com/datasets/police-stations/explore?location=43.081769%2C-89.391550%2C12.81\n",
"- Then click on \"I want to use this\" > \"View API Resources\" > \"GeoJSON\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3a095d5e",
"metadata": {},
"outputs": [],
"source": [
"url = \"https://maps.cityofmadison.com/arcgis/rest/services/Public/OPEN_DATA/MapServer/2/query?outFields=*&where=1%3D1&f=geojson\"\n",
"police2 = gpd.read_file(url).to_crs(city.crs)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "248be81e",
"metadata": {},
"outputs": [],
"source": [
"ax = city.plot(color=\"lightgray\")\n",
"water.plot(color=\"lightblue\", ax=ax)\n",
"fire.plot(color=\"red\", ax=ax, marker=\"+\", label=\"Fire\")\n",
"police2.plot(color=\"blue\", ax=ax, label=\"Police\")\n",
"ax.legend(loc=\"upper left\", frameon=False)\n",
"ax.set_axis_off()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3a609d81",
"metadata": {},
"outputs": [],
"source": [
"fire.to_file(\"fire.geojson\")"
]
},
{
"cell_type": "markdown",
"id": "77600884",
"metadata": {},
"source": [
"### Geocoding: street address => lat / lon\n",
"\n",
"\n",
"- `gpd.tools.geocode(<street address>, provider=<geocoding service name>, user_agent=<user agent name>)`: converts street address into lat/long\n",
"\n",
"\n",
"#### Daily incident reports: https://www.cityofmadison.com/fire/daily-reports"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6b0b2aa0",
"metadata": {},
"outputs": [],
"source": [
"url = \"https://www.cityofmadison.com/fire/daily-reports\"\n",
"r = requests.get(url)\n",
"r"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bee28b41",
"metadata": {},
"outputs": [],
"source": [
"r.raise_for_status() # give me an exception if not 200 (e.g., 404)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0bae00e7",
"metadata": {},
"outputs": [],
"source": [
"# doesn't work\n",
"# pd.read_html(url)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "47173ec2",
"metadata": {},
"outputs": [],
"source": [
"# print(r.text)"
]
},
{
"cell_type": "markdown",
"id": "39f166c5",
"metadata": {},
"source": [
"Find all **span** tags with **streetAddress** using regex."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ac7b9482-5512-49e4-b0b8-7f9ed34b5844",
"metadata": {},
"outputs": [],
"source": [
"# <p>1700 block Thierer Road<br>\n",
"# addrs = re.findall(r'<p>1700 block Thierer Road<br>', r.text)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8e9b49d2-3e0d-4a39-9dcf-13b288a165e7",
"metadata": {},
"outputs": [],
"source": [
"addrs = re.findall(r'', r.text)\n",