From 8533814df62b2240873f85e44da51cd708e21dd4 Mon Sep 17 00:00:00 2001 From: nixevol Date: Mon, 21 Sep 2026 15:28:39 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=A1=A5=E5=85=A8=E6=A5=BC=E5=AE=87?= =?UTF-8?q?=E5=9B=BE=E5=B1=82=E5=B7=A5=E7=A8=8B=E5=9D=90=E6=A0=87=E7=B3=BB?= =?UTF-8?q?=E5=92=8C=E6=95=B0=E6=8D=AE=E5=85=83=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- marketgrid/nrdata.py | 12 +++++++++++- marketgrid/qgis.py | 9 ++++++++- tests/test_nrdata.py | 12 ++++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/marketgrid/nrdata.py b/marketgrid/nrdata.py index f0c69ed..32692a6 100644 --- a/marketgrid/nrdata.py +++ b/marketgrid/nrdata.py @@ -62,6 +62,8 @@ def build_gpkg(data_csv, polygon_csv, output_path): if output_path.exists(): output_path.unlink() conn=sqlite3.connect(output_path) try: + conn.execute("pragma application_id = 1196444487") + conn.execute("pragma user_version = 10400") 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" @@ -71,10 +73,18 @@ def build_gpkg(data_csv, polygon_csv, output_path): "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) + conn.executemany("insert into gpkg_spatial_ref_sys values (?,?,?,?,?,?)", [ + ("Undefined Cartesian SRS", -1, "NONE", -1, "undefined", "undefined Cartesian coordinate reference system"), + ("Undefined geographic SRS", 0, "NONE", 0, "undefined", "undefined geographic coordinate reference system"), + 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('CREATE TABLE gpkg_ogr_contents (table_name TEXT PRIMARY KEY, feature_count INTEGER)') + conn.execute('CREATE TABLE gpkg_extensions (table_name TEXT, column_name TEXT, extension_name TEXT NOT NULL, definition TEXT NOT NULL, scope TEXT NOT NULL, CONSTRAINT ge_tce UNIQUE (table_name, column_name, extension_name))') + conn.execute('INSERT INTO gpkg_ogr_contents VALUES (?,?)', (LAYER_NAME, len(rows))) + conn.execute('INSERT INTO gpkg_extensions VALUES (?,?,?,?,?)', (LAYER_NAME, GEOM_COLUMN, 'gpkg_rtree_index', 'http://www.geopackage.org/spec120/#extension_rtree', 'write-only')) 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) diff --git a/marketgrid/qgis.py b/marketgrid/qgis.py index c2fc548..9057b12 100644 --- a/marketgrid/qgis.py +++ b/marketgrid/qgis.py @@ -153,7 +153,14 @@ def add_nr_buildings_layer(root, result, fallback_extent): for tag, value in (("id", layer_id), ("datasource", datasource), ("layername", "NRBuildingsData")): ET.SubElement(layer, tag).text = value spatial_ref = ET.SubElement(ET.SubElement(layer, "srs"), "spatialrefsys", nativeFormat="Wkt") - for tag, value in (("srid", "4326"), ("authid", "EPSG:4326"), ("description", "WGS 84")): + from .nrdata import CRS84 + for tag, value in ( + ("wkt", CRS84[4]), + ("proj4", "+proj=longlat +datum=WGS84 +no_defs"), + ("authid", "OGC:CRS84"), ("description", "WGS 84 (CRS84)"), + ("projectionacronym", "longlat"), ("ellipsoidacronym", "EPSG:7030"), + ("geographicflag", "true"), + ): ET.SubElement(spatial_ref, tag).text = value ET.SubElement(layer, "provider", encoding="UTF-8").text = "ogr" ET.SubElement(layer, "displayfield").text = "building_id" diff --git a/tests/test_nrdata.py b/tests/test_nrdata.py index 3644a46..e5df684 100644 --- a/tests/test_nrdata.py +++ b/tests/test_nrdata.py @@ -37,6 +37,8 @@ class NrDataTest(unittest.TestCase): self.assertIn("WKT", columns) self.assertEqual(conn.execute('select count(*) from "NRBuildingsData"').fetchone()[0], 1) self.assertEqual(conn.execute("select count(*) from rtree_NRBuildingsData_geom").fetchone()[0], 1) + self.assertEqual(conn.execute("pragma application_id").fetchone()[0], 1196444487) + self.assertEqual(conn.execute("select count(*) from gpkg_extensions").fetchone()[0], 1) finally: conn.close() @@ -51,3 +53,13 @@ class NrDataTest(unittest.TestCase): build_gpkg(data, polygon, output) with data.open(encoding="utf-8", newline="") as stream: self.assertEqual(next(csv.reader(stream)), ["building_id", "WKT"]) + + def test_qgis_layer_has_complete_crs(self): + import xml.etree.ElementTree as ET + from marketgrid.qgis import add_nr_buildings_layer + root = ET.fromstring('') + add_nr_buildings_layer(root, {"extent": None}, (112, 22, 113, 23)) + crs = root.find('projectlayers/maplayer/srs/spatialrefsys') + self.assertEqual(crs.findtext('authid'), 'OGC:CRS84') + self.assertIn('AXIS["Longitude",EAST]', crs.findtext('wkt')) + self.assertTrue(crs.findtext('proj4'))