Пример #1
0
    def testPointGenerationRelativeExtrusion(self):
        """
        Points layer with relative clamping and extrusion
        """
        vl = QgsVectorLayer(os.path.join(unitTestDataPath(), '3d', 'points_with_z.shp'), 'trees')
        self.assertTrue(vl.isValid())

        vl.elevationProperties().setClamping(Qgis.AltitudeClamping.Relative)
        vl.elevationProperties().setZScale(2.5)
        vl.elevationProperties().setZOffset(10)
        vl.elevationProperties().setExtrusionEnabled(True)
        vl.elevationProperties().setExtrusionHeight(7)

        rl = QgsRasterLayer(os.path.join(unitTestDataPath(), '3d', 'dtm.tif'), 'DTM')
        self.assertTrue(rl.isValid())

        curve = QgsLineString()
        curve.fromWkt(
            'LineString (-347557.39478182751918212 6632716.59644229710102081, -346435.3875386503059417 6632277.86440025269985199, -346234.1061912341392599 6632242.03022097796201706, -346185.31071307259844616 6632150.53869942482560873)')
        req = QgsProfileRequest(curve)
        req.setTransformContext(self.create_transform_context())
        terrain_provider = QgsRasterDemTerrainProvider()
        terrain_provider.setLayer(rl)
        terrain_provider.setScale(0.3)
        terrain_provider.setOffset(-5)

        req.setTerrainProvider(terrain_provider)
        req.setCrs(QgsCoordinateReferenceSystem('EPSG:3857'))
        generator = vl.createProfileGenerator(req)
        self.assertTrue(generator.generateProfile())

        req.setTolerance(15)
        generator = vl.createProfileGenerator(req)
        self.assertTrue(generator.generateProfile())
        results = generator.takeResults()

        if QgsProjUtils.projVersionMajor() >= 8:
            self.assertEqual(self.round_dict(results.distanceToHeightMap(), 1),
                             {31.2: 333.5, 175.6: 333.5, 1242.5: 267.0})
        else:
            self.assertEqual(self.round_dict(results.distanceToHeightMap(), 1),
                             {31.2: 331.2, 175.6: 329.8, 1242.5: 264.0})

        if QgsProjUtils.projVersionMajor() >= 8:
            self.assertCountEqual([g.asWkt(1) for g in results.asGeometries()],
                                  ['LineStringZ (-347395 6632649.6 333.5, -347395 6632649.6 340.5)',
                                   'LineStringZ (-347533.4 6632692.2 333.5, -347533.4 6632692.2 340.5)',
                                   'LineStringZ (-346399.2 6632265.6 267, -346399.2 6632265.6 274)'])
            self.assertAlmostEqual(results.zRange().lower(), 267.0, 2)
            self.assertAlmostEqual(results.zRange().upper(), 340.5, 2)
        else:
            self.assertCountEqual([g.asWkt(1) for g in results.asGeometries()],
                                  ['LineStringZ (-347395 6632649.6 329.8, -347395 6632649.6 336.8)',
                                   'LineStringZ (-347533.4 6632692.2 331.3, -347533.4 6632692.2 338.3)',
                                   'LineStringZ (-346399.2 6632265.6 264, -346399.2 6632265.6 271)'])
            self.assertAlmostEqual(results.zRange().lower(), 264.0, 2)
            self.assertAlmostEqual(results.zRange().upper(), 338.25, 2)
