Zeel commited on
Commit
d35782f
1 Parent(s): 1803795
Files changed (1) hide show
  1. app.py +69 -40
app.py CHANGED
@@ -57,20 +57,40 @@ st.markdown(
57
  ############################################
58
  st.write("<h2><div style='text-align: center;'>User Inputs</div></h2>", unsafe_allow_html=True)
59
 
60
- st.write("Select the vegetation indices to calculate:")
61
- all_veg_indices = ["NDVI", "EVI", "EVI2"]
62
- formulas = {
63
- "NDVI": r"$\frac{NIR - Red}{NIR + Red}$",
64
- "EVI": r"$G \times \frac{NIR - Red}{NIR + C1 \times Red - C2 \times Blue + L}$",
65
- "EVI2": r"$G \times \frac{NIR - Red}{NIR + L + C \times Red}$",
66
- }
67
- defaults = [True, False, False]
68
- veg_indices = []
69
- for veg_index, default in zip(all_veg_indices, defaults):
70
- if st.checkbox(f"{veg_index} = {formulas[veg_index]}", value=default):
71
- veg_indices.append(veg_index)
72
 
73
- with st.expander("EVI/EVI2 Parameters"):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
  st.write("Select the parameters for the EVI/EVI2 calculation (default is as per EVI's Wikipedia page)")
75
  cols = st.columns(5)
76
  evi_vars = {}
@@ -85,8 +105,6 @@ with st.expander("EVI/EVI2 Parameters"):
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]
@@ -419,10 +437,10 @@ wayback_mapping = st.session_state.wayback_mapping
419
  ############################################
420
 
421
  # Input: Satellite Sources
422
- st.write("Select the satellite sources:")
423
  satellite_selected = {}
424
  for satellite in satellites:
425
- satellite_selected[satellite] = st.checkbox(satellite, value=True, disabled=True)
426
 
427
  # Date range input
428
  max_year = datetime.now().year
@@ -431,15 +449,13 @@ dec_31 = pd.to_datetime(f"{max_year}/12/31", format="%Y/%m/%d")
431
  nov_15 = pd.to_datetime(f"{max_year}/11/15", format="%Y/%m/%d")
432
  dec_15 = pd.to_datetime(f"{max_year}/12/15", format="%Y/%m/%d")
433
  input_daterange = st.date_input(
434
- "Date Range (Ignore year. App will compute indices for all possible years)", (nov_15, dec_15), jan_1, dec_31
435
  )
436
- min_year = int(st.number_input("Minimum Year", value=2019, min_value=2015, step=1))
437
- max_year = int(st.number_input("Maximum Year", value=max_year, min_value=2015, step=1))
438
-
439
- # Input: GeoJSON/KML file
440
- file_url = st.query_params.get("file_url", None)
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
 
@@ -502,12 +518,33 @@ stats_df = pd.DataFrame(
502
  }
503
  )
504
  st.write("<h3><div style='text-align: center;'>Geometry Metrics</div></h3>", unsafe_allow_html=True)
 
 
 
 
 
 
 
505
  st.markdown(
506
- f"""| Metric | Value |
507
- | --- | --- |
508
- | Area (m^2) | {stats_df['Area (m^2)'].item():.2f} m^2 = {stats_df['Area (m^2)'].item()/10000:.2f} ha |
509
- | Perimeter (m) | {stats_df['Perimeter (m)'].item():.2f} m |
510
- """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
511
  )
512
 
513
  stats_csv = stats_df.to_csv(index=False)
@@ -516,9 +553,8 @@ st.download_button("Download Geometry Metrics", stats_csv, "geometry_metrics.csv
516
  # Submit
517
  submit = st.button("Calculate Vegetation Indices", use_container_width=True)
518
 
519
- st.write("<h2><div style='text-align: center;'>Results</div></h2>", unsafe_allow_html=True)
520
-
521
  if submit:
 
522
  if not any(satellite_selected.values()):
523
  st.error("Please select at least one satellite source")
524
  st.stop()
@@ -697,11 +733,4 @@ if "result" in st.session_state:
697
  )
698
  m.to_streamlit()
699
 
700
-
701
- # Add credits
702
- st.write(
703
- """
704
- <div style="display: flex; justify-content: center; align-items: center;">
705
- <p style="text-align: left;">This tool is developed by <a href="https://sustainability-lab.github.io/">Sustainability Lab</a>, <a href="https://www.iitgn.ac.in/">IIT Gandhinagar</a> and supported by <a href="https://forests.gujarat.gov.in/">Gujarat Forest Department</a></p>""",
706
- unsafe_allow_html=True,
707
- )
 
57
  ############################################
58
  st.write("<h2><div style='text-align: center;'>User Inputs</div></h2>", unsafe_allow_html=True)
59
 
60
+ # Input: GeoJSON/KML file
61
+ file_url = st.query_params.get("file_url", None)
62
+ if file_url is None:
63
+ file_url = st.file_uploader("Upload KML/GeoJSON file", type=["geojson", "kml", "shp"])
 
 
 
 
 
 
 
 
64
 
