diff --git a/GetNRBuildingsData.py b/GetNRBuildingsData.py index 09a805d..60a0037 100644 --- a/GetNRBuildingsData.py +++ b/GetNRBuildingsData.py @@ -24,7 +24,7 @@ REFERER = "https://nqi.gmcc.net:20443/pro-problem-warehouse-web/" INPUT_CSV = Path(__file__).resolve().parent / "source" / "NRBuildingsPoint.csv" OUTPUT_CSV = Path(__file__).resolve().parent / "output" / "NRBuildingsData.csv" INPUT_ENCODING = "gb18030" # 当前源文件为 GBK;UTF-8 文件可改为 utf-8-sig。 -OUTPUT_ENCODING = "utf-8-sig" # 带 BOM,方便 Excel 识别中文。 +OUTPUT_ENCODING = "gbk" # 供 Excel/传统工具直接打开;无法编码的字符用 errors=replace。 ID_COLUMN = "序号" LONGITUDE_COLUMN = "经度" LATITUDE_COLUMN = "维度" # 源文件实际写的是“维度”。 @@ -122,7 +122,7 @@ class StreamingCsvWriter: def __init__(self, output_path): self.path = output_path self.fields = list(QUERY_COLUMNS) - self.file = self.path.open("w", encoding=OUTPUT_ENCODING, newline="") + self.file = self.path.open("w", encoding=OUTPUT_ENCODING, newline="", errors="replace") try: self.writer = csv.DictWriter(self.file, fieldnames=self.fields) self.writer.writeheader() @@ -137,16 +137,17 @@ class StreamingCsvWriter: try: # 后出现的字段需补到旧行;独占写线程执行原子替换,其他协程只入队。 with tempfile.NamedTemporaryFile("w", encoding=OUTPUT_ENCODING, newline="", delete=False, - dir=self.path.parent, prefix=f".{self.path.name}.", suffix=".tmp") as target: + dir=self.path.parent, prefix=f".{self.path.name}.", suffix=".tmp", + errors="replace") as target: temporary = Path(target.name) writer = csv.DictWriter(target, fieldnames=fields) writer.writeheader() - with self.path.open("r", encoding=OUTPUT_ENCODING, newline="") as source: + with self.path.open("r", encoding=OUTPUT_ENCODING, newline="", errors="replace") as source: writer.writerows(csv.DictReader(source)) self.file.close() temporary.replace(self.path) self.fields = fields - self.file = self.path.open("a", encoding=OUTPUT_ENCODING, newline="") + self.file = self.path.open("a", encoding=OUTPUT_ENCODING, newline="", errors="replace") self.writer = csv.DictWriter(self.file, fieldnames=self.fields) finally: if temporary is not None: diff --git a/README.md b/README.md index 940e162..5986603 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,7 @@ AOI 最大重叠面积匹配的预处理不在当前仓库内,界面直接使 1. 在脚本顶部填写完整 WFS `URL`(含 `ak`、`sig` 等原参数)和 `COOKIES`。 2. 默认读取 `source/NRBuildingsPoint.csv`,使用 `序号`、`经度`、`维度` 三列,输入编码为 GBK/GB18030。 -3. 运行 `python GetNRBuildingsData.py`,当前 `LIMIT = 1000`,输出 `output/NRBuildingsData.csv`。将 `LIMIT` 改为 `None` 可查询剩余全部;`START_ROW` 可指定起始数据行。 +3. 运行 `python GetNRBuildingsData.py`,当前 `LIMIT = 1000`,输出 `output/NRBuildingsData.csv`(GBK 编码)。将 `LIMIT` 改为 `None` 可查询剩余全部;`START_ROW` 可指定起始数据行。 脚本自动把 URL 中的 `CQL_FILTER` 替换成 `POINT(经度 纬度)` 点查询,坐标之间使用空格。输出表头包含查询坐标、状态以及所有返回 `properties` 字段;一个坐标命中多个楼宇时输出多行,未命中或失败时保留状态行。脚本顶部保存现有本地连接配置;界面可覆盖本次连接参数,覆盖值不写入任务配置或日志。 diff --git a/tests/test_nr_buildings.py b/tests/test_nr_buildings.py index 3dfb4af..96a1c45 100644 --- a/tests/test_nr_buildings.py +++ b/tests/test_nr_buildings.py @@ -31,7 +31,7 @@ class AsyncBuildingsTest(unittest.IsolatedAsyncioTestCase): self.addCleanup(settings.stop) def read_rows(self): - with self.output.open(encoding="utf-8-sig", newline="") as source: + with self.output.open(encoding="gbk", newline="", errors="replace") as source: reader = csv.DictReader(source) return reader.fieldnames, list(reader) diff --git a/tests/test_toolbox.py b/tests/test_toolbox.py index 37f6ddc..f22cab4 100644 --- a/tests/test_toolbox.py +++ b/tests/test_toolbox.py @@ -95,7 +95,7 @@ class TaskIntegrationTest(unittest.TestCase): state = self.wait_for(task_id, lambda s: s["status"] not in tasks.ACTIVE) self.assertEqual(state["status"], "success", state) self.assertEqual(state["counts"]["查询坐标"], 3) - with (Path(state["output"]) / "NRBuildingsData.csv").open(encoding="utf-8-sig") as source: + with (Path(state["output"]) / "NRBuildingsData.csv").open(encoding="gbk", errors="replace") as source: self.assertEqual(len(list(csv.DictReader(source))), 3) history = (tasks.TASKS / task_id / "state.json").read_text(encoding="utf-8") + tasks.tail_log(tasks.TASKS / task_id) self.assertNotIn(credentials["COOKIES"], history) @@ -111,7 +111,7 @@ class TaskIntegrationTest(unittest.TestCase): self.assertEqual(state["status"], "cancelled", state) self.assertLess(state["done"], 30) self.assertGreater(state["done"], 0) - with (Path(state["output"]) / "NRBuildingsData.csv").open(encoding="utf-8-sig") as source: + with (Path(state["output"]) / "NRBuildingsData.csv").open(encoding="gbk", errors="replace") as source: self.assertEqual(len(list(csv.DictReader(source))), state["done"]) def test_authentication_failure_is_not_success(self): diff --git a/toolbox/artifacts.py b/toolbox/artifacts.py index 0d98ff7..6ccc356 100644 --- a/toolbox/artifacts.py +++ b/toolbox/artifacts.py @@ -15,8 +15,8 @@ import tifffile from marketgrid import gpkg -def csv_preview(path, encoding="utf-8-sig", limit=500): - with Path(path).open(encoding=encoding, newline="") as source: +def csv_preview(path, encoding="gbk", limit=500): + with Path(path).open(encoding=encoding, newline="", errors="replace") as source: reader = csv.DictReader(source) rows = [] for index, row in enumerate(reader): diff --git a/ui/market_page.py b/ui/market_page.py index a29b4c9..772534b 100644 --- a/ui/market_page.py +++ b/ui/market_page.py @@ -23,15 +23,20 @@ def render(): except (OSError, sqlite3.Error, ValueError) as error: st.warning(f"市场边界不可用:{error}") st.markdown("#### 2 · 选择市场与参数") - # 市场勾选必须放在 form 外,否则取消“全部”后多选框不会立刻解除 disabled。 - all_markets = st.checkbox("处理全部市场", value=True, disabled=not options) - labels = {fid: f"{fid} · {name}" for fid, name in options} - chosen = st.multiselect( - "指定市场", - list(labels), - format_func=labels.get, - disabled=all_markets or not options, - ) + # 勾选交互放在 form 外;用复选框列表代替 multiselect,避免下拉被页面样式挡住。 + all_markets = st.checkbox("处理全部市场", value=True, disabled=not options, key="market_all") + chosen = [] + if options and not all_markets: + st.caption("指定市场") + with st.container(height=280, border=True): + for fid, name in options: + if st.checkbox(f"{fid} · {name}", key=f"market_pick_{fid}"): + chosen.append(fid) + elif options: + st.caption(f"将处理全部 {len(options)} 个市场") + else: + st.caption("请先导入可用的市场边界。") + with st.form("market_run"): c1, c2, c3 = st.columns(3) params["MAX_WORKERS"] = c1.number_input("并行进程数", 1, 16, 4) @@ -48,7 +53,7 @@ def render(): params["PROJECT_EXTENT_PADDING_RATIO"] = st.number_input("地图留白比例", 0.0, 1.0, 0.05, 0.01) submitted = st.form_submit_button("开始生成", type="primary", disabled=not options) if submitted: - params["SELECTED_MARKET_IDS"] = list(labels) if all_markets else chosen + params["SELECTED_MARKET_IDS"] = [fid for fid, _ in options] if all_markets else chosen try: if any(value is None for key, value in params.items() if key in SOURCES and (key != "CELL_DATA_GPKG" or params["SUMMARY_OUTPUT_ENABLED"])): raise ValueError("请完成所需数据的导入。")