Zeel commited on
Commit
cc66d4e
·
1 Parent(s): 7be40d9

remove duplicate code

Browse files
Files changed (2) hide show
  1. app.py +7 -7
  2. sandbox.ipynb +336 -0
app.py CHANGED
@@ -127,17 +127,17 @@ with st.expander("Advanced Controls", expanded=False):
127
  )
128
  height = st.number_input("Map height (px)", 1, 10000, 600, 1)
129
 
130
- geometry_gdf = input_gdf[input_gdf.index == 0]
131
 
132
 
133
- def check_valid_geometry(geometry_gdf):
134
- geometry = geometry_gdf.geometry.item()
135
- if geometry.type != "Polygon":
136
- st.error(f"Selected geometry is of type '{geometry.type}'. Please provide a 'Polygon' geometry.")
137
- st.stop()
138
 
139
 
140
- check_valid_geometry(geometry_gdf)
141
 
142
  m = leafmap.Map()
143
 
 
127
  )
128
  height = st.number_input("Map height (px)", 1, 10000, 600, 1)
129
 
130
+ # geometry_gdf = input_gdf[input_gdf.index == 0]
131
 
132
 
133
+ # def check_valid_geometry(geometry_gdf):
134
+ # geometry = geometry_gdf.geometry.item()
135
+ # if geometry.type != "Polygon":
136
+ # st.error(f"Selected geometry is of type '{geometry.type}'. Please provide a 'Polygon' geometry.")
137
+ # st.stop()
138
 
139
 
140
+ # check_valid_geometry(geometry_gdf)
141
 
142
  m = leafmap.Map()
143
 
sandbox.ipynb CHANGED
@@ -233,6 +233,342 @@
233
  "Map"
234
  ]
235
  },
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
236
  {
237
  "cell_type": "code",
238
  "execution_count": 10,
 
233
  "Map"
234
  ]
235
  },
