"""AOI 样板字段和 QGIS 样式。""" from pathlib import Path import sqlite3 from contextlib import closing import xml.etree.ElementTree as ET from .gpkg import quote_identifier ROOT = Path(__file__).with_name("styles") TABLE = "AOI市场网格匹配" FIELDS = ["fid", "geom", "所属市场网格", "AOI唯一标签", "AOI名称", "移动C端采样点占比", "联通C端采样点占比", "电信C端采样点占比", "广电C端采样点占比", "电联C端采样点占比"] def normalize_aoi_gpkg(path): with closing(sqlite3.connect(path)) as conn, conn: existing = {row[1] for row in conn.execute(f"pragma table_info({quote_identifier(TABLE)})")} missing = set(FIELDS) - existing if missing: raise ValueError(f"AOI 缺少字段:{sorted(missing)}") for column in existing - set(FIELDS): conn.execute(f"alter table {quote_identifier(TABLE)} drop column {quote_identifier(column)}") qml = (ROOT / "aoi.qml").read_text(encoding="utf-8") conn.execute("CREATE TABLE IF NOT EXISTS layer_styles (id INTEGER PRIMARY KEY, f_table_catalog TEXT, f_table_schema TEXT, f_table_name TEXT, f_geometry_column TEXT, styleName TEXT, styleQML TEXT, styleSLD TEXT, useAsDefault BOOLEAN, description TEXT, owner TEXT, ui TEXT, update_time DATETIME)") if "update_time" not in {r[1] for r in conn.execute("pragma table_info(layer_styles)")}: conn.execute("ALTER TABLE layer_styles ADD COLUMN update_time DATETIME") conn.execute("delete from layer_styles") conn.execute("INSERT OR REPLACE INTO gpkg_contents(table_name,data_type,srs_id) VALUES ('layer_styles','attributes',0)") conn.execute("insert into layer_styles (id,f_table_catalog,f_table_schema,f_table_name,f_geometry_column,styleName,styleQML,styleSLD,useAsDefault,description,owner,ui) values (1,'','','AOI市场网格匹配','geom','Default',?,'',1,'','','')", (qml,)) def aoi_layer_template(): return ET.parse(ROOT / "aoi.xml").getroot()