40 lines
2.4 KiB
Python
40 lines
2.4 KiB
Python
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))
|