fix: 矩形竞对格子按整窗填色
This commit is contained in:
@@ -9,6 +9,7 @@ from .gpkg import iter_geometry_batches
|
|||||||
from .raster_math import market_raster_layout
|
from .raster_math import market_raster_layout
|
||||||
from .metrics import metric_row
|
from .metrics import metric_row
|
||||||
from .raster_math import pixel_center_mask_for_bounds
|
from .raster_math import pixel_center_mask_for_bounds
|
||||||
|
from .raster_math import rectangle_center_window
|
||||||
from .raster_math import pixel_center_mask_for_geometry
|
from .raster_math import pixel_center_mask_for_geometry
|
||||||
from .raster_math import pixel_window_for_bounds
|
from .raster_math import pixel_window_for_bounds
|
||||||
from .raster_math import pixel_window_from_point
|
from .raster_math import pixel_window_from_point
|
||||||
@@ -36,6 +37,80 @@ def competitor_signal_filter_reason(mobile_weak, mobile_total, competitor_weak,
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def _numeric_column(batch_rows, offset):
|
||||||
|
values = np.empty(len(batch_rows), dtype=np.float64)
|
||||||
|
for index, row in enumerate(batch_rows):
|
||||||
|
value = row[offset]
|
||||||
|
if value is None or value == "":
|
||||||
|
values[index] = np.nan
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
values[index] = float(value)
|
||||||
|
except (TypeError, ValueError):
|
||||||
|
values[index] = np.nan
|
||||||
|
return values
|
||||||
|
|
||||||
|
|
||||||
|
def _paint_rectangles(
|
||||||
|
batch_rows, fields, raster_fields, selected, bounds,
|
||||||
|
minx, maxy, pixel_width, pixel_height, width, height,
|
||||||
|
value_sum, value_weight, zero_total_mask, exclusion_counts,
|
||||||
|
):
|
||||||
|
"""相交的轴对齐格子按整窗填色,避免逐格做点在面内判断。"""
|
||||||
|
if not np.any(selected):
|
||||||
|
return 0, 0, 0
|
||||||
|
offset = {name: 2 + fields.index(name) for name in raster_fields}
|
||||||
|
mobile_weak = _numeric_column(batch_rows, offset[config.MOBILE_WEAK_COVER_FIELD])
|
||||||
|
mobile_total = _numeric_column(batch_rows, offset[config.MOBILE_TOTAL_FIELD])
|
||||||
|
competitor_weak = _numeric_column(batch_rows, offset[config.WEAK_COVER_FIELD])
|
||||||
|
competitor_total = _numeric_column(batch_rows, offset[config.EXCELLENT_CELL_FIELD])
|
||||||
|
competitor_rsrp = _numeric_column(batch_rows, offset[config.RSRP_FIELD])
|
||||||
|
|
||||||
|
missing = ~np.isfinite(mobile_weak) | ~np.isfinite(mobile_total) | ~np.isfinite(competitor_total)
|
||||||
|
invalid = selected & (missing | (mobile_total <= 0) | (mobile_weak < 0) | (competitor_total < 0))
|
||||||
|
exclusion_counts["invalid_fields"] += int(np.count_nonzero(invalid))
|
||||||
|
remaining = selected & ~invalid
|
||||||
|
mobile_ratio = np.zeros(len(batch_rows), dtype=np.float64)
|
||||||
|
np.divide(mobile_weak, mobile_total, out=mobile_ratio, where=remaining)
|
||||||
|
mobile_bad = remaining & (mobile_ratio * 100.0 >= config.COMPETITOR_SIGNAL_MOBILE_WEAK_RATIO_MAX)
|
||||||
|
exclusion_counts["mobile_weak_ratio"] += int(np.count_nonzero(mobile_bad))
|
||||||
|
remaining = remaining & ~mobile_bad
|
||||||
|
nonzero = remaining & (competitor_total != 0)
|
||||||
|
weak_missing = nonzero & (~np.isfinite(competitor_weak) | (competitor_weak < 0))
|
||||||
|
exclusion_counts["invalid_fields"] += int(np.count_nonzero(weak_missing))
|
||||||
|
nonzero = nonzero & ~weak_missing
|
||||||
|
weak_ratio = np.zeros(len(batch_rows), dtype=np.float64)
|
||||||
|
np.divide(competitor_weak, competitor_total, out=weak_ratio, where=nonzero)
|
||||||
|
weak_bad = nonzero & (weak_ratio * 100.0 <= config.COMPETITOR_SIGNAL_WEAK_RATIO_MIN)
|
||||||
|
exclusion_counts["competitor_weak_ratio"] += int(np.count_nonzero(weak_bad))
|
||||||
|
nonzero = nonzero & ~weak_bad
|
||||||
|
bad_rsrp = nonzero & (~np.isfinite(competitor_rsrp) | (competitor_rsrp > 0))
|
||||||
|
exclusion_counts["invalid_rsrp"] += int(np.count_nonzero(bad_rsrp))
|
||||||
|
valued = nonzero & ~bad_rsrp
|
||||||
|
zero = remaining & (competitor_total == 0)
|
||||||
|
|
||||||
|
painted = 0
|
||||||
|
zero_cells = 0
|
||||||
|
pixels = 0
|
||||||
|
for index in np.flatnonzero(zero | valued):
|
||||||
|
window = rectangle_center_window(
|
||||||
|
bounds[index], minx, maxy, pixel_width, pixel_height, width, height,
|
||||||
|
)
|
||||||
|
if window is None:
|
||||||
|
exclusion_counts["no_center_pixels"] += 1
|
||||||
|
continue
|
||||||
|
row_start, row_stop, col_start, col_stop = window
|
||||||
|
if competitor_total[index] == 0:
|
||||||
|
zero_total_mask[row_start:row_stop, col_start:col_stop] = True
|
||||||
|
zero_cells += 1
|
||||||
|
else:
|
||||||
|
value_sum[row_start:row_stop, col_start:col_stop] += competitor_rsrp[index]
|
||||||
|
value_weight[row_start:row_stop, col_start:col_stop] += 1
|
||||||
|
painted += 1
|
||||||
|
pixels += (row_stop - row_start) * (col_stop - col_start)
|
||||||
|
return painted, zero_cells, pixels
|
||||||
|
|
||||||
|
|
||||||
def competitor_signal_is_eligible(mobile_weak, mobile_total, competitor_weak, competitor_total):
|
def competitor_signal_is_eligible(mobile_weak, mobile_total, competitor_weak, competitor_total):
|
||||||
return competitor_signal_filter_reason(mobile_weak, mobile_total, competitor_weak, competitor_total) is None
|
return competitor_signal_filter_reason(mobile_weak, mobile_total, competitor_weak, competitor_total) is None
|
||||||
|
|
||||||
@@ -200,7 +275,21 @@ def export_market_grid_outputs(
|
|||||||
& (shapely.get_num_coordinates(geometries) == 5)
|
& (shapely.get_num_coordinates(geometries) == 5)
|
||||||
& (np.abs(shapely.area(geometries) - bbox_area) <= np.maximum(bbox_area * 1e-6, 1e-18))
|
& (np.abs(shapely.area(geometries) - bbox_area) <= np.maximum(bbox_area * 1e-6, 1e-18))
|
||||||
)
|
)
|
||||||
for index, row in enumerate(batch_rows):
|
if config.CLIP_MODE == "intersects" and csv_writer is None:
|
||||||
|
painted, zero_cells, pixels = _paint_rectangles(
|
||||||
|
batch_rows, fields, raster_fields, intersects_mask & rectangular, geometry_bounds,
|
||||||
|
minx, maxy, pixel_width, pixel_height, width, height,
|
||||||
|
competitor_signal_value_sum, competitor_signal_value_weight, zero_total_mask,
|
||||||
|
exclusion_counts,
|
||||||
|
)
|
||||||
|
competitor_signal_cells += painted
|
||||||
|
zero_total_cells += zero_cells
|
||||||
|
exact_geometry_pixels += pixels
|
||||||
|
slow_indexes = np.flatnonzero(intersects_mask & ~rectangular)
|
||||||
|
else:
|
||||||
|
slow_indexes = range(len(batch_rows))
|
||||||
|
for index in slow_indexes:
|
||||||
|
row = batch_rows[index]
|
||||||
values = dict(zip(fields, row[2:]))
|
values = dict(zip(fields, row[2:]))
|
||||||
numeric_values = {field: to_float(values.get(field)) for field in raster_fields}
|
numeric_values = {field: to_float(values.get(field)) for field in raster_fields}
|
||||||
|
|
||||||
|
|||||||
@@ -176,6 +176,22 @@ def pixel_center_mask_for_geometry(geometry, window, minx, maxy, pixel_width, pi
|
|||||||
return np.asarray(shapely.contains_xy(geometry, x_grid, y_grid), dtype=bool)
|
return np.asarray(shapely.contains_xy(geometry, x_grid, y_grid), dtype=bool)
|
||||||
|
|
||||||
|
|
||||||
|
def rectangle_center_window(bounds, minx, maxy, pixel_width, pixel_height, width, height):
|
||||||
|
"""像元中心严格落在轴对齐矩形内的窗口。整窗都在矩形内,可直接填充。"""
|
||||||
|
geom_minx, geom_miny, geom_maxx, geom_maxy = bounds
|
||||||
|
col_first = math.floor((geom_minx - minx) / pixel_width - 0.5) + 1
|
||||||
|
col_last = math.ceil((geom_maxx - minx) / pixel_width - 0.5) - 1
|
||||||
|
row_first = math.floor((maxy - geom_maxy) / pixel_height - 0.5) + 1
|
||||||
|
row_last = math.ceil((maxy - geom_miny) / pixel_height - 0.5) - 1
|
||||||
|
col_start = max(0, col_first)
|
||||||
|
col_stop = min(width, col_last + 1)
|
||||||
|
row_start = max(0, row_first)
|
||||||
|
row_stop = min(height, row_last + 1)
|
||||||
|
if row_start >= row_stop or col_start >= col_stop:
|
||||||
|
return None
|
||||||
|
return row_start, row_stop, col_start, col_stop
|
||||||
|
|
||||||
|
|
||||||
def pixel_center_mask_for_bounds(bounds, window, minx, maxy, pixel_width, pixel_height):
|
def pixel_center_mask_for_bounds(bounds, window, minx, maxy, pixel_width, pixel_height):
|
||||||
"""轴对齐矩形的像元中心判断。边界上的中心点不算在内,和多边形 contains 一致。"""
|
"""轴对齐矩形的像元中心判断。边界上的中心点不算在内,和多边形 contains 一致。"""
|
||||||
row_start, row_stop, col_start, col_stop = window
|
row_start, row_stop, col_start, col_stop = window
|
||||||
|
|||||||
Reference in New Issue
Block a user