fix: 修正楼宇边界 GeoPackage 几何编码
This commit is contained in:
@@ -19,7 +19,7 @@ def _encoding(path):
|
|||||||
raise ValueError("无法识别楼宇结果 CSV 编码")
|
raise ValueError("无法识别楼宇结果 CSV 编码")
|
||||||
|
|
||||||
def _gpkg_blob(geometry):
|
def _gpkg_blob(geometry):
|
||||||
return b"GP" + bytes((0, 1, 0, 0, 0, 0, 0, 0)) + to_wkb(geometry)
|
return b"GP" + bytes((0, 1)) + (4326).to_bytes(4, "little") + to_wkb(geometry)
|
||||||
|
|
||||||
def build_gpkg(data_csv, polygon_csv, output_path):
|
def build_gpkg(data_csv, polygon_csv, output_path):
|
||||||
data_csv, polygon_csv, output_path = map(Path, (data_csv, polygon_csv, output_path))
|
data_csv, polygon_csv, output_path = map(Path, (data_csv, polygon_csv, output_path))
|
||||||
@@ -33,8 +33,9 @@ def build_gpkg(data_csv, polygon_csv, output_path):
|
|||||||
data_encoding = _encoding(data_csv)
|
data_encoding = _encoding(data_csv)
|
||||||
with data_csv.open(encoding=data_encoding, newline="") as f:
|
with data_csv.open(encoding=data_encoding, newline="") as f:
|
||||||
reader = csv.DictReader(f)
|
reader = csv.DictReader(f)
|
||||||
fields = [x for x in (reader.fieldnames or []) if x not in DROP_COLUMNS]
|
fields = list(dict.fromkeys(x for x in (reader.fieldnames or []) if x not in DROP_COLUMNS))
|
||||||
if "building_id" not in fields: raise ValueError("楼宇结果 CSV 缺少 building_id 字段")
|
if "building_id" not in fields: raise ValueError("楼宇结果 CSV 缺少 building_id 字段")
|
||||||
|
if "WKT" not in fields:
|
||||||
fields.append("WKT")
|
fields.append("WKT")
|
||||||
rows=[]
|
rows=[]
|
||||||
for row in reader:
|
for row in reader:
|
||||||
|
|||||||
@@ -39,3 +39,15 @@ class NrDataTest(unittest.TestCase):
|
|||||||
self.assertEqual(conn.execute("select count(*) from rtree_NRBuildingsData_geom").fetchone()[0], 1)
|
self.assertEqual(conn.execute("select count(*) from rtree_NRBuildingsData_geom").fetchone()[0], 1)
|
||||||
finally:
|
finally:
|
||||||
conn.close()
|
conn.close()
|
||||||
|
|
||||||
|
def test_build_gpkg_is_idempotent_after_csv_already_contains_wkt(self):
|
||||||
|
with tempfile.TemporaryDirectory() as directory:
|
||||||
|
root = Path(directory)
|
||||||
|
polygon = root / "NRBuildPolygon.csv"
|
||||||
|
data = root / "NRBuildingsData.csv"
|
||||||
|
output = root / "NRBuildingsData.gpkg"
|
||||||
|
polygon.write_text("WKT,building_id\n\"POLYGON ((0 0,1 0,1 1,0 0))\",A\n", encoding="utf-8")
|
||||||
|
data.write_text("building_id,WKT\nA,\"POLYGON ((0 0,1 0,1 1,0 0))\"\n", encoding="utf-8")
|
||||||
|
build_gpkg(data, polygon, output)
|
||||||
|
with data.open(encoding="utf-8", newline="") as stream:
|
||||||
|
self.assertEqual(next(csv.reader(stream)), ["building_id", "WKT"])
|
||||||
|
|||||||
Reference in New Issue
Block a user