def test_boundigBox(self): radius = random.uniform(0, 100) point = Point(random.uniform(0, 100), random.uniform(0, 100)) r = point.boundingBox(radius) self.assertEqual(round(r.center().x() * 100), round(point.x() * 100)) self.assertEqual(round(r.center().y() * 100), round(point.y() * 100)) self.assertEqual(round(r.width() * 100), round(radius * 200)) self.assertEqual(round(r.height() * 100), round(radius * 200))
def test_get_vertices_list(self): p = [[Point(0, 0), Point(1, 0), Point(1, 1), Point(0, 0)]] mp = [[[Point(2, 0), Point(3, 0), Point(3, 1), Point(2, 0)]], p] f = QgsFeature(QgsFields()) f.setGeometry(Geometry.fromMultiPolygonXY(mp)) v = [mp[0][0][0], mp[0][0][1], mp[0][0][2], p[0][0], p[0][1], p[0][2]] self.assertEqual(Geometry.get_vertices_list(f), v)
def test_get_multipolygon(self): p = [[Point(0, 0), Point(1, 0), Point(1, 1), Point(0, 0)]] mp = [[[Point(2, 0), Point(3, 0), Point(3, 1), Point(2, 0)]], p] f = QgsFeature(QgsFields()) g = Geometry.fromPolygonXY(p) f.setGeometry(g) self.assertEqual(Geometry.get_multipolygon(f), [p]) self.assertEqual(Geometry.get_multipolygon(g), [p]) g = Geometry.fromMultiPolygonXY(mp) f.setGeometry(g) self.assertEqual(Geometry.get_multipolygon(f), mp) self.assertEqual(Geometry.get_multipolygon(g), mp)
def get_address(cbcn, parcel): """Get Cadastre style addresses from CBCN dataset.""" address = AddressLayer() address.rename = {} address.resolve = {} address.setCrs(cbcn.crs()) address.startEditing() index = parcel.get_index() pa_feat = {f.id(): f for f in parcel.getFeatures()} for ad in cbcn.getFeatures(): if ad["NOM_VIA"] == None: # NOQA continue attr = get_cat_address(ad) pt = Point(ad.geometry().asPoint()) area_of_candidates = pt.boundingBox(cbcn_thr) fids = index.intersects(area_of_candidates) parcel = None sep = cbcn_thr for fid in fids: dist = pa_feat[fid].geometry().closestSegmentWithContext(pt)[0] if is_inside(ad, pa_feat[fid]): parcel = pa_feat[fid] break elif dist < sep: parcel = pa_feat[fid] sep = dist if parcel: ref = f"{ad['CODICARRER']}.{attr['designator']}.{parcel['localId']}" attr["localId"] = ref feat = QgsFeature(address.fields()) for key, value in list(attr.items()): feat[key] = value feat.setGeometry(ad.geometry()) address.addFeature(feat) address.commitChanges() msg = _("Read %d features in '%s'") log.info(msg, address.featureCount(), address.name()) return address
def test_get_outer_vertices(self): p1 = [Point(1, 1), Point(2, 1), Point(2, 2), Point(1, 1)] p2 = [Point(0, 0), Point(3, 0), Point(3, 3), Point(0, 0)] p3 = [Point(4, 0), Point(5, 0), Point(5, 1), Point(4, 0)] mp = [[p1, p2], [p3]] f = QgsFeature(QgsFields()) f.setGeometry(Geometry.fromMultiPolygonXY(mp)) v = p1[:-1] + p3[:-1] self.assertEqual(Geometry.get_outer_vertices(f), v)
def test_init(self): p = Point(1, 2) q = Point(p) r = Point((1, 2)) self.assertEqual(q.x(), r.x()) self.assertEqual(q.y(), r.y())
def test_get_spike_context(self): square = QgsGeometry.fromPolygonXY([[ Point(0, 50), # spike angle_v < 5 angle_a > 5 c < 0.5 Point(50, 50.4), Point(49.9, 76), Point(50, 74), # zig-zag Point(50, 130), Point(50.4, 100), Point(75, 110), # spike Point(99, 100), Point(100, 130), # spike but c > 0.5 Point(100.2, 60), Point(100, 90), # zig-zag but c > 0.1 Point(99.8, 0), # spike Point(99.5, 50), Point(70, 55), Point(60, 50), # not zig-zag Point(0, 50), ]]) acute_thr = 5 straight_thr = 5 threshold = 0.5 angle_v, angle_a, ndx, ndxa, is_acute, is_zigzag, is_spike, vx = Point( 50, 50.4).get_spike_context(square, acute_thr, straight_thr, threshold) self.assertFalse(is_spike) angle_v, angle_a, ndx, ndxa, is_acute, is_zigzag, is_spike, vx = Point( 0, 50.1).get_spike_context(square, acute_thr, straight_thr, threshold) self.assertTrue(is_spike) self.assertEqual(ndxa, 1) self.assertEqual(round(vx.x(), 4), 50.0016) self.assertEqual(vx.y(), 50.0) angle_v, angle_a, ndx, ndxa, is_acute, is_zigzag, is_spike, vx = Point( 50, 74).get_spike_context(square, acute_thr, straight_thr, threshold) self.assertTrue(is_zigzag) self.assertEqual(ndx, 3) self.assertEqual(ndxa, 2) angle_v, angle_a, ndx, ndxa, is_acute, is_zigzag, is_spike, vx = Point( 50, 130).get_spike_context(square, acute_thr, straight_thr, threshold) self.assertTrue(is_spike) self.assertEqual(ndx, 4) self.assertEqual(ndxa, 5) self.assertEqual(vx.x(), 50) self.assertEqual(round(vx.y(), 4), 99.8374) angle_v, angle_a, ndx, ndxa, is_acute, is_zigzag, is_spike, vx = Point( 100, 130).get_spike_context(square, acute_thr, straight_thr, threshold) self.assertTrue(is_acute) self.assertFalse(is_spike) angle_v, angle_a, ndx, ndxa, is_acute, is_zigzag, is_spike, vx = Point( 100, 90).get_spike_context(square, acute_thr, straight_thr, 0.1) self.assertFalse(is_spike) self.assertFalse(is_zigzag) angle_v, angle_a, ndx, ndxa, is_acute, is_zigzag, is_spike, vx = Point( 100, 0).get_spike_context(square, acute_thr, straight_thr, threshold) self.assertTrue(is_spike) self.assertEqual(ndx, 11) self.assertEqual(ndxa, 12) self.assertEqual(round(vx.x(), 4), 99.9109) self.assertEqual(round(vx.y(), 4), 49.9234) angle_v, angle_a, ndx, ndxa, is_acute, is_zigzag, is_spike, vx = Point( 60, 50).get_spike_context(square, acute_thr, straight_thr, threshold) self.assertFalse(is_zigzag)
def test_get_corner_context(self): square = QgsGeometry.fromPolygonXY([[ Point(0, 0), Point(50, 0.6), # dist > 0.5, angle < 5 Point(100, 0), Point(105, 50), # dist > 0.5, angle > 5 Point(100, 100), Point(2, 100.3), # dist < 0.5, angle > 5 Point(0, 100), Point(0.3, 50), # dist < 0.5, angle < 5 Point(0, 1), Point(-50, 0), # acute Point(0, 0), ]]) acute_thr = 10 straight_thr = 5 cath_thr = 0.5 (a, is_acute, is_corner, c) = Point(50, 0.4).get_corner_context(square, acute_thr, straight_thr, cath_thr) self.assertFalse(is_acute) self.assertFalse(is_corner, "%f %s %s %f" % (a, is_acute, is_corner, c)) (a, is_acute, is_corner, c) = Point(105, 51).get_corner_context(square, acute_thr, straight_thr, cath_thr) self.assertTrue(is_corner, "%f %s %s %f" % (a, is_acute, is_corner, c)) (a, is_acute, is_corner, c) = Point(5.1, 100).get_corner_context(square, acute_thr, straight_thr, cath_thr) self.assertFalse(is_corner, "%f %s %s %f" % (a, is_acute, is_corner, c)) (a, is_acute, is_corner, c) = Point(0.4, 50).get_corner_context(square, acute_thr, straight_thr, cath_thr) self.assertFalse(is_corner, "%f %s %s %f" % (a, is_acute, is_corner, c)) (a, is_acute, is_corner, c) = Point(-51, 0).get_corner_context(square, acute_thr, straight_thr, cath_thr) self.assertTrue(is_acute)