fix: 统一楼宇边界图层使用 CRS84

This commit is contained in:
nixevol
2026-09-21 11:02:07 +08:00
parent c7e1777207
commit 0e3fbd2a23
+21 -4
View File
@@ -7,6 +7,14 @@ from shapely import wkt, to_wkb
DROP_COLUMNS = {"输入行号", "查询状态", "错误信息"}
LAYER_NAME = "NRBuildingsData"
GEOM_COLUMN = "geom"
SRS_ID = 100000
CRS84 = (
"WGS 84 (CRS84)", SRS_ID, "NONE", SRS_ID,
'GEOGCS["WGS 84 (CRS84)",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563]],'
'PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],'
'AXIS["Longitude",EAST],AXIS["Latitude",NORTH]]',
"",
)
def _encoding(path):
for enc in ("gbk", "utf-8-sig", "utf-8"):
@@ -19,7 +27,7 @@ def _encoding(path):
raise ValueError("无法识别楼宇结果 CSV 编码")
def _gpkg_blob(geometry):
return b"GP" + bytes((0, 1)) + (4326).to_bytes(4, "little") + to_wkb(geometry)
return b"GP" + bytes((0, 1)) + SRS_ID.to_bytes(4, "little") + to_wkb(geometry)
def build_gpkg(data_csv, polygon_csv, output_path):
data_csv, polygon_csv, output_path = map(Path, (data_csv, polygon_csv, output_path))
@@ -54,12 +62,21 @@ def build_gpkg(data_csv, polygon_csv, output_path):
if output_path.exists(): output_path.unlink()
conn=sqlite3.connect(output_path)
try:
conn.executescript("""CREATE TABLE gpkg_spatial_ref_sys (srs_name TEXT NOT NULL, srs_id INTEGER PRIMARY KEY, organization TEXT NOT NULL, organization_coordsys_id INTEGER NOT NULL, definition TEXT NOT NULL, description TEXT); INSERT INTO gpkg_spatial_ref_sys VALUES ('WGS 84 geodetic',4326,'EPSG',4326,'GEOGCS[\"WGS 84\"]',''); CREATE TABLE gpkg_contents (table_name TEXT PRIMARY KEY, data_type TEXT NOT NULL, identifier TEXT UNIQUE, description TEXT DEFAULT '', last_change DATETIME NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ','now')), min_x DOUBLE, min_y DOUBLE, max_x DOUBLE, max_y DOUBLE, srs_id INTEGER); CREATE TABLE gpkg_geometry_columns (table_name TEXT PRIMARY KEY, column_name TEXT NOT NULL, geometry_type_name TEXT NOT NULL, srs_id INTEGER NOT NULL, z TINYINT NOT NULL, m TINYINT NOT NULL);""")
conn.executescript("""CREATE TABLE gpkg_spatial_ref_sys (srs_name TEXT NOT NULL, srs_id INTEGER PRIMARY KEY, organization TEXT NOT NULL, organization_coordsys_id INTEGER NOT NULL, definition TEXT NOT NULL, description TEXT); CREATE TABLE gpkg_contents (table_name TEXT PRIMARY KEY, data_type TEXT NOT NULL, identifier TEXT UNIQUE, description TEXT DEFAULT '', last_change DATETIME NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ','now')), min_x DOUBLE, min_y DOUBLE, max_x DOUBLE, max_y DOUBLE, srs_id INTEGER); CREATE TABLE gpkg_geometry_columns (table_name TEXT PRIMARY KEY, column_name TEXT NOT NULL, geometry_type_name TEXT NOT NULL, srs_id INTEGER NOT NULL, z TINYINT NOT NULL, m TINYINT NOT NULL);""")
crs = CRS84
building_info = Path(__file__).resolve().parents[1] / "source" / "BuildingInfo.gpkg"
if building_info.is_file():
with sqlite3.connect(building_info) as source:
crs = source.execute(
"select srs_name, srs_id, organization, organization_coordsys_id, definition, description "
"from gpkg_spatial_ref_sys where srs_id = ?", (SRS_ID,),
).fetchone() or CRS84
conn.execute("insert into gpkg_spatial_ref_sys values (?,?,?,?,?,?)", crs)
esc=lambda x:'"'+x.replace('"','""')+'"'
conn.execute(f'CREATE TABLE {esc(LAYER_NAME)} (fid INTEGER PRIMARY KEY AUTOINCREMENT, '+', '.join(esc(x)+' TEXT' for x in fields)+f', {esc(GEOM_COLUMN)} BLOB NOT NULL)')
conn.execute('CREATE VIRTUAL TABLE rtree_NRBuildingsData_geom USING rtree(id,minx,maxx,miny,maxy)')
conn.execute('INSERT INTO gpkg_contents(table_name,data_type,identifier,srs_id) VALUES (?,?,?,?)',(LAYER_NAME,'features',LAYER_NAME,4326))
conn.execute('INSERT INTO gpkg_geometry_columns VALUES (?,?,?,?,?,?)',(LAYER_NAME,GEOM_COLUMN,'MULTIPOLYGON',4326,0,0))
conn.execute('INSERT INTO gpkg_contents(table_name,data_type,identifier,srs_id) VALUES (?,?,?,?)',(LAYER_NAME,'features',LAYER_NAME,SRS_ID))
conn.execute('INSERT INTO gpkg_geometry_columns VALUES (?,?,?,?,?,?)',(LAYER_NAME,GEOM_COLUMN,'MULTIPOLYGON',SRS_ID,0,0))
cols=','.join(esc(x) for x in fields)
for clean,geometry in rows:
vals=[clean[x] for x in fields]+[_gpkg_blob(geometry)]; cur=conn.execute(f'INSERT INTO {esc(LAYER_NAME)} ({cols},{esc(GEOM_COLUMN)}) VALUES ({",".join("?" for _ in vals)})',vals); fid=cur.lastrowid; x1,y1,x2,y2=geometry.bounds; conn.execute('INSERT INTO rtree_NRBuildingsData_geom VALUES (?,?,?,?,?)',(fid,x1,x2,y1,y2))