Пример #2
0
    def testPolygonGenerationTerrain(self):
        vl = QgsVectorLayer('PolygonZ?crs=EPSG:27700', 'lines', 'memory')
        self.assertTrue(vl.isValid())

        for line in [
            'PolygonZ ((321829.48893365426920354 129991.38697145861806348 1, 321847.89668515208177269 129996.63588572069420479 1, 321848.97131609614007175 129979.22330882755341008 1, 321830.31725845142500475 129978.07136809575604275 1, 321829.48893365426920354 129991.38697145861806348 1))',
            'PolygonZ ((321920.00953056826256216 129924.58260190498549491 2, 321924.65299345907988027 129908.43546159457764588 2, 321904.78543491888558492 129903.99811821122420952 2, 321900.80605239619035274 129931.39860145389684476 2, 321904.84799937985371798 129931.71552911199978553 2, 321908.93646715773502365 129912.90030360443051904 2, 321914.20495146053144708 129913.67693978428724222 2, 321911.30165811872575432 129923.01272751353099011 2, 321920.00953056826256216 129924.58260190498549491 2))',
            'PolygonZ ((321923.10517279652412981 129919.61521573827485554 3, 321922.23537852568551898 129928.3598982143739704 3, 321928.60423935484141111 129934.22530528216157109 3, 321929.39881197665818036 129923.29054521876969375 3, 321930.55804549407912418 129916.53248518184409477 3, 321923.10517279652412981 129919.61521573827485554 3))',
            'PolygonZ ((321990.47451346553862095 129909.63588680300745182 4, 321995.04325810901354998 129891.84052284323843196 4, 321989.66826330573530868 129890.5092018858413212 4, 321990.78512359503656626 129886.49917887404444627 4, 321987.37291929306229576 129885.64982962771318853 4, 321985.2254804756375961 129893.81317058412241749 4, 321987.63158903241856024 129894.41078495365218259 4, 321984.34022761805681512 129907.57450046355370432 4, 321990.47451346553862095 129909.63588680300745182 4))',
                'PolygonZ ((322103.03910495212767273 129795.91051736124791205 5, 322108.25568856322206557 129804.76113295342656784 5, 322113.29666162584908307 129803.9285887333098799 5, 322117.78645010641776025 129794.48194090687320568 5, 322103.03910495212767273 129795.91051736124791205 5))']:
            f = QgsFeature()
            f.setGeometry(QgsGeometry.fromWkt(line))
            self.assertTrue(vl.dataProvider().addFeature(f))

        vl.elevationProperties().setClamping(Qgis.AltitudeClamping.Terrain)
        vl.elevationProperties().setZScale(2.5)
        vl.elevationProperties().setZOffset(10)

        curve = QgsLineString()
        curve.fromWkt(
            'LineString (-347701.59207547508412972 6632766.96282589063048363, -346577.00878971704514697 6632369.7371364813297987, -346449.93654899462126195 6632331.81857067719101906, -346383.52035177784273401 6632216.85897350125014782)')
        req = QgsProfileRequest(curve)
        req.setTransformContext(self.create_transform_context())

        rl = QgsRasterLayer(os.path.join(unitTestDataPath(), '3d', 'dtm.tif'), 'DTM')
        self.assertTrue(rl.isValid())
        terrain_provider = QgsRasterDemTerrainProvider()
        terrain_provider.setLayer(rl)
        terrain_provider.setScale(0.3)
        terrain_provider.setOffset(-5)
        req.setTerrainProvider(terrain_provider)

        req.setCrs(QgsCoordinateReferenceSystem('EPSG:3857'))

        generator = vl.createProfileGenerator(req)
        self.assertTrue(generator.generateProfile())
        results = generator.takeResults()

        if QgsProjUtils.projVersionMajor() >= 8:
            self.assertEqual(self.round_dict(results.distanceToHeightMap(), 1),
                             {1041.8: 55.3, 1042.4: 55.2, 1049.5: 55.2, 1070.2: 55.2, 1073.1: 55.2, 1074.8: 55.3,
                              1078.9: 54.5, 1083.9: 54.5, 1091.1: 54.5, 1186.8: 49.3, 1189.8: 49.2, 1192.7: 49.2,
                              1199.2: 49.2, 1450.0: 53.0, 1455.6: 53.0, 1458.1: 53.0})
            self.assertAlmostEqual(results.zRange().lower(), 49.25, 2)
            self.assertAlmostEqual(results.zRange().upper(), 55.250, 2)
        else:
            self.assertEqual(self.round_dict(results.distanceToHeightMap(), 1),
                             {1041.8: 48.5, 1042.4: 48.5, 1049.5: 48.5, 1070.2: 48.5, 1073.1: 48.5, 1074.8: 48.5,
                              1078.9: 48.5, 1083.9: 48.5, 1091.1: 48.5, 1186.8: 52.3, 1189.8: 52.2, 1192.7: 52.2,
                              1199.2: 52.2, 1450.0: 54.5, 1455.6: 54.5, 1458.1: 54.5})
            self.assertAlmostEqual(results.zRange().lower(), 48.5, 2)
            self.assertAlmostEqual(results.zRange().upper(), 54.500000, 2)
