diff --git a/marketgrid/jobs.py b/marketgrid/jobs.py index 99ac456..f3c2ed0 100644 --- a/marketgrid/jobs.py +++ b/marketgrid/jobs.py @@ -7,6 +7,7 @@ from . import config from .building_attributes import enrich_building_attributes from .building_themes import export_themes from .aoi_theme import normalize_aoi_gpkg +from .result_xlsx import create_result_xlsx from .gpkg import build_summary_market from .files import competitor_signal_raster_filename from .files import copy_full_source_file @@ -106,11 +107,16 @@ def process_market(market, pixel_width, pixel_height, output_dir): nr_buildings_path, building_info_conn, building_info_layer, ) theme_results = export_themes(nr_buildings_path) - nr_buildings_path.unlink() aoi_result = export_market_aoi_gpkg( aoi_conn, aoi_layer, aoi_rtree, market, market_dir / config.AOI_OUTPUT_FILENAME, ) normalize_aoi_gpkg(market_dir / config.AOI_OUTPUT_FILENAME) + result_xlsx = create_result_xlsx( + market_dir / "Result.xlsx", + next(item["output"] for item in theme_results if item["name"] == "移动C端采样点比例_楼宇"), + market_dir / config.AOI_OUTPUT_FILENAME, + ) + nr_buildings_path.unlink() gis_project_result = copy_gis_project_to_market( config.GIS_PROJECT_SOURCE, gis_project_path, @@ -139,6 +145,7 @@ def process_market(market, pixel_width, pixel_height, output_dir): ), "building_info_result": rewrite_result_for_zip(building_info_result, zip_path, market_dir), "building_theme_results": [rewrite_result_for_zip(r, zip_path, market_dir) for r in theme_results], + "result_xlsx": rewrite_result_for_zip(result_xlsx, zip_path, market_dir), "aoi_result": rewrite_result_for_zip(aoi_result, zip_path, market_dir), "gis_project_result": rewrite_result_for_zip(gis_project_result, zip_path, market_dir), "zip_result": zip_result, diff --git a/marketgrid/result_xlsx.py b/marketgrid/result_xlsx.py new file mode 100644 index 0000000..177d3e3 --- /dev/null +++ b/marketgrid/result_xlsx.py @@ -0,0 +1,46 @@ +"""按市场成果生成 Result.xlsx。""" +from pathlib import Path +import sqlite3 + +from . import config + + +def _write_sheet(workbook, title, gpkg_path, table_name, fields, 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) + header_fill = PatternFill("solid", fgColor="D9EAF7") + thin = Side(style="thin", color="D9E2F3") + for cell in sheet[1]: + cell.font = Font(name="等线", size=11) + cell.fill = header_fill + cell.border = Border(bottom=thin) + 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.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) + sheet.add_table(table) + return sheet.max_row - 1 + + +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]) + output_path = Path(output_path) + output_path.parent.mkdir(parents=True, exist_ok=True) + workbook.save(output_path) + return {"output": str(output_path), "building_rows": building_count, "aoi_rows": aoi_count} diff --git a/requirements-ui.txt b/requirements-ui.txt index f13da0f..184efb9 100644 --- a/requirements-ui.txt +++ b/requirements-ui.txt @@ -4,3 +4,4 @@ streamlit==1.64.0 filelock==3.32.7 psutil==7.2.2 Pillow==12.3.0 +openpyxl==3.1.5