import os
from datetime import datetime
import ee
import json
import geemap
import numpy as np
import geemap.foliumap as gee_folium
import leafmap.foliumap as leaf_folium
import streamlit as st
import pandas as pd
import geopandas as gpd
from shapely.ops import transform
from functools import reduce
import plotly.express as px
import branca.colormap as cm
import folium
import pyproj
from io import StringIO, BytesIO
import requests
import kml2geojson
import base64
def force_stop():
show_visitor_counter('counter.txt')
show_credits()
st.stop()
def one_time_setup():
credentials_path = os.path.expanduser("~/.config/earthengine/credentials")
if os.path.exists(credentials_path):
pass # Earth Engine credentials already exist
elif "EE" in os.environ: # write the credentials to the file
ee_credentials = os.environ.get("EE")
os.makedirs(os.path.dirname(credentials_path), exist_ok=True)
with open(credentials_path, "w") as f:
f.write(ee_credentials)
else:
raise ValueError(
f"Earth Engine credentials not found at {credentials_path} or in the environment variable 'EE'"
)
ee.Initialize()
def show_credits():
# Add credits
pdf_path = "User Manual - Kamlan App (PERG v1.0)n.pdf"
with open(pdf_path, "rb") as f:
pdf_bytes = f.read()
b64_pdf = base64.b64encode(pdf_bytes).decode()
st.markdown(f"""
""",
unsafe_allow_html=True,
)
def get_gdf_from_file_url(file_url):
if isinstance(file_url, str):
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}"
elif file_url.startswith("https://drive.google.com/open?id="):
ID = file_url.replace("https://drive.google.com/open?id=", "")
file_url = f"https://drive.google.com/uc?id={ID}"
response = requests.get(file_url)
bytes_data = BytesIO(response.content)
string_data = response.text
else:
bytes_data = BytesIO(file_url.getvalue())
string_data = file_url.getvalue().decode("utf-8")
if string_data.startswith("{info}", unsafe_allow_html=True)
else:
st.write(f"{info}", unsafe_allow_html=True)
def read_counter(counter_file):
if not os.path.exists(counter_file):
with open(counter_file, "w") as f:
f.write("0")
with open(counter_file, "r") as f:
content = f.read().strip()
return int(content) if content.isdigit() else 0
def write_counter(counter_file, count):
with open(counter_file, "w") as f:
f.write(str(count))
def show_visitor_counter(counter_file="counter.txt"):
if "visitor_counted" not in st.session_state:
count = read_counter(counter_file) + 1
write_counter(counter_file, count)
st.session_state.visitor_counted = True
else:
count = read_counter(counter_file)
count_str = str(count).zfill(6)
st.markdown(
f"""
Visitor Counter
{''.join([
f'
{digit}
'
for digit in count_str
])}
""",
unsafe_allow_html=True,
)