Пример #3
0
    def testLineGenerationRelative(self):
        vl = QgsVectorLayer('LineStringZ?crs=EPSG:27700', 'lines', 'memory')
        self.assertTrue(vl.isValid())

        for line in ['LineStringZ(322006 129874 12, 322008 129910 13, 322038 129909 14, 322037 129868 15)',
                     'LineStringZ(322068 129900 16, 322128 129813 17)',
                     'LineStringZ(321996 129914 11, 321990 129896 15)',
                     'LineStringZ(321595 130176 1, 321507 130104 10)',
                     'LineStringZ(321558 129930 1, 321568 130029 10, 321516 130049 5)',
                     'LineStringZ(321603 129967 3, 321725 130042 9)']:
            f = QgsFeature()
            f.setGeometry(QgsGeometry.fromWkt(line))
            self.assertTrue(vl.dataProvider().addFeature(f))

        vl.elevationProperties().setClamping(Qgis.AltitudeClamping.Relative)
        vl.elevationProperties().setZScale(2.5)
        vl.elevationProperties().setZOffset(10)

        curve = QgsLineString()
        curve.fromWkt(
            'LineString (-347692.88994020794052631 6632796.97473032586276531, -346576.99897185183363035 6632367.38372825458645821, -346396.02439485350623727 6632344.35087973903864622, -346374.34608158958144486 6632220.09952207934111357)')
        req = QgsProfileRequest(curve)
        req.setTransformContext(self.create_transform_context())

        rl = QgsRasterLayer(os.path.join(unitTestDataPath(), '3d', 'dtm.tif'), 'DTM')
        self.assertTrue(rl.isValid())
        terrain_provider = QgsRasterDemTerrainProvider()
        terrain_provider.setLayer(rl)
        terrain_provider.setScale(0.3)
        terrain_provider.setOffset(-5)
        req.setTerrainProvider(terrain_provider)

        req.setCrs(QgsCoordinateReferenceSystem('EPSG:3857'))

        generator = vl.createProfileGenerator(req)
        self.assertTrue(generator.generateProfile())
        results = generator.takeResults()

        if QgsProjUtils.projVersionMajor() >= 8:
            self.assertEqual(self.round_dict(results.distanceToHeightMap(), 1),
                             {675.2: 84.2, 1195.7: 86.8, 1223.1: 81.4, 1272.0: 90.0, 1339.4: 98.7, 1444.4: 100.0})
            self.assertAlmostEqual(results.zRange().lower(), 81.358, 2)
            self.assertAlmostEqual(results.zRange().upper(), 100.009, 2)
        else:
            self.assertEqual(self.round_dict(results.distanceToHeightMap(), 1),
                             {675.2: 80.5, 1195.7: 90.5, 1223.1: 87.4, 1272.0: 94.5, 1339.4: 98.0, 1444.4: 94.0})
            self.assertAlmostEqual(results.zRange().lower(), 80.4564, 2)
            self.assertAlmostEqual(results.zRange().upper(), 97.9811, 2)
Пример #4
0
    def testPointGenerationTerrain(self):
        """
        Points layer with terrain clamping
        """
        vl = QgsVectorLayer(os.path.join(unitTestDataPath(), '3d', 'points_with_z.shp'), 'trees')
        self.assertTrue(vl.isValid())

        vl.elevationProperties().setClamping(Qgis.AltitudeClamping.Terrain)
        vl.elevationProperties().setZScale(2.5)
        vl.elevationProperties().setZOffset(10)

        rl = QgsRasterLayer(os.path.join(unitTestDataPath(), '3d', 'dtm.tif'), 'DTM')
        self.assertTrue(rl.isValid())

        curve = QgsLineString()
        curve.fromWkt(
            'LineString (-347557.39478182751918212 6632716.59644229710102081, -346435.3875386503059417 6632277.86440025269985199, -346234.1061912341392599 6632242.03022097796201706, -346185.31071307259844616 6632150.53869942482560873)')
        req = QgsProfileRequest(curve)
        req.setTransformContext(self.create_transform_context())
        terrain_provider = QgsRasterDemTerrainProvider()
        terrain_provider.setLayer(rl)
        terrain_provider.setScale(0.3)
        terrain_provider.setOffset(-5)

        req.setTerrainProvider(terrain_provider)
        req.setCrs(QgsCoordinateReferenceSystem('EPSG:3857'))
        generator = vl.createProfileGenerator(req)
        self.assertTrue(generator.generateProfile())

        # no tolerance => no hits
        results = generator.takeResults()
        self.assertFalse(results.distanceToHeightMap())

        req.setTolerance(15)
        generator = vl.createProfileGenerator(req)
        self.assertTrue(generator.generateProfile())
        results = generator.takeResults()

        if QgsProjUtils.projVersionMajor() >= 8:
            self.assertEqual(self.round_dict(results.distanceToHeightMap(), 1),
                             {175.6: 69.5, 31.2: 69.5, 1242.5: 55.2})
            self.assertAlmostEqual(results.zRange().lower(), 55.249, 2)
            self.assertAlmostEqual(results.zRange().upper(), 69.5, 2)
        else:
            self.assertEqual(self.round_dict(results.distanceToHeightMap(), 1),
                             {31.2: 67.2, 175.6: 65.8, 1242.5: 52.2})
            self.assertAlmostEqual(results.zRange().lower(), 52.25, 2)
            self.assertAlmostEqual(results.zRange().upper(), 67.25, 2)
