File size: 3,279 Bytes
5262418
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import streamlit as st
import folium
from streamlit_folium import st_folium
import gdown
import os
import json
import requests

# Function to download from Google Drive
def download_from_gdrive(gdrive_url, output_path="temp_file"):
    # Extract the file ID from the URL
    file_id = gdrive_url.split('/d/')[1].split('/')[0]
    gdrive_download_url = f"https://drive.google.com/uc?id={file_id}"
    gdown.download(gdrive_download_url, output_path, quiet=False)
    return output_path

# Title
st.title("KML/GeoJSON Viewer with Satellite Basemap")

# Get URL from Streamlit's query parameters
query_params = st.experimental_get_query_params()
file_url = query_params.get("file_url", [None])[0]

if file_url:
    # Check if it's a Google Drive link
    if "drive.google.com" in file_url:
        # Download the file locally
        local_file_path = download_from_gdrive(file_url)
        file_extension = local_file_path.split('.')[-1]
    else:
        # Standard HTTP URL for direct access
        try:
            response = requests.get(file_url)
            response.raise_for_status()  # Ensure URL is valid
            file_extension = file_url.split('.')[-1]

            # Save the content to a local file
            with open("temp_file." + file_extension, 'wb') as file:
                file.write(response.content)
            local_file_path = "temp_file." + file_extension
        except requests.exceptions.RequestException as e:
            st.error(f"Error loading file: {e}")
            local_file_path = None

    if local_file_path:
        # Create map without default tiles
        m = folium.Map(tiles=None)

        # Add Google Maps basemap
        folium.TileLayer(
            tiles='https://mt1.google.com/vt/lyrs=r&x={x}&y={y}&z={z}',
            attr='Google Maps',
            name='Google Maps',
            overlay=True,
            control=True
        ).add_to(m)

        # Add Google Satellite basemap
        folium.TileLayer(
            tiles='https://mt1.google.com/vt/lyrs=s&x={x}&y={y}&z={z}',
            attr='Google Satellite',
            name='Google Satellite',
            overlay=True,
            control=True
        ).add_to(m)

        # Determine if file is KML or GeoJSON and add accordingly
        if file_extension.lower() == 'kml':
            folium.Kml(local_file_path).add_to(m)
        elif file_extension.lower() == 'geojson':
            with open(local_file_path, 'r') as file:
                geojson_data = json.load(file)
            folium.GeoJson(geojson_data, name="GeoJSON Data").add_to(m)
        else:
            st.error("Unsupported file format. Please provide a KML or GeoJSON file.")
            local_file_path = None

        # Center and zoom on the layer
        if local_file_path:
            bounds = m.get_bounds()  # Adjusting the view to include KML or GeoJSON bounds
            m.fit_bounds(bounds)

        # Add a layer control to switch between basemaps
        folium.LayerControl().add_to(m)

        # Display the map in Streamlit
        st_folium(m, width=700, height=500)

        # Clean up the downloaded file after use
        os.remove(local_file_path)

else:
    st.warning("Please provide a KML or GeoJSON URL as a query parameter, e.g., `?file_url=<your_file_url>`")