feat: 按最大重叠面积匹配楼宇楼层和高度
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
import sqlite3
|
||||
import tempfile
|
||||
import unittest
|
||||
from contextlib import closing
|
||||
from pathlib import Path
|
||||
|
||||
from shapely.geometry import box
|
||||
from marketgrid.models import LayerInfo
|
||||
from marketgrid.nrdata import _gpkg_blob
|
||||
from marketgrid.building_attributes import match_attributes, enrich_building_attributes
|
||||
|
||||
|
||||
class BuildingAttributesTest(unittest.TestCase):
|
||||
def test_maximum_overlap_ties_touch_and_defaults(self):
|
||||
with closing(sqlite3.connect(":memory:")) as source:
|
||||
source.executescript('CREATE TABLE buildings(fid INTEGER PRIMARY KEY, geom BLOB, floor INTEGER, height REAL); CREATE VIRTUAL TABLE rtree_buildings_geom USING rtree(id,minx,maxx,miny,maxy);')
|
||||
for fid, geometry, floor, height in [
|
||||
(1, box(0, 0, 1, 1), 2, 7.8),
|
||||
(2, box(1, 0, 4, 1), 8, 31.2),
|
||||
(3, box(5, 0, 6, 1), None, None),
|
||||
]:
|
||||
source.execute('INSERT INTO buildings VALUES (?,?,?,?)', (fid, _gpkg_blob(geometry), floor, height))
|
||||
x1, y1, x2, y2 = geometry.bounds
|
||||
source.execute('INSERT INTO rtree_buildings_geom VALUES (?,?,?,?,?)', (fid, x1, x2, y1, y2))
|
||||
layer = LayerInfo('buildings', 'geom', 'fid', 100000)
|
||||
self.assertEqual(match_attributes(source, layer, box(0, 0, 4, 1)), (8, 31.2, True))
|
||||
self.assertEqual(match_attributes(source, layer, box(0, 0, 2, 1)), (2, 7.8, True))
|
||||
self.assertEqual(match_attributes(source, layer, box(-1, 0, 0, 1)), (1, 3.9, False))
|
||||
self.assertEqual(match_attributes(source, layer, box(10, 0, 11, 1)), (1, 3.9, False))
|
||||
self.assertEqual(match_attributes(source, layer, box(5, 0, 6, 1)), (1, 3.9, True))
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
path = Path(directory) / 'target.gpkg'
|
||||
with closing(sqlite3.connect(path)) as target, target:
|
||||
target.execute('CREATE TABLE NRBuildingsData(fid INTEGER PRIMARY KEY, geom BLOB)')
|
||||
target.execute('INSERT INTO NRBuildingsData VALUES (1,?)', (_gpkg_blob(box(0, 0, 4, 1)),))
|
||||
self.assertEqual(enrich_building_attributes(path, source, layer), {'matched': 1, 'defaults': 0})
|
||||
enrich_building_attributes(path, source, layer)
|
||||
with closing(sqlite3.connect(path)) as target:
|
||||
self.assertEqual(target.execute('SELECT floor,height FROM NRBuildingsData').fetchone(), (8, 31.2))
|
||||
@@ -169,11 +169,13 @@ class TaskIntegrationTest(unittest.TestCase):
|
||||
values.update(mro_total_yd=100, rsrp_lt_neg105_yd=10, ssb_rsrp_dl=-115, mro_total_dl=0)
|
||||
create_gpkg(self.root / "grid.gpkg", "grid", ','.join(f'"{f}" real' for f in fields),
|
||||
[(1, a, [values[f] for f in fields]), (2, b, [values[f] for f in fields])])
|
||||
create_gpkg(self.root / "buildings.gpkg", "BuildingInfo", '"name" text', [(1, a, ["建筑甲"]), (2, b, ["建筑乙"])])
|
||||
create_gpkg(self.root / "buildings.gpkg", "BuildingInfo", '"name" text,"floor" integer,"height" real', [(1, a, ["建筑甲", 2, 7.8]), (2, b, ["建筑乙", 3, 11.7])])
|
||||
create_gpkg(self.root / "nr.gpkg", "NRBuildingsData", '"building_id" text', [(1, a, ["A"]), (2, b, ["B"])])
|
||||
create_gpkg(self.root / "aoi.gpkg", config.AOI_LAYER,
|
||||
'"所属市场网格" text,"AOI唯一标签" text,"AOI名称" text',
|
||||
[(1, a, ["甲", "a", "甲AOI"]), (2, b, ["乙", "b", "乙AOI"])])
|
||||
params = {"MARKET_GPKG": str(self.root / "markets.gpkg"), "GRID_5G_GPKG": str(self.root / "grid.gpkg"),
|
||||
"NR_BUILDINGS_DATA_GPKG": str(self.root / "nr.gpkg"),
|
||||
"BUILDING_INFO_GPKG": str(self.root / "buildings.gpkg"), "AOI_GPKG": str(self.root / "aoi.gpkg"),
|
||||
"GIS_PROJECT_SOURCE": str(tasks.ROOT / "source/GIS.qgz"), "MAX_WORKERS": 2,
|
||||
"SCALE": 1, "MAX_RASTER_PIXELS": 64, "SUMMARY_MAX_RASTER_PIXELS": 64,
|
||||
|
||||
Reference in New Issue
Block a user