fix: 支持透明高清栅格导出
This commit is contained in:
@@ -7,4 +7,7 @@
|
|||||||
- 新增 `scripts/export_market_grid_tif.py`,用于按 `source/MarketMap.gpkg` 的市场网格裁切 `source/grid.gpkg`,并按电信 RSRP 字段导出彩色 GeoTIFF。
|
- 新增 `scripts/export_market_grid_tif.py`,用于按 `source/MarketMap.gpkg` 的市场网格裁切 `source/grid.gpkg`,并按电信 RSRP 字段导出彩色 GeoTIFF。
|
||||||
- 脚本默认读取市场图层字段 `micro_grid`/`micro_gr_1` 作为输出文件编码和名称,避免使用上级 `grid_code`/`grid_name` 导致文件名重复。
|
- 脚本默认读取市场图层字段 `micro_grid`/`micro_gr_1` 作为输出文件编码和名称,避免使用上级 `grid_code`/`grid_name` 导致文件名重复。
|
||||||
- 脚本默认使用 Grid 图层字段 `ssb_rsrp_dx` 分级着色,颜色按 RSRP 从强到弱划分;输出目录为 `output/market_grid_tif/`,同时生成 `legend.csv` 和 `summary.json`。
|
- 脚本默认使用 Grid 图层字段 `ssb_rsrp_dx` 分级着色,颜色按 RSRP 从强到弱划分;输出目录为 `output/market_grid_tif/`,同时生成 `legend.csv` 和 `summary.json`。
|
||||||
- 新增 Python 依赖:`shapely` 用于几何裁切,`numpy` 用于栅格数组,`tifffile` 用于写入带 EPSG:4326 标签的 RGB GeoTIFF。
|
- 新增 Python 依赖:`shapely` 用于几何裁切,`numpy` 用于栅格数组,`tifffile` 用于写入带 EPSG:4326 标签的 RGBA GeoTIFF。
|
||||||
|
- GeoTIFF 输出已改为 RGBA,背景/无数据区域使用 alpha=0 透明,有数据像元使用 alpha=255。
|
||||||
|
- 脚本新增 `--scale` 参数用于提高导出分辨率,例如 `--scale 2` 会把像元宽高减半,并按 Grid 单元范围填充像素块,减少放大查看时的模糊感。
|
||||||
|
- 2026-07-13 已用 `--scale 2 --overwrite` 重新生成 `output/market_grid_tif/` 全量结果,共 106 个透明 GeoTIFF,约 740MB。
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ DEFAULT_OUTPUT_DIR = Path("output") / "market_grid_tif"
|
|||||||
DEFAULT_MARKET_GPKG = DEFAULT_SOURCE_DIR / "MarketMap.gpkg"
|
DEFAULT_MARKET_GPKG = DEFAULT_SOURCE_DIR / "MarketMap.gpkg"
|
||||||
DEFAULT_GRID_GPKG = DEFAULT_SOURCE_DIR / "grid.gpkg"
|
DEFAULT_GRID_GPKG = DEFAULT_SOURCE_DIR / "grid.gpkg"
|
||||||
DEFAULT_FIELD = "ssb_rsrp_dx"
|
DEFAULT_FIELD = "ssb_rsrp_dx"
|
||||||
NODATA_COLOR = (255, 255, 255)
|
TRANSPARENT_PIXEL = (0, 0, 0, 0)
|
||||||
|
|
||||||
COLOR_BREAKS = [
|
COLOR_BREAKS = [
|
||||||
(-80.0, (26, 152, 80), ">= -80"),
|
(-80.0, (26, 152, 80), ">= -80"),
|
||||||
@@ -176,8 +176,8 @@ def estimate_pixel_size(conn, layer, sample_size):
|
|||||||
def color_for_value(value):
|
def color_for_value(value):
|
||||||
for threshold, color, _label in COLOR_BREAKS:
|
for threshold, color, _label in COLOR_BREAKS:
|
||||||
if value >= threshold:
|
if value >= threshold:
|
||||||
return color
|
return (*color, 255)
|
||||||
return COLOR_BREAKS[-1][1]
|
return (*COLOR_BREAKS[-1][1], 255)
|
||||||
|
|
||||||
|
|
||||||
def to_float(value):
|
def to_float(value):
|
||||||
@@ -209,11 +209,12 @@ def geotiff_extratags(minx, maxy, pixel_width, pixel_height):
|
|||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
def write_rgb_geotiff(path, image, minx, maxy, pixel_width, pixel_height):
|
def write_rgba_geotiff(path, image, minx, maxy, pixel_width, pixel_height):
|
||||||
tifffile.imwrite(
|
tifffile.imwrite(
|
||||||
path,
|
path,
|
||||||
image,
|
image,
|
||||||
photometric="rgb",
|
photometric="rgb",
|
||||||
|
extrasamples=["unassalpha"],
|
||||||
metadata=None,
|
metadata=None,
|
||||||
extratags=geotiff_extratags(minx, maxy, pixel_width, pixel_height),
|
extratags=geotiff_extratags(minx, maxy, pixel_width, pixel_height),
|
||||||
)
|
)
|
||||||
@@ -223,15 +224,26 @@ def create_canvas(bounds, pixel_width, pixel_height):
|
|||||||
minx, miny, maxx, maxy = bounds
|
minx, miny, maxx, maxy = bounds
|
||||||
width = max(1, int(math.ceil((maxx - minx) / pixel_width)))
|
width = max(1, int(math.ceil((maxx - minx) / pixel_width)))
|
||||||
height = max(1, int(math.ceil((maxy - miny) / pixel_height)))
|
height = max(1, int(math.ceil((maxy - miny) / pixel_height)))
|
||||||
image = np.full((height, width, 3), NODATA_COLOR, dtype=np.uint8)
|
image = np.full((height, width, 4), TRANSPARENT_PIXEL, dtype=np.uint8)
|
||||||
return image, minx, maxy
|
return image, minx, maxy
|
||||||
|
|
||||||
|
|
||||||
def pixel_from_point(x, y, minx, maxy, pixel_width, pixel_height, width, height):
|
def pixel_window_for_bounds(bounds, minx, maxy, pixel_width, pixel_height, width, height):
|
||||||
|
geom_minx, geom_miny, geom_maxx, geom_maxy = bounds
|
||||||
|
col_start = max(0, int(math.floor((geom_minx - minx) / pixel_width)))
|
||||||
|
col_stop = min(width, int(math.ceil((geom_maxx - minx) / pixel_width)))
|
||||||
|
row_start = max(0, int(math.floor((maxy - geom_maxy) / pixel_height)))
|
||||||
|
row_stop = min(height, int(math.ceil((maxy - geom_miny) / pixel_height)))
|
||||||
|
if row_start >= row_stop or col_start >= col_stop:
|
||||||
|
return None
|
||||||
|
return row_start, row_stop, col_start, col_stop
|
||||||
|
|
||||||
|
|
||||||
|
def pixel_window_from_point(x, y, minx, maxy, pixel_width, pixel_height, width, height):
|
||||||
col = int((x - minx) / pixel_width)
|
col = int((x - minx) / pixel_width)
|
||||||
row = int((maxy - y) / pixel_height)
|
row = int((maxy - y) / pixel_height)
|
||||||
if 0 <= row < height and 0 <= col < width:
|
if 0 <= row < height and 0 <= col < width:
|
||||||
return row, col
|
return row, row + 1, col, col + 1
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
@@ -286,29 +298,38 @@ def export_market_tif(grid_conn, grid_layer, rtree, market, field, pixel_width,
|
|||||||
continue
|
continue
|
||||||
if target_point is None:
|
if target_point is None:
|
||||||
target_point = grid_geom.representative_point().coords[0]
|
target_point = grid_geom.representative_point().coords[0]
|
||||||
|
window = pixel_window_for_bounds(
|
||||||
|
grid_geom.bounds,
|
||||||
|
minx,
|
||||||
|
maxy,
|
||||||
|
pixel_width,
|
||||||
|
pixel_height,
|
||||||
|
width,
|
||||||
|
height,
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
if target_point is None:
|
if target_point is None:
|
||||||
continue
|
continue
|
||||||
if not prepared_market.covers(Point(target_point)):
|
if not prepared_market.covers(Point(target_point)):
|
||||||
continue
|
continue
|
||||||
|
window = pixel_window_from_point(
|
||||||
pixel = pixel_from_point(
|
target_point[0],
|
||||||
target_point[0],
|
target_point[1],
|
||||||
target_point[1],
|
minx,
|
||||||
minx,
|
maxy,
|
||||||
maxy,
|
pixel_width,
|
||||||
pixel_width,
|
pixel_height,
|
||||||
pixel_height,
|
width,
|
||||||
width,
|
height,
|
||||||
height,
|
)
|
||||||
)
|
if window is None:
|
||||||
if pixel is None:
|
|
||||||
continue
|
continue
|
||||||
|
|
||||||
image[pixel] = color_for_value(value)
|
row_start, row_stop, col_start, col_stop = window
|
||||||
|
image[row_start:row_stop, col_start:col_stop] = color_for_value(value)
|
||||||
painted += 1
|
painted += 1
|
||||||
|
|
||||||
write_rgb_geotiff(output_path, image, minx, maxy, pixel_width, pixel_height)
|
write_rgba_geotiff(output_path, image, minx, maxy, pixel_width, pixel_height)
|
||||||
return {
|
return {
|
||||||
"market_code": market.code,
|
"market_code": market.code,
|
||||||
"market_name": market.name,
|
"market_name": market.name,
|
||||||
@@ -325,10 +346,10 @@ def write_legend(output_dir, field):
|
|||||||
legend_path = output_dir / "legend.csv"
|
legend_path = output_dir / "legend.csv"
|
||||||
with legend_path.open("w", newline="", encoding="utf-8-sig") as file:
|
with legend_path.open("w", newline="", encoding="utf-8-sig") as file:
|
||||||
writer = csv.writer(file)
|
writer = csv.writer(file)
|
||||||
writer.writerow(["field", "range", "red", "green", "blue", "meaning"])
|
writer.writerow(["field", "range", "red", "green", "blue", "alpha", "meaning"])
|
||||||
for _threshold, color, label in COLOR_BREAKS:
|
for _threshold, color, label in COLOR_BREAKS:
|
||||||
writer.writerow([field, label, color[0], color[1], color[2], "strong-to-weak RSRP"])
|
writer.writerow([field, label, color[0], color[1], color[2], 255, "strong-to-weak RSRP"])
|
||||||
writer.writerow([field, "nodata", NODATA_COLOR[0], NODATA_COLOR[1], NODATA_COLOR[2], "no data"])
|
writer.writerow([field, "nodata", 0, 0, 0, 0, "transparent no data"])
|
||||||
return legend_path
|
return legend_path
|
||||||
|
|
||||||
|
|
||||||
@@ -344,6 +365,7 @@ def parse_args():
|
|||||||
parser.add_argument("--market-name-field", default="micro_gr_1")
|
parser.add_argument("--market-name-field", default="micro_gr_1")
|
||||||
parser.add_argument("--pixel-width", type=float)
|
parser.add_argument("--pixel-width", type=float)
|
||||||
parser.add_argument("--pixel-height", type=float)
|
parser.add_argument("--pixel-height", type=float)
|
||||||
|
parser.add_argument("--scale", type=float, default=1.0, help="导出分辨率倍数,2 表示像元宽高减半。")
|
||||||
parser.add_argument("--sample-size", type=int, default=5000)
|
parser.add_argument("--sample-size", type=int, default=5000)
|
||||||
parser.add_argument("--limit", type=int, help="只处理前 N 个市场网格,用于快速验证。")
|
parser.add_argument("--limit", type=int, help="只处理前 N 个市场网格,用于快速验证。")
|
||||||
parser.add_argument("--clip-mode", choices=["intersects", "center"], default="intersects")
|
parser.add_argument("--clip-mode", choices=["intersects", "center"], default="intersects")
|
||||||
@@ -353,6 +375,8 @@ def parse_args():
|
|||||||
|
|
||||||
def main():
|
def main():
|
||||||
args = parse_args()
|
args = parse_args()
|
||||||
|
if args.scale <= 0:
|
||||||
|
raise ValueError("--scale 必须大于 0")
|
||||||
args.output_dir.mkdir(parents=True, exist_ok=True)
|
args.output_dir.mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
with sqlite3.connect(args.market_gpkg) as market_conn, sqlite3.connect(args.grid_gpkg) as grid_conn:
|
with sqlite3.connect(args.market_gpkg) as market_conn, sqlite3.connect(args.grid_gpkg) as grid_conn:
|
||||||
@@ -369,6 +393,8 @@ def main():
|
|||||||
estimated_width, estimated_height = estimate_pixel_size(grid_conn, grid_layer, args.sample_size)
|
estimated_width, estimated_height = estimate_pixel_size(grid_conn, grid_layer, args.sample_size)
|
||||||
pixel_width = pixel_width or estimated_width
|
pixel_width = pixel_width or estimated_width
|
||||||
pixel_height = pixel_height or estimated_height
|
pixel_height = pixel_height or estimated_height
|
||||||
|
pixel_width = pixel_width / args.scale
|
||||||
|
pixel_height = pixel_height / args.scale
|
||||||
|
|
||||||
markets = load_market_features(
|
markets = load_market_features(
|
||||||
market_conn,
|
market_conn,
|
||||||
@@ -384,6 +410,7 @@ def main():
|
|||||||
print(f"市场网格数量:{len(markets)}")
|
print(f"市场网格数量:{len(markets)}")
|
||||||
print(f"Grid 图层:{grid_layer.table_name}")
|
print(f"Grid 图层:{grid_layer.table_name}")
|
||||||
print(f"RSRP 字段:{args.field}")
|
print(f"RSRP 字段:{args.field}")
|
||||||
|
print(f"分辨率倍数:{args.scale}")
|
||||||
print(f"像元大小:{pixel_width}, {pixel_height}")
|
print(f"像元大小:{pixel_width}, {pixel_height}")
|
||||||
print(f"图例:{legend_path}")
|
print(f"图例:{legend_path}")
|
||||||
|
|
||||||
@@ -415,6 +442,7 @@ def main():
|
|||||||
"grid_gpkg": str(args.grid_gpkg),
|
"grid_gpkg": str(args.grid_gpkg),
|
||||||
"field": args.field,
|
"field": args.field,
|
||||||
"clip_mode": args.clip_mode,
|
"clip_mode": args.clip_mode,
|
||||||
|
"scale": args.scale,
|
||||||
"pixel_width": pixel_width,
|
"pixel_width": pixel_width,
|
||||||
"pixel_height": pixel_height,
|
"pixel_height": pixel_height,
|
||||||
"results": results,
|
"results": results,
|
||||||
|
|||||||
Reference in New Issue
Block a user