feat: 将信号图改为连续热力图
This commit is contained in:
@@ -38,7 +38,7 @@ BUILDING_INFO_LAYER = None # BuildingInfo 建筑信息图层名;None 表示
|
||||
MARKET_CODE_FIELD = "fid" # 市场网格唯一 ID 前缀字段,新版 MarketMap 使用 fid。
|
||||
MARKET_NAME_FIELD = "Name" # 市场网格名称字段,新版 MarketMap 使用 Name。
|
||||
MARKET_ID_SEPARATOR = "-" # 市场网格唯一 ID 拼接符,输出为 fid-name,例如 1-城区蓬江。
|
||||
SIGNAL_OUTPUT_FILENAME = "信号.tif" # RSRP 三色信号图输出文件名。
|
||||
SIGNAL_OUTPUT_FILENAME = "信号.tif" # RSRP 连续红黄绿热力图输出文件名。
|
||||
BUILDING_INFO_OUTPUT_FILENAME = "BuildingInfo.gpkg" # 每个市场网格结果包内输出的建筑信息拆分文件名。
|
||||
BUILDING_METRICS_CSV_FILENAME = "楼宇指标.csv" # 每个市场网格结果包内输出的楼宇指标 CSV 文件名。
|
||||
GRID_METRICS_CSV_FILENAME = "栅格指标.csv" # 每个市场网格结果包内输出的栅格指标 CSV 文件名。
|
||||
@@ -49,6 +49,7 @@ SIGNAL_NORMAL_THRESHOLD = -95.0 # 信号等级“一般/正常”的分界,
|
||||
GIS_PROJECT_OUTPUT_FILENAME = "GIS.qgz" # 每个市场网格结果包内输出的 QGIS 项目文件名,画布范围按 BuildingInfo 自动更新。
|
||||
HEATMAP_MID_RSRP = -95.0 # 热力图黄色中间阈值。
|
||||
HEATMAP_BAD_RSRP = -105.0 # 热力图红色端阈值;RSRP 小于等于该值视为信号差。
|
||||
HEATMAP_GOOD_RSRP = -80.0 # 热力图绿色端阈值;RSRP 大于等于该值使用纯绿色。
|
||||
HEATMAP_SMOOTH_RADIUS = 10 # 热力图平滑半径,数值越大边界感越弱但细节越柔。
|
||||
HEATMAP_ALPHA = 191 # 热力图有效像元透明度;191 约等于 75% 不透明度。
|
||||
PROJECT_EXTENT_PADDING_RATIO = 0.05 # QGIS 打开后的画布留白比例;0.05 表示在 BuildingInfo 范围四周各增加 5%。
|
||||
@@ -61,12 +62,10 @@ EXCELLENT_COLORS = (
|
||||
(0, 112, 255), # 偏低值,蓝色。
|
||||
(0, 176, 80), # 低值端,绿色。
|
||||
)
|
||||
WEAK_COVER_PERCENT_STEPS = (1 / 3, 2 / 3) # 信号差用户量按最终有效像元降序三等分,取 1/3、2/3 位置作为结果记录阈值。
|
||||
WEAK_COVER_COLORS = (
|
||||
(255, 0, 0), # 高值端,红色。
|
||||
(255, 255, 0), # 中值,黄色。
|
||||
(0, 176, 80), # 低值端,绿色。
|
||||
)
|
||||
CONTINUOUS_HEATMAP_RED = (255, 0, 0) # 连续热力图高风险端颜色。
|
||||
CONTINUOUS_HEATMAP_YELLOW = (255, 255, 0) # 连续热力图中间颜色。
|
||||
CONTINUOUS_HEATMAP_GREEN = (0, 176, 80) # 连续热力图低风险端颜色。
|
||||
WEAK_COVER_YELLOW_PERCENTILE = 0.75 # 信号差用户量黄色锚点百分位;0.75 使红色集中在最高值约 25% 区域。
|
||||
PIXEL_WIDTH = None # 输出像元宽度;None 表示自动按 Grid 单元宽度估算。
|
||||
PIXEL_HEIGHT = None # 输出像元高度;None 表示自动按 Grid 单元高度估算。
|
||||
SCALE = 5.0 # 分辨率倍数;2.0 表示像元宽高减半,放大查看更清晰。
|
||||
@@ -844,15 +843,61 @@ def estimate_pixel_size(conn, layer, sample_size):
|
||||
return float(np.median(widths)), float(np.median(heights))
|
||||
|
||||
|
||||
def continuous_heatmap_rgb_for_scores(scores, yellow_score=0.5):
|
||||
if not 0 < yellow_score < 1:
|
||||
raise ValueError("yellow_score 必须大于 0 且小于 1")
|
||||
normalized = np.clip(np.asarray(scores, dtype=np.float32), 0.0, 1.0)
|
||||
result = np.empty((*normalized.shape, 3), dtype=np.float32)
|
||||
green = np.asarray(CONTINUOUS_HEATMAP_GREEN, dtype=np.float32)
|
||||
yellow = np.asarray(CONTINUOUS_HEATMAP_YELLOW, dtype=np.float32)
|
||||
red = np.asarray(CONTINUOUS_HEATMAP_RED, dtype=np.float32)
|
||||
|
||||
lower_mask = normalized <= yellow_score
|
||||
lower_weight = (normalized[lower_mask] / yellow_score)[..., None]
|
||||
result[lower_mask] = green + (yellow - green) * lower_weight
|
||||
|
||||
upper_mask = ~lower_mask
|
||||
upper_weight = ((normalized[upper_mask] - yellow_score) / (1.0 - yellow_score))[..., None]
|
||||
result[upper_mask] = yellow + (red - yellow) * upper_weight
|
||||
return np.rint(np.clip(result, 0, 255)).astype(np.uint8)
|
||||
|
||||
|
||||
def heatmap_rgb_for_values(values):
|
||||
red = (255, 0, 0)
|
||||
yellow = (255, 255, 0)
|
||||
green = (0, 176, 80)
|
||||
result = np.zeros((*values.shape, 3), dtype=np.uint8)
|
||||
result[values <= HEATMAP_BAD_RSRP] = red
|
||||
result[(values > HEATMAP_BAD_RSRP) & (values <= HEATMAP_MID_RSRP)] = yellow
|
||||
result[values > HEATMAP_MID_RSRP] = green
|
||||
return result
|
||||
values = np.asarray(values, dtype=np.float32)
|
||||
scores = np.empty(values.shape, dtype=np.float32)
|
||||
scores[values <= HEATMAP_BAD_RSRP] = 1.0
|
||||
|
||||
middle_mask = (values > HEATMAP_BAD_RSRP) & (values <= HEATMAP_MID_RSRP)
|
||||
scores[middle_mask] = 1.0 - 0.5 * (
|
||||
(values[middle_mask] - HEATMAP_BAD_RSRP) / (HEATMAP_MID_RSRP - HEATMAP_BAD_RSRP)
|
||||
)
|
||||
|
||||
good_mask = (values > HEATMAP_MID_RSRP) & (values < HEATMAP_GOOD_RSRP)
|
||||
scores[good_mask] = 0.5 * (
|
||||
(HEATMAP_GOOD_RSRP - values[good_mask]) / (HEATMAP_GOOD_RSRP - HEATMAP_MID_RSRP)
|
||||
)
|
||||
scores[values >= HEATMAP_GOOD_RSRP] = 0.0
|
||||
return continuous_heatmap_rgb_for_scores(scores)
|
||||
|
||||
|
||||
def percentile_scores_for_values(values, data_mask, anchor_scores=(0.0, 0.5, 1.0)):
|
||||
scores = np.zeros(values.shape, dtype=np.float32)
|
||||
valid_values = values[data_mask]
|
||||
if valid_values.size == 0:
|
||||
return scores, []
|
||||
|
||||
unique_values, inverse, counts = np.unique(valid_values, return_inverse=True, return_counts=True)
|
||||
if unique_values.size == 1:
|
||||
return scores, [float(unique_values[0])] * len(anchor_scores)
|
||||
|
||||
cumulative_before = np.cumsum(counts, dtype=np.int64) - counts
|
||||
rank_scores = cumulative_before.astype(np.float32) / float(cumulative_before[-1])
|
||||
scores[data_mask] = rank_scores[inverse]
|
||||
anchor_values = []
|
||||
for anchor_score in anchor_scores:
|
||||
index = min(int(np.searchsorted(rank_scores, anchor_score, side="left")), unique_values.size - 1)
|
||||
anchor_values.append(float(unique_values[index]))
|
||||
return scores, anchor_values
|
||||
|
||||
|
||||
def percentile_thresholds_from_values(values, percent_steps):
|
||||
@@ -1033,16 +1078,20 @@ def market_raster_layout(bounds, pixel_width, pixel_height):
|
||||
return minx, miny, maxx, maxy, pixel_width, pixel_height, width, height, scale_factor
|
||||
|
||||
|
||||
def render_signal_raster(state, market, output_path, minx, maxy, pixel_width, pixel_height, width, height, candidates):
|
||||
image = np.full((height, width, 4), TRANSPARENT_PIXEL, dtype=np.uint8)
|
||||
value_sum = state["value_sum"]
|
||||
value_weight = state["value_weight"]
|
||||
def smooth_weighted_values(value_sum, value_weight):
|
||||
data_mask = value_weight > 0
|
||||
smoothed_value = np.zeros_like(value_sum)
|
||||
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)
|
||||
return smoothed_value, data_mask
|
||||
|
||||
|
||||
def render_signal_raster(state, market, output_path, minx, maxy, pixel_width, pixel_height, width, height, candidates):
|
||||
image = np.full((height, width, 4), TRANSPARENT_PIXEL, dtype=np.uint8)
|
||||
smoothed_value, data_mask = smooth_weighted_values(state["value_sum"], state["value_weight"])
|
||||
if np.any(data_mask):
|
||||
image[data_mask, :3] = heatmap_rgb_for_values(smoothed_value)[data_mask]
|
||||
image[data_mask, 3] = HEATMAP_ALPHA
|
||||
|
||||
@@ -1055,6 +1104,59 @@ def render_signal_raster(state, market, output_path, minx, maxy, pixel_width, pi
|
||||
"painted_pixels": state["painted_cells"],
|
||||
"data_pixels": int(np.count_nonzero(data_mask)),
|
||||
"skipped_no_value": state["skipped_no_value"],
|
||||
"color_mode": "continuous_fixed_rsrp",
|
||||
"color_anchors": {
|
||||
"red": HEATMAP_BAD_RSRP,
|
||||
"yellow": HEATMAP_MID_RSRP,
|
||||
"green": HEATMAP_GOOD_RSRP,
|
||||
},
|
||||
"smooth_radius": HEATMAP_SMOOTH_RADIUS,
|
||||
"width": width,
|
||||
"height": height,
|
||||
"skipped": False,
|
||||
}
|
||||
|
||||
|
||||
def render_percentile_heatmap_raster(
|
||||
state,
|
||||
market,
|
||||
output_path,
|
||||
minx,
|
||||
maxy,
|
||||
pixel_width,
|
||||
pixel_height,
|
||||
width,
|
||||
height,
|
||||
candidates,
|
||||
):
|
||||
image = np.full((height, width, 4), TRANSPARENT_PIXEL, dtype=np.uint8)
|
||||
smoothed_value, data_mask = smooth_weighted_values(state["value_sum"], state["value_weight"])
|
||||
anchor_values = []
|
||||
if np.any(data_mask):
|
||||
anchor_scores = (0.0, WEAK_COVER_YELLOW_PERCENTILE, 1.0)
|
||||
scores, anchor_values = percentile_scores_for_values(smoothed_value, data_mask, anchor_scores)
|
||||
image[data_mask, :3] = continuous_heatmap_rgb_for_scores(
|
||||
scores,
|
||||
yellow_score=WEAK_COVER_YELLOW_PERCENTILE,
|
||||
)[data_mask]
|
||||
image[data_mask, 3] = HEATMAP_ALPHA
|
||||
|
||||
write_rgba_geotiff(output_path, image, minx, maxy, pixel_width, pixel_height)
|
||||
return {
|
||||
"market_code": market.code,
|
||||
"market_name": market.name,
|
||||
"output": str(output_path),
|
||||
"candidates": candidates,
|
||||
"painted_cells": state["painted_cells"],
|
||||
"data_pixels": int(np.count_nonzero(data_mask)),
|
||||
"skipped_no_value": state["skipped_no_value"],
|
||||
"color_mode": "continuous_percentile",
|
||||
"yellow_percentile": WEAK_COVER_YELLOW_PERCENTILE,
|
||||
"color_anchor_values": {
|
||||
"green": anchor_values[0],
|
||||
"yellow": anchor_values[1],
|
||||
"red": anchor_values[2],
|
||||
} if anchor_values else None,
|
||||
"smooth_radius": HEATMAP_SMOOTH_RADIUS,
|
||||
"width": width,
|
||||
"height": height,
|
||||
@@ -1264,7 +1366,7 @@ def export_market_grid_outputs(
|
||||
)
|
||||
del user_count_state
|
||||
weak_cover_state = build_state(WEAK_COVER_FIELD, 2)
|
||||
weak_cover_result = render_ranked_raster(
|
||||
weak_cover_result = render_percentile_heatmap_raster(
|
||||
weak_cover_state,
|
||||
market,
|
||||
weak_cover_path,
|
||||
@@ -1275,8 +1377,6 @@ def export_market_grid_outputs(
|
||||
width,
|
||||
height,
|
||||
candidates,
|
||||
WEAK_COVER_COLORS,
|
||||
WEAK_COVER_PERCENT_STEPS,
|
||||
)
|
||||
del weak_cover_state
|
||||
for result in (signal_result, user_count_result, weak_cover_result):
|
||||
@@ -1298,10 +1398,10 @@ def write_heatmap_legend(output_dir, field):
|
||||
legend_path = output_dir / "heatmap_legend.csv"
|
||||
with legend_path.open("w", newline="", encoding="utf-8-sig") as file:
|
||||
writer = csv.writer(file)
|
||||
writer.writerow(["field", "rsrp", "red", "green", "blue", "alpha", "meaning"])
|
||||
writer.writerow([field, f"> {HEATMAP_MID_RSRP}", 0, 176, 80, HEATMAP_ALPHA, "normal signal"])
|
||||
writer.writerow([field, f"({HEATMAP_BAD_RSRP}, {HEATMAP_MID_RSRP}]", 255, 255, 0, HEATMAP_ALPHA, "middle signal"])
|
||||
writer.writerow([field, f"<= {HEATMAP_BAD_RSRP}", 255, 0, 0, HEATMAP_ALPHA, "bad signal"])
|
||||
writer.writerow(["field", "rsrp_anchor", "red", "green", "blue", "alpha", "meaning"])
|
||||
writer.writerow([field, f"<= {HEATMAP_BAD_RSRP}", *CONTINUOUS_HEATMAP_RED, HEATMAP_ALPHA, "bad signal red endpoint"])
|
||||
writer.writerow([field, HEATMAP_MID_RSRP, *CONTINUOUS_HEATMAP_YELLOW, HEATMAP_ALPHA, "continuous yellow anchor"])
|
||||
writer.writerow([field, f">= {HEATMAP_GOOD_RSRP}", *CONTINUOUS_HEATMAP_GREEN, HEATMAP_ALPHA, "good signal green endpoint"])
|
||||
writer.writerow([field, "nodata", 0, 0, 0, 0, "transparent no data"])
|
||||
return legend_path
|
||||
|
||||
@@ -1322,10 +1422,10 @@ def write_weak_cover_heatmap_legend(output_dir, field):
|
||||
legend_path = output_dir / "weak_cover_heatmap_legend.csv"
|
||||
with legend_path.open("w", newline="", encoding="utf-8-sig") as file:
|
||||
writer = csv.writer(file)
|
||||
writer.writerow(["field", "rank_range", "red", "green", "blue", "alpha", "meaning"])
|
||||
rank_ranges = ("top_third", "middle_third", "bottom_third")
|
||||
for rank_range, color in zip(rank_ranges, WEAK_COVER_COLORS):
|
||||
writer.writerow([field, rank_range, *color, HEATMAP_ALPHA, "ranked-pixel third"])
|
||||
writer.writerow(["field", "percentile_anchor", "red", "green", "blue", "alpha", "meaning"])
|
||||
writer.writerow([field, "highest_value", *CONTINUOUS_HEATMAP_RED, HEATMAP_ALPHA, "most weak-cover users"])
|
||||
writer.writerow([field, f"percentile_{WEAK_COVER_YELLOW_PERCENTILE}", *CONTINUOUS_HEATMAP_YELLOW, HEATMAP_ALPHA, "continuous yellow anchor"])
|
||||
writer.writerow([field, "lowest_value", *CONTINUOUS_HEATMAP_GREEN, HEATMAP_ALPHA, "fewest weak-cover users"])
|
||||
writer.writerow([field, "nodata", 0, 0, 0, 0, "transparent no data"])
|
||||
return legend_path
|
||||
|
||||
@@ -1457,6 +1557,10 @@ def main():
|
||||
raise ValueError("GEOMETRY_BATCH_SIZE 必须大于 0")
|
||||
if PROJECT_EXTENT_PADDING_RATIO < 0:
|
||||
raise ValueError("PROJECT_EXTENT_PADDING_RATIO 不能小于 0")
|
||||
if not HEATMAP_BAD_RSRP < HEATMAP_MID_RSRP < HEATMAP_GOOD_RSRP:
|
||||
raise ValueError("RSRP 热力图阈值必须满足 HEATMAP_BAD_RSRP < HEATMAP_MID_RSRP < HEATMAP_GOOD_RSRP")
|
||||
if not 0 < WEAK_COVER_YELLOW_PERCENTILE < 1:
|
||||
raise ValueError("WEAK_COVER_YELLOW_PERCENTILE 必须大于 0 且小于 1")
|
||||
if MAX_RASTER_PIXELS is not None and MAX_RASTER_PIXELS <= 0:
|
||||
raise ValueError("MAX_RASTER_PIXELS 必须大于 0 或设置为 None")
|
||||
if CLIP_MODE not in {"intersects", "center"}:
|
||||
|
||||
Reference in New Issue
Block a user