| import streamlit as st |
| import folium |
| from streamlit_folium import st_folium |
| import gdown |
| import os |
| import json |
| import requests |
|
|
| |
| def download_from_gdrive(gdrive_url, output_path="temp_file"): |
| |
| 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 |
|
|
| |
| st.title("KML/GeoJSON Viewer with Satellite Basemap") |
|
|
| |
| query_params = st.experimental_get_query_params() |
| file_url = query_params.get("file_url", [None])[0] |
|
|
| if file_url: |
| |
| if "drive.google.com" in file_url: |
| |
| local_file_path = download_from_gdrive(file_url) |
| file_extension = local_file_path.split('.')[-1] |
| else: |
| |
| try: |
| response = requests.get(file_url) |
| response.raise_for_status() |
| file_extension = file_url.split('.')[-1] |
|
|
| |
| 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: |
| |
| m = folium.Map(tiles=None) |
|
|
| |
| 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) |
|
|
| |
| 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) |
|
|
| |
| 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 |
|
|
| |
| if local_file_path: |
| bounds = m.get_bounds() |
| m.fit_bounds(bounds) |
|
|
| |
| folium.LayerControl().add_to(m) |
|
|
| |
| st_folium(m, width=700, height=500) |
|
|
| |
| 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>`") |
|
|