diff --git a/aidocs/project_context.md b/aidocs/project_context.md index 843e480..46850f7 100644 --- a/aidocs/project_context.md +++ b/aidocs/project_context.md @@ -34,7 +34,7 @@ - 输出目录已清理为热力图测试结构:当前仅保留 `output/JM010404/ssb_rsrp_dx_heatmap.tif`、`output/heatmap_legend.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)` 计算阈值;默认规则为 `>20%阈值` 红、`>40%阈值` 黄、`>60%阈值` 蓝、其余绿。 -- 采样点数量热力图颜色由 `EXCELLENT_COLORS` 配置,图例写入 `output/excellent_heatmap_legend.csv`,每个市场的实际阈值写入 `summary.json` 的 `excellent_heatmap_results`。 -- 当前测试市场 `JM010404` 的 `mr_dl_excellent_cell_dl` 去重分位阈值为 `[1204.0, 749.0, 471.0]`,输出验证为四种有效颜色加透明背景。 +- 新增 `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)` 计算连续渐变控制阈值。 +- 采样点数量热力图会先平滑字段值,再按 `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 种有效颜色加透明背景。 diff --git a/main.py b/main.py index fc74316..bdcc026 100644 --- a/main.py +++ b/main.py @@ -30,12 +30,12 @@ HEATMAP_BAD_RSRP = -110.0 # 热力图红色端阈值;RSRP 小于等于该值 HEATMAP_SMOOTH_RADIUS = 10 # 热力图平滑半径,数值越大边界感越弱但细节越柔。 HEATMAP_ALPHA = 191 # 热力图有效像元透明度;191 约等于 75% 不透明度。 EXCELLENT_HEATMAP_OUTPUT_SUFFIX = "_excellent_heatmap" # 采样点数量热力图文件名后缀。 -EXCELLENT_PERCENT_STEPS = (0.2, 0.4, 0.6) # 去重值降序分位阈值,默认按前 20%、40%、60% 分四档。 +EXCELLENT_PERCENT_STEPS = (0.2, 0.4, 0.6) # 去重值降序分位阈值,默认按前 20%、40%、60% 作为连续渐变控制点。 EXCELLENT_COLORS = ( - (255, 0, 0), # 高于前 20% 阈值,红色。 - (255, 255, 0), # 高于前 40% 阈值,黄色。 - (0, 112, 255), # 高于前 60% 阈值,蓝色。 - (0, 176, 80), # 其余较低值,绿色。 + (255, 0, 0), # 高值端,红色。 + (255, 255, 0), # 偏高值,黄色。 + (0, 112, 255), # 偏低值,蓝色。 + (0, 176, 80), # 低值端,绿色。 ) PIXEL_WIDTH = None # 输出像元宽度;None 表示自动按 Grid 单元宽度估算。 PIXEL_HEIGHT = None # 输出像元高度;None 表示自动按 Grid 单元高度估算。 @@ -238,14 +238,34 @@ def percentile_thresholds_from_unique(values, percent_steps): return thresholds -def color_for_excellent_value(value, thresholds): - if len(thresholds) >= 1 and value > thresholds[0]: - return (*EXCELLENT_COLORS[0], HEATMAP_ALPHA) - if len(thresholds) >= 2 and value > thresholds[1]: - return (*EXCELLENT_COLORS[1], HEATMAP_ALPHA) - if len(thresholds) >= 3 and value > thresholds[2]: - return (*EXCELLENT_COLORS[2], HEATMAP_ALPHA) - return (*EXCELLENT_COLORS[3], HEATMAP_ALPHA) +def excellent_rgb_for_values(values, thresholds): + result = np.zeros((*values.shape, 3), dtype=np.float32) + if len(thresholds) < 3: + 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 + + 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) + + return np.clip(result, 0, 255).astype(np.uint8) def gaussian_kernel(radius): @@ -465,6 +485,8 @@ def export_market_excellent_heatmap_tif( ): image, minx, maxy = create_canvas(market.geometry.bounds, pixel_width, pixel_height) height, width = image.shape[:2] + value_sum = np.zeros((height, width), dtype=np.float32) + value_weight = np.zeros((height, width), dtype=np.float32) prepared_market = prep(market.geometry) table = quote_identifier(grid_layer.table_name) geom = quote_identifier(grid_layer.geom_col) @@ -487,7 +509,6 @@ def export_market_excellent_heatmap_tif( params = (max_market_x, min_market_x, max_market_y, min_market_y) candidates = 0 skipped_no_value = 0 - matched_cells = [] matched_values = [] for _fid, geom_blob, raw_value, raw_lon, raw_lat in grid_conn.execute(query, params): @@ -539,13 +560,20 @@ def export_market_excellent_heatmap_tif( if window is None: continue - matched_cells.append((window, value)) + row_start, row_stop, col_start, col_stop = window + value_sum[row_start:row_stop, col_start:col_stop] += value + 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) - for window, value in matched_cells: - row_start, row_stop, col_start, col_stop = window - image[row_start:row_stop, col_start:col_stop] = color_for_excellent_value(value, thresholds) + data_mask = value_weight > 0 + if np.any(data_mask): + smoothed_sum = gaussian_blur(value_sum, HEATMAP_SMOOTH_RADIUS) + smoothed_weight = gaussian_blur(value_weight, HEATMAP_SMOOTH_RADIUS) + smoothed_value = np.zeros_like(value_sum) + np.divide(smoothed_sum, smoothed_weight, out=smoothed_value, where=smoothed_weight > 0) + image[data_mask, :3] = excellent_rgb_for_values(smoothed_value, thresholds)[data_mask] + image[data_mask, 3] = HEATMAP_ALPHA write_rgba_geotiff(output_path, image, minx, maxy, pixel_width, pixel_height) return { @@ -553,7 +581,7 @@ def export_market_excellent_heatmap_tif( "market_name": market.name, "output": str(output_path), "candidates": candidates, - "painted_cells": len(matched_cells), + "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)), @@ -580,11 +608,11 @@ def write_excellent_heatmap_legend(output_dir, field): legend_path = output_dir / "excellent_heatmap_legend.csv" with legend_path.open("w", newline="", encoding="utf-8-sig") as file: writer = csv.writer(file) - writer.writerow(["field", "rule", "red", "green", "blue", "alpha", "meaning"]) - writer.writerow([field, "> threshold_20_percent", *EXCELLENT_COLORS[0], HEATMAP_ALPHA, "highest values"]) - writer.writerow([field, "> threshold_40_percent", *EXCELLENT_COLORS[1], HEATMAP_ALPHA, "high values"]) - writer.writerow([field, "> threshold_60_percent", *EXCELLENT_COLORS[2], HEATMAP_ALPHA, "middle values"]) - writer.writerow([field, "else", *EXCELLENT_COLORS[3], HEATMAP_ALPHA, "lower values"]) + 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"]) writer.writerow([field, "nodata", 0, 0, 0, 0, "transparent no data"]) return legend_path