feat: 自动定位QGIS项目到楼宇区域
This commit is contained in:
@@ -44,7 +44,9 @@ BuildingInfo.gpkg
|
|||||||
|
|
||||||
来源:`source/MapPark/GIS.qgz`
|
来源:`source/MapPark/GIS.qgz`
|
||||||
|
|
||||||
说明:直接复制到每个结果包内,不修改项目文件。QGIS 项目默认引用结果包内的同名图层文件。
|
说明:以该文件为模板生成到每个结果包。程序会读取 QGZ 内的项目 XML,把 BuildingInfo 图层范围更新为当前市场实际楼宇范围,并将 QGIS 主画布设置为该范围四周增加 5% 留白。打开项目后会直接定位到当前市场区域。
|
||||||
|
|
||||||
|
QGZ 内的样式数据库、其他图层配置和项目结构保持不变;如果当前市场没有 BuildingInfo 要素,画布范围会回退到市场网格范围。
|
||||||
|
|
||||||
### BuildingInfo.gpkg
|
### BuildingInfo.gpkg
|
||||||
|
|
||||||
@@ -183,6 +185,7 @@ CSV 中的 `移动信号` 和 `竟对信号` 使用同一套文字等级:
|
|||||||
| `MAX_WORKERS` | `4` | 市场网格并行处理进程数;当前 4 核、32GB、NVMe 环境实测 4 进程吞吐最佳 |
|
| `MAX_WORKERS` | `4` | 市场网格并行处理进程数;当前 4 核、32GB、NVMe 环境实测 4 进程吞吐最佳 |
|
||||||
| `GEOMETRY_BATCH_SIZE` | `10000` | Shapely 批量解析和空间判断的每批要素数,通常无需修改 |
|
| `GEOMETRY_BATCH_SIZE` | `10000` | Shapely 批量解析和空间判断的每批要素数,通常无需修改 |
|
||||||
| `MAX_RASTER_PIXELS` | `25000000` | 单张 TIF 最大像元数;超限市场自动降低分辨率,防止内存溢出 |
|
| `MAX_RASTER_PIXELS` | `25000000` | 单张 TIF 最大像元数;超限市场自动降低分辨率,防止内存溢出 |
|
||||||
|
| `PROJECT_EXTENT_PADDING_RATIO` | `0.05` | QGIS 初始画布在 BuildingInfo 范围四周的留白比例 |
|
||||||
| `SCALE` | `5.0` | 分辨率倍数,越大输出越清晰,文件也越大 |
|
| `SCALE` | `5.0` | 分辨率倍数,越大输出越清晰,文件也越大 |
|
||||||
| `CLIP_MODE` | `intersects` | Grid 热力图裁切模式 |
|
| `CLIP_MODE` | `intersects` | Grid 热力图裁切模式 |
|
||||||
| `HEATMAP_BAD_RSRP` | `-105.0` | RSRP 红色端 |
|
| `HEATMAP_BAD_RSRP` | `-105.0` | RSRP 红色端 |
|
||||||
|
|||||||
@@ -47,7 +47,7 @@
|
|||||||
- BuildingInfo 拆分会复制源 GeoPackage 的 `layer_styles`、`gpkg_metadata`、`gpkg_metadata_reference`、`gpkg_extensions`、`gpkg_ogr_contents`、`gpkg_spatial_ref_sys` 等支撑表,并重建输出 RTree;避免复制依赖 SpatiaLite 函数的 RTree 触发器。
|
- 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)`。
|
- `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` 已验证与源文件完全一致。
|
- 当前测试市场 `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`;模板内图层、样式数据库和其他项目配置保持不变,项目 XML 的主画布和 BuildingInfo 图层范围会按当前市场结果自动更新。
|
||||||
- 市场网格结果改为 zip 包输出:`python main.py` 会先在系统临时目录生成结果,再写入 `output/JMXXXX.zip`,zip 根目录直接包含 `GIS.qgz`、`信号.tif`、`用户量.tif`、`信号差用户量.tif`、`BuildingInfo.gpkg`,不再保留 `output/JMXXXX/` 文件夹。
|
- 市场网格结果改为 zip 包输出:`python main.py` 会先在系统临时目录生成结果,再写入 `output/JMXXXX.zip`,zip 根目录直接包含 `GIS.qgz`、`信号.tif`、`用户量.tif`、`信号差用户量.tif`、`BuildingInfo.gpkg`,不再保留 `output/JMXXXX/` 文件夹。
|
||||||
|
|
||||||
## 2026-07-17
|
## 2026-07-17
|
||||||
@@ -84,3 +84,5 @@
|
|||||||
- 批量优化后 4 市场/4 进程总耗时由 55.6 秒降至 21.4 秒,提速约 61.5%;单进程与多进程生成的 7 个 zip 成员完全一致。
|
- 批量优化后 4 市场/4 进程总耗时由 55.6 秒降至 21.4 秒,提速约 61.5%;单进程与多进程生成的 7 个 zip 成员完全一致。
|
||||||
- 新旧版本的 BuildingInfo、GIS 和两个 CSV 字节一致,TIF 的范围、透明通道、阈值和空间位置一致;因浮点累加顺序变化,连续平滑颜色可能有极少像元出现 1 个色阶舍入差异。现有 9 个标准库单元测试覆盖信号边界、同值分类、批量卷积、批量 STRtree 匹配、像元上限和 zip 完整性。
|
- 新旧版本的 BuildingInfo、GIS 和两个 CSV 字节一致,TIF 的范围、透明通道、阈值和空间位置一致;因浮点累加顺序变化,连续平滑颜色可能有极少像元出现 1 个色阶舍入差异。现有 9 个标准库单元测试覆盖信号边界、同值分类、批量卷积、批量 STRtree 匹配、像元上限和 zip 完整性。
|
||||||
- 最终 profile 中主要剩余耗时为 SQLite `fetchmany`、三张栅格的必要像元填充,以及多候选楼宇的真实相交面积计算;这些路径均直接对应输出需求,继续优化需要明显增加内存或引入额外依赖,当前版本不再为小幅收益增加复杂度。
|
- 最终 profile 中主要剩余耗时为 SQLite `fetchmany`、三张栅格的必要像元填充,以及多候选楼宇的真实相交面积计算;这些路径均直接对应输出需求,继续优化需要明显增加内存或引入额外依赖,当前版本不再为小幅收益增加复杂度。
|
||||||
|
- `GIS.qgz` 现在按 ZIP 容器读取并解析其中唯一的 `.qgs` XML;生成 BuildingInfo.gpkg 后,将 BuildingInfo 实际要素范围写入项目图层缓存范围,并把主画布 `theMapCanvas` 设置为四周增加 5% 留白的同一区域。无 BuildingInfo 要素时回退到市场网格范围。
|
||||||
|
- QGZ 重写会保留原始成员名称、压缩方式、ZIP 注释、QGIS DOCTYPE 和 `drceGv_styles.db` 内容;真实市场 `1-城区蓬江` 已验证 XML 图层范围与拆分结果一致,主画布范围留白正确,样式数据库 SHA256 与模板一致。新增 QGZ 回归测试后当前测试数为 10 项。
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import re
|
|||||||
import shutil
|
import shutil
|
||||||
import sqlite3
|
import sqlite3
|
||||||
import tempfile
|
import tempfile
|
||||||
|
import xml.etree.ElementTree as ET
|
||||||
import zipfile
|
import zipfile
|
||||||
from concurrent.futures import ProcessPoolExecutor, as_completed
|
from concurrent.futures import ProcessPoolExecutor, as_completed
|
||||||
from contextlib import contextmanager
|
from contextlib import contextmanager
|
||||||
@@ -25,8 +26,8 @@ MARKET_GPKG = DEFAULT_SOURCE_DIR / "MarketMap.gpkg" # 市场网格 GeoPackage
|
|||||||
GRID_GPKG = DEFAULT_SOURCE_DIR / "grid.gpkg" # Grid 栅格指标 GeoPackage 文件路径。
|
GRID_GPKG = DEFAULT_SOURCE_DIR / "grid.gpkg" # Grid 栅格指标 GeoPackage 文件路径。
|
||||||
BUILDING_GPKG = DEFAULT_SOURCE_DIR / "Building.gpkg" # 建筑指标 GeoPackage 文件路径,用于生成楼宇指标 CSV。
|
BUILDING_GPKG = DEFAULT_SOURCE_DIR / "Building.gpkg" # 建筑指标 GeoPackage 文件路径,用于生成楼宇指标 CSV。
|
||||||
BUILDING_INFO_GPKG = DEFAULT_SOURCE_DIR / "BuildingInfo.gpkg" # 建筑信息 GeoPackage 文件路径,用于结果包 BuildingInfo 分割和楼宇指标 CSV 名称匹配。
|
BUILDING_INFO_GPKG = DEFAULT_SOURCE_DIR / "BuildingInfo.gpkg" # 建筑信息 GeoPackage 文件路径,用于结果包 BuildingInfo 分割和楼宇指标 CSV 名称匹配。
|
||||||
MAP_PARK_DIR = DEFAULT_SOURCE_DIR / "MapPark" # QGIS 项目模板目录,里面的 GIS.qgz 会复制到每个市场网格结果包。
|
MAP_PARK_DIR = DEFAULT_SOURCE_DIR / "MapPark" # QGIS 项目模板目录,里面的 GIS.qgz 用于生成各市场网格项目。
|
||||||
GIS_PROJECT_SOURCE = MAP_PARK_DIR / "GIS.qgz" # 每个市场网格结果包要复制的 QGIS 项目文件来源。
|
GIS_PROJECT_SOURCE = MAP_PARK_DIR / "GIS.qgz" # 每个市场网格结果包要使用的 QGIS 项目模板来源。
|
||||||
RSRP_FIELD = "ssb_rsrp_dl" # 用于生成热力图的电信 RSRP 字段名。
|
RSRP_FIELD = "ssb_rsrp_dl" # 用于生成热力图的电信 RSRP 字段名。
|
||||||
EXCELLENT_CELL_FIELD = "mro_total_dl" # 用于生成用户量热力图的字段名,值越大越红。
|
EXCELLENT_CELL_FIELD = "mro_total_dl" # 用于生成用户量热力图的字段名,值越大越红。
|
||||||
WEAK_COVER_FIELD = "rsrp_lt_neg105_dl" # 用于生成信号差用户量热力图的字段名,值越大越红。
|
WEAK_COVER_FIELD = "rsrp_lt_neg105_dl" # 用于生成信号差用户量热力图的字段名,值越大越红。
|
||||||
@@ -45,11 +46,12 @@ CSV_ENCODING = "gbk" # 指标 CSV 输出编码,供 Excel/传统 GIS 工具直
|
|||||||
CSV_SOURCE_FIELDS = ("area_type", "area_name", "longitude", "latitude", "mro_total_dl", "rsrp_lt_neg105_dl", "ssb_rsrp_dl", "ssb_rsrp_yd") # 指标 CSV 从源图层提取的字段。
|
CSV_SOURCE_FIELDS = ("area_type", "area_name", "longitude", "latitude", "mro_total_dl", "rsrp_lt_neg105_dl", "ssb_rsrp_dl", "ssb_rsrp_yd") # 指标 CSV 从源图层提取的字段。
|
||||||
SIGNAL_BAD_THRESHOLD = -105.0 # 信号等级“较差”的阈值,小于等于该值输出较差。
|
SIGNAL_BAD_THRESHOLD = -105.0 # 信号等级“较差”的阈值,小于等于该值输出较差。
|
||||||
SIGNAL_NORMAL_THRESHOLD = -95.0 # 信号等级“一般/正常”的分界,大于该值输出正常。
|
SIGNAL_NORMAL_THRESHOLD = -95.0 # 信号等级“一般/正常”的分界,大于该值输出正常。
|
||||||
GIS_PROJECT_OUTPUT_FILENAME = "GIS.qgz" # 每个市场网格结果包内输出的 QGIS 项目文件名,内容不修改直接复制。
|
GIS_PROJECT_OUTPUT_FILENAME = "GIS.qgz" # 每个市场网格结果包内输出的 QGIS 项目文件名,画布范围按 BuildingInfo 自动更新。
|
||||||
HEATMAP_MID_RSRP = -95.0 # 热力图黄色中间阈值。
|
HEATMAP_MID_RSRP = -95.0 # 热力图黄色中间阈值。
|
||||||
HEATMAP_BAD_RSRP = -105.0 # 热力图红色端阈值;RSRP 小于等于该值视为信号差。
|
HEATMAP_BAD_RSRP = -105.0 # 热力图红色端阈值;RSRP 小于等于该值视为信号差。
|
||||||
HEATMAP_SMOOTH_RADIUS = 10 # 热力图平滑半径,数值越大边界感越弱但细节越柔。
|
HEATMAP_SMOOTH_RADIUS = 10 # 热力图平滑半径,数值越大边界感越弱但细节越柔。
|
||||||
HEATMAP_ALPHA = 191 # 热力图有效像元透明度;191 约等于 75% 不透明度。
|
HEATMAP_ALPHA = 191 # 热力图有效像元透明度;191 约等于 75% 不透明度。
|
||||||
|
PROJECT_EXTENT_PADDING_RATIO = 0.05 # QGIS 打开后的画布留白比例;0.05 表示在 BuildingInfo 范围四周各增加 5%。
|
||||||
USER_COUNT_OUTPUT_FILENAME = "用户量.tif" # 采样点数量热力图输出文件名。
|
USER_COUNT_OUTPUT_FILENAME = "用户量.tif" # 采样点数量热力图输出文件名。
|
||||||
WEAK_COVER_OUTPUT_FILENAME = "信号差用户量.tif" # 信号差用户量热力图输出文件名。
|
WEAK_COVER_OUTPUT_FILENAME = "信号差用户量.tif" # 信号差用户量热力图输出文件名。
|
||||||
EXCELLENT_PERCENT_STEPS = (0.25, 0.5, 0.75) # 按最终有效像元降序四等分,取 25%、50%、75% 位置作为结果记录阈值。
|
EXCELLENT_PERCENT_STEPS = (0.25, 0.5, 0.75) # 按最终有效像元降序四等分,取 25%、50%、75% 位置作为结果记录阈值。
|
||||||
@@ -127,7 +129,33 @@ def weak_cover_heatmap_raster_filename(_field):
|
|||||||
return WEAK_COVER_OUTPUT_FILENAME
|
return WEAK_COVER_OUTPUT_FILENAME
|
||||||
|
|
||||||
|
|
||||||
def copy_gis_project_to_market(source_path, output_path, market):
|
def set_xml_extent(extent_element, extent):
|
||||||
|
for tag, value in zip(("xmin", "ymin", "xmax", "ymax"), extent):
|
||||||
|
child = extent_element.find(tag)
|
||||||
|
if child is None:
|
||||||
|
child = ET.SubElement(extent_element, tag)
|
||||||
|
child.text = format(float(value), ".17g")
|
||||||
|
|
||||||
|
|
||||||
|
def padded_extent(extent, padding_ratio):
|
||||||
|
minx, miny, maxx, maxy = (float(value) for value in extent)
|
||||||
|
width = maxx - minx
|
||||||
|
height = maxy - miny
|
||||||
|
padding_x = width * padding_ratio if width > 0 else (1e-6 if padding_ratio > 0 else 0.0)
|
||||||
|
padding_y = height * padding_ratio if height > 0 else (1e-6 if padding_ratio > 0 else 0.0)
|
||||||
|
return minx - padding_x, miny - padding_y, maxx + padding_x, maxy + padding_y
|
||||||
|
|
||||||
|
|
||||||
|
def serialize_qgis_project(root, original_data):
|
||||||
|
bom = b"\xef\xbb\xbf" if original_data.startswith(b"\xef\xbb\xbf") else b""
|
||||||
|
payload = original_data[len(bom):]
|
||||||
|
first_line = payload.splitlines()[0] if payload else b""
|
||||||
|
doctype = first_line if first_line.startswith(b"<!DOCTYPE ") else b""
|
||||||
|
xml_data = ET.tostring(root, encoding="utf-8", short_empty_elements=True)
|
||||||
|
return bom + (doctype + b"\r\n" if doctype else b"") + xml_data
|
||||||
|
|
||||||
|
|
||||||
|
def copy_gis_project_to_market(source_path, output_path, market, building_extent):
|
||||||
if output_path.exists() and not OVERWRITE:
|
if output_path.exists() and not OVERWRITE:
|
||||||
return {
|
return {
|
||||||
"market_code": market.code,
|
"market_code": market.code,
|
||||||
@@ -137,12 +165,65 @@ def copy_gis_project_to_market(source_path, output_path, market):
|
|||||||
"skipped": True,
|
"skipped": True,
|
||||||
}
|
}
|
||||||
|
|
||||||
shutil.copy2(source_path, output_path)
|
extent_source = "building_info" if building_extent is not None else "market"
|
||||||
|
layer_extent = tuple(building_extent) if building_extent is not None else market.geometry.bounds
|
||||||
|
canvas_extent = padded_extent(layer_extent, PROJECT_EXTENT_PADDING_RATIO)
|
||||||
|
temp_output_path = output_path.with_name(f".{output_path.name}.tmp")
|
||||||
|
if temp_output_path.exists():
|
||||||
|
temp_output_path.unlink()
|
||||||
|
|
||||||
|
try:
|
||||||
|
with zipfile.ZipFile(source_path, "r") as source_zip:
|
||||||
|
qgs_members = [info.filename for info in source_zip.infolist() if info.filename.lower().endswith(".qgs")]
|
||||||
|
if len(qgs_members) != 1:
|
||||||
|
raise ValueError(f"QGIS 项目模板必须包含且仅包含一个 .qgs 文件,当前数量:{len(qgs_members)}")
|
||||||
|
project_member = qgs_members[0]
|
||||||
|
|
||||||
|
with zipfile.ZipFile(temp_output_path, "w") as target_zip:
|
||||||
|
target_zip.comment = source_zip.comment
|
||||||
|
for info in source_zip.infolist():
|
||||||
|
data = source_zip.read(info.filename)
|
||||||
|
if info.filename == project_member:
|
||||||
|
root = ET.fromstring(data)
|
||||||
|
canvas = root.find(".//mapcanvas[@name='theMapCanvas']")
|
||||||
|
if canvas is None:
|
||||||
|
raise ValueError("QGIS 项目模板缺少主画布 theMapCanvas")
|
||||||
|
canvas_extent_element = canvas.find("extent")
|
||||||
|
if canvas_extent_element is None:
|
||||||
|
canvas_extent_element = ET.SubElement(canvas, "extent")
|
||||||
|
set_xml_extent(canvas_extent_element, canvas_extent)
|
||||||
|
|
||||||
|
building_layer = None
|
||||||
|
expected_layer_name = Path(BUILDING_INFO_OUTPUT_FILENAME).stem
|
||||||
|
for layer in root.findall(".//projectlayers/maplayer"):
|
||||||
|
datasource = (layer.findtext("datasource") or "").replace("\\", "/")
|
||||||
|
if layer.findtext("layername") == expected_layer_name and BUILDING_INFO_OUTPUT_FILENAME in datasource:
|
||||||
|
building_layer = layer
|
||||||
|
break
|
||||||
|
if building_layer is None:
|
||||||
|
raise ValueError(f"QGIS 项目模板缺少 {expected_layer_name} 图层")
|
||||||
|
layer_extent_element = building_layer.find("extent")
|
||||||
|
if layer_extent_element is None:
|
||||||
|
layer_extent_element = ET.SubElement(building_layer, "extent")
|
||||||
|
set_xml_extent(layer_extent_element, layer_extent)
|
||||||
|
data = serialize_qgis_project(root, data)
|
||||||
|
target_zip.writestr(info, data)
|
||||||
|
temp_output_path.replace(output_path)
|
||||||
|
except Exception:
|
||||||
|
if temp_output_path.exists():
|
||||||
|
temp_output_path.unlink()
|
||||||
|
raise
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"market_code": market.code,
|
"market_code": market.code,
|
||||||
"market_name": market.name,
|
"market_name": market.name,
|
||||||
"source": str(source_path),
|
"source": str(source_path),
|
||||||
"output": str(output_path),
|
"output": str(output_path),
|
||||||
|
"project_member": project_member,
|
||||||
|
"extent_source": extent_source,
|
||||||
|
"building_extent": list(layer_extent),
|
||||||
|
"canvas_extent": list(canvas_extent),
|
||||||
|
"padding_ratio": PROJECT_EXTENT_PADDING_RATIO,
|
||||||
"bytes": output_path.stat().st_size,
|
"bytes": output_path.stat().st_size,
|
||||||
"skipped": False,
|
"skipped": False,
|
||||||
}
|
}
|
||||||
@@ -464,6 +545,7 @@ def export_market_building_info_gpkg(source_conn, layer, rtree, market, output_p
|
|||||||
"output": str(output_path),
|
"output": str(output_path),
|
||||||
"candidates": candidates,
|
"candidates": candidates,
|
||||||
"feature_count": None,
|
"feature_count": None,
|
||||||
|
"extent": list(extent) if extent else None,
|
||||||
"skipped": True,
|
"skipped": True,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -484,6 +566,7 @@ def export_market_building_info_gpkg(source_conn, layer, rtree, market, output_p
|
|||||||
"output": str(output_path),
|
"output": str(output_path),
|
||||||
"candidates": candidates,
|
"candidates": candidates,
|
||||||
"feature_count": len(rows),
|
"feature_count": len(rows),
|
||||||
|
"extent": list(extent) if extent else None,
|
||||||
"skipped": False,
|
"skipped": False,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1292,7 +1375,6 @@ def process_market(market, pixel_width, pixel_height, output_dir):
|
|||||||
with tempfile.TemporaryDirectory(prefix=f"{safe_filename(market.code)}_") as temp_dir:
|
with tempfile.TemporaryDirectory(prefix=f"{safe_filename(market.code)}_") as temp_dir:
|
||||||
market_dir = Path(temp_dir)
|
market_dir = Path(temp_dir)
|
||||||
gis_project_path = market_dir / GIS_PROJECT_OUTPUT_FILENAME
|
gis_project_path = market_dir / GIS_PROJECT_OUTPUT_FILENAME
|
||||||
gis_project_result = copy_gis_project_to_market(GIS_PROJECT_SOURCE, gis_project_path, market)
|
|
||||||
|
|
||||||
signal_path = market_dir / heatmap_raster_filename(RSRP_FIELD)
|
signal_path = market_dir / heatmap_raster_filename(RSRP_FIELD)
|
||||||
user_count_path = market_dir / excellent_heatmap_raster_filename(EXCELLENT_CELL_FIELD)
|
user_count_path = market_dir / excellent_heatmap_raster_filename(EXCELLENT_CELL_FIELD)
|
||||||
@@ -1322,6 +1404,12 @@ def process_market(market, pixel_width, pixel_height, output_dir):
|
|||||||
market,
|
market,
|
||||||
building_info_path,
|
building_info_path,
|
||||||
)
|
)
|
||||||
|
gis_project_result = copy_gis_project_to_market(
|
||||||
|
GIS_PROJECT_SOURCE,
|
||||||
|
gis_project_path,
|
||||||
|
market,
|
||||||
|
building_info_result["extent"],
|
||||||
|
)
|
||||||
building_metrics_path = market_dir / BUILDING_METRICS_CSV_FILENAME
|
building_metrics_path = market_dir / BUILDING_METRICS_CSV_FILENAME
|
||||||
building_metrics_result = export_market_building_metrics_csv(
|
building_metrics_result = export_market_building_metrics_csv(
|
||||||
building_conn,
|
building_conn,
|
||||||
@@ -1367,6 +1455,8 @@ def main():
|
|||||||
raise ValueError("MAX_WORKERS 必须大于 0")
|
raise ValueError("MAX_WORKERS 必须大于 0")
|
||||||
if GEOMETRY_BATCH_SIZE <= 0:
|
if GEOMETRY_BATCH_SIZE <= 0:
|
||||||
raise ValueError("GEOMETRY_BATCH_SIZE 必须大于 0")
|
raise ValueError("GEOMETRY_BATCH_SIZE 必须大于 0")
|
||||||
|
if PROJECT_EXTENT_PADDING_RATIO < 0:
|
||||||
|
raise ValueError("PROJECT_EXTENT_PADDING_RATIO 不能小于 0")
|
||||||
if MAX_RASTER_PIXELS is not None and MAX_RASTER_PIXELS <= 0:
|
if MAX_RASTER_PIXELS is not None and MAX_RASTER_PIXELS <= 0:
|
||||||
raise ValueError("MAX_RASTER_PIXELS 必须大于 0 或设置为 None")
|
raise ValueError("MAX_RASTER_PIXELS 必须大于 0 或设置为 None")
|
||||||
if CLIP_MODE not in {"intersects", "center"}:
|
if CLIP_MODE not in {"intersects", "center"}:
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import tempfile
|
import tempfile
|
||||||
import unittest
|
import unittest
|
||||||
|
import xml.etree.ElementTree as ET
|
||||||
import zipfile
|
import zipfile
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
@@ -111,5 +112,45 @@ class OutputSafetyTest(unittest.TestCase):
|
|||||||
self.assertFalse((root / ".market.zip.tmp").exists())
|
self.assertFalse((root / ".market.zip.tmp").exists())
|
||||||
|
|
||||||
|
|
||||||
|
class QgisProjectTest(unittest.TestCase):
|
||||||
|
def test_project_extent_tracks_building_info(self):
|
||||||
|
project_xml = b'''\xef\xbb\xbf<!DOCTYPE qgis PUBLIC 'http://mrcc.com/qgis.dtd' 'SYSTEM'>
|
||||||
|
<qgis>
|
||||||
|
<mapcanvas name="theMapCanvas"><extent><xmin>0</xmin><ymin>0</ymin><xmax>1</xmax><ymax>1</ymax></extent></mapcanvas>
|
||||||
|
<mapcanvas name="mAreaCanvas"><extent><xmin>100</xmin><ymin>100</ymin><xmax>200</xmax><ymax>200</ymax></extent></mapcanvas>
|
||||||
|
<projectlayers>
|
||||||
|
<maplayer><layername>BuildingInfo</layername><datasource>./BuildingInfo.gpkg|layername=BuildingInfo</datasource><extent><xmin>0</xmin><ymin>0</ymin><xmax>1</xmax><ymax>1</ymax></extent></maplayer>
|
||||||
|
</projectlayers>
|
||||||
|
</qgis>'''
|
||||||
|
with tempfile.TemporaryDirectory() as temp_dir:
|
||||||
|
root_dir = Path(temp_dir)
|
||||||
|
source_path = root_dir / "source.qgz"
|
||||||
|
output_path = root_dir / "GIS.qgz"
|
||||||
|
with zipfile.ZipFile(source_path, "w", compression=zipfile.ZIP_DEFLATED) as archive:
|
||||||
|
archive.writestr("GIS.qgs", project_xml)
|
||||||
|
archive.writestr("styles.db", b"style-data")
|
||||||
|
|
||||||
|
market = main.MarketFeature(1, "1-test", "test", box(0, 0, 1, 1))
|
||||||
|
result = main.copy_gis_project_to_market(source_path, output_path, market, (10, 20, 30, 40))
|
||||||
|
|
||||||
|
with zipfile.ZipFile(output_path) as archive:
|
||||||
|
updated_xml = archive.read("GIS.qgs")
|
||||||
|
self.assertTrue(updated_xml.startswith(b"\xef\xbb\xbf<!DOCTYPE qgis "))
|
||||||
|
self.assertEqual(archive.read("styles.db"), b"style-data")
|
||||||
|
project = ET.fromstring(updated_xml)
|
||||||
|
|
||||||
|
def read_extent(element):
|
||||||
|
return tuple(float(element.findtext(tag)) for tag in ("xmin", "ymin", "xmax", "ymax"))
|
||||||
|
|
||||||
|
canvas = project.find(".//mapcanvas[@name='theMapCanvas']/extent")
|
||||||
|
overview = project.find(".//mapcanvas[@name='mAreaCanvas']/extent")
|
||||||
|
building_layer = project.find(".//projectlayers/maplayer/extent")
|
||||||
|
self.assertEqual(read_extent(canvas), (9, 19, 31, 41))
|
||||||
|
self.assertEqual(read_extent(overview), (100, 100, 200, 200))
|
||||||
|
self.assertEqual(read_extent(building_layer), (10, 20, 30, 40))
|
||||||
|
self.assertEqual(result["extent_source"], "building_info")
|
||||||
|
self.assertEqual(result["canvas_extent"], [9.0, 19.0, 31.0, 41.0])
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|||||||
Reference in New Issue
Block a user