Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -9,6 +9,7 @@ import geojson
|
|
9 |
from shapely.geometry import Polygon, MultiPolygon, shape, Point
|
10 |
from io import BytesIO
|
11 |
import fiona
|
|
|
12 |
|
13 |
|
14 |
# Enable fiona driver
|
@@ -26,22 +27,11 @@ with open(os.path.expanduser("~/.config/earthengine/credentials"), "w") as f:
|
|
26 |
ee.Initialize(project='in793-aq-nb-24330048')
|
27 |
|
28 |
# Functions
|
29 |
-
def convert_to_2d_geometry(geom):
|
30 |
if geom is None:
|
31 |
return None
|
32 |
elif geom.has_z:
|
33 |
-
|
34 |
-
exterior_coords = geom.exterior.coords[:] # Get all coordinates of the exterior ring
|
35 |
-
exterior_coords_2d = [(x, y) for x, y, *_ in exterior_coords] # Keep only the x and y coordinates, ignoring z
|
36 |
-
|
37 |
-
# Handle interior rings (holes) if any
|
38 |
-
interior_coords_2d = []
|
39 |
-
for interior in geom.interiors:
|
40 |
-
interior_coords = interior.coords[:]
|
41 |
-
interior_coords_2d.append([(x, y) for x, y, *_ in interior_coords])
|
42 |
-
|
43 |
-
# Create a new Polygon with 2D coordinates
|
44 |
-
return type(geom)(exterior_coords_2d, interior_coords_2d)
|
45 |
else:
|
46 |
return geom
|
47 |
|
@@ -61,7 +51,7 @@ def validate_KML_file(gdf):
|
|
61 |
polygon_info = {}
|
62 |
|
63 |
# Check if it's a single polygon or multipolygon
|
64 |
-
if isinstance(gdf.iloc[0].geometry, Polygon):
|
65 |
polygon_info['is_single_polygon'] = True
|
66 |
|
67 |
polygon = convert_to_2d_geometry(gdf.geometry.iloc[0])
|
@@ -130,16 +120,19 @@ st.title("Mean NDVI Calculator")
|
|
130 |
col = st.columns(2)
|
131 |
start_date = col[0].date_input("Start Date", value=pd.to_datetime('2021-01-01'))
|
132 |
end_date = col[1].date_input("End Date", value=pd.to_datetime('2021-01-30'))
|
|
|
|
|
|
|
|
|
133 |
start_date = start_date.strftime("%Y-%m-%d")
|
134 |
end_date = end_date.strftime("%Y-%m-%d")
|
135 |
|
|
|
136 |
max_cloud_cover = st.number_input("Max Cloud Cover", value=20)
|
137 |
|
138 |
# Get the geojson file from the user
|
139 |
uploaded_file = st.file_uploader("Upload KML/GeoJSON file", type=["geojson", "kml"])
|
140 |
|
141 |
-
|
142 |
-
|
143 |
if uploaded_file is not None:
|
144 |
try:
|
145 |
if uploaded_file.name.endswith("kml"):
|
@@ -192,11 +185,26 @@ if uploaded_file is not None:
|
|
192 |
# Re-order the columns of the resultant dataframe
|
193 |
resultant_df = resultant_df[['Date', 'Imagery', 'AvgNDVI_Inside', 'Avg_NDVI_Buffer', 'Ratio']]
|
194 |
|
|
|
195 |
st.write(resultant_df)
|
196 |
|
197 |
# plot the time series
|
198 |
st.write("Time Series Plot")
|
199 |
st.line_chart(resultant_df[['AvgNDVI_Inside', 'Avg_NDVI_Buffer', 'Date']].set_index('Date'))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
200 |
|
201 |
else:
|
202 |
st.write('ValueError: "Input must have single polygon geometry"')
|
|
|
9 |
from shapely.geometry import Polygon, MultiPolygon, shape, Point
|
10 |
from io import BytesIO
|
11 |
import fiona
|
12 |
+
from shapely import wkb
|
13 |
|
14 |
|
15 |
# Enable fiona driver
|
|
|
27 |
ee.Initialize(project='in793-aq-nb-24330048')
|
28 |
|
29 |
# Functions
|
30 |
+
def convert_to_2d_geometry(geom):
|
31 |
if geom is None:
|
32 |
return None
|
33 |
elif geom.has_z:
|
34 |
+
return df.geometry.transform(lambda geom: wkb.loads(wkb.dumps(geom, output_dimension=2)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
else:
|
36 |
return geom
|
37 |
|
|
|
51 |
polygon_info = {}
|
52 |
|
53 |
# Check if it's a single polygon or multipolygon
|
54 |
+
if isinstance(gdf.iloc[0].geometry, Polygon) and len(gdf)==1:
|
55 |
polygon_info['is_single_polygon'] = True
|
56 |
|
57 |
polygon = convert_to_2d_geometry(gdf.geometry.iloc[0])
|
|
|
120 |
col = st.columns(2)
|
121 |
start_date = col[0].date_input("Start Date", value=pd.to_datetime('2021-01-01'))
|
122 |
end_date = col[1].date_input("End Date", value=pd.to_datetime('2021-01-30'))
|
123 |
+
# Check if start and end dates are valid
|
124 |
+
if start_date>end_date:
|
125 |
+
st.stop()
|
126 |
+
|
127 |
start_date = start_date.strftime("%Y-%m-%d")
|
128 |
end_date = end_date.strftime("%Y-%m-%d")
|
129 |
|
130 |
+
|
131 |
max_cloud_cover = st.number_input("Max Cloud Cover", value=20)
|
132 |
|
133 |
# Get the geojson file from the user
|
134 |
uploaded_file = st.file_uploader("Upload KML/GeoJSON file", type=["geojson", "kml"])
|
135 |
|
|
|
|
|
136 |
if uploaded_file is not None:
|
137 |
try:
|
138 |
if uploaded_file.name.endswith("kml"):
|
|
|
185 |
# Re-order the columns of the resultant dataframe
|
186 |
resultant_df = resultant_df[['Date', 'Imagery', 'AvgNDVI_Inside', 'Avg_NDVI_Buffer', 'Ratio']]
|
187 |
|
188 |
+
# Write the final table
|
189 |
st.write(resultant_df)
|
190 |
|
191 |
# plot the time series
|
192 |
st.write("Time Series Plot")
|
193 |
st.line_chart(resultant_df[['AvgNDVI_Inside', 'Avg_NDVI_Buffer', 'Date']].set_index('Date'))
|
194 |
+
|
195 |
+
# Visualize map on ESRI basemap
|
196 |
+
# Set the title of the app
|
197 |
+
st.write("Map Visualization")
|
198 |
+
|
199 |
+
# Create a Leafmap object
|
200 |
+
m = leafmap.Map()
|
201 |
+
|
202 |
+
# Add ESRI latest imagery basemap and polygon
|
203 |
+
m.add_basemap("ESRI/WorldImagery")
|
204 |
+
m.add_polygon(gdf.iloc[0].geometry, "Polygon Example", color="red", fill_opacity=0.5)
|
205 |
+
|
206 |
+
# Display the map
|
207 |
+
m.to_streamlit(height=600)
|
208 |
|
209 |
else:
|
210 |
st.write('ValueError: "Input must have single polygon geometry"')
|