feat: 补齐全量网格成果并改用竞对图层名
全量包与单市场包含同类完整数据且不含 online;AOI 由报表按最大接触面积自动匹配。Excel 占比写成文本百分号,竞对信号文件和图层改用正确用字。
This commit is contained in:
+46
-11
@@ -1,20 +1,57 @@
|
||||
"""按市场成果生成 Result.xlsx。"""
|
||||
from contextlib import closing
|
||||
from pathlib import Path
|
||||
import sqlite3
|
||||
|
||||
from . import config
|
||||
|
||||
BUILDING_COLUMNS = [
|
||||
("building_id", "楼宇ID", False),
|
||||
("longitude", "经度", False),
|
||||
("latitude", "纬度", False),
|
||||
("mro_ratio_yd", "移动采样点占比", True),
|
||||
("mro_ratio_dx", "电信采样点占比", True),
|
||||
("mro_ratio_lt", "联通采样点占比", True),
|
||||
("mro_ratio_gd", "广电采样点占比", True),
|
||||
("mro_ratio_dxlt", "电联采样点占比", True),
|
||||
]
|
||||
AOI_COLUMNS = [
|
||||
("所属市场网格", "所属市场网格", False),
|
||||
("AOI唯一标签", "AOI唯一标签", False),
|
||||
("AOI名称", "AOI名称", False),
|
||||
("移动C端采样点占比", "移动C端采样点占比", True),
|
||||
("联通C端采样点占比", "联通C端采样点占比", True),
|
||||
("电信C端采样点占比", "电信C端采样点占比", True),
|
||||
("广电C端采样点占比", "广电C端采样点占比", True),
|
||||
("电联C端采样点占比", "电联C端采样点占比", True),
|
||||
]
|
||||
|
||||
def _write_sheet(workbook, title, gpkg_path, table_name, fields, widths):
|
||||
|
||||
def _percent_text(value):
|
||||
"""源数据已是百分数,写成文本 99.65%,不依赖单元格格式。"""
|
||||
if value is None or value == "":
|
||||
return None
|
||||
text = str(value).strip()
|
||||
if text.endswith("%"):
|
||||
return text
|
||||
return f"{float(text):.2f}%"
|
||||
|
||||
|
||||
def _write_sheet(workbook, title, gpkg_path, table_name, columns, widths):
|
||||
from openpyxl.styles import Font, PatternFill, Border, Side, Alignment
|
||||
from openpyxl.utils import get_column_letter
|
||||
from openpyxl.worksheet.table import Table, TableStyleInfo
|
||||
sheet = workbook.create_sheet(title[:31])
|
||||
sheet.append(fields)
|
||||
quoted = ', '.join('"' + field.replace('"', '""') + '"' for field in fields)
|
||||
with sqlite3.connect(gpkg_path) as conn:
|
||||
for row in conn.execute(f'SELECT {quoted} FROM "{table_name}"'):
|
||||
sheet.append(row)
|
||||
sheet.append([label for _, label, _ in columns])
|
||||
quoted = ", ".join('"' + name.replace('"', '""') + '"' for name, _, _ in columns)
|
||||
with closing(sqlite3.connect(gpkg_path)) as conn:
|
||||
rows = list(conn.execute(f'SELECT {quoted} FROM "{table_name}"'))
|
||||
percent_at = [index for index, (_, _, percent) in enumerate(columns) if percent]
|
||||
for row in rows:
|
||||
values = list(row)
|
||||
for index in percent_at:
|
||||
values[index] = _percent_text(values[index])
|
||||
sheet.append(values)
|
||||
header_fill = PatternFill("solid", fgColor="D9EAF7")
|
||||
thin = Side(style="thin", color="D9E2F3")
|
||||
for cell in sheet[1]:
|
||||
@@ -24,7 +61,7 @@ def _write_sheet(workbook, title, gpkg_path, table_name, fields, widths):
|
||||
cell.alignment = Alignment(vertical="center")
|
||||
for index, width in enumerate(widths, 1):
|
||||
sheet.column_dimensions[get_column_letter(index)].width = width
|
||||
sheet.auto_filter.ref = f"A1:{get_column_letter(len(fields))}{sheet.max_row}"
|
||||
sheet.auto_filter.ref = f"A1:{get_column_letter(len(columns))}{sheet.max_row}"
|
||||
sheet.freeze_panes = "A2"
|
||||
table = Table(displayName=f"Table{len(workbook.worksheets)}", ref=sheet.auto_filter.ref)
|
||||
table.tableStyleInfo = TableStyleInfo(name="TableStyleMedium2", showFirstColumn=False, showLastColumn=False, showRowStripes=False, showColumnStripes=False)
|
||||
@@ -36,10 +73,8 @@ def create_result_xlsx(output_path, building_gpkg, aoi_gpkg):
|
||||
from openpyxl import Workbook
|
||||
workbook = Workbook()
|
||||
workbook.remove(workbook.active)
|
||||
building_fields = ["building_id", "longitude", "latitude", "mro_ratio_yd", "mro_ratio_dx", "mro_ratio_lt", "mro_ratio_gd", "mro_ratio_dxlt"]
|
||||
aoi_fields = ["所属市场网格", "AOI唯一标签", "AOI名称", "移动C端采样点占比", "联通C端采样点占比", "电信C端采样点占比", "广电C端采样点占比", "电联C端采样点占比"]
|
||||
building_count = _write_sheet(workbook, "移动C端采样点比例_楼宇", building_gpkg, "NRBuildingsData", building_fields, [15, 12, 12, 14, 14, 14, 14, 14])
|
||||
aoi_count = _write_sheet(workbook, "移动C端采样点比例_AOI", aoi_gpkg, config.AOI_LAYER, aoi_fields, [16, 18, 40, 18, 18, 18, 18, 18])
|
||||
building_count = _write_sheet(workbook, "移动C端采样点比例_楼宇", building_gpkg, "NRBuildingsData", BUILDING_COLUMNS, [16, 12, 12, 16, 16, 16, 16, 16])
|
||||
aoi_count = _write_sheet(workbook, "移动C端采样点比例_AOI", aoi_gpkg, config.AOI_LAYER, AOI_COLUMNS, [16, 18, 40, 20, 20, 20, 20, 20])
|
||||
output_path = Path(output_path)
|
||||
output_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
workbook.save(output_path)
|
||||
|
||||
Reference in New Issue
Block a user