Zeel commited on
Commit
62dd0f3
·
1 Parent(s): 8f51ef1

fix kml multi-polygon issue

Browse files
Files changed (3) hide show
  1. app.py +20 -1
  2. requirements.txt +1 -0
  3. sandbox.ipynb +100 -0
app.py CHANGED
@@ -6,6 +6,9 @@ import geopandas as gpd
6
  import leafmap.foliumap as leafmap
7
  from optree import tree_map
8
  from shapely.ops import transform
 
 
 
9
 
10
 
11
  def shape_3d_to_2d(shape):
@@ -66,6 +69,7 @@ if ("file_url" in st.session_state) and ("input_gdf" in st.session_state) and (s
66
  input_gdf = st.session_state.input_gdf
67
  else:
68
  st.session_state.file_url = file_url
 
69
  if isinstance(file_url, str):
70
  if file_url.startswith("https://drive.google.com/file/d/"):
71
  ID = file_url.replace("https://drive.google.com/file/d/", "").split("/")[0]
@@ -74,7 +78,22 @@ else:
74
  ID = file_url.replace("https://drive.google.com/open?id=", "")
75
  file_url = f"https://drive.google.com/uc?id={ID}"
76
 
77
- input_gdf = preprocess_gdf(gpd.read_file(file_url))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
  if len(input_gdf) > 1:
79
  st.warning(f"Only the first polygon in the KML will be processed; all other geometries will be ignored.")
80
 
 
6
  import leafmap.foliumap as leafmap
7
  from optree import tree_map
8
  from shapely.ops import transform
9
+ import kml2geojson
10
+ from io import BytesIO, StringIO
11
+ import requests
12
 
13
 
14
  def shape_3d_to_2d(shape):
 
69
  input_gdf = st.session_state.input_gdf
70
  else:
71
  st.session_state.file_url = file_url
72
+ print(file_url, type(file_url))
73
  if isinstance(file_url, str):
74
  if file_url.startswith("https://drive.google.com/file/d/"):
75
  ID = file_url.replace("https://drive.google.com/file/d/", "").split("/")[0]
 
78
  ID = file_url.replace("https://drive.google.com/open?id=", "")
79
  file_url = f"https://drive.google.com/uc?id={ID}"
80
 
81
+ response = requests.get(file_url)
82
+ bytes_data = BytesIO(response.content)
83
+ string_data = response.text
84
+ else:
85
+ bytes_data = BytesIO(file_url.getvalue())
86
+ string_data = file_url.getvalue().decode("utf-8")
87
+
88
+ if string_data.startswith("<?xml"):
89
+ geojson = kml2geojson.convert(bytes_data)
90
+ features = geojson[0]["features"]
91
+ epsg = 4326
92
+ input_gdf = gpd.GeoDataFrame.from_features(features, crs=f"EPSG:{epsg}")
93
+ else:
94
+ input_gdf = gpd.read_file(bytes_data)
95
+
96
+ input_gdf = preprocess_gdf(input_gdf)
97
  if len(input_gdf) > 1:
98
  st.warning(f"Only the first polygon in the KML will be processed; all other geometries will be ignored.")
99
 
requirements.txt CHANGED
@@ -1,3 +1,4 @@
1
  leafmap
2
  geopandas
3
  optree
 
 
1
  leafmap
2
  geopandas
3
  optree
4
+ kml2geojson
sandbox.ipynb ADDED
@@ -0,0 +1,100 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 10,
6
+ "metadata": {},
7
+ "outputs": [],
8
+ "source": [
9
+ "import pandas as pd\n",
10
+ "import geopandas as gpd\n",
11
+ "from tqdm.notebook import tqdm\n",
12
+ "from joblib import Parallel, delayed"
13
+ ]
14
+ },
15
+ {
16
+ "cell_type": "code",
17
+ "execution_count": 12,
18
+ "metadata": {},
19
+ "outputs": [
20
+ {
21
+ "data": {
22
+ "application/vnd.jupyter.widget-view+json": {
23
+ "model_id": "e90c443ceec74b3e9bf223587db76898",
24
+ "version_major": 2,
25
+ "version_minor": 0
26
+ },
27
+ "text/plain": [
28
+ " 0%| | 0/15 [00:00<?, ?it/s]"
29
+ ]
30
+ },
31
+ "metadata": {},
32
+ "output_type": "display_data"
33
+ },
34
+ {
35
+ "name": "stdout",
36
+ "output_type": "stream",
37
+ "text": [
38
+ "https://drive.google.com/open?id=1skBV8miaZN3gE2lPVnHQQG-n-RwbMH2d has 5 geometries\n",
39
+ "https://drive.google.com/open?id=12OJzdi0RE5BDdMHzBovnYK_QhC5jIiSy has 6 geometries\n",
40
+ "https://drive.google.com/open?id=1lge4nZKw7gxuDbQtM_v7D8eBHzUsLoBI has 3 geometries\n"
41
+ ]
42
+ }
43
+ ],
44
+ "source": [
45
+ "files = \"\"\"https://drive.google.com/open?id=1QHbcxHRw3EQWsw5-UN6AUnVI-bhmhCQ5\n",
46
+ "https://drive.google.com/open?id=1skBV8miaZN3gE2lPVnHQQG-n-RwbMH2d\n",
47
+ "https://drive.google.com/open?id=1jyG_D6UudyAgicZjsvEfNLT_vNK1G3EB\n",
48
+ "https://drive.google.com/open?id=1NxO399MvzufeT6-xigq3qi79psA40rkQ\n",
49
+ "https://drive.google.com/open?id=1P2ZPMsxX82P12m460EBjpYMrdXUkZ05l\n",
50
+ "https://drive.google.com/open?id=1Pc4sIYbRcu761fuXV5HVDG3EaqQC-uiz\n",
51
+ "https://drive.google.com/open?id=1xskOyh888HrxCrB0Ue9bNGG6r1CvlH3J\n",
52
+ "https://drive.google.com/open?id=12OJzdi0RE5BDdMHzBovnYK_QhC5jIiSy\n",
53
+ "https://drive.google.com/open?id=18bDqdNIZOlp97GHpEOaYsCLSTML--aEA\n",
54
+ "https://drive.google.com/open?id=1lge4nZKw7gxuDbQtM_v7D8eBHzUsLoBI\n",
55
+ "https://drive.google.com/open?id=1XzzPh1q0uU3eMPoSC5stCLC7r8oMtgok\n",
56
+ "https://drive.google.com/open?id=1JGmGCVYjdcUeFVmcoa7LcJE36HH0MGoF\n",
57
+ "https://drive.google.com/open?id=1EPp6DZUGFcUESQSeMjQ9famygnWM-3BM\n",
58
+ "https://drive.google.com/open?id=1XY7HbmY0fakAaZaxvUFF5UYk7Qovnit2\n",
59
+ "https://drive.google.com/open?id=1VSVOpw9TFAxPKClHGPFXI_anBSHZOIi-\"\"\"\n",
60
+ "\n",
61
+ "df = pd.DataFrame(columns=['Failed URL', '# of geometries'])\n",
62
+ "i = 0\n",
63
+ "def get_gdf(link):\n",
64
+ " link = link.strip()\n",
65
+ " original_link = link\n",
66
+ " if link.startswith('https://drive.google.com/open?id='):\n",
67
+ " link = 'https://drive.google.com/uc?id=' + link.split('=')[-1]\n",
68
+ " gdf = gpd.read_file(link)\n",
69
+ " if len(gdf) != 1:\n",
70
+ " print(f\"{original_link} has {len(gdf)} geometries\")\n",
71
+ " # df.loc[i, ['Failed URL', '# of geometries']] = [original_link, len(gdf)]\n",
72
+ " # i += 1\n",
73
+ " \n",
74
+ "# df.to_csv('failed_links.csv', index=False)\n",
75
+ "_ = Parallel(n_jobs=32)(delayed(get_gdf)(link) for link in tqdm(files.split('\\n')))"
76
+ ]
77
+ }
78
+ ],
79
+ "metadata": {
80
+ "kernelspec": {
81
+ "display_name": "zeel_py310",
82
+ "language": "python",
83
+ "name": "python3"
84
+ },
85
+ "language_info": {
86
+ "codemirror_mode": {
87
+ "name": "ipython",
88
+ "version": 3
89
+ },
90
+ "file_extension": ".py",
91
+ "mimetype": "text/x-python",
92
+ "name": "python",
93
+ "nbconvert_exporter": "python",
94
+ "pygments_lexer": "ipython3",
95
+ "version": "3.10.15"
96
+ }
97
+ },
98
+ "nbformat": 4,
99
+ "nbformat_minor": 2
100
+ }