65
+ def show_credits():
66
+ # Add credits
67
+ st.write(
68
+ """
69
+ <div style="display: flex; justify-content: center; align-items: center; margin-top: 20px;">
70
+ <p style="text-align: left;">This tool is developed by <a href="https://sustainability-lab.github.io/">Sustainability Lab</a>, <a href="https://www.iitgn.ac.in/">IIT Gandhinagar</a> and supported by <a href="https://forests.gujarat.gov.in/">Gujarat Forest Department</a></p>""",
71
+ unsafe_allow_html=True,
72
+ )
73
+
74
+ if file_url is None:
75
+ st.warning(
76
+ "Please provide a KML or GeoJSON URL as a query parameter, e.g., `?file_url=<your_file_url>` or upload a file."
77
+ )
78
+ show_credits()
79
+ st.stop()
80
+
81
+ with st.expander("Advanced Settings"):
82
+ st.write("Select the vegetation indices to calculate:")
83
+ all_veg_indices = ["NDVI", "EVI", "EVI2"]
84
+ formulas = {
85
+ "NDVI": r"$\frac{NIR - Red}{NIR + Red}$",
86
+ "EVI": r"$G \times \frac{NIR - Red}{NIR + C1 \times Red - C2 \times Blue + L}$",
87
+ "EVI2": r"$G \times \frac{NIR - Red}{NIR + L + C \times Red}$",
88
+ }
89
+ defaults = [True, False, False]
90
+ veg_indices = []
91
+ for veg_index, default in zip(all_veg_indices, defaults):
92
+ if st.checkbox(f"{veg_index} = {formulas[veg_index]}", value=default):
93
+ veg_indices.append(veg_index)
94
  st.write("Select the parameters for the EVI/EVI2 calculation (default is as per EVI's Wikipedia page)")
95
  cols = st.columns(5)
96
  evi_vars = {}
 
105
 
106
  # Function of find best suited statewise EPSG code
107
  def get_gdf_from_file_url(file_url):
 
 
108
  if isinstance(file_url, str):
109
  if file_url.startswith("https://drive.google.com/file/d/"):
110
  ID = file_url.replace("https://drive.google.com/file/d/", "").split("/")[0]
 
437
  ############################################
438
 
439
  # Input: Satellite Sources
440
+ st.markdown(f"Satellite source: `{list(satellites.keys())[0]}`")
441
  satellite_selected = {}
442
  for satellite in satellites:
443
+ satellite_selected[satellite] = satellite
444
 
445
  # Date range input
446
  max_year = datetime.now().year
 
449
  nov_15 = pd.to_datetime(f"{max_year}/11/15", format="%Y/%m/%d")
450
  dec_15 = pd.to_datetime(f"{max_year}/12/15", format="%Y/%m/%d")
451
  input_daterange = st.date_input(
452
+ "Date Range (Ignore year. App will compute indices for this date range in each year starting from \"Minimum Year\" to \"Maximum Year\")", (nov_15, dec_15), jan_1, dec_31
453
  )
454
+ cols = st.columns(2)
455
+ with cols[0]:
456
+ min_year = int(st.number_input("Minimum Year", value=2019, min_value=2015, step=1))
457
+ with cols[1]:
458
+ max_year = int(st.number_input("Maximum Year", value=max_year, min_value=2015, step=1))
 
 
459
 
460
  buffer = st.number_input("Buffer (m)", value=50, min_value=0, step=1)
461
 
 
518
  }
519
  )
520
  st.write("<h3><div style='text-align: center;'>Geometry Metrics</div></h3>", unsafe_allow_html=True)
521
+ # st.markdown(
522
+ # f"""| Metric | Value |
523
+ # | --- | --- |
524
+ # | Area (m^2) | {stats_df['Area (m^2)'].item():.2f} m^2 = {stats_df['Area (m^2)'].item()/10000:.2f} ha |
525
+ # | Perimeter (m) | {stats_df['Perimeter (m)'].item():.2f} m |
526
+ # """
527
+ # )
528
  st.markdown(
529
+ f"""
530
+ <div style="display: flex; justify-content: center;">
531
+ <table style="border-collapse: collapse; width: 50%; text-align: center;">
532
+ <tr>
533
+ <th style="border: 1px solid black; padding: 8px;">Metric</th>
534
+ <th style="border: 1px solid black; padding: 8px;">Value</th>
535
+ </tr>
536
+ <tr>
537
+ <td style="border: 1px solid black; padding: 8px;">Area</td>
538
+ <td style="border: 1px solid black; padding: 8px;">{stats_df['Area (m^2)'].item()/10000:.2f} ha</td>
539
+ </tr>
540
+ <tr>
541
+ <td style="border: 1px solid black; padding: 8px;">Perimeter</td>
542
+ <td style="border: 1px solid black; padding: 8px;">{stats_df['Perimeter (m)'].item():.2f} m</td>
543
+ </tr>
544
+ </table>
545
+ </div>
546
+ """,
547
+ unsafe_allow_html=True
548
  )
549
 
550
  stats_csv = stats_df.to_csv(index=False)
 
553
  # Submit
554
  submit = st.button("Calculate Vegetation Indices", use_container_width=True)
555
 
 
 
556
  if submit:
557
+ st.write("<h2><div style='text-align: center;'>Results</div></h2>", unsafe_allow_html=True)
558
  if not any(satellite_selected.values()):
559
  st.error("Please select at least one satellite source")
560
  st.stop()
 
733
  )
734
  m.to_streamlit()
735
 
736
+ show_credits()