Zeel commited on
Commit
114dd95
1 Parent(s): 28ff3df

fix some kml issue

Browse files
Files changed (3) hide show
  1. app.py +34 -12
  2. requirements.txt +1 -0
  3. sandbox.ipynb +169 -0
app.py CHANGED
@@ -15,6 +15,9 @@ import plotly.express as px
15
  import branca.colormap as cm
16
  import folium
17
  import pyproj
 
 
 
18
 
19
  st.set_page_config(layout="wide")
20
  m = st.markdown(
@@ -81,6 +84,34 @@ with st.expander("EVI/EVI2 Parameters"):
81
  ############################################
82
 
83
  # Function of find best suited statewise EPSG code
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
  def find_best_epsg(geometry):
85
  if geometry.geom_type == 'Polygon':
86
  centroid = geometry.centroid
@@ -410,20 +441,11 @@ file_url = st.query_params.get("file_url", None)
410
  if file_url is None:
411
  file_url = st.file_uploader("Upload KML/GeoJSON file", type=["geojson", "kml", "shp"])
412
 
413
- if file_url is None:
414
- st.stop()
415
-
416
- if isinstance(file_url, str):
417
- if file_url.startswith("https://drive.google.com/file/d/"):
418
- ID = file_url.replace("https://drive.google.com/file/d/", "").split("/")[0]
419
- file_url = f"https://drive.google.com/uc?id={ID}"
420
- elif file_url.startswith("https://drive.google.com/open?id="):
421
- ID = file_url.replace("https://drive.google.com/open?id=", "")
422
- file_url = f"https://drive.google.com/uc?id={ID}"
423
-
424
  buffer = st.number_input("Buffer (m)", value=50, min_value=0, step=1)
425
 
426
- input_gdf = preprocess_gdf(gpd.read_file(file_url))
 
 
427
  if len(input_gdf) > 1:
428
  st.warning(f"Only the first polygon in the KML will be processed; all other geometries will be ignored.")
429
 
 
15
  import branca.colormap as cm
16
  import folium
17
  import pyproj
18
+ from io import StringIO, BytesIO
19
+ import requests
20
+ import kml2geojson
21
 
22
  st.set_page_config(layout="wide")
23
  m = st.markdown(
 
84
  ############################################
85
 
86
  # Function of find best suited statewise EPSG code
87
+ def get_gdf_from_file_url(file_url):
88
+ if file_url is None:
89
+ st.stop()
90
+ if isinstance(file_url, str):
91
+ if file_url.startswith("https://drive.google.com/file/d/"):
92
+ ID = file_url.replace("https://drive.google.com/file/d/", "").split("/")[0]
93
+ file_url = f"https://drive.google.com/uc?id={ID}"
94
+ elif file_url.startswith("https://drive.google.com/open?id="):
95
+ ID = file_url.replace("https://drive.google.com/open?id=", "")
96
+ file_url = f"https://drive.google.com/uc?id={ID}"
97
+
98
+ response = requests.get(file_url)
99
+ bytes_data = BytesIO(response.content)
100
+ string_data = response.text
101
+ else:
102
+ bytes_data = BytesIO(file_url.getvalue())
103
+ string_data = file_url.getvalue().decode("utf-8")
104
+
105
+ if string_data.startswith("<?xml"):
106
+ geojson = kml2geojson.convert(bytes_data)
107
+ features = geojson[0]["features"]
108
+ epsg = 4326
109
+ input_gdf = gpd.GeoDataFrame.from_features(features, crs=f"EPSG:{epsg}")
110
+ else:
111
+ input_gdf = gpd.read_file(bytes_data)
112
+
113
+ return input_gdf
114
+
115
  def find_best_epsg(geometry):
116
  if geometry.geom_type == 'Polygon':
117
  centroid = geometry.centroid
 
441
  if file_url is None:
442
  file_url = st.file_uploader("Upload KML/GeoJSON file", type=["geojson", "kml", "shp"])
443
 
 
 
 
 
 
 
 
 
 
 
 
444
  buffer = st.number_input("Buffer (m)", value=50, min_value=0, step=1)
445
 
446
+ input_gdf = get_gdf_from_file_url(file_url)
447
+ input_gdf = preprocess_gdf(input_gdf)
448
+
449
  if len(input_gdf) > 1:
450
  st.warning(f"Only the first polygon in the KML will be processed; all other geometries will be ignored.")
451
 
requirements.txt CHANGED
@@ -3,3 +3,4 @@ geemap
3
  leafmap
4
  pandas
5
  pyproj
 
 
3
  leafmap
4
  pandas
5
  pyproj
6
+ kml2geojson
sandbox.ipynb CHANGED
@@ -1,5 +1,174 @@
1
  {
2
  "cells": [
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  {
4
  "cell_type": "code",
5
  "execution_count": 14,
 
1
  {
2
  "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 13,
6
+ "metadata": {},
7
+ "outputs": [],
8
+ "source": [
9
+ "from io import StringIO, BytesIO\n",
10
+ "import kml2geojson\n",
11
+ "import requests\n",
12
+ "import geopandas as gpd\n",
13
+ "\n",
14
+ "data = requests.get(\"https://drive.google.com/uc?id=1bN8lkSQkGZcSmZPLF8jm-AWTRA7FsEcr\")"
15
+ ]
16
+ },
17
+ {
18
+ "cell_type": "code",
19
+ "execution_count": 14,
20
+ "metadata": {},
21
+ "outputs": [
22
+ {
23
+ "data": {
24
+ "text/html": [
25
+ "<div>\n",
26
+ "<style scoped>\n",
27
+ " .dataframe tbody tr th:only-of-type {\n",
28
+ " vertical-align: middle;\n",
29
+ " }\n",
30
+ "\n",
31
+ " .dataframe tbody tr th {\n",
32
+ " vertical-align: top;\n",
33
+ " }\n",
34
+ "\n",
35
+ " .dataframe thead th {\n",
36
+ " text-align: right;\n",
37
+ " }\n",
38
+ "</style>\n",
39
+ "<table border=\"1\" class=\"dataframe\">\n",
40
+ " <thead>\n",
41
+ " <tr style=\"text-align: right;\">\n",
42
+ " <th></th>\n",
43
+ " <th>geometry</th>\n",
44
+ " <th>name</th>\n",
45
+ " <th>styleUrl</th>\n",
46
+ " <th>description</th>\n",
47
+ " </tr>\n",
48
+ " </thead>\n",
49
+ " <tbody>\n",
50
+ " <tr>\n",
51
+ " <th>0</th>\n",
52
+ " <td>POLYGON Z ((73.42161 21.55846 0, 73.42216 21.5...</td>\n",
53
+ " <td>Pingot-549 10=00 Hq Gree</td>\n",
54
+ " <td>#inline1</td>\n",
55
+ " <td>NaN</td>\n",
56
+ " </tr>\n",
57
+ " <tr>\n",
58
+ " <th>1</th>\n",
59
+ " <td>POINT Z (73.41723 21.56071 0)</td>\n",
60
+ " <td>21°33'38.54\"N 73°25'2.03\"E</td>\n",
61
+ " <td>#0_0</td>\n",
62
+ " <td>&lt;style type='text/css'&gt;*{font-family:Verdana,A...</td>\n",
63
+ " </tr>\n",
64
+ " <tr>\n",
65
+ " <th>2</th>\n",
66
+ " <td>POINT Z (73.41938 21.55855 0)</td>\n",
67
+ " <td>21°33'30.79\"N 73°25'9.76\"E</td>\n",
68
+ " <td>#0_0</td>\n",
69
+ " <td>&lt;style type='text/css'&gt;*{font-family:Verdana,A...</td>\n",
70
+ " </tr>\n",
71
+ " <tr>\n",
72
+ " <th>3</th>\n",
73
+ " <td>POINT Z (73.42016 21.55905 0)</td>\n",
74
+ " <td>21°33'32.57\"N 73°25'12.58\"E</td>\n",
75
+ " <td>#0_0</td>\n",
76
+ " <td>&lt;style type='text/css'&gt;*{font-family:Verdana,A...</td>\n",
77
+ " </tr>\n",
78
+ " <tr>\n",
79
+ " <th>4</th>\n",
80
+ " <td>POINT Z (73.42024 21.55862 0)</td>\n",
81
+ " <td>21°33'31.02\"N 73°25'12.85\"E</td>\n",
82
+ " <td>#0_0</td>\n",
83
+ " <td>&lt;style type='text/css'&gt;*{font-family:Verdana,A...</td>\n",
84
+ " </tr>\n",
85
+ " <tr>\n",
86
+ " <th>5</th>\n",
87
+ " <td>POINT Z (73.4216 21.55846 0)</td>\n",
88
+ " <td>21°33'30.46\"N 73°25'17.77\"E</td>\n",
89
+ " <td>#0_0</td>\n",
90
+ " <td>&lt;style type='text/css'&gt;*{font-family:Verdana,A...</td>\n",
91
+ " </tr>\n",
92
+ " <tr>\n",
93
+ " <th>6</th>\n",
94
+ " <td>POINT Z (73.42217 21.55951 0)</td>\n",
95
+ " <td>21°33'34.25\"N 73°25'19.82\"E</td>\n",
96
+ " <td>#0_0</td>\n",
97
+ " <td>&lt;style type='text/css'&gt;*{font-family:Verdana,A...</td>\n",
98
+ " </tr>\n",
99
+ " <tr>\n",
100
+ " <th>7</th>\n",
101
+ " <td>POINT Z (73.42244 21.56044 0)</td>\n",
102
+ " <td>21°33'37.59\"N 73°25'20.79\"E</td>\n",
103
+ " <td>#0_0</td>\n",
104
+ " <td>&lt;style type='text/css'&gt;*{font-family:Verdana,A...</td>\n",
105
+ " </tr>\n",
106
+ " <tr>\n",
107
+ " <th>8</th>\n",
108
+ " <td>POINT Z (73.42052 21.56026 0)</td>\n",
109
+ " <td>21°33'36.92\"N 73°25'13.86\"E</td>\n",
110
+ " <td>#0_0</td>\n",
111
+ " <td>&lt;style type='text/css'&gt;*{font-family:Verdana,A...</td>\n",
112
+ " </tr>\n",
113
+ " <tr>\n",
114
+ " <th>9</th>\n",
115
+ " <td>POINT Z (73.41944 21.56159 0)</td>\n",
116
+ " <td>21°33'41.72\"N 73°25'10.00\"E</td>\n",
117
+ " <td>#0_0</td>\n",
118
+ " <td>&lt;style type='text/css'&gt;*{font-family:Verdana,A...</td>\n",
119
+ " </tr>\n",
120
+ " </tbody>\n",
121
+ "</table>\n",
122
+ "</div>"
123
+ ],
124
+ "text/plain": [
125
+ " geometry \\\n",
126
+ "0 POLYGON Z ((73.42161 21.55846 0, 73.42216 21.5... \n",
127
+ "1 POINT Z (73.41723 21.56071 0) \n",
128
+ "2 POINT Z (73.41938 21.55855 0) \n",
129
+ "3 POINT Z (73.42016 21.55905 0) \n",
130
+ "4 POINT Z (73.42024 21.55862 0) \n",
131
+ "5 POINT Z (73.4216 21.55846 0) \n",
132
+ "6 POINT Z (73.42217 21.55951 0) \n",
133
+ "7 POINT Z (73.42244 21.56044 0) \n",
134
+ "8 POINT Z (73.42052 21.56026 0) \n",
135
+ "9 POINT Z (73.41944 21.56159 0) \n",
136
+ "\n",
137
+ " name styleUrl \\\n",
138
+ "0 Pingot-549 10=00 Hq Gree #inline1 \n",
139
+ "1 21°33'38.54\"N 73°25'2.03\"E #0_0 \n",
140
+ "2 21°33'30.79\"N 73°25'9.76\"E #0_0 \n",
141
+ "3 21°33'32.57\"N 73°25'12.58\"E #0_0 \n",
142
+ "4 21°33'31.02\"N 73°25'12.85\"E #0_0 \n",
143
+ "5 21°33'30.46\"N 73°25'17.77\"E #0_0 \n",
144
+ "6 21°33'34.25\"N 73°25'19.82\"E #0_0 \n",
145
+ "7 21°33'37.59\"N 73°25'20.79\"E #0_0 \n",
146
+ "8 21°33'36.92\"N 73°25'13.86\"E #0_0 \n",
147
+ "9 21°33'41.72\"N 73°25'10.00\"E #0_0 \n",
148
+ "\n",
149
+ " description \n",
150
+ "0 NaN \n",
151
+ "1 <style type='text/css'>*{font-family:Verdana,A... \n",
152
+ "2 <style type='text/css'>*{font-family:Verdana,A... \n",
153
+ "3 <style type='text/css'>*{font-family:Verdana,A... \n",
154
+ "4 <style type='text/css'>*{font-family:Verdana,A... \n",
155
+ "5 <style type='text/css'>*{font-family:Verdana,A... \n",
156
+ "6 <style type='text/css'>*{font-family:Verdana,A... \n",
157
+ "7 <style type='text/css'>*{font-family:Verdana,A... \n",
158
+ "8 <style type='text/css'>*{font-family:Verdana,A... \n",
159
+ "9 <style type='text/css'>*{font-family:Verdana,A... "
160
+ ]
161
+ },
162
+ "execution_count": 14,
163
+ "metadata": {},
164
+ "output_type": "execute_result"
165
+ }
166
+ ],
167
+ "source": [
168
+ "gpd.GeoDataFrame.from_features(kml2geojson.convert(BytesIO(data.content))[0]['features'])\n",
169
+ "# kml2geojson.convert(BytesIO(data.content))[0]['features']"
170
+ ]
171
+ },
172
  {
173
  "cell_type": "code",
174
  "execution_count": 14,