From c6383dffdc53f59b79d879a6fedb9ca4cb27f927 Mon Sep 17 00:00:00 2001 From: RBP15P Date: Thu, 16 Jul 2026 09:48:13 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=B0=83=E6=95=B4=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E9=87=8F=E7=83=AD=E5=8A=9B=E5=9B=BE=E5=88=86=E4=BD=8D=E8=A7=84?= =?UTF-8?q?=E5=88=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- aidocs/project_context.md | 23 +++++------ main.py | 85 ++++++++++++++++++++++----------------- 2 files changed, 60 insertions(+), 48 deletions(-) diff --git a/aidocs/project_context.md b/aidocs/project_context.md index e024db5..c6896fa 100644 --- a/aidocs/project_context.md +++ b/aidocs/project_context.md @@ -29,24 +29,23 @@ - `summary.json` 新增 `heatmap_results`,当前测试输出含 1 条 tif、1 条 heatmap、1 条 build 结果。 ## 2026-07-15 -- 按当前测试需求,`main.py` 已收敛为仅生成真正的连续热力图,不再生成分级 tif,也不再拆分 `build.gpkg`。 -- 当前 `python main.py` 只读取 `source/MarketMap.gpkg` 和 `source/grid.gpkg`,按市场网格与 Grid 相交关系生成 `output/JMXXXX/ssb_rsrp_dx_heatmap.tif`。 -- 输出目录已清理为热力图测试结构:当前仅保留 `output/JM010404/ssb_rsrp_dx_heatmap.tif`、`output/heatmap_legend.csv` 和 `output/summary.json`。 +- 当前 `main.py` 保留真正的连续热力图流程,不再生成分级 tif,也不再拆分旧的 `build.gpkg`。 +- 当前 `python main.py` 会读取 `source/MarketMap.gpkg`、`source/grid.gpkg`、`source/BuildingInfo.gpkg` 和 `source/MapPark/GIS.qgz`,按市场网格生成 zip 结果包。 +- 输出目录已改为 zip 测试结构:当前市场结果写入 `output/JM010404.zip`,全局保留图例 CSV 和 `output/summary.json`。 - `LIMIT = 1` 仍保留为测试阶段配置,后续确认字段和效果后再改回 `None` 全量生成。 - 热力图红色端已改回纯红 `(255, 0, 0)`,有效像元统一使用 `HEATMAP_ALPHA = 191`(约 75% 不透明度),无数据区域仍为 alpha=0;当前测试配置为 `SCALE = 5.0`、`HEATMAP_SMOOTH_RADIUS = 10`、`SAMPLE_SIZE = 10000`。 -- 新增 `mr_dl_excellent_cell_dl` 采样点数量连续热力图,输出为 `output/JMXXXX/mr_dl_excellent_cell_dl_excellent_heatmap.tif`。 -- 采样点数量热力图按每个市场网格内相交 Grid 的字段值去重后降序排列,使用 `EXCELLENT_PERCENT_STEPS = (0.2, 0.4, 0.6)` 计算连续渐变控制阈值。 +- `mro_total_dl` 用户量连续热力图输出为 `用户量.tif`。 +- 用户量和信号差用户量热力图按每个市场网格内相交 Grid 的所有字段值直接降序排列,不再去重;当前使用 `EXCELLENT_PERCENT_STEPS = (0.2, 0.4, 0.6, 0.8)` 计算连续渐变控制阈值。 - 采样点数量热力图会先平滑字段值,再按 `EXCELLENT_COLORS` 在红、黄、蓝、绿之间连续插值;图例写入 `output/excellent_heatmap_legend.csv`,每个市场的实际阈值写入 `summary.json` 的 `excellent_heatmap_results`。 -- 当前测试市场 `JM010404` 的 `mr_dl_excellent_cell_dl` 去重分位阈值为 `[1204.0, 749.0, 471.0]`,输出验证为 886 种有效颜色加透明背景。 -- 输出文件名已改为业务中文名:RSRP 连续热力图输出 `信号.tif`,`mr_dl_excellent_cell_dl` 用户量热力图输出 `用户量.tif`;文件名由 `SIGNAL_OUTPUT_FILENAME` 和 `USER_COUNT_OUTPUT_FILENAME` 配置。 -- 新增 `mr_dl_lt105_weak_cover_rate_dl` 信号差用户量连续热力图,输出为 `output/JMXXXX/信号差用户量.tif`,复用用户量热力图的去重分位阈值、红黄蓝绿连续渐变、透明度和平滑规则。 -- 当前测试市场 `JM010404` 的 `mr_dl_lt105_weak_cover_rate_dl` 去重分位阈值为 `[30.43, 16.95, 9.47]`,输出验证为 889 种有效颜色加透明背景。 +- 当前测试市场 `JM010404` 的 `mro_total_dl` 不去重分位阈值为 `[456.0, 138.0, 30.0, 3.0]`,阈值样本数为 17883。 +- 输出文件名已改为业务中文名:RSRP 连续热力图输出 `信号.tif`,`mro_total_dl` 用户量热力图输出 `用户量.tif`;文件名由 `SIGNAL_OUTPUT_FILENAME` 和 `USER_COUNT_OUTPUT_FILENAME` 配置。 +- `rsrp_lt_neg105_dl` 信号差用户量连续热力图输出为 `信号差用户量.tif`,复用用户量热力图的不去重分位阈值、红黄蓝绿连续渐变、透明度和平滑规则。 +- 当前测试市场 `JM010404` 的 `rsrp_lt_neg105_dl` 不去重分位阈值为 `[10.0, 2.0, 0.0, 0.0]`,阈值样本数为 17883。 - 字段口径已切换为 DL:`信号.tif` 使用 `ssb_rsrp_dl`,`用户量.tif` 使用 `mro_total_dl`,`信号差用户量.tif` 使用 `rsrp_lt_neg105_dl`。 -- 当前测试市场 `JM010404` 新字段阈值:`mro_total_dl` 为 `[2583.0, 1428.0, 871.0]`,`rsrp_lt_neg105_dl` 为 `[389.0, 219.0, 138.0]`。 - `main.py` 已重新纳入 `source/BuildingInfo.gpkg` 拆分流程,仍通过 `python main.py` 一次运行完成三张热力图和 BuildingInfo 拆分。 -- BuildingInfo 输出路径为 `output/JMXXXX/BuildingInfo.gpkg`,匹配规则沿用 `intersects`:建筑面只要与市场网格相交或边界触碰,就会写入对应市场目录,几何和属性行保持原样不裁剪。 +- BuildingInfo 在结果包内路径为 `output/JMXXXX.zip/BuildingInfo.gpkg`,匹配规则沿用 `intersects`:建筑面只要与市场网格相交或边界触碰,就会写入对应市场结果,几何和属性行保持原样不裁剪。 - BuildingInfo 拆分会复制源 GeoPackage 的 `layer_styles`、`gpkg_metadata`、`gpkg_metadata_reference`、`gpkg_extensions`、`gpkg_ogr_contents`、`gpkg_spatial_ref_sys` 等支撑表,并重建输出 RTree;避免复制依赖 SpatiaLite 函数的 RTree 触发器。 - `source/BuildingInfo.gpkg` 的图层 `srs_id` 为 `100000`(CRS84,经纬度坐标值可与市场网格相交判断),因此 `BUILDING_INFO_ALLOWED_SRS_IDS` 默认允许 `(4326, 100000)`。 - 当前测试市场 `JM010404` 已生成 `BuildingInfo.gpkg`,包含 9495 个建筑要素,RTree 和 `gpkg_ogr_contents.feature_count` 均为 9495;`layer_styles.styleQML` 与 `gpkg_metadata.metadata` 已验证与源文件完全一致。 -- 每个市场网格输出目录会额外复制 `source/MapPark/GIS.qgz` 为 `GIS.qgz`,复制过程不修改 QGIS 项目文件;当前测试市场 `JM010404` 已通过 SHA256 对比确认输出文件与源文件一致。 +- 每个市场网格结果包会额外复制 `source/MapPark/GIS.qgz` 为 `GIS.qgz`,复制过程不修改 QGIS 项目文件;当前测试市场 `JM010404` 已通过 SHA256 对比确认输出文件与源文件一致。 - 市场网格结果改为 zip 包输出:`python main.py` 会先在系统临时目录生成结果,再写入 `output/JMXXXX.zip`,zip 根目录直接包含 `GIS.qgz`、`信号.tif`、`用户量.tif`、`信号差用户量.tif`、`BuildingInfo.gpkg`,不再保留 `output/JMXXXX/` 文件夹。 diff --git a/main.py b/main.py index 6fa1d10..8ece9d5 100644 --- a/main.py +++ b/main.py @@ -41,7 +41,7 @@ HEATMAP_SMOOTH_RADIUS = 10 # 热力图平滑半径,数值越大边界感越 HEATMAP_ALPHA = 191 # 热力图有效像元透明度;191 约等于 75% 不透明度。 USER_COUNT_OUTPUT_FILENAME = "用户量.tif" # 采样点数量热力图输出文件名。 WEAK_COVER_OUTPUT_FILENAME = "信号差用户量.tif" # 信号差用户量热力图输出文件名。 -EXCELLENT_PERCENT_STEPS = (0.2, 0.4, 0.6) # 去重值降序分位阈值,默认按前 20%、40%、60% 作为连续渐变控制点。 +EXCELLENT_PERCENT_STEPS = (0.2, 0.4, 0.6, 0.8) # 不去重,按所有匹配值降序取前 20%、40%、60%、80% 作为连续渐变控制点。 EXCELLENT_COLORS = ( (255, 0, 0), # 高值端,红色。 (255, 255, 0), # 偏高值,黄色。 @@ -518,45 +518,58 @@ def heatmap_rgb_for_values(values): return np.clip(result, 0, 255).astype(np.uint8) -def percentile_thresholds_from_unique(values, percent_steps): - unique_values = sorted({value for value in values if value is not None}, reverse=True) - if not unique_values: +def percentile_thresholds_from_values(values, percent_steps): + sorted_values = sorted((value for value in values if value is not None), reverse=True) + if not sorted_values: return [] thresholds = [] for percent in percent_steps: - index = max(0, math.ceil(len(unique_values) * percent) - 1) - index = min(index, len(unique_values) - 1) - thresholds.append(unique_values[index]) + index = max(0, math.ceil(len(sorted_values) * percent) - 1) + index = min(index, len(sorted_values) - 1) + thresholds.append(sorted_values[index]) return thresholds +def palette_colors_for_steps(step_count): + if step_count <= 0: + return [] + if step_count == 1: + return [EXCELLENT_COLORS[0]] + + colors = [] + palette_max = len(EXCELLENT_COLORS) - 1 + for index in range(step_count): + position = index / (step_count - 1) + scaled = position * palette_max + start_index = int(math.floor(scaled)) + end_index = min(start_index + 1, palette_max) + ratio = scaled - start_index + color = interpolate_color(EXCELLENT_COLORS[start_index], EXCELLENT_COLORS[end_index], ratio) + colors.append(tuple(int(round(channel)) for channel in color)) + return colors + + def excellent_rgb_for_values(values, thresholds): result = np.zeros((*values.shape, 3), dtype=np.float32) - if len(thresholds) < 3: + if not thresholds: result[:] = EXCELLENT_COLORS[-1] return result.astype(np.uint8) - red_threshold, yellow_threshold, blue_threshold = thresholds[:3] - red, yellow, blue, green = EXCELLENT_COLORS - - high = values >= red_threshold - high_middle = (values < red_threshold) & (values >= yellow_threshold) - low_middle = (values < yellow_threshold) & (values >= blue_threshold) - low = values < blue_threshold + control_colors = palette_colors_for_steps(len(thresholds)) + result[:] = control_colors[-1] + high = values >= thresholds[0] if np.any(high): - result[high] = red - if np.any(high_middle): - ratio = (values[high_middle] - yellow_threshold) / max(red_threshold - yellow_threshold, 1e-9) - result[high_middle] = interpolate_color(yellow, red, ratio) - if np.any(low_middle): - ratio = (values[low_middle] - blue_threshold) / max(yellow_threshold - blue_threshold, 1e-9) - result[low_middle] = interpolate_color(blue, yellow, ratio) - if np.any(low): - min_value = float(np.min(values[low])) if np.any(low) else blue_threshold - ratio = (values[low] - min_value) / max(blue_threshold - min_value, 1e-9) - result[low] = interpolate_color(green, blue, ratio) + result[high] = control_colors[0] + + for index in range(1, len(thresholds)): + upper_threshold = thresholds[index - 1] + lower_threshold = thresholds[index] + between = (values < upper_threshold) & (values >= lower_threshold) + if np.any(between): + ratio = (values[between] - lower_threshold) / max(upper_threshold - lower_threshold, 1e-9) + result[between] = interpolate_color(control_colors[index], control_colors[index - 1], ratio) return np.clip(result, 0, 255).astype(np.uint8) @@ -858,7 +871,7 @@ def export_market_excellent_heatmap_tif( value_weight[row_start:row_stop, col_start:col_stop] += 1.0 matched_values.append(value) - thresholds = percentile_thresholds_from_unique(matched_values, EXCELLENT_PERCENT_STEPS) + thresholds = percentile_thresholds_from_values(matched_values, EXCELLENT_PERCENT_STEPS) data_mask = value_weight > 0 if np.any(data_mask): smoothed_sum = gaussian_blur(value_sum, HEATMAP_SMOOTH_RADIUS) @@ -877,7 +890,7 @@ def export_market_excellent_heatmap_tif( "painted_cells": len(matched_values), "data_pixels": int(np.count_nonzero(image[..., 3] > 0)), "skipped_no_value": skipped_no_value, - "unique_value_count": len(set(matched_values)), + "threshold_value_count": len(matched_values), "thresholds": thresholds, "percent_steps": list(EXCELLENT_PERCENT_STEPS), "width": width, @@ -902,10 +915,10 @@ def write_excellent_heatmap_legend(output_dir, field): with legend_path.open("w", newline="", encoding="utf-8-sig") as file: writer = csv.writer(file) writer.writerow(["field", "control_point", "red", "green", "blue", "alpha", "meaning"]) - writer.writerow([field, "threshold_20_percent", *EXCELLENT_COLORS[0], HEATMAP_ALPHA, "high end"]) - writer.writerow([field, "threshold_40_percent", *EXCELLENT_COLORS[1], HEATMAP_ALPHA, "high-middle control"]) - writer.writerow([field, "threshold_60_percent", *EXCELLENT_COLORS[2], HEATMAP_ALPHA, "low-middle control"]) - writer.writerow([field, "minimum", *EXCELLENT_COLORS[3], HEATMAP_ALPHA, "low end"]) + for percent, color in zip(EXCELLENT_PERCENT_STEPS, palette_colors_for_steps(len(EXCELLENT_PERCENT_STEPS))): + writer.writerow( + [field, f"threshold_{int(round(percent * 100))}_percent", *color, HEATMAP_ALPHA, "sorted-value control"] + ) writer.writerow([field, "nodata", 0, 0, 0, 0, "transparent no data"]) return legend_path @@ -915,10 +928,10 @@ def write_weak_cover_heatmap_legend(output_dir, field): with legend_path.open("w", newline="", encoding="utf-8-sig") as file: writer = csv.writer(file) writer.writerow(["field", "control_point", "red", "green", "blue", "alpha", "meaning"]) - writer.writerow([field, "threshold_20_percent", *EXCELLENT_COLORS[0], HEATMAP_ALPHA, "high end"]) - writer.writerow([field, "threshold_40_percent", *EXCELLENT_COLORS[1], HEATMAP_ALPHA, "high-middle control"]) - writer.writerow([field, "threshold_60_percent", *EXCELLENT_COLORS[2], HEATMAP_ALPHA, "low-middle control"]) - writer.writerow([field, "minimum", *EXCELLENT_COLORS[3], HEATMAP_ALPHA, "low end"]) + for percent, color in zip(EXCELLENT_PERCENT_STEPS, palette_colors_for_steps(len(EXCELLENT_PERCENT_STEPS))): + writer.writerow( + [field, f"threshold_{int(round(percent * 100))}_percent", *color, HEATMAP_ALPHA, "sorted-value control"] + ) writer.writerow([field, "nodata", 0, 0, 0, 0, "transparent no data"]) return legend_path