fix: 补全楼宇图层工程坐标系和数据元信息
This commit is contained in:
+11
-1
@@ -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)
|
||||
|
||||
+8
-1
@@ -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"
|
||||
|
||||
@@ -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('<qgis><projectlayers/><layer-tree-group/></qgis>')
|
||||
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'))
|
||||
|
||||
Reference in New Issue
Block a user