feat: 楼宇查询连接信息本地保存并默认 GBK 编码

This commit is contained in:
nixevol
2026-09-17 09:32:34 +08:00
parent 48e68469ad
commit bbff3f7b5f
5 changed files with 45 additions and 17 deletions
+33 -9
View File
@@ -1,23 +1,45 @@
import json
import streamlit as st
from toolbox.tasks import start_task
from toolbox.tasks import ROOT, start_task
from .inputs import file_input
from .task_panel import show_started, task_panel
# 上次填写的服务地址/登录信息保存在本地 output 目录(已被 .gitignore 忽略),下次自动读取。
CONNECTION_FILE = ROOT / "output" / "toolbox" / "nr_connection.json"
CONNECTION_KEYS = ("URL", "COOKIES", "REFERER")
def load_connection():
try:
data = json.loads(CONNECTION_FILE.read_text(encoding="utf-8"))
except (OSError, ValueError):
return {key: "" for key in CONNECTION_KEYS}
return {key: str(data.get(key, "") or "") for key in CONNECTION_KEYS}
def save_connection(values):
if values is None:
CONNECTION_FILE.unlink(missing_ok=True)
return
CONNECTION_FILE.parent.mkdir(parents=True, exist_ok=True)
CONNECTION_FILE.write_text(json.dumps(values, ensure_ascii=False, indent=2), encoding="utf-8")
def render():
st.markdown("## 楼宇数据查询")
st.caption("按楼宇轮廓(WKT)查询:先计算落在轮廓内部的点,再按该点查询。")
source = file_input("楼宇轮廓清单", "source/NRBuildPolygon.csv", "csv", "building_csv")
saved = load_connection()
with st.form("building_run"):
c1, c2, c3 = st.columns(3)
start = c1.number_input("起始行", 1, value=1)
limit = c2.number_input("查询数量(0 表示全部)", 0, value=1000)
concurrency = c3.number_input("并发数", 1, 32, 8)
encoding = st.selectbox("文件编码", ["utf-8-sig", "gb18030"])
st.markdown("**连接信息**(留空则使用脚本内默认配置)")
url = st.text_input("服务地址", type="password", placeholder="https://...wfs?...ak=...&sig=...")
cookies = st.text_input("登录信息", type="password", placeholder="CASTGC=...")
referer = st.text_input("来源页(可留空)")
encoding = st.selectbox("文件编码", ["gbk", "utf-8-sig"])
st.markdown("**连接信息**(填写后保存到本地,下次自动读取;留空则使用脚本内默认配置)")
url = st.text_input("服务地址", value=saved["URL"], placeholder="https://...wfs?...ak=...&sig=...")
cookies = st.text_input("登录信息(Cookies)", value=saved["COOKIES"], placeholder="CASTGC=...")
referer = st.text_input("来源页(可留空)", value=saved["REFERER"])
with st.expander("高级参数"):
timeout = st.number_input("超时(秒)", 1, 300, 30)
retries = st.number_input("失败重试", 0, 10, 2)
@@ -27,13 +49,15 @@ def render():
try:
if not source:
raise ValueError("请先导入楼宇轮廓文件。")
override = bool(url.strip() or cookies.strip())
if override and (not url.strip().startswith(("http://", "https://")) or not cookies.strip()):
url, cookies, referer = url.strip(), cookies.strip(), referer.strip()
override = bool(url or cookies)
if override and (not url.startswith(("http://", "https://")) or not cookies):
raise ValueError("服务地址和登录信息需同时填写,且地址以 http(s):// 开头。")
params = {"INPUT_CSV": source, "INPUT_ENCODING": encoding, "START_ROW": start,
"LIMIT": limit or None, "CONCURRENCY": concurrency, "TIMEOUT": timeout,
"MAX_RETRIES": retries, "REQUEST_INTERVAL": interval}
credentials = {"URL": url.strip(), "COOKIES": cookies.strip(), "REFERER": referer.strip()} if override else None
credentials = {"URL": url, "COOKIES": cookies, "REFERER": referer} if override else None
save_connection(credentials)
show_started(start_task("buildings", params, credentials))
except (OSError, ValueError, UnicodeError) as error:
st.error(str(error))