diff --git a/.gitignore b/.gitignore index f5630d8..58da789 100644 --- a/.gitignore +++ b/.gitignore @@ -3,7 +3,10 @@ __pycache__/ *.py[cod] # Local data artifacts -source/ +source/* +!source/MapPark/ +source/MapPark/* +!source/MapPark/GIS.qgz output/ output_*/ tmp/ diff --git a/README.md b/README.md index 5009d0e..a3c79a3 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,7 @@ BuildingInfo.gpkg 用户量.tif 信号差用户量.tif 弱覆盖占比.tif +移动优势弱覆盖用户.tif 楼宇指标.csv 栅格指标.csv ``` @@ -44,10 +45,11 @@ output/所有网格汇总.zip ├── 信号.tif ├── 用户量.tif ├── 信号差用户量.tif -└── 弱覆盖占比.tif +├── 弱覆盖占比.tif +└── 移动优势弱覆盖用户.tif ``` -汇总包不包含 CSV。程序会先合并本次参与处理的全部市场网格边界,再从原始 Grid 一次扫描生成四张全域 TIF;网格之间没有数据的区域保持透明。`BuildingInfo.gpkg` 为完整源文件副本,保留全部样式和支撑表;`GIS.qgz` 使用全网 BuildingInfo 范围作为初始画布。`用户量.tif` 和 `信号差用户量.tif` 的动态阈值按全域有效像元重新计算,不是简单拼接各市场图片。设置 `LIMIT` 时,TIF 汇总范围与本次受限处理的市场保持一致。 +汇总包不包含 CSV。程序会先合并本次参与处理的全部市场网格边界,再从原始 Grid 一次扫描生成五张全域 TIF;网格之间没有数据的区域保持透明。`BuildingInfo.gpkg` 为完整源文件副本,保留全部样式和支撑表;`GIS.qgz` 使用全网 BuildingInfo 范围作为初始画布。`用户量.tif`、`信号差用户量.tif` 和 `移动优势弱覆盖用户.tif` 的动态阈值按全域有效像元重新计算,不是简单拼接各市场图片。设置 `LIMIT` 时,TIF 汇总范围与本次受限处理的市场保持一致。 ## 裁切规则 @@ -59,7 +61,7 @@ output/所有网格汇总.zip 来源:`source/MapPark/GIS.qgz` -说明:以该文件为模板生成到每个结果包。模板已包含“弱覆盖占比”图层,默认不勾选。程序会读取 QGZ 内的项目 XML,把 BuildingInfo 图层范围更新为当前市场实际楼宇范围,并将 QGIS 主画布设置为该范围四周增加 5% 留白。打开项目后会直接定位到当前市场区域。 +说明:以该文件为模板生成到每个结果包。模板已包含五张专题 TIF 图层;“移动优势弱覆盖用户”作为当前核心目标图层默认勾选,其他专题栅格默认关闭,避免透明区域露出下层颜色而混淆目标区域。程序会读取 QGZ 内的项目 XML,把 BuildingInfo 图层范围更新为当前市场实际楼宇范围,并将 QGIS 主画布设置为该范围四周增加 5% 留白。打开项目后会直接定位到当前市场区域。 QGZ 内的样式数据库、其他图层配置和项目结构保持不变;如果当前市场没有 BuildingInfo 要素,画布范围会回退到市场网格范围。 @@ -133,7 +135,7 @@ QGZ 内的样式数据库、其他图层配置和项目结构保持不变;如 绿色到黄色、黄色到红色之间均为连续渐变。黄色设置在第 75 百分位,使红色热点主要集中于最高值约 25% 的区域。每个市场的数据范围不同,运行后可在 `output/summary.json` 的 `weak_cover_heatmap_results.color_anchor_values` 查看实际绿、黄、红数值锚点。 -相同数值始终映射为相同颜色;最低值和大量 `0` 值保持绿色,不会为了凑比例而按空间位置拆色。 +相同数值始终映射为相同颜色;最低值和大量 `0` 值保持绿色,不会为了凑比例而按空间位置拆色。若当前范围内所有有效值完全相同,则统一显示黄色,表示没有可用于区分内部高低的相对差异。 ### 弱覆盖占比.tif @@ -154,6 +156,25 @@ QGZ 内的样式数据库、其他图层配置和项目结构保持不变;如 `10% ~ 20%` 区间内占比越大越接近红色;达到 `20%` 后统一保持纯红色,不再继续加深。因此该图层表达的是“弱覆盖占比越高,颜色越偏红”,而不是弱覆盖采样点的绝对数量。 +### 移动优势弱覆盖用户.tif + +筛选字段:`grid.gpkg` 的 `ssb_rsrp_yd` + +热点强度字段:`grid.gpkg` 的 `rsrp_lt_neg105_dl` + +说明:用于定位“移动信号达到正常水平,同时电信弱覆盖用户采样量较多”的 Grid。仅当 `ssb_rsrp_yd > -95` 且 `rsrp_lt_neg105_dl > 0` 时参与渲染;移动信号未达到门槛、字段无效或电信弱覆盖采样量为 `0` 的区域保持透明。 + +| 动态位置 | 颜色 | 含义 | +| --- | --- | --- | +| 最高值 | 纯红 `RGB(255, 0, 0)` | 目标弱覆盖用户采样量最高 | +| 第 75 百分位 | 黄色 `RGB(255, 255, 0)` | 黄色动态锚点 | +| 最低正值 | 纯绿 `RGB(0, 176, 80)` | 目标弱覆盖用户采样量最低 | +| 不满足条件或弱覆盖量为 0 | 透明 | 非目标区域 | + +颜色在绿、黄、红三个锚点之间连续变化,默认使用独立平滑半径 `5`,在突出热点聚集趋势的同时保留较清晰的位置边界。单市场结果按当前市场内符合条件的像元建立动态色带;`所有网格汇总.zip` 按全部参与市场的符合条件像元重新建立全域色带,因此全网汇总图可用于跨市场统一比较。若当前范围内所有目标值完全相同,则统一显示黄色,表示目标存在但没有内部高低差异。 + +该字段表示弱覆盖采样量,不是去重后的真实用户数。图层的业务读法是:“有颜色表示移动覆盖达标且存在电信弱覆盖采样;颜色越红,目标弱覆盖用户采样越集中。” + ## CSV 指标表 两个 CSV 都使用 GBK 编码,方便直接用 Excel 打开。第一列均为 `网格ID`,值为当前市场网格唯一 ID,例如 `1-城区蓬江`,方便后续多市场合并。 @@ -235,13 +256,17 @@ CSV 中的 `移动信号` 和 `竟对信号` 使用同一套文字等级: | `WEAK_COVER_RATIO_BLUE_ANCHOR` | `5.0` | 弱覆盖占比蓝色锚点,单位 `%` | | `WEAK_COVER_RATIO_YELLOW_ANCHOR` | `10.0` | 弱覆盖占比黄色锚点,单位 `%` | | `WEAK_COVER_RATIO_RED_ANCHOR` | `20.0` | 弱覆盖占比红色端点,单位 `%` | +| `MOBILE_RSRP_FIELD` | `ssb_rsrp_yd` | 移动优势门槛使用的移动 RSRP 字段 | +| `MOBILE_ADVANTAGE_GOOD_RSRP_THRESHOLD` | `-95.0` | 移动优势门槛;仅严格大于该值的 Grid 参与目标图 | +| `MOBILE_ADVANTAGE_YELLOW_PERCENTILE` | `0.75` | 目标图黄色锚点百分位 | +| `MOBILE_ADVANTAGE_SMOOTH_RADIUS` | `5` | 目标图独立平滑半径;可改为 `10` 或其他非负整数试验效果 | | `HEATMAP_ALPHA` | `191` | 热力图有效像元透明度,约 75% 不透明 | ## 性能优化 当前处理流程已做以下加速: -- 同一市场的 Grid 数据只执行一次 RTree 查询和几何相交判断,同时缓存像元窗口并写出 `栅格指标.csv`;四张 TIF 顺序复用栅格矩阵内存。 +- 同一市场的 Grid 数据只执行一次 RTree 查询和几何相交判断,同时缓存像元窗口并写出 `栅格指标.csv`;五张 TIF 顺序复用栅格矩阵内存。 - Grid、Building 和 BuildingInfo 使用 Shapely 批量解析 WKB、判断几何相交并提取边界,减少大量 Python 逐要素调用。 - 市场边界使用 Prepared Geometry 加速同一边界与大量 Grid 候选的重复拓扑判断;汇总任务会先修复边界副本的无效拓扑,再执行全域合并。 - 热力图平滑使用 NumPy 滑动窗口矩阵乘法,一次处理完整像元矩阵,不再逐行执行卷积。 diff --git a/aidocs/project_context.md b/aidocs/project_context.md index bd896f8..11135a2 100644 --- a/aidocs/project_context.md +++ b/aidocs/project_context.md @@ -111,3 +111,11 @@ - 全网连续渐变汇总已重新生成并成功替换为正式 `output/所有网格汇总.zip`,大小为 137,972,582 字节;`弱覆盖占比.tif` 为 `8724 × 11440`,含 911 种有效颜色,绿、蓝、黄、红四个锚点和 ZIP CRC 均校验正常,不再保留 `_new` 过渡文件。 - `README.md` 与汇报 Markdown 已统一补齐成果图层图例:信号采用固定 RSRP 连续锚点,用户量采用动态四分位,信号差用户量采用动态百分位连续色带,弱覆盖占比采用 `0%` 绿、`5%` 蓝、`10%` 黄、`20%` 红的固定连续锚点;`>=20%` 保持纯红且不再继续加深。 - Word 汇报文档已依据最新 Markdown 重新生成,弱覆盖占比图例独立完整排版,表格行禁止跨页并启用跨页重复表头;通过 Word 导出 PDF 后逐页检查全部 15 页,未发现重叠、裁切、乱码或图例跨页问题。 +- 在 `dev` 分支新增 `移动优势弱覆盖用户.tif`:仅 `ssb_rsrp_yd > -95` 且 `rsrp_lt_neg105_dl > 0` 的 Grid 参与渲染,以电信弱覆盖采样量作为热点强度;不满足移动门槛、字段无效或弱覆盖量为 `0` 的区域保持透明。 +- 移动优势目标图采用最低正值绿、第 75 百分位黄、最高值红的连续动态色带;新增独立配置 `MOBILE_ADVANTAGE_SMOOTH_RADIUS = 5`,便于后续调整为 `10` 或其他非负值。单市场包按市场内分布建立色带,全网汇总包按全域符合条件像元重新计算色带。 +- 每个市场包和 `所有网格汇总.zip` 均新增移动优势目标图,`summary.json` 记录移动门槛、强度字段、平滑半径、符合条件 Grid 数和排除统计;独立图例写入 `mobile_advantage_legend.csv`。 +- `source/MapPark/GIS.qgz` 已永久加入默认勾选的“移动优势弱覆盖用户”RGBA 图层,其他四张专题栅格默认关闭,避免透明区域露出下层信号颜色而混淆目标范围;运行时同步刷新新图范围。为使分支和后续合并具备完整模板,`.gitignore` 仅放行并跟踪该 QGZ,其他 `source/` 大体量数据继续忽略。 +- 动态色带补充单值退化处理:当前范围内所有有效值完全相同时统一显示黄色,避免同一数值同时作为最大值却显示为绿色;平滑半径配置增加非负整数校验。 +- QGIS 中五张栅格图层的 `extent` 和 `wgs84extent` 改为按实际 TIF 宽高与像元尺寸计算,不再直接写市场边界,消除栅格 `ceil` 取整造成的最多一个像元范围偏差。 +- 单市场 `1-城区蓬江` 端到端验证通过:结果包包含 5 张 TIF,新图尺寸为 `2466 × 2581`,有效像元 1,530,902、有效颜色 660 种;动态绿/黄/红数值锚点约为 `1 / 32.16 / 3764.75`,符合条件 Grid 56,140 个。QGZ CRC、图层引用、默认勾选状态和 TIF 透明通道均校验正常;标准库单元测试增至 21 项并全部通过。 +- Word 汇报文档已同步新增目标图定位模型、图例、成果包和业务价值说明;导出 PDF 后逐页检查全部 16 页,未发现文字或表格重叠、裁切、乱码和跨页断裂。 diff --git a/aidocs/technical_presentation.md b/aidocs/technical_presentation.md index c1ae79a..64791cf 100644 --- a/aidocs/technical_presentation.md +++ b/aidocs/technical_presentation.md @@ -110,7 +110,7 @@ flowchart LR - 写入地理配准信息。 - 输出带透明度的专题图层。 -其中,信号质量、弱覆盖用户量和弱覆盖占比专题图采用连续热力表达,而不是将像元机械切成几种色块。颜色在空间上形成连续过渡,可以更自然地表现信号质量、弱覆盖规模和弱覆盖比例的聚集、扩散与变化趋势。 +其中,信号质量、弱覆盖用户量、弱覆盖占比和移动优势弱覆盖用户专题图采用连续热力表达,而不是将像元机械切成几种色块。颜色在空间上形成连续过渡,可以更自然地表现信号质量、弱覆盖规模、弱覆盖比例和目标用户聚集趋势。 所有专题 TIF 均采用 RGBA 透明图层:有效像元透明度约为 75%,无数据区域完全透明,可直接叠加卫星影像、建筑图层和其他 GIS 底图。空间平滑只作用于有效数据区域,不会为无采集区域虚构指标值。 @@ -150,6 +150,8 @@ flowchart LR 颜色在三个锚点之间连续变化,红色主要聚焦最高值约 25% 的热点区域;相同数值保持同色,不按像元位置强制拆分。 +当当前处理范围内所有有效值完全相同时,图层统一显示黄色,表示存在弱覆盖用户采样,但没有可用于区分内部高低的相对差异。 + 其优势在于: - 能适配不同市场之间的规模差异。 @@ -191,7 +193,27 @@ flowchart LR 该图采用固定锚点,便于不同市场之间进行同口径横向比较。需要特别说明的是:`10% ~ 20%` 区间内数值越大越红;达到 `20%` 后统一使用纯红色,因此 `20%`、`30%` 和更高占比在图例上均属于同一红色高风险端。 -### 10. GeoPackage 图层结构与样式保留 +### 10. 移动优势弱覆盖用户定位模型 + +移动优势弱覆盖用户图用于直接识别“移动网络具备承接能力、电信网络存在弱覆盖、且受影响用户采样较集中”的目标 Grid。 + +模型采用“准入门槛 + 热点强度”的简洁业务规则: + +- 移动信号门槛:`ssb_rsrp_yd > -95`。 +- 热点强度:电信弱覆盖采样量 `rsrp_lt_neg105_dl`。 +- 弱覆盖采样量为 `0`、移动信号未达到门槛或字段无效时保持透明。 +- 符合条件的区域按弱覆盖采样量进行绿、黄、红连续映射。 + +| 动态位置 | 颜色 | 业务含义 | +| --- | --- | --- | +| 最高值 | 纯红 | 移动具备优势且电信弱覆盖用户采样最集中 | +| 第 75 百分位 | 黄色 | 重点目标区域的动态锚点 | +| 最低正值 | 纯绿 | 存在目标用户采样,但聚集程度较低 | +| 不满足条件或弱覆盖量为 0 | 透明 | 非目标区域 | + +目标图使用独立平滑半径 `5`,在表达聚集趋势的同时保留较清晰的空间定位。市场级图按本市场数据分布建立色带,用于发现市场内部热点;全网汇总图按全域数据重新建立统一色带,用于跨市场比较。若当前范围内所有目标值完全相同,则统一显示黄色,表示目标存在但没有内部高低差异。该指标描述的是弱覆盖用户采样量,并非去重后的真实用户数。 + +### 11. GeoPackage 图层结构与样式保留 建筑信息成果并非普通属性表导出,而是保留为标准 GeoPackage 空间图层。 @@ -206,9 +228,9 @@ flowchart LR BuildingInfo 图层不由平台重新定义业务颜色,其图例直接沿用源空间数据中已配置的 QGIS 样式。市场级成果保留相交楼宇和原始样式;全网汇总成果保留完整 BuildingInfo 图层及其样式信息。 -### 11. QGIS 工程化成果封装 +### 12. QGIS 工程化成果封装 -平台会将 GIS 项目模板与每个市场的专题图层、建筑图层和指标表统一打包。项目模板已注册信号、用户量、弱覆盖用户量和弱覆盖占比四张专题图,打开后可以直接切换图层查看。 +平台会将 GIS 项目模板与每个市场的专题图层、建筑图层和指标表统一打包。项目模板已注册信号、用户量、弱覆盖用户量、弱覆盖占比和移动优势弱覆盖用户五张专题图,打开后可以直接切换图层查看。其中移动优势弱覆盖用户图作为核心目标图层默认勾选,其他专题栅格默认关闭,避免透明区域露出下层颜色而混淆目标范围。 最终交付物不再是零散文件,而是围绕市场网格组织好的工程化成果包。 @@ -221,7 +243,7 @@ BuildingInfo 图层不由平台重新定义业务颜色,其图例直接沿用 项目打开时会根据当前市场的 BuildingInfo 实际空间范围自动设置主画布,并在范围四周保留适当留白。这样成果包打开后即可进入当前市场的楼宇区域,减少人工缩放和图层定位操作。 -### 12. 指标表标准化输出 +### 13. 指标表标准化输出 平台同步输出楼宇指标表和栅格指标表,并统一增加市场网格标识字段。 @@ -252,10 +274,11 @@ BuildingInfo 图层不由平台重新定义业务颜色,其图例直接沿用 | 用户量.tif | 总采样点(mro_total_dl) | 市场内动态四分位:红、黄、蓝、绿 | 透明 | | 信号差用户量.tif | 弱覆盖采样点(rsrp_lt_neg105_dl) | 最低值绿、第 75 百分位黄、最高值红,连续渐变 | 透明 | | 弱覆盖占比.tif | rsrp_lt_neg105_dl / mro_total_dl | `0%` 绿、`5%` 蓝、`10%` 黄、`20%` 红,区间连续渐变 | 总采样点无效时透明 | +| 移动优势弱覆盖用户.tif | `ssb_rsrp_yd > -95` 时的 `rsrp_lt_neg105_dl` | 最低正值绿、第 75 百分位黄、最高值红,连续渐变 | 门槛不满足或弱覆盖量为 0 时透明 | | BuildingInfo.gpkg | 建筑信息空间图层 | 沿用源 QGIS 图层样式 | 不适用 | | GIS.qgz | GIS 项目与图层组织 | 集中加载上述图层并自动定位 | 不适用 | -四张专题 TIF 的有效像元均使用约 75% 不透明度,便于在保留专题颜色的同时观察底图和建筑纹理。信号和弱覆盖占比采用固定物理或业务锚点,适合跨市场比较;用户量和弱覆盖用户量采用动态分布规则,重点反映当前市场内部的相对热点。 +五张专题 TIF 的有效像元均使用约 75% 不透明度,便于在保留专题颜色的同时观察底图和建筑纹理。信号和弱覆盖占比采用固定物理或业务锚点,适合跨市场比较;用户量、弱覆盖用户量和移动优势弱覆盖用户采用动态分布规则,重点反映当前处理范围内的相对热点。 ### 2. 标准成果包 @@ -267,10 +290,11 @@ BuildingInfo 图层不由平台重新定义业务颜色,其图例直接沿用 - 用户量专题图 - 弱覆盖用户量专题图 - 弱覆盖占比专题图 +- 移动优势弱覆盖用户专题图 - 楼宇指标明细表 - 栅格指标明细表 -同时生成全网汇总成果包,包含四张全域专题 TIF、完整 BuildingInfo 空间图层和 GIS 项目,不包含指标 CSV。全网汇总中的用户量和弱覆盖用户量会按全域有效像元重新计算动态阈值;信号和弱覆盖占比继续使用固定锚点,确保市场级与全网级解释口径一致。 +同时生成全网汇总成果包,包含五张全域专题 TIF、完整 BuildingInfo 空间图层和 GIS 项目,不包含指标 CSV。全网汇总中的用户量、弱覆盖用户量和移动优势弱覆盖用户会按全域有效像元重新计算动态阈值;信号和弱覆盖占比继续使用固定锚点,确保市场级与全网级解释口径一致。 全局还会生成图例说明和处理汇总信息,用于追溯每个市场的输出结果、阈值记录和处理统计。 @@ -290,7 +314,7 @@ BuildingInfo 图层不由平台重新定义业务颜色,其图例直接沿用 ### 4. 支撑精细化运营 -成果可以直接用于识别信号较差区域、弱覆盖用户聚集区域、弱覆盖高占比区域和高用户量区域,为后续优化决策提供依据。 +成果可以直接用于识别信号较差区域、弱覆盖用户聚集区域、弱覆盖高占比区域、高用户量区域,以及“移动信号良好且电信弱覆盖用户集中”的目标区域,为网络优化和精细化运营提供依据。 ### 5. 便于汇报展示 diff --git a/main.py b/main.py index 9fce1d1..d7f9b52 100644 --- a/main.py +++ b/main.py @@ -31,6 +31,7 @@ GIS_PROJECT_SOURCE = MAP_PARK_DIR / "GIS.qgz" # 每个市场网格结果包要 RSRP_FIELD = "ssb_rsrp_dl" # 用于生成热力图的电信 RSRP 字段名。 EXCELLENT_CELL_FIELD = "mro_total_dl" # 用于生成用户量热力图的字段名,值越大越红。 WEAK_COVER_FIELD = "rsrp_lt_neg105_dl" # 用于生成信号差用户量热力图的字段名,值越大越红。 +MOBILE_RSRP_FIELD = "ssb_rsrp_yd" # 移动 RSRP 字段名,用于筛选移动信号达到正常水平的 Grid。 MARKET_LAYER = None # 市场网格图层名;None 表示自动使用 GPKG 中第一个 feature 图层。 GRID_LAYER = None # Grid 指标图层名;None 表示自动使用 GPKG 中第一个 feature 图层。 BUILDING_LAYER = None # Building 建筑指标图层名;None 表示自动使用 GPKG 中第一个 feature 图层。 @@ -56,6 +57,10 @@ PROJECT_EXTENT_PADDING_RATIO = 0.05 # QGIS 打开后的画布留白比例;0.0 USER_COUNT_OUTPUT_FILENAME = "用户量.tif" # 采样点数量热力图输出文件名。 WEAK_COVER_OUTPUT_FILENAME = "信号差用户量.tif" # 信号差用户量热力图输出文件名。 WEAK_COVER_RATIO_OUTPUT_FILENAME = "弱覆盖占比.tif" # 弱覆盖采样点占总采样点比例的分级 TIF 输出文件名。 +MOBILE_ADVANTAGE_OUTPUT_FILENAME = "移动优势弱覆盖用户.tif" # 移动信号好且电信弱覆盖用户聚集的目标热力图文件名。 +MOBILE_ADVANTAGE_GOOD_RSRP_THRESHOLD = -95.0 # 移动信号门槛;仅 ssb_rsrp_yd 严格大于该值的 Grid 参与目标热力图。 +MOBILE_ADVANTAGE_YELLOW_PERCENTILE = 0.75 # 目标热力图黄色锚点百分位;高于该位置的热点逐步由黄色趋向红色。 +MOBILE_ADVANTAGE_SMOOTH_RADIUS = 5 # 目标热力图平滑半径;默认 5 以保留定位精度,可改为 10 或其他非负整数。 WEAK_COVER_RATIO_GREEN_ANCHOR = 0.0 # 弱覆盖占比连续色带绿色端点,单位为百分比。 WEAK_COVER_RATIO_BLUE_ANCHOR = 5.0 # 弱覆盖占比连续色带蓝色锚点,单位为百分比。 WEAK_COVER_RATIO_YELLOW_ANCHOR = 10.0 # 弱覆盖占比连续色带黄色锚点,单位为百分比。 @@ -65,7 +70,7 @@ WEAK_COVER_RATIO_BLUE = (0, 112, 255) # 弱覆盖占比偏低档颜色。 WEAK_COVER_RATIO_YELLOW = (255, 255, 0) # 弱覆盖占比偏高档颜色。 WEAK_COVER_RATIO_RED = (255, 0, 0) # 弱覆盖占比高值档颜色。 SUMMARY_OUTPUT_ENABLED = True # 是否额外生成全部市场网格汇总 TIF 结果包。 -SUMMARY_OUTPUT_FILENAME = "所有网格汇总.zip" # 汇总结果包文件名,包含四张 TIF、GIS.qgz 和完整 BuildingInfo.gpkg。 +SUMMARY_OUTPUT_FILENAME = "所有网格汇总.zip" # 汇总结果包文件名,包含五张 TIF、GIS.qgz 和完整 BuildingInfo.gpkg。 SUMMARY_MARKET_CODE = "所有网格汇总" # 汇总任务在日志和 summary.json 中使用的标识。 SUMMARY_MAX_RASTER_PIXELS = 100_000_000 # 汇总 TIF 最大像元数;提高该值可增强清晰度,但会增加内存、耗时和文件体积。 EXCELLENT_PERCENT_STEPS = (0.25, 0.5, 0.75) # 按最终有效像元降序四等分,取 25%、50%、75% 位置作为结果记录阈值。 @@ -155,6 +160,10 @@ def weak_cover_ratio_raster_filename(): return WEAK_COVER_RATIO_OUTPUT_FILENAME +def mobile_advantage_raster_filename(): + return MOBILE_ADVANTAGE_OUTPUT_FILENAME + + def set_xml_extent(extent_element, extent): for tag, value in zip(("xmin", "ymin", "xmax", "ymax"), extent): child = extent_element.find(tag) @@ -181,7 +190,7 @@ def serialize_qgis_project(root, original_data): 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): +def copy_gis_project_to_market(source_path, output_path, market, building_extent, raster_extent=None): if output_path.exists() and not OVERWRITE: return { "market_code": market.code, @@ -193,6 +202,7 @@ def copy_gis_project_to_market(source_path, output_path, market, building_extent 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 + raster_layer_extent = tuple(raster_extent) if raster_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(): @@ -238,16 +248,17 @@ def copy_gis_project_to_market(source_path, output_path, market, building_extent USER_COUNT_OUTPUT_FILENAME, WEAK_COVER_OUTPUT_FILENAME, WEAK_COVER_RATIO_OUTPUT_FILENAME, + MOBILE_ADVANTAGE_OUTPUT_FILENAME, } for layer in root.findall(".//projectlayers/maplayer"): datasource = (layer.findtext("datasource") or "").replace("\\", "/") if Path(datasource).name not in raster_filenames: continue for extent_tag in ("extent", "wgs84extent"): - raster_extent = layer.find(extent_tag) - if raster_extent is None: - raster_extent = ET.SubElement(layer, extent_tag) - set_xml_extent(raster_extent, market.geometry.bounds) + raster_extent_element = layer.find(extent_tag) + if raster_extent_element is None: + raster_extent_element = ET.SubElement(layer, extent_tag) + set_xml_extent(raster_extent_element, raster_layer_extent) data = serialize_qgis_project(root, data) target_zip.writestr(info, data) temp_output_path.replace(output_path) @@ -264,6 +275,7 @@ def copy_gis_project_to_market(source_path, output_path, market, building_extent "project_member": project_member, "extent_source": extent_source, "building_extent": list(layer_extent), + "raster_extent": list(raster_layer_extent), "canvas_extent": list(canvas_extent), "padding_ratio": PROJECT_EXTENT_PADDING_RATIO, "bytes": output_path.stat().st_size, @@ -952,6 +964,7 @@ def percentile_scores_for_values(values, data_mask, anchor_scores=(0.0, 0.5, 1.0 unique_values, inverse, counts = np.unique(valid_values, return_inverse=True, return_counts=True) if unique_values.size == 1: + scores[data_mask] = anchor_scores[len(anchor_scores) // 2] return scores, [float(unique_values[0])] * len(anchor_scores) cumulative_before = np.cumsum(counts, dtype=np.int64) - counts @@ -1025,6 +1038,11 @@ def gaussian_kernel(radius): return kernel / kernel.sum() +def validate_smoothing_radius(name, radius): + if isinstance(radius, bool) or not isinstance(radius, int) or radius < 0: + raise ValueError(f"{name} 必须是非负整数") + + def convolve_axis(array, kernel, axis): if kernel.size == 1: return array @@ -1060,6 +1078,14 @@ def to_float(value): return None +def mobile_advantage_target_value(mobile_rsrp, weak_cover_users): + if mobile_rsrp is None or weak_cover_users is None: + return None + if mobile_rsrp <= MOBILE_ADVANTAGE_GOOD_RSRP_THRESHOLD or weak_cover_users <= 0: + return None + return weak_cover_users + + def geotiff_extratags(minx, maxy, pixel_width, pixel_height): model_pixel_scale = (pixel_width, pixel_height, 0.0) model_tiepoint = (0.0, 0.0, 0.0, minx, maxy, 0.0) @@ -1143,19 +1169,23 @@ def market_raster_layout(bounds, pixel_width, pixel_height, max_raster_pixels=No return minx, miny, maxx, maxy, pixel_width, pixel_height, width, height, scale_factor -def smooth_weighted_values(value_sum, value_weight): +def smooth_weighted_values(value_sum, value_weight, radius): 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_sum = gaussian_blur(value_sum, radius) + smoothed_weight = gaussian_blur(value_weight, radius) 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"]) + smoothed_value, data_mask = smooth_weighted_values( + state["value_sum"], + state["value_weight"], + HEATMAP_SMOOTH_RADIUS, + ) if np.any(data_mask): image[data_mask, :3] = heatmap_rgb_for_values(smoothed_value)[data_mask] image[data_mask, 3] = HEATMAP_ALPHA @@ -1204,16 +1234,22 @@ def render_percentile_heatmap_raster( width, height, candidates, + yellow_percentile, + smooth_radius, ): image = np.full((height, width, 4), TRANSPARENT_PIXEL, dtype=np.uint8) - smoothed_value, data_mask = smooth_weighted_values(state["value_sum"], state["value_weight"]) + smoothed_value, data_mask = smooth_weighted_values( + state["value_sum"], + state["value_weight"], + smooth_radius, + ) anchor_values = [] if np.any(data_mask): - anchor_scores = (0.0, WEAK_COVER_YELLOW_PERCENTILE, 1.0) + anchor_scores = (0.0, 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, + yellow_score=yellow_percentile, )[data_mask] image[data_mask, 3] = HEATMAP_ALPHA @@ -1227,13 +1263,13 @@ def render_percentile_heatmap_raster( "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, + "yellow_percentile": 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, + "smooth_radius": smooth_radius, "width": width, "height": height, "skipped": False, @@ -1253,7 +1289,11 @@ def render_weak_cover_ratio_raster( 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"]) + smoothed_value, data_mask = smooth_weighted_values( + state["value_sum"], + state["value_weight"], + HEATMAP_SMOOTH_RADIUS, + ) if np.any(data_mask): image[data_mask, :3] = weak_cover_ratio_rgb_for_values(smoothed_value)[data_mask] image[data_mask, 3] = HEATMAP_ALPHA @@ -1344,6 +1384,7 @@ def export_market_grid_outputs( user_count_path, weak_cover_path, weak_cover_ratio_path, + mobile_advantage_path, csv_path, max_raster_pixels=None, ): @@ -1353,7 +1394,7 @@ def export_market_grid_outputs( pixel_height, max_raster_pixels, ) - raster_fields = (RSRP_FIELD, EXCELLENT_CELL_FIELD, WEAK_COVER_FIELD) + raster_fields = (RSRP_FIELD, EXCELLENT_CELL_FIELD, WEAK_COVER_FIELD, MOBILE_RSRP_FIELD) skipped_no_value = {field: 0 for field in raster_fields} painted_cells = {field: 0 for field in raster_fields} raster_cells = [] @@ -1444,6 +1485,7 @@ def export_market_grid_outputs( numeric_values[RSRP_FIELD], numeric_values[EXCELLENT_CELL_FIELD], numeric_values[WEAK_COVER_FIELD], + numeric_values[MOBILE_RSRP_FIELD], ) ) finally: @@ -1500,6 +1542,8 @@ def export_market_grid_outputs( width, height, candidates, + WEAK_COVER_YELLOW_PERCENTILE, + HEATMAP_SMOOTH_RADIUS, ) del weak_cover_state @@ -1538,7 +1582,70 @@ def export_market_grid_outputs( ) del ratio_state, ratio_value_sum, ratio_value_weight - for result in (signal_result, user_count_result, weak_cover_result, weak_cover_ratio_result): + mobile_advantage_value_sum = np.zeros((height, width), dtype=np.float32) + mobile_advantage_value_weight = np.zeros((height, width), dtype=np.uint16) + mobile_advantage_cells = 0 + excluded_by_mobile_gate = 0 + excluded_without_weak_users = 0 + for cell in raster_cells: + mobile_rsrp = cell[7] + weak_cover_users = cell[6] + target_value = mobile_advantage_target_value(mobile_rsrp, weak_cover_users) + if target_value is None: + if mobile_rsrp is None or mobile_rsrp <= MOBILE_ADVANTAGE_GOOD_RSRP_THRESHOLD: + excluded_by_mobile_gate += 1 + else: + excluded_without_weak_users += 1 + continue + row_start, row_stop, col_start, col_stop = cell[:4] + mobile_advantage_value_sum[row_start:row_stop, col_start:col_stop] += target_value + mobile_advantage_value_weight[row_start:row_stop, col_start:col_stop] += 1 + mobile_advantage_cells += 1 + mobile_advantage_state = { + "value_sum": mobile_advantage_value_sum, + "value_weight": mobile_advantage_value_weight, + "painted_cells": mobile_advantage_cells, + "skipped_no_value": excluded_by_mobile_gate + excluded_without_weak_users, + } + mobile_advantage_result = render_percentile_heatmap_raster( + mobile_advantage_state, + market, + mobile_advantage_path, + minx, + maxy, + pixel_width, + pixel_height, + width, + height, + candidates, + MOBILE_ADVANTAGE_YELLOW_PERCENTILE, + MOBILE_ADVANTAGE_SMOOTH_RADIUS, + ) + mobile_advantage_result.update({ + "gate_field": MOBILE_RSRP_FIELD, + "gate_operator": ">", + "gate_threshold": MOBILE_ADVANTAGE_GOOD_RSRP_THRESHOLD, + "value_field": WEAK_COVER_FIELD, + "zero_weak_cover_is_transparent": True, + "eligible_cells": mobile_advantage_cells, + "excluded_by_mobile_gate": excluded_by_mobile_gate, + "excluded_without_weak_users": excluded_without_weak_users, + }) + del mobile_advantage_state, mobile_advantage_value_sum, mobile_advantage_value_weight + + for result in ( + signal_result, + user_count_result, + weak_cover_result, + weak_cover_ratio_result, + mobile_advantage_result, + ): + result["raster_extent"] = [ + minx, + maxy - height * pixel_height, + minx + width * pixel_width, + maxy, + ] result["pixel_width"] = pixel_width result["pixel_height"] = pixel_height result["resolution_scale_factor"] = scale_factor @@ -1552,7 +1659,14 @@ def export_market_grid_outputs( "row_count": csv_rows, "skipped": csv_skipped, } - return signal_result, user_count_result, weak_cover_result, weak_cover_ratio_result, csv_result + return ( + signal_result, + user_count_result, + weak_cover_result, + weak_cover_ratio_result, + mobile_advantage_result, + csv_result, + ) def write_heatmap_legend(output_dir, field): @@ -1605,6 +1719,19 @@ def write_weak_cover_ratio_legend(output_dir): return legend_path +def write_mobile_advantage_legend(output_dir): + legend_path = output_dir / "mobile_advantage_legend.csv" + with legend_path.open("w", newline="", encoding="utf-8-sig") as file: + writer = csv.writer(file) + writer.writerow(["gate", "value_field", "percentile_anchor", "red", "green", "blue", "alpha", "meaning"]) + gate = f"{MOBILE_RSRP_FIELD} > {MOBILE_ADVANTAGE_GOOD_RSRP_THRESHOLD}" + writer.writerow([gate, WEAK_COVER_FIELD, "highest_value", *CONTINUOUS_HEATMAP_RED, HEATMAP_ALPHA, "most target weak-cover users"]) + writer.writerow([gate, WEAK_COVER_FIELD, f"percentile_{MOBILE_ADVANTAGE_YELLOW_PERCENTILE}", *CONTINUOUS_HEATMAP_YELLOW, HEATMAP_ALPHA, "continuous yellow anchor"]) + writer.writerow([gate, WEAK_COVER_FIELD, "lowest_positive_value", *CONTINUOUS_HEATMAP_GREEN, HEATMAP_ALPHA, "fewest target weak-cover users"]) + writer.writerow([gate, WEAK_COVER_FIELD, "gate_failed_or_zero", 0, 0, 0, 0, "transparent non-target area"]) + return legend_path + + @contextmanager def open_read_connection(path): conn = sqlite3.connect(path) @@ -1669,9 +1796,17 @@ def process_market(market, pixel_width, pixel_height, output_dir): user_count_path = market_dir / excellent_heatmap_raster_filename(EXCELLENT_CELL_FIELD) weak_cover_path = market_dir / weak_cover_heatmap_raster_filename(WEAK_COVER_FIELD) weak_cover_ratio_path = market_dir / weak_cover_ratio_raster_filename() + mobile_advantage_path = market_dir / mobile_advantage_raster_filename() grid_metrics_path = market_dir / GRID_METRICS_CSV_FILENAME grid_started_at = perf_counter() - signal_result, user_count_result, weak_cover_result, weak_cover_ratio_result, grid_metrics_result = export_market_grid_outputs( + ( + signal_result, + user_count_result, + weak_cover_result, + weak_cover_ratio_result, + mobile_advantage_result, + grid_metrics_result, + ) = export_market_grid_outputs( grid_conn, grid_layer, grid_rtree, @@ -1682,6 +1817,7 @@ def process_market(market, pixel_width, pixel_height, output_dir): user_count_path, weak_cover_path, weak_cover_ratio_path, + mobile_advantage_path, grid_metrics_path, ) grid_elapsed = perf_counter() - grid_started_at @@ -1700,6 +1836,7 @@ def process_market(market, pixel_width, pixel_height, output_dir): gis_project_path, market, building_info_result["extent"], + signal_result["raster_extent"], ) building_metrics_path = market_dir / BUILDING_METRICS_CSV_FILENAME building_metrics_result = export_market_building_metrics_csv( @@ -1727,6 +1864,7 @@ def process_market(market, pixel_width, pixel_height, output_dir): "excellent_heatmap_result": rewrite_result_for_zip(user_count_result, zip_path, market_dir), "weak_cover_heatmap_result": rewrite_result_for_zip(weak_cover_result, zip_path, market_dir), "weak_cover_ratio_result": rewrite_result_for_zip(weak_cover_ratio_result, zip_path, market_dir), + "mobile_advantage_result": rewrite_result_for_zip(mobile_advantage_result, zip_path, market_dir), "building_info_result": rewrite_result_for_zip(building_info_result, zip_path, market_dir), "building_metrics_csv_result": rewrite_result_for_zip(building_metrics_result, zip_path, market_dir), "grid_metrics_csv_result": rewrite_result_for_zip(grid_metrics_result, zip_path, market_dir), @@ -1768,7 +1906,15 @@ def process_summary_grid_outputs(markets, pixel_width, pixel_height, output_dir) user_count_path = summary_dir / excellent_heatmap_raster_filename(EXCELLENT_CELL_FIELD) weak_cover_path = summary_dir / weak_cover_heatmap_raster_filename(WEAK_COVER_FIELD) weak_cover_ratio_path = summary_dir / weak_cover_ratio_raster_filename() - signal_result, user_count_result, weak_cover_result, weak_cover_ratio_result, _ = export_market_grid_outputs( + mobile_advantage_path = summary_dir / mobile_advantage_raster_filename() + ( + signal_result, + user_count_result, + weak_cover_result, + weak_cover_ratio_result, + mobile_advantage_result, + _, + ) = export_market_grid_outputs( grid_conn, grid_layer, grid_rtree, @@ -1779,6 +1925,7 @@ def process_summary_grid_outputs(markets, pixel_width, pixel_height, output_dir) user_count_path, weak_cover_path, weak_cover_ratio_path, + mobile_advantage_path, None, SUMMARY_MAX_RASTER_PIXELS, ) @@ -1803,6 +1950,7 @@ def process_summary_grid_outputs(markets, pixel_width, pixel_height, output_dir) gis_project_path, summary_market, building_info_extent, + signal_result["raster_extent"], ) zip_result = write_market_zip(summary_dir, zip_path, summary_market) return { @@ -1811,6 +1959,7 @@ def process_summary_grid_outputs(markets, pixel_width, pixel_height, output_dir) "excellent_heatmap_result": rewrite_result_for_zip(user_count_result, zip_path, summary_dir), "weak_cover_heatmap_result": rewrite_result_for_zip(weak_cover_result, zip_path, summary_dir), "weak_cover_ratio_result": rewrite_result_for_zip(weak_cover_ratio_result, zip_path, summary_dir), + "mobile_advantage_result": rewrite_result_for_zip(mobile_advantage_result, zip_path, summary_dir), "building_info_result": rewrite_result_for_zip(building_info_result, zip_path, summary_dir), "gis_project_result": rewrite_result_for_zip(gis_project_result, zip_path, summary_dir), "zip_result": zip_result, @@ -1831,6 +1980,12 @@ def main(): 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") + validate_smoothing_radius("HEATMAP_SMOOTH_RADIUS", HEATMAP_SMOOTH_RADIUS) + if not math.isfinite(MOBILE_ADVANTAGE_GOOD_RSRP_THRESHOLD): + raise ValueError("MOBILE_ADVANTAGE_GOOD_RSRP_THRESHOLD 必须是有限数值") + if not 0 < MOBILE_ADVANTAGE_YELLOW_PERCENTILE < 1: + raise ValueError("MOBILE_ADVANTAGE_YELLOW_PERCENTILE 必须大于 0 且小于 1") + validate_smoothing_radius("MOBILE_ADVANTAGE_SMOOTH_RADIUS", MOBILE_ADVANTAGE_SMOOTH_RADIUS) if not 0 <= WEAK_COVER_RATIO_GREEN_ANCHOR < WEAK_COVER_RATIO_BLUE_ANCHOR < WEAK_COVER_RATIO_YELLOW_ANCHOR < WEAK_COVER_RATIO_RED_ANCHOR: raise ValueError("弱覆盖占比锚点必须满足 0 <= GREEN < BLUE < YELLOW < RED") if MAX_RASTER_PIXELS is not None and MAX_RASTER_PIXELS <= 0: @@ -1898,6 +2053,7 @@ def main(): excellent_legend_path = write_excellent_heatmap_legend(OUTPUT_DIR, EXCELLENT_CELL_FIELD) weak_cover_legend_path = write_weak_cover_heatmap_legend(OUTPUT_DIR, WEAK_COVER_FIELD) weak_cover_ratio_legend_path = write_weak_cover_ratio_legend(OUTPUT_DIR) + mobile_advantage_legend_path = write_mobile_advantage_legend(OUTPUT_DIR) worker_count = max(1, min(MAX_WORKERS, len(markets))) if markets else 1 print(f"市场网格数量:{len(markets)}") print(f"并行进程数:{worker_count}") @@ -1909,6 +2065,7 @@ def main(): print(f"用户量图例:{excellent_legend_path}") print(f"信号差用户量图例:{weak_cover_legend_path}") print(f"弱覆盖占比图例:{weak_cover_ratio_legend_path}") + print(f"移动优势弱覆盖用户图例:{mobile_advantage_legend_path}") started_at = perf_counter() outcomes = [] @@ -1958,6 +2115,7 @@ def main(): "excellent_heatmap_results": "excellent_heatmap_result", "weak_cover_heatmap_results": "weak_cover_heatmap_result", "weak_cover_ratio_results": "weak_cover_ratio_result", + "mobile_advantage_results": "mobile_advantage_result", "building_info_results": "building_info_result", "building_metrics_csv_results": "building_metrics_csv_result", "grid_metrics_csv_results": "grid_metrics_csv_result", @@ -1973,6 +2131,10 @@ def main(): "rsrp_field": RSRP_FIELD, "excellent_cell_field": EXCELLENT_CELL_FIELD, "weak_cover_field": WEAK_COVER_FIELD, + "mobile_rsrp_field": MOBILE_RSRP_FIELD, + "mobile_advantage_gate": f"{MOBILE_RSRP_FIELD} > {MOBILE_ADVANTAGE_GOOD_RSRP_THRESHOLD}", + "mobile_advantage_value_field": WEAK_COVER_FIELD, + "mobile_advantage_smooth_radius": MOBILE_ADVANTAGE_SMOOTH_RADIUS, "weak_cover_ratio_formula": f"{WEAK_COVER_FIELD} / {EXCELLENT_CELL_FIELD} * 100", "clip_mode": CLIP_MODE, "scale": SCALE, diff --git a/source/MapPark/GIS.qgz b/source/MapPark/GIS.qgz new file mode 100644 index 0000000..f2c45d0 Binary files /dev/null and b/source/MapPark/GIS.qgz differ diff --git a/tests/test_main.py b/tests/test_main.py index d5c00ac..6fefb2a 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -76,6 +76,37 @@ class ContinuousHeatmapTest(unittest.TestCase): ]) self.assertEqual(colors[0, 4].tolist(), list(main.CONTINUOUS_HEATMAP_RED)) + def test_single_value_percentile_heatmap_uses_yellow_anchor(self): + values = np.full((2, 2), 7.0, dtype=np.float32) + mask = np.ones(values.shape, dtype=bool) + anchor_scores = (0.0, main.WEAK_COVER_YELLOW_PERCENTILE, 1.0) + scores, anchors = main.percentile_scores_for_values(values, mask, anchor_scores) + colors = main.continuous_heatmap_rgb_for_scores(scores, main.WEAK_COVER_YELLOW_PERCENTILE) + + self.assertEqual(anchors, [7.0, 7.0, 7.0]) + self.assertTrue(np.all(colors == main.CONTINUOUS_HEATMAP_YELLOW)) + + +class MobileAdvantageHeatmapTest(unittest.TestCase): + def test_target_requires_good_mobile_signal_and_positive_weak_users(self): + self.assertIsNone(main.mobile_advantage_target_value(None, 10.0)) + self.assertIsNone(main.mobile_advantage_target_value(-95.0, 10.0)) + self.assertIsNone(main.mobile_advantage_target_value(-94.9, None)) + self.assertIsNone(main.mobile_advantage_target_value(-94.9, 0.0)) + self.assertIsNone(main.mobile_advantage_target_value(-94.9, -1.0)) + self.assertEqual(main.mobile_advantage_target_value(-94.9, 12.0), 12.0) + + def test_target_heatmap_uses_independent_smoothing_radius(self): + self.assertEqual(main.MOBILE_ADVANTAGE_SMOOTH_RADIUS, 5) + + def test_smoothing_radius_must_be_a_nonnegative_integer(self): + main.validate_smoothing_radius("radius", 0) + main.validate_smoothing_radius("radius", 10) + for value in (-1, 1.5, True): + with self.subTest(value=value): + with self.assertRaises(ValueError): + main.validate_smoothing_radius("radius", value) + class WeakCoverRatioTest(unittest.TestCase): def test_continuous_percentage_color_anchors(self): @@ -197,6 +228,7 @@ class QgisProjectTest(unittest.TestCase): BuildingInfo./BuildingInfo.gpkg|layername=BuildingInfo0011 弱覆盖占比./弱覆盖占比.tif100100200200100100200200 + 移动优势弱覆盖用户./移动优势弱覆盖用户.tif100100200200100100200200 ''' with tempfile.TemporaryDirectory() as temp_dir: @@ -208,7 +240,13 @@ class QgisProjectTest(unittest.TestCase): 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)) + result = main.copy_gis_project_to_market( + source_path, + output_path, + market, + (10, 20, 30, 40), + (0, -1, 2, 1), + ) with zipfile.ZipFile(output_path) as archive: updated_xml = archive.read("GIS.qgs") @@ -227,14 +265,51 @@ class QgisProjectTest(unittest.TestCase): for layer in project.findall(".//projectlayers/maplayer") if layer.findtext("datasource") == "./弱覆盖占比.tif" ) + mobile_advantage_layer = next( + layer + for layer in project.findall(".//projectlayers/maplayer") + if layer.findtext("datasource") == f"./{main.MOBILE_ADVANTAGE_OUTPUT_FILENAME}" + ) 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(read_extent(ratio_layer.find("extent")), (0, 0, 1, 1)) - self.assertEqual(read_extent(ratio_layer.find("wgs84extent")), (0, 0, 1, 1)) + self.assertEqual(read_extent(ratio_layer.find("extent")), (0, -1, 2, 1)) + self.assertEqual(read_extent(ratio_layer.find("wgs84extent")), (0, -1, 2, 1)) + self.assertEqual(read_extent(mobile_advantage_layer.find("extent")), (0, -1, 2, 1)) + self.assertEqual(read_extent(mobile_advantage_layer.find("wgs84extent")), (0, -1, 2, 1)) self.assertEqual(result["extent_source"], "building_info") + self.assertEqual(result["raster_extent"], [0, -1, 2, 1]) self.assertEqual(result["canvas_extent"], [9.0, 19.0, 31.0, 41.0]) + def test_repository_template_contains_mobile_advantage_layer(self): + with zipfile.ZipFile(main.GIS_PROJECT_SOURCE) as archive: + self.assertIsNone(archive.testzip()) + qgs_members = [name for name in archive.namelist() if name.lower().endswith(".qgs")] + self.assertEqual(len(qgs_members), 1) + project = ET.fromstring(archive.read(qgs_members[0])) + + layer = next( + item + for item in project.findall(".//projectlayers/maplayer") + if item.findtext("layername") == Path(main.MOBILE_ADVANTAGE_OUTPUT_FILENAME).stem + ) + self.assertEqual(layer.findtext("datasource"), f"./{main.MOBILE_ADVANTAGE_OUTPUT_FILENAME}") + layer_id = layer.findtext("id") + tree_layer = next( + item + for item in project.iter("layer-tree-layer") + if item.get("id") == layer_id + ) + self.assertEqual(tree_layer.get("checked"), "Qt::Checked") + other_raster_names = {"信号", "用户量", "信号差用户量", "弱覆盖占比"} + other_raster_layers = [ + item + for item in project.iter("layer-tree-layer") + if item.get("name") in other_raster_names + ] + self.assertEqual(len(other_raster_layers), len(other_raster_names)) + self.assertTrue(all(item.get("checked") == "Qt::Unchecked" for item in other_raster_layers)) + if __name__ == "__main__": unittest.main()