Пример #5
0
    def testBasic(self):
        req = QgsProfileRequest(QgsLineString([[1, 2], [3, 4]]))
        self.assertEqual(req.profileCurve().asWkt(), 'LineString (1 2, 3 4)')

        req.setCrs(QgsCoordinateReferenceSystem('EPSG:3857')).setTolerance(
            5).setStepDistance(15)
        self.assertEqual(req.crs().authid(), 'EPSG:3857')
        self.assertEqual(req.tolerance(), 5)
        self.assertEqual(req.stepDistance(), 15)

        proj_string = '+proj=pipeline +step +inv +proj=lcc +lat_0=-37 +lon_0=145 +lat_1=-36 +lat_2=-38 +x_0=2500000 +y_0=2500000 +ellps=GRS80 +step +proj=unitconvert +xy_in=rad +xy_out=deg +step +proj=axisswap +order=2,1'
        transform_context = QgsCoordinateTransformContext()
        transform_context.addCoordinateOperation(
            QgsCoordinateReferenceSystem('EPSG:3111'),
            QgsCoordinateReferenceSystem('EPSG:4283'), proj_string)
        req.setTransformContext(transform_context)
        self.assertEqual(
            req.transformContext().calculateCoordinateOperation(
                QgsCoordinateReferenceSystem('EPSG:3111'),
                QgsCoordinateReferenceSystem('EPSG:4283')), proj_string)

        exp_context = QgsExpressionContext()
        context_scope = QgsExpressionContextScope()
        context_scope.setVariable('test_var', 5, True)
        exp_context.appendScope(context_scope)
        req.setExpressionContext(exp_context)

        self.assertEqual(req.expressionContext().variable('test_var'), 5)

        terrain = QgsFlatTerrainProvider()
        terrain.setOffset(5)
        req.setTerrainProvider(terrain)
        self.assertEqual(req.terrainProvider().offset(), 5)

        copy = QgsProfileRequest(req)
        self.assertEqual(copy.profileCurve().asWkt(), 'LineString (1 2, 3 4)')
        self.assertEqual(copy.crs().authid(), 'EPSG:3857')
        self.assertEqual(copy.tolerance(), 5)
        self.assertEqual(copy.stepDistance(), 15)
        self.assertEqual(
            copy.transformContext().calculateCoordinateOperation(
                QgsCoordinateReferenceSystem('EPSG:3111'),
                QgsCoordinateReferenceSystem('EPSG:4283')), proj_string)
        self.assertIsInstance(copy.terrainProvider(), QgsFlatTerrainProvider)
        self.assertEqual(copy.terrainProvider().offset(), 5)
        self.assertEqual(copy.expressionContext().variable('test_var'), 5)
Пример #6
0
    def testEquality(self):
        """
        Test equality operator
        """
        req = QgsProfileRequest(None)
        req2 = QgsProfileRequest(None)
        self.assertEqual(req, req2)

        req.setProfileCurve(QgsLineString([[1, 2], [3, 4]]))
        self.assertNotEqual(req, req2)

        req2.setProfileCurve(QgsLineString([[1, 2], [3, 5]]))
        self.assertNotEqual(req, req2)

        req.setProfileCurve(None)
        self.assertNotEqual(req, req2)

        req.setProfileCurve(QgsLineString([[1, 2], [3, 5]]))
        self.assertEqual(req, req2)

        req.setCrs(QgsCoordinateReferenceSystem('EPSG:3857'))
        self.assertNotEqual(req, req2)
        req2.setCrs(QgsCoordinateReferenceSystem('EPSG:3857'))
        self.assertEqual(req, req2)

        proj_string = '+proj=pipeline +step +inv +proj=lcc +lat_0=-37 +lon_0=145 +lat_1=-36 +lat_2=-38 +x_0=2500000 +y_0=2500000 +ellps=GRS80 +step +proj=unitconvert +xy_in=rad +xy_out=deg +step +proj=axisswap +order=2,1'
        transform_context = QgsCoordinateTransformContext()
        transform_context.addCoordinateOperation(
            QgsCoordinateReferenceSystem('EPSG:3111'),
            QgsCoordinateReferenceSystem('EPSG:4283'), proj_string)

        req.setTransformContext(transform_context)
        self.assertNotEqual(req, req2)
        req2.setTransformContext(transform_context)
        self.assertEqual(req, req2)

        req.setTolerance(5)
        self.assertNotEqual(req, req2)
        req2.setTolerance(5)
        self.assertEqual(req, req2)

        req.setStepDistance(15)
        self.assertNotEqual(req, req2)
        req2.setStepDistance(15)
        self.assertEqual(req, req2)

        terrain = QgsFlatTerrainProvider()
        terrain.setOffset(5)
        req.setTerrainProvider(terrain)
        self.assertNotEqual(req, req2)

        req2.setTerrainProvider(QgsMeshTerrainProvider())
        self.assertNotEqual(req, req2)

        req.setTerrainProvider(None)
        self.assertNotEqual(req, req2)

        req.setTerrainProvider(QgsFlatTerrainProvider())
        self.assertNotEqual(req, req2)

        req.setTerrainProvider(QgsMeshTerrainProvider())
        self.assertEqual(req, req2)