236
+ {
237
+ "cell_type": "code",
238
+ "execution_count": 1,
239
+ "metadata": {},
240
+ "outputs": [],
241
+ "source": [
242
+ "from io import BytesIO\n",
243
+ "import requests\n",
244
+ "\n",
245
+ "file_url = \"https://drive.google.com/file/d/1fOujaaj_hJBMJIT-EsFzg6_6I-eFCgVs/view?usp=drivesdk\"\n",
246
+ "if isinstance(file_url, str):\n",
247
+ " if file_url.startswith(\"https://drive.google.com/file/d/\"):\n",
248
+ " ID = file_url.replace(\"https://drive.google.com/file/d/\", \"\").split(\"/\")[0]\n",
249
+ " file_url = f\"https://drive.google.com/uc?id={ID}\"\n",
250
+ " elif file_url.startswith(\"https://drive.google.com/open?id=\"):\n",
251
+ " ID = file_url.replace(\"https://drive.google.com/open?id=\", \"\")\n",
252
+ " file_url = f\"https://drive.google.com/uc?id={ID}\"\n",
253
+ "\n",
254
+ " response = requests.get(file_url)\n",
255
+ " bytes_data = BytesIO(response.content)\n",
256
+ " string_data = response.text"
257
+ ]
258
+ },
259
+ {
260
+ "cell_type": "code",
261
+ "execution_count": 2,
262
+ "metadata": {},
263
+ "outputs": [],
264
+ "source": [
265
+ "import geopandas as gpd\n",
266
+ "import kml2geojson\n",
267
+ "if string_data.startswith(\"<?xml\"):\n",
268
+ " geojson = kml2geojson.convert(bytes_data)\n",
269
+ " features = geojson[0][\"features\"]\n",
270
+ " epsg = 4326\n",
271
+ " input_gdf = gpd.GeoDataFrame.from_features(features, crs=f\"EPSG:{epsg}\")\n",
272
+ "else:\n",
273
+ " input_gdf = gpd.read_file(bytes_data)"
274
+ ]
275
+ },
276
+ {
277
+ "cell_type": "code",
278
+ "execution_count": 3,
279
+ "metadata": {},
280
+ "outputs": [
281
+ {
282
+ "data": {
283
+ "text/html": [
284
+ "<div>\n",
285
+ "<style scoped>\n",
286
+ " .dataframe tbody tr th:only-of-type {\n",
287
+ " vertical-align: middle;\n",
288
+ " }\n",
289
+ "\n",
290
+ " .dataframe tbody tr th {\n",
291
+ " vertical-align: top;\n",
292
+ " }\n",
293
+ "\n",
294
+ " .dataframe thead th {\n",
295
+ " text-align: right;\n",
296
+ " }\n",
297
+ "</style>\n",
298
+ "<table border=\"1\" class=\"dataframe\">\n",
299
+ " <thead>\n",
300
+ " <tr style=\"text-align: right;\">\n",
301
+ " <th></th>\n",
302
+ " <th>geometry</th>\n",
303
+ " <th>name</th>\n",
304
+ " <th>styleUrl</th>\n",
305
+ " </tr>\n",
306
+ " </thead>\n",
307
+ " <tbody>\n",
308
+ " <tr>\n",
309
+ " <th>0</th>\n",
310
+ " <td>POINT Z (73.90178 20.91581 0)</td>\n",
311
+ " <td>1</td>\n",
312
+ " <td>#m_ylw-pushpin</td>\n",
313
+ " </tr>\n",
314
+ " <tr>\n",
315
+ " <th>1</th>\n",
316
+ " <td>POINT Z (73.89839 20.91803 0)</td>\n",
317
+ " <td>3</td>\n",
318
+ " <td>#m_ylw-pushpin</td>\n",
319
+ " </tr>\n",
320
+ " <tr>\n",
321
+ " <th>2</th>\n",
322
+ " <td>POINT Z (73.89878 20.91578 0)</td>\n",
323
+ " <td>2</td>\n",
324
+ " <td>#m_ylw-pushpin</td>\n",
325
+ " </tr>\n",
326
+ " <tr>\n",
327
+ " <th>3</th>\n",
328
+ " <td>POINT Z (73.90075 20.91961 0)</td>\n",
329
+ " <td>4</td>\n",
330
+ " <td>#m_ylw-pushpin</td>\n",
331
+ " </tr>\n",
332
+ " <tr>\n",
333
+ " <th>4</th>\n",
334
+ " <td>POLYGON Z ((73.89878 20.91579 0, 73.90179 20.9...</td>\n",
335
+ " <td>Polygon Measure</td>\n",
336
+ " <td>#inline1</td>\n",
337
+ " </tr>\n",
338
+ " </tbody>\n",
339
+ "</table>\n",
340
+ "</div>"
341
+ ],
342
+ "text/plain": [
343
+ " geometry name \\\n",
344
+ "0 POINT Z (73.90178 20.91581 0) 1 \n",
345
+ "1 POINT Z (73.89839 20.91803 0) 3 \n",
346
+ "2 POINT Z (73.89878 20.91578 0) 2 \n",
347
+ "3 POINT Z (73.90075 20.91961 0) 4 \n",
348
+ "4 POLYGON Z ((73.89878 20.91579 0, 73.90179 20.9... Polygon Measure \n",
349
+ "\n",
350
+ " styleUrl \n",
351
+ "0 #m_ylw-pushpin \n",
352
+ "1 #m_ylw-pushpin \n",
353
+ "2 #m_ylw-pushpin \n",
354
+ "3 #m_ylw-pushpin \n",
355
+ "4 #inline1 "
356
+ ]
357
+ },
358
+ "execution_count": 3,
359
+ "metadata": {},
360
+ "output_type": "execute_result"
361
+ }
362
+ ],
363
+ "source": [
364
+ "input_gdf"
365
+ ]
366
+ },
367
+ {
368
+ "cell_type": "code",
369
+ "execution_count": 4,
370
+ "metadata": {},
371
+ "outputs": [],
372
+ "source": [
373
+ "from shapely.ops import transform\n",
374
+ "\n",
375
+ "def shape_3d_to_2d(shape):\n",
376
+ " if shape.has_z:\n",
377
+ " return transform(lambda x, y, z: (x, y), shape)\n",
378
+ " else:\n",
379
+ " return shape\n",
380
+ "\n",
381
+ "def preprocess_gdf(gdf):\n",
382
+ " gdf = gdf.to_crs(epsg=7761) # epsg for Gujarat\n",
383
+ " gdf[\"geometry\"] = gdf[\"geometry\"].apply(shape_3d_to_2d)\n",
384
+ " gdf[\"geometry\"] = gdf.buffer(0) # Fixes some invalid geometries\n",
385
+ " return gdf\n",
386
+ "input_gdf = preprocess_gdf(input_gdf)"
387
+ ]
388
+ },
389
+ {
390
+ "cell_type": "code",
391
+ "execution_count": 5,
392
+ "metadata": {},
393
+ "outputs": [
394
+ {
395
+ "data": {
396
+ "text/html": [
397
+ "<div>\n",
398
+ "<style scoped>\n",
399
+ " .dataframe tbody tr th:only-of-type {\n",
400
+ " vertical-align: middle;\n",
401
+ " }\n",
402
+ "\n",
403
+ " .dataframe tbody tr th {\n",
404
+ " vertical-align: top;\n",
405
+ " }\n",
406
+ "\n",
407
+ " .dataframe thead th {\n",
408
+ " text-align: right;\n",
409
+ " }\n",
410
+ "</style>\n",
411
+ "<table border=\"1\" class=\"dataframe\">\n",
412
+ " <thead>\n",
413
+ " <tr style=\"text-align: right;\">\n",
414
+ " <th></th>\n",
415
+ " <th>geometry</th>\n",
416
+ " <th>name</th>\n",
417
+ " <th>styleUrl</th>\n",
418
+ " </tr>\n",
419
+ " </thead>\n",
420
+ " <tbody>\n",
421
+ " <tr>\n",
422
+ " <th>0</th>\n",
423
+ " <td>POLYGON EMPTY</td>\n",
424
+ " <td>1</td>\n",
425
+ " <td>#m_ylw-pushpin</td>\n",
426
+ " </tr>\n",
427
+ " <tr>\n",
428
+ " <th>1</th>\n",
429
+ " <td>POLYGON EMPTY</td>\n",
430
+ " <td>3</td>\n",
431
+ " <td>#m_ylw-pushpin</td>\n",
432
+ " </tr>\n",
433
+ " <tr>\n",
434
+ " <th>2</th>\n",
435
+ " <td>POLYGON EMPTY</td>\n",
436
+ " <td>2</td>\n",
437
+ " <td>#m_ylw-pushpin</td>\n",
438
+ " </tr>\n",
439
+ " <tr>\n",
440
+ " <th>3</th>\n",
441
+ " <td>POLYGON EMPTY</td>\n",
442
+ " <td>4</td>\n",
443
+ " <td>#m_ylw-pushpin</td>\n",
444
+ " </tr>\n",
445
+ " <tr>\n",
446
+ " <th>4</th>\n",
447
+ " <td>POLYGON ((1262518.164 840333.056, 1262472.46 8...</td>\n",
448
+ " <td>Polygon Measure</td>\n",
449
+ " <td>#inline1</td>\n",
450
+ " </tr>\n",
451
+ " </tbody>\n",
452
+ "</table>\n",
453
+ "</div>"
454
+ ],
455
+ "text/plain": [
456
+ " geometry name \\\n",
457
+ "0 POLYGON EMPTY 1 \n",
458
+ "1 POLYGON EMPTY 3 \n",
459
+ "2 POLYGON EMPTY 2 \n",
460
+ "3 POLYGON EMPTY 4 \n",
461
+ "4 POLYGON ((1262518.164 840333.056, 1262472.46 8... Polygon Measure \n",
462
+ "\n",
463
+ " styleUrl \n",
464
+ "0 #m_ylw-pushpin \n",
465
+ "1 #m_ylw-pushpin \n",
466
+ "2 #m_ylw-pushpin \n",
467
+ "3 #m_ylw-pushpin \n",
468
+ "4 #inline1 "
469
+ ]
470
+ },
471
+ "execution_count": 5,
472
+ "metadata": {},
473
+ "output_type": "execute_result"
474
+ }
475
+ ],
476
+ "source": [
477
+ "input_gdf"
478
+ ]
479
+ },
480
+ {
481
+ "cell_type": "code",
482
+ "execution_count": 6,
483
+ "metadata": {},
484
+ "outputs": [
485
+ {
486
+ "name": "stderr",
487
+ "output_type": "stream",
488
+ "text": [
489
+ "/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",
490
+ " return (geometry.type == \"Polygon\") and (not geometry.is_empty)\n",
491
+ "/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",
492
+ " return (geometry.type == \"Polygon\") and (not geometry.is_empty)\n",
493
+ "/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",
494
+ " return (geometry.type == \"Polygon\") and (not geometry.is_empty)\n",
495
+ "/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",
496
+ " return (geometry.type == \"Polygon\") and (not geometry.is_empty)\n",
497
+ "/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",
498
+ " return (geometry.type == \"Polygon\") and (not geometry.is_empty)\n"
499
+ ]
500
+ }
501
+ ],
502
+ "source": [
503
+ "def is_valid_polygon(geometry_gdf):\n",
504
+ " geometry = geometry_gdf.geometry.item()\n",
505
+ " return (geometry.type == \"Polygon\") and (not geometry.is_empty)\n",
506
+ "\n",
507
+ "for i in range(len(input_gdf)):\n",
508
+ " geometry_gdf = input_gdf[input_gdf.index == i]\n",
509
+ " if is_valid_polygon(geometry_gdf):\n",
510
+ " break"
511
+ ]
512
+ },
513
+ {
514
+ "cell_type": "code",
515
+ "execution_count": 7,
516
+ "metadata": {},
517
+ "outputs": [
518
+ {
519
+ "data": {
520
+ "text/html": [
521
+ "<div>\n",
522
+ "<style scoped>\n",
523
+ " .dataframe tbody tr th:only-of-type {\n",
524
+ " vertical-align: middle;\n",
525
+ " }\n",
526
+ "\n",
527
+ " .dataframe tbody tr th {\n",
528
+ " vertical-align: top;\n",
529
+ " }\n",
530
+ "\n",
531
+ " .dataframe thead th {\n",
532
+ " text-align: right;\n",
533
+ " }\n",
534
+ "</style>\n",
535
+ "<table border=\"1\" class=\"dataframe\">\n",
536
+ " <thead>\n",
537
+ " <tr style=\"text-align: right;\">\n",
538
+ " <th></th>\n",
539
+ " <th>geometry</th>\n",
540
+ " <th>name</th>\n",
541
+ " <th>styleUrl</th>\n",
542
+ " </tr>\n",
543
+ " </thead>\n",
544
+ " <tbody>\n",
545
+ " <tr>\n",
546
+ " <th>4</th>\n",
547
+ " <td>POLYGON ((1262518.164 840333.056, 1262472.46 8...</td>\n",
548
+ " <td>Polygon Measure</td>\n",
549
+ " <td>#inline1</td>\n",
550
+ " </tr>\n",
551
+ " </tbody>\n",
552
+ "</table>\n",
553
+ "</div>"
554
+ ],
555
+ "text/plain": [
556
+ " geometry name \\\n",
557
+ "4 POLYGON ((1262518.164 840333.056, 1262472.46 8... Polygon Measure \n",
558
+ "\n",
559
+ " styleUrl \n",
560
+ "4 #inline1 "
561
+ ]
562
+ },
563
+ "execution_count": 7,
564
+ "metadata": {},
565
+ "output_type": "execute_result"
566
+ }
567
+ ],
568
+ "source": [
569
+ "geometry_gdf"
570
+ ]
571
+ },
572
  {
573
  "cell_type": "code",
574
  "execution_count": 10,