File size: 2,044 Bytes
9914376
73b9278
 
 
 
 
 
 
 
 
 
 
 
 
9914376
 
 
 
73b9278
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import re
import json
import streamlit as st
import pandas as pd
import geopandas as gpd
import leafmap.foliumap as leafmap

# wide streamlit display
st.set_page_config(layout="wide")

query_params = st.experimental_get_query_params()
file_url = query_params.get("file_url", [None])[0]

if file_url:
    if file_url.startswith("https://drive.google.com/file/d/"):
        ID = file_url.replace("https://drive.google.com/file/d/", "").split("/")[0]
        file_url = f"https://drive.google.com/uc?id={ID}"

    input_gdf = gpd.read_file(file_url)
    input_gdf = input_gdf.to_crs(epsg=7761)  # Gujarat zone

    def format_fn(x):
        return input_gdf.drop(columns=["geometry"]).loc[x].to_dict()

    input_geometry_idx = st.selectbox("Select the geometry", input_gdf.index, format_func=format_fn)
    geometry_gdf = input_gdf[input_gdf.index == input_geometry_idx]

    m = leafmap.Map()
    m.add_wms_layer(
        "https://wayback.maptiles.arcgis.com/arcgis/rest/services/World_Imagery/WMTS/1.0.0/GoogleMapsCompatible/MapServer/tile/56450/{z}/{y}/{x}"
    )
    m.add_gdf(
        geometry_gdf,
        layer_name="Geometry",
        zoom_to_layer=True,
        style_function=lambda x: {"color": "red", "fillOpacity": 0.0},
    )
    m.to_streamlit()

    # Metrics
    stats_df = pd.DataFrame()
    stats_df.loc["Points"] = json.loads(geometry_gdf.to_crs(4326).to_json())["features"][0]["geometry"]["coordinates"]
    stats_df.loc["Area (ha)"] = geometry_gdf.geometry.area.item() / 10000
    stats_df.loc["Perimeter (m)"] = geometry_gdf.geometry.length.item()

    st.write("<h3><div style='text-align: center;'>Geometry Metrics</div></h3>", unsafe_allow_html=True)
    st.markdown(
        f"""| Metric | Value |
    | --- | --- |
    | Area (m^2) | {stats_df['Area (m^2)'].item():.2f} m^2 = {stats_df['Area (m^2)'].item()/10000:.2f} ha |
    | Perimeter (m) | {stats_df['Perimeter (m)'].item():.2f} m |
    """
    )
else:
    st.warning("Please provide a KML or GeoJSON URL as a query parameter, e.g., `?file_url=<your_file_url>`")