File size: 28,901 Bytes
62dd0f3
 
99b822c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cc66d4e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62dd0f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<iframe srcdoc=\"&lt;!DOCTYPE html&gt;\n",
       "&lt;html&gt;\n",
       "&lt;head&gt;\n",
       "    \n",
       "    &lt;meta http-equiv=&quot;content-type&quot; content=&quot;text/html; charset=UTF-8&quot; /&gt;\n",
       "    \n",
       "        &lt;script&gt;\n",
       "            L_NO_TOUCH = false;\n",
       "            L_DISABLE_3D = false;\n",
       "        &lt;/script&gt;\n",
       "    \n",
       "    &lt;style&gt;html, body {width: 100%;height: 100%;margin: 0;padding: 0;}&lt;/style&gt;\n",
       "    &lt;style&gt;#map {position:absolute;top:0;bottom:0;right:0;left:0;}&lt;/style&gt;\n",
       "    &lt;script src=&quot;https://cdn.jsdelivr.net/npm/leaflet@1.9.3/dist/leaflet.js&quot;&gt;&lt;/script&gt;\n",
       "    &lt;script src=&quot;https://code.jquery.com/jquery-3.7.1.min.js&quot;&gt;&lt;/script&gt;\n",
       "    &lt;script src=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/js/bootstrap.bundle.min.js&quot;&gt;&lt;/script&gt;\n",
       "    &lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/Leaflet.awesome-markers/2.0.2/leaflet.awesome-markers.js&quot;&gt;&lt;/script&gt;\n",
       "    &lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.jsdelivr.net/npm/leaflet@1.9.3/dist/leaflet.css&quot;/&gt;\n",
       "    &lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/css/bootstrap.min.css&quot;/&gt;\n",
       "    &lt;link rel=&quot;stylesheet&quot; href=&quot;https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-glyphicons.css&quot;/&gt;\n",
       "    &lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6.2.0/css/all.min.css&quot;/&gt;\n",
       "    &lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdnjs.cloudflare.com/ajax/libs/Leaflet.awesome-markers/2.0.2/leaflet.awesome-markers.css&quot;/&gt;\n",
       "    &lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.jsdelivr.net/gh/python-visualization/folium/folium/templates/leaflet.awesome.rotate.min.css&quot;/&gt;\n",
       "    \n",
       "            &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width,\n",
       "                initial-scale=1.0, maximum-scale=1.0, user-scalable=no&quot; /&gt;\n",
       "            &lt;style&gt;\n",
       "                #map_f1ce6eb921b4d7b11d49de3df8093d9e {\n",
       "                    position: relative;\n",
       "                    width: 100.0%;\n",
       "                    height: 100.0%;\n",
       "                    left: 0.0%;\n",
       "                    top: 0.0%;\n",
       "                }\n",
       "                .leaflet-container { font-size: 1rem; }\n",
       "            &lt;/style&gt;\n",
       "        \n",
       "    &lt;script src=&quot;https://cdn.jsdelivr.net/npm/leaflet.fullscreen@3.0.0/Control.FullScreen.min.js&quot;&gt;&lt;/script&gt;\n",
       "    &lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.jsdelivr.net/npm/leaflet.fullscreen@3.0.0/Control.FullScreen.css&quot;/&gt;\n",
       "    &lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/leaflet.draw/1.0.2/leaflet.draw.js&quot;&gt;&lt;/script&gt;\n",
       "    &lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdnjs.cloudflare.com/ajax/libs/leaflet.draw/1.0.2/leaflet.draw.css&quot;/&gt;\n",
       "    &lt;script src=&quot;https://cdn.jsdelivr.net/gh/ljagis/leaflet-measure@2.1.7/dist/leaflet-measure.min.js&quot;&gt;&lt;/script&gt;\n",
       "    &lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.jsdelivr.net/gh/ljagis/leaflet-measure@2.1.7/dist/leaflet-measure.min.css&quot;/&gt;\n",
       "    &lt;script src=&quot;https://unpkg.com/leaflet-control-geocoder/dist/Control.Geocoder.js&quot;&gt;&lt;/script&gt;\n",
       "    &lt;link rel=&quot;stylesheet&quot; href=&quot;https://unpkg.com/leaflet-control-geocoder/dist/Control.Geocoder.css&quot;/&gt;\n",
       "&lt;/head&gt;\n",
       "&lt;body&gt;\n",
       "    \n",
       "    \n",
       "            &lt;div class=&quot;folium-map&quot; id=&quot;map_f1ce6eb921b4d7b11d49de3df8093d9e&quot; &gt;&lt;/div&gt;\n",
       "        \n",
       "&lt;/body&gt;\n",
       "&lt;script&gt;\n",
       "    \n",
       "    \n",
       "            var map_f1ce6eb921b4d7b11d49de3df8093d9e = L.map(\n",
       "                &quot;map_f1ce6eb921b4d7b11d49de3df8093d9e&quot;,\n",
       "                {\n",
       "                    center: [20.0, 0.0],\n",
       "                    crs: L.CRS.EPSG3857,\n",
       "                    zoom: 2,\n",
       "                    zoomControl: true,\n",
       "                    preferCanvas: false,\n",
       "                    drawExport: false,\n",
       "                    layersControl: true,\n",
       "                }\n",
       "            );\n",
       "            L.control.scale().addTo(map_f1ce6eb921b4d7b11d49de3df8093d9e);\n",
       "\n",
       "            \n",
       "\n",
       "        \n",
       "    \n",
       "            var tile_layer_625eed014e405c2ee851b9417f647dec = L.tileLayer(\n",
       "                &quot;https://tile.openstreetmap.org/{z}/{x}/{y}.png&quot;,\n",
       "                {&quot;attribution&quot;: &quot;\\u0026copy; \\u003ca href=\\&quot;https://www.openstreetmap.org/copyright\\&quot;\\u003eOpenStreetMap\\u003c/a\\u003e contributors&quot;, &quot;detectRetina&quot;: false, &quot;maxNativeZoom&quot;: 19, &quot;maxZoom&quot;: 24, &quot;minZoom&quot;: 0, &quot;noWrap&quot;: false, &quot;opacity&quot;: 1, &quot;subdomains&quot;: &quot;abc&quot;, &quot;tms&quot;: false}\n",
       "            );\n",
       "        \n",
       "    \n",
       "            tile_layer_625eed014e405c2ee851b9417f647dec.addTo(map_f1ce6eb921b4d7b11d49de3df8093d9e);\n",
       "        \n",
       "    \n",
       "            L.control.fullscreen(\n",
       "                {&quot;forceSeparateButton&quot;: false, &quot;position&quot;: &quot;topleft&quot;, &quot;title&quot;: &quot;Full Screen&quot;, &quot;titleCancel&quot;: &quot;Exit Full Screen&quot;}\n",
       "            ).addTo(map_f1ce6eb921b4d7b11d49de3df8093d9e);\n",
       "        \n",
       "    \n",
       "            var options = {\n",
       "              position: &quot;topleft&quot;,\n",
       "              draw: {},\n",
       "              edit: {},\n",
       "            }\n",
       "            // FeatureGroup is to store editable layers.\n",
       "            var drawnItems_draw_control_9dd44c4d87fa4af03ae29411df47a757 = new L.featureGroup().addTo(\n",
       "                map_f1ce6eb921b4d7b11d49de3df8093d9e\n",
       "            );\n",
       "            options.edit.featureGroup = drawnItems_draw_control_9dd44c4d87fa4af03ae29411df47a757;\n",
       "            var draw_control_9dd44c4d87fa4af03ae29411df47a757 = new L.Control.Draw(\n",
       "                options\n",
       "            ).addTo( map_f1ce6eb921b4d7b11d49de3df8093d9e );\n",
       "            map_f1ce6eb921b4d7b11d49de3df8093d9e.on(L.Draw.Event.CREATED, function(e) {\n",
       "                var layer = e.layer,\n",
       "                    type = e.layerType;\n",
       "                var coords = JSON.stringify(layer.toGeoJSON());\n",
       "                layer.on(&#x27;click&#x27;, function() {\n",
       "                    alert(coords);\n",
       "                    console.log(coords);\n",
       "                });\n",
       "                drawnItems_draw_control_9dd44c4d87fa4af03ae29411df47a757.addLayer(layer);\n",
       "             });\n",
       "            map_f1ce6eb921b4d7b11d49de3df8093d9e.on(&#x27;draw:created&#x27;, function(e) {\n",
       "                drawnItems_draw_control_9dd44c4d87fa4af03ae29411df47a757.addLayer(e.layer);\n",
       "            });\n",
       "            \n",
       "        \n",
       "    \n",
       "            var measure_control_cd8157b3b01df939f478306bae2624de = new L.Control.Measure(\n",
       "                {&quot;position&quot;: &quot;bottomleft&quot;, &quot;primaryAreaUnit&quot;: &quot;sqmeters&quot;, &quot;primaryLengthUnit&quot;: &quot;meters&quot;, &quot;secondaryAreaUnit&quot;: &quot;acres&quot;, &quot;secondaryLengthUnit&quot;: &quot;miles&quot;});\n",
       "            map_f1ce6eb921b4d7b11d49de3df8093d9e.addControl(measure_control_cd8157b3b01df939f478306bae2624de);\n",
       "\n",
       "            // Workaround for using this plugin with Leaflet&gt;=1.8.0\n",
       "            // https://github.com/ljagis/leaflet-measure/issues/171\n",
       "            L.Control.Measure.include({\n",
       "                _setCaptureMarkerIcon: function () {\n",
       "                    // disable autopan\n",
       "                    this._captureMarker.options.autoPanOnFocus = false;\n",
       "                    // default function\n",
       "                    this._captureMarker.setIcon(\n",
       "                        L.divIcon({\n",
       "                            iconSize: this._map.getSize().multiplyBy(2)\n",
       "                        })\n",
       "                    );\n",
       "                },\n",
       "            });\n",
       "\n",
       "        \n",
       "    \n",
       "\n",
       "            var geocoderOpts_geocoder_c3c41f24bdc9ab7c647dd5f6e71755d2 = {&quot;collapsed&quot;: true, &quot;defaultMarkGeocode&quot;: true, &quot;position&quot;: &quot;topleft&quot;, &quot;provider&quot;: &quot;nominatim&quot;, &quot;providerOptions&quot;: {}, &quot;zoom&quot;: 11};\n",
       "\n",
       "            // note: geocoder name should start with lowercase\n",
       "            var geocoderName_geocoder_c3c41f24bdc9ab7c647dd5f6e71755d2 = geocoderOpts_geocoder_c3c41f24bdc9ab7c647dd5f6e71755d2[&quot;provider&quot;];\n",
       "\n",
       "            var customGeocoder_geocoder_c3c41f24bdc9ab7c647dd5f6e71755d2 = L.Control.Geocoder[ geocoderName_geocoder_c3c41f24bdc9ab7c647dd5f6e71755d2 ](\n",
       "                geocoderOpts_geocoder_c3c41f24bdc9ab7c647dd5f6e71755d2[&#x27;providerOptions&#x27;]\n",
       "            );\n",
       "            geocoderOpts_geocoder_c3c41f24bdc9ab7c647dd5f6e71755d2[&quot;geocoder&quot;] = customGeocoder_geocoder_c3c41f24bdc9ab7c647dd5f6e71755d2;\n",
       "\n",
       "            L.Control.geocoder(\n",
       "                geocoderOpts_geocoder_c3c41f24bdc9ab7c647dd5f6e71755d2\n",
       "            ).on(&#x27;markgeocode&#x27;, function(e) {\n",
       "                var zoom = geocoderOpts_geocoder_c3c41f24bdc9ab7c647dd5f6e71755d2[&#x27;zoom&#x27;] || map_f1ce6eb921b4d7b11d49de3df8093d9e.getZoom();\n",
       "                map_f1ce6eb921b4d7b11d49de3df8093d9e.setView(e.geocode.center, zoom);\n",
       "            }).addTo(map_f1ce6eb921b4d7b11d49de3df8093d9e);\n",
       "\n",
       "        \n",
       "    \n",
       "            map_f1ce6eb921b4d7b11d49de3df8093d9e.fitBounds(\n",
       "                [[20, 0], [20, 0]],\n",
       "                {&quot;maxZoom&quot;: 2}\n",
       "            );\n",
       "        \n",
       "    \n",
       "            var macro_element_3831fc723f2cfc11a7f3230a34b4b9f6 = L.tileLayer.wms(\n",
       "                &quot;https://www.mrlc.gov/geoserver/mrlc_display/NLCD_2016_Land_Cover_L48/wms?&quot;,\n",
       "                {&quot;attribution&quot;: &quot;&quot;, &quot;format&quot;: &quot;image/png&quot;, &quot;layers&quot;: &quot;NLCD_2016_Land_Cover_L48&quot;, &quot;styles&quot;: &quot;&quot;, &quot;transparent&quot;: true, &quot;version&quot;: &quot;1.1.1&quot;}\n",
       "            );\n",
       "        \n",
       "    \n",
       "            macro_element_3831fc723f2cfc11a7f3230a34b4b9f6.addTo(map_f1ce6eb921b4d7b11d49de3df8093d9e);\n",
       "        \n",
       "    \n",
       "            var layer_control_293101c4cdf231dcd55e9b691be121de_layers = {\n",
       "                base_layers : {\n",
       "                    &quot;openstreetmap&quot; : tile_layer_625eed014e405c2ee851b9417f647dec,\n",
       "                },\n",
       "                overlays :  {\n",
       "                    &quot;NLCD 2016 CONUS Land Cover&quot; : macro_element_3831fc723f2cfc11a7f3230a34b4b9f6,\n",
       "                },\n",
       "            };\n",
       "            let layer_control_293101c4cdf231dcd55e9b691be121de = L.control.layers(\n",
       "                layer_control_293101c4cdf231dcd55e9b691be121de_layers.base_layers,\n",
       "                layer_control_293101c4cdf231dcd55e9b691be121de_layers.overlays,\n",
       "                {&quot;autoZIndex&quot;: true, &quot;collapsed&quot;: true, &quot;position&quot;: &quot;topright&quot;}\n",
       "            ).addTo(map_f1ce6eb921b4d7b11d49de3df8093d9e);\n",
       "\n",
       "        \n",
       "&lt;/script&gt;\n",
       "&lt;/html&gt;\" width=\"100%\" height=\"600\"style=\"border:none !important;\" \"allowfullscreen\" \"webkitallowfullscreen\" \"mozallowfullscreen\"></iframe>"
      ],
      "text/plain": [
       "<leafmap.foliumap.Map at 0x7f2341d2cca0>"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import leafmap as leafmap\n",
    "import leafmap.foliumap as leaf_folium\n",
    "import leafmap.toolbar\n",
    "# import leafmap.toolbar as toolbar\n",
    "\n",
    "tmp_map = leafmap.Map()\n",
    "\n",
    "Map = leaf_folium.Map()\n",
    "\n",
    "url = \"https://www.mrlc.gov/geoserver/mrlc_display/NLCD_2016_Land_Cover_L48/wms?\"\n",
    "Map.add_wms_layer(\n",
    "    url,\n",
    "    layers=\"NLCD_2016_Land_Cover_L48\",\n",
    "    name=\"NLCD 2016 CONUS Land Cover\",\n",
    "    format=\"image/png\",\n",
    "    transparent=True,\n",
    ")\n",
    "# Map.add_legend(builtin_legend=\"NLCD\", draggable=False)\n",
    "Map.sandbox_path = \"/home/patel_zeel/bhuvan\"\n",
    "# toolbar.tool_template(Map)\n",
    "\n",
    "Map.toolbar = tmp_map.toolbar\n",
    "Map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from io import BytesIO\n",
    "import requests\n",
    "\n",
    "file_url = \"https://drive.google.com/file/d/1fOujaaj_hJBMJIT-EsFzg6_6I-eFCgVs/view?usp=drivesdk\"\n",
    "if isinstance(file_url, str):\n",
    "    if file_url.startswith(\"https://drive.google.com/file/d/\"):\n",
    "        ID = file_url.replace(\"https://drive.google.com/file/d/\", \"\").split(\"/\")[0]\n",
    "        file_url = f\"https://drive.google.com/uc?id={ID}\"\n",
    "    elif file_url.startswith(\"https://drive.google.com/open?id=\"):\n",
    "        ID = file_url.replace(\"https://drive.google.com/open?id=\", \"\")\n",
    "        file_url = f\"https://drive.google.com/uc?id={ID}\"\n",
    "\n",
    "    response = requests.get(file_url)\n",
    "    bytes_data = BytesIO(response.content)\n",
    "    string_data = response.text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import geopandas as gpd\n",
    "import kml2geojson\n",
    "if string_data.startswith(\"<?xml\"):\n",
    "    geojson = kml2geojson.convert(bytes_data)\n",
    "    features = geojson[0][\"features\"]\n",
    "    epsg = 4326\n",
    "    input_gdf = gpd.GeoDataFrame.from_features(features, crs=f\"EPSG:{epsg}\")\n",
    "else:\n",
    "    input_gdf = gpd.read_file(bytes_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>geometry</th>\n",
       "      <th>name</th>\n",
       "      <th>styleUrl</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>POINT Z (73.90178 20.91581 0)</td>\n",
       "      <td>1</td>\n",
       "      <td>#m_ylw-pushpin</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>POINT Z (73.89839 20.91803 0)</td>\n",
       "      <td>3</td>\n",
       "      <td>#m_ylw-pushpin</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>POINT Z (73.89878 20.91578 0)</td>\n",
       "      <td>2</td>\n",
       "      <td>#m_ylw-pushpin</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>POINT Z (73.90075 20.91961 0)</td>\n",
       "      <td>4</td>\n",
       "      <td>#m_ylw-pushpin</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>POLYGON Z ((73.89878 20.91579 0, 73.90179 20.9...</td>\n",
       "      <td>Polygon Measure</td>\n",
       "      <td>#inline1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                            geometry             name  \\\n",
       "0                      POINT Z (73.90178 20.91581 0)                1   \n",
       "1                      POINT Z (73.89839 20.91803 0)                3   \n",
       "2                      POINT Z (73.89878 20.91578 0)                2   \n",
       "3                      POINT Z (73.90075 20.91961 0)                4   \n",
       "4  POLYGON Z ((73.89878 20.91579 0, 73.90179 20.9...  Polygon Measure   \n",
       "\n",
       "         styleUrl  \n",
       "0  #m_ylw-pushpin  \n",
       "1  #m_ylw-pushpin  \n",
       "2  #m_ylw-pushpin  \n",
       "3  #m_ylw-pushpin  \n",
       "4        #inline1  "
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "input_gdf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "from shapely.ops import transform\n",
    "\n",
    "def shape_3d_to_2d(shape):\n",
    "    if shape.has_z:\n",
    "        return transform(lambda x, y, z: (x, y), shape)\n",
    "    else:\n",
    "        return shape\n",
    "\n",
    "def preprocess_gdf(gdf):\n",
    "    gdf = gdf.to_crs(epsg=7761)  # epsg for Gujarat\n",
    "    gdf[\"geometry\"] = gdf[\"geometry\"].apply(shape_3d_to_2d)\n",
    "    gdf[\"geometry\"] = gdf.buffer(0)  # Fixes some invalid geometries\n",
    "    return gdf\n",
    "input_gdf = preprocess_gdf(input_gdf)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>geometry</th>\n",
       "      <th>name</th>\n",
       "      <th>styleUrl</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>POLYGON EMPTY</td>\n",
       "      <td>1</td>\n",
       "      <td>#m_ylw-pushpin</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>POLYGON EMPTY</td>\n",
       "      <td>3</td>\n",
       "      <td>#m_ylw-pushpin</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>POLYGON EMPTY</td>\n",
       "      <td>2</td>\n",
       "      <td>#m_ylw-pushpin</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>POLYGON EMPTY</td>\n",
       "      <td>4</td>\n",
       "      <td>#m_ylw-pushpin</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>POLYGON ((1262518.164 840333.056, 1262472.46 8...</td>\n",
       "      <td>Polygon Measure</td>\n",
       "      <td>#inline1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                            geometry             name  \\\n",
       "0                                      POLYGON EMPTY                1   \n",
       "1                                      POLYGON EMPTY                3   \n",
       "2                                      POLYGON EMPTY                2   \n",
       "3                                      POLYGON EMPTY                4   \n",
       "4  POLYGON ((1262518.164 840333.056, 1262472.46 8...  Polygon Measure   \n",
       "\n",
       "         styleUrl  \n",
       "0  #m_ylw-pushpin  \n",
       "1  #m_ylw-pushpin  \n",
       "2  #m_ylw-pushpin  \n",
       "3  #m_ylw-pushpin  \n",
       "4        #inline1  "
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "input_gdf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_850691/2543908025.py:3: ShapelyDeprecationWarning: The 'type' attribute is deprecated, and will be removed in the future. You can use the 'geom_type' attribute instead.\n",
      "  return (geometry.type == \"Polygon\") and (not geometry.is_empty)\n",
      "/tmp/ipykernel_850691/2543908025.py:3: ShapelyDeprecationWarning: The 'type' attribute is deprecated, and will be removed in the future. You can use the 'geom_type' attribute instead.\n",
      "  return (geometry.type == \"Polygon\") and (not geometry.is_empty)\n",
      "/tmp/ipykernel_850691/2543908025.py:3: ShapelyDeprecationWarning: The 'type' attribute is deprecated, and will be removed in the future. You can use the 'geom_type' attribute instead.\n",
      "  return (geometry.type == \"Polygon\") and (not geometry.is_empty)\n",
      "/tmp/ipykernel_850691/2543908025.py:3: ShapelyDeprecationWarning: The 'type' attribute is deprecated, and will be removed in the future. You can use the 'geom_type' attribute instead.\n",
      "  return (geometry.type == \"Polygon\") and (not geometry.is_empty)\n",
      "/tmp/ipykernel_850691/2543908025.py:3: ShapelyDeprecationWarning: The 'type' attribute is deprecated, and will be removed in the future. You can use the 'geom_type' attribute instead.\n",
      "  return (geometry.type == \"Polygon\") and (not geometry.is_empty)\n"
     ]
    }
   ],
   "source": [
    "def is_valid_polygon(geometry_gdf):\n",
    "    geometry = geometry_gdf.geometry.item()\n",
    "    return (geometry.type == \"Polygon\") and (not geometry.is_empty)\n",
    "\n",
    "for i in range(len(input_gdf)):\n",
    "    geometry_gdf = input_gdf[input_gdf.index == i]\n",
    "    if is_valid_polygon(geometry_gdf):\n",
    "        break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>geometry</th>\n",
       "      <th>name</th>\n",
       "      <th>styleUrl</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>POLYGON ((1262518.164 840333.056, 1262472.46 8...</td>\n",
       "      <td>Polygon Measure</td>\n",
       "      <td>#inline1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                            geometry             name  \\\n",
       "4  POLYGON ((1262518.164 840333.056, 1262472.46 8...  Polygon Measure   \n",
       "\n",
       "   styleUrl  \n",
       "4  #inline1  "
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "geometry_gdf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import geopandas as gpd\n",
    "from tqdm.notebook import tqdm\n",
    "from joblib import Parallel, delayed"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "e90c443ceec74b3e9bf223587db76898",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/15 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "https://drive.google.com/open?id=1skBV8miaZN3gE2lPVnHQQG-n-RwbMH2d has 5 geometries\n",
      "https://drive.google.com/open?id=12OJzdi0RE5BDdMHzBovnYK_QhC5jIiSy has 6 geometries\n",
      "https://drive.google.com/open?id=1lge4nZKw7gxuDbQtM_v7D8eBHzUsLoBI has 3 geometries\n"
     ]
    }
   ],
   "source": [
    "files = \"\"\"https://drive.google.com/open?id=1QHbcxHRw3EQWsw5-UN6AUnVI-bhmhCQ5\n",
    "https://drive.google.com/open?id=1skBV8miaZN3gE2lPVnHQQG-n-RwbMH2d\n",
    "https://drive.google.com/open?id=1jyG_D6UudyAgicZjsvEfNLT_vNK1G3EB\n",
    "https://drive.google.com/open?id=1NxO399MvzufeT6-xigq3qi79psA40rkQ\n",
    "https://drive.google.com/open?id=1P2ZPMsxX82P12m460EBjpYMrdXUkZ05l\n",
    "https://drive.google.com/open?id=1Pc4sIYbRcu761fuXV5HVDG3EaqQC-uiz\n",
    "https://drive.google.com/open?id=1xskOyh888HrxCrB0Ue9bNGG6r1CvlH3J\n",
    "https://drive.google.com/open?id=12OJzdi0RE5BDdMHzBovnYK_QhC5jIiSy\n",
    "https://drive.google.com/open?id=18bDqdNIZOlp97GHpEOaYsCLSTML--aEA\n",
    "https://drive.google.com/open?id=1lge4nZKw7gxuDbQtM_v7D8eBHzUsLoBI\n",
    "https://drive.google.com/open?id=1XzzPh1q0uU3eMPoSC5stCLC7r8oMtgok\n",
    "https://drive.google.com/open?id=1JGmGCVYjdcUeFVmcoa7LcJE36HH0MGoF\n",
    "https://drive.google.com/open?id=1EPp6DZUGFcUESQSeMjQ9famygnWM-3BM\n",
    "https://drive.google.com/open?id=1XY7HbmY0fakAaZaxvUFF5UYk7Qovnit2\n",
    "https://drive.google.com/open?id=1VSVOpw9TFAxPKClHGPFXI_anBSHZOIi-\"\"\"\n",
    "\n",
    "df = pd.DataFrame(columns=['Failed URL', '# of geometries'])\n",
    "i = 0\n",
    "def get_gdf(link):\n",
    "    link = link.strip()\n",
    "    original_link = link\n",
    "    if link.startswith('https://drive.google.com/open?id='):\n",
    "        link = 'https://drive.google.com/uc?id=' + link.split('=')[-1]\n",
    "    gdf = gpd.read_file(link)\n",
    "    if len(gdf) != 1:\n",
    "        print(f\"{original_link} has {len(gdf)} geometries\")\n",
    "        # df.loc[i, ['Failed URL', '# of geometries']] = [original_link, len(gdf)]\n",
    "        # i += 1\n",
    "        \n",
    "# df.to_csv('failed_links.csv', index=False)\n",
    "_ = Parallel(n_jobs=32)(delayed(get_gdf)(link) for link in tqdm(files.split('\\n')))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "zeel_py310",
   "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.10.15"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}