def test_ExpressionFieldNestedGeometry(self): myShpFile = os.path.join(TEST_DATA_DIR, 'points.shp') layer = QgsVectorLayer(myShpFile, 'Points', 'ogr') self.assertTrue(layer.isValid()) idx = layer.addExpressionField('$x*2', QgsField('exp1', QVariant.LongLong)) # NOQA idx = layer.addExpressionField('"exp1"/1.5', QgsField('exp2', QVariant.LongLong)) # NOQA fet = next(layer.getFeatures(QgsFeatureRequest().setFlags(QgsFeatureRequest.NoGeometry).setSubsetOfAttributes(['exp2'], layer.fields()))) # nested virtual fields should have made geometry be fetched self.assertEqual(fet['exp2'], -156) self.assertEqual(fet['exp1'], -234)
def test_ExpressionFieldNested(self): myShpFile = os.path.join(TEST_DATA_DIR, 'points.shp') layer = QgsVectorLayer(myShpFile, 'Points', 'ogr') self.assertTrue(layer.isValid()) idx = layer.addExpressionField('"Staff"*2', QgsField('exp1', QVariant.LongLong)) # NOQA idx = layer.addExpressionField('"exp1"-1', QgsField('exp2', QVariant.LongLong)) # NOQA fet = next(layer.getFeatures(QgsFeatureRequest().setSubsetOfAttributes(['exp2'], layer.fields()))) self.assertEqual(fet['Class'], NULL) # nested virtual fields should make all these attributes be fetched self.assertEqual(fet['Staff'], 2) self.assertEqual(fet['exp2'], 3) self.assertEqual(fet['exp1'], 4)
def test_ExpressionFieldNestedCircular(self): """ test circular virtual field definitions """ myShpFile = os.path.join(TEST_DATA_DIR, 'points.shp') layer = QgsVectorLayer(myShpFile, 'Points', 'ogr') self.assertTrue(layer.isValid()) cnt = layer.pendingFields().count() # NOQA idx = layer.addExpressionField('"exp3"*2', QgsField('exp1', QVariant.LongLong)) # NOQA idx = layer.addExpressionField('"exp1"-1', QgsField('exp2', QVariant.LongLong)) # NOQA idx = layer.addExpressionField('"exp2"*3', QgsField('exp3', QVariant.LongLong)) # NOQA # really just testing that this doesn't hang/crash... there's no good result here! fet = next(layer.getFeatures(QgsFeatureRequest().setSubsetOfAttributes(['exp2'], layer.fields()))) self.assertEqual(fet['Class'], NULL)
def test_ExpressionFieldNested(self): myShpFile = os.path.join(TEST_DATA_DIR, "points.shp") layer = QgsVectorLayer(myShpFile, "Points", "ogr") self.assertTrue(layer.isValid()) cnt = layer.pendingFields().count() idx = layer.addExpressionField('"Staff"*2', QgsField("exp1", QVariant.LongLong)) idx = layer.addExpressionField('"exp1"-1', QgsField("exp2", QVariant.LongLong)) fet = next(layer.getFeatures(QgsFeatureRequest().setSubsetOfAttributes(["exp2"], layer.fields()))) self.assertEqual(fet["Class"], NULL) # nested virtual fields should make all these attributes be fetched self.assertEqual(fet["Staff"], 2) self.assertEqual(fet["exp2"], 3) self.assertEqual(fet["exp1"], 4)
def test_ExpressionFieldEllipsoidLengthCalculation(self): #create a temporary layer temp_layer = QgsVectorLayer("LineString?crs=epsg:3111&field=pk:int", "vl", "memory") self.assertTrue(temp_layer.isValid()) f1 = QgsFeature(temp_layer.dataProvider().fields(), 1) f1.setAttribute("pk", 1) f1.setGeometry(QgsGeometry.fromPolyline([QgsPoint(2484588, 2425722), QgsPoint(2482767, 2398853)])) temp_layer.dataProvider().addFeatures([f1]) # set project CRS and ellipsoid srs = QgsCoordinateReferenceSystem(3111, QgsCoordinateReferenceSystem.EpsgCrsId) QgsProject.instance().writeEntry("SpatialRefSys", "/ProjectCRSProj4String", srs.toProj4()) QgsProject.instance().writeEntry("SpatialRefSys", "/ProjectCRSID", srs.srsid()) QgsProject.instance().writeEntry("SpatialRefSys", "/ProjectCrs", srs.authid()) QgsProject.instance().writeEntry("Measure", "/Ellipsoid", "WGS84") QgsProject.instance().writeEntry("Measurement", "/DistanceUnits", QgsUnitTypes.encodeUnit(QGis.Meters)) idx = temp_layer.addExpressionField('$length', QgsField('length', QVariant.Double)) # NOQA # check value f = temp_layer.getFeatures().next() expected = 26932.156 self.assertAlmostEqual(f['length'], expected, 3) # change project length unit, check calculation respects unit QgsProject.instance().writeEntry("Measurement", "/DistanceUnits", QgsUnitTypes.encodeUnit(QGis.Feet)) f = temp_layer.getFeatures().next() expected = 88360.0918635 self.assertAlmostEqual(f['length'], expected, 3)
def test_ExpressionFieldEllipsoidAreaCalculation(self): #create a temporary layer temp_layer = QgsVectorLayer("Polygon?crs=epsg:3111&field=pk:int", "vl", "memory") self.assertTrue(temp_layer.isValid()) f1 = QgsFeature(temp_layer.dataProvider().fields(), 1) f1.setAttribute("pk", 1) f1.setGeometry(QgsGeometry.fromPolygon([[QgsPoint(2484588, 2425722), QgsPoint(2482767, 2398853), QgsPoint(2520109, 2397715), QgsPoint(2520792, 2425494), QgsPoint(2484588, 2425722)]])) temp_layer.dataProvider().addFeatures([f1]) # set project CRS and ellipsoid srs = QgsCoordinateReferenceSystem(3111, QgsCoordinateReferenceSystem.EpsgCrsId) QgsProject.instance().writeEntry("SpatialRefSys", "/ProjectCRSProj4String", srs.toProj4()) QgsProject.instance().writeEntry("SpatialRefSys", "/ProjectCRSID", srs.srsid()) QgsProject.instance().writeEntry("SpatialRefSys", "/ProjectCrs", srs.authid()) QgsProject.instance().writeEntry("Measure", "/Ellipsoid", "WGS84") QgsProject.instance().writeEntry("Measurement", "/AreaUnits", QgsUnitTypes.encodeUnit(QgsUnitTypes.SquareMeters)) idx = temp_layer.addExpressionField('$area', QgsField('area', QVariant.Double)) # NOQA # check value f = temp_layer.getFeatures().next() expected = 1009089817.0 self.assertAlmostEqual(f['area'], expected, delta=1.0) # change project area unit, check calculation respects unit QgsProject.instance().writeEntry("Measurement", "/AreaUnits", QgsUnitTypes.encodeUnit(QgsUnitTypes.SquareMiles)) f = temp_layer.getFeatures().next() expected = 389.6117565069 self.assertAlmostEqual(f['area'], expected, 3)
def test_ExpressionFieldNestedGeometry(self): myShpFile = os.path.join(TEST_DATA_DIR, "points.shp") layer = QgsVectorLayer(myShpFile, "Points", "ogr") self.assertTrue(layer.isValid()) cnt = layer.pendingFields().count() idx = layer.addExpressionField("$x*2", QgsField("exp1", QVariant.LongLong)) idx = layer.addExpressionField('"exp1"/1.5', QgsField("exp2", QVariant.LongLong)) fet = next( layer.getFeatures( QgsFeatureRequest() .setFlags(QgsFeatureRequest.NoGeometry) .setSubsetOfAttributes(["exp2"], layer.fields()) ) ) # nested virtual fields should have made geometry be fetched self.assertEqual(fet["exp2"], -156) self.assertEqual(fet["exp1"], -234)
def test_JoinUsingExpression2(self): """ test joining a layer using a virtual field (the other way!) """ joinLayer = QgsVectorLayer( "Point?field=x:string&field=y:integer&field=z:integer", "joinlayer", "memory") pr = joinLayer.dataProvider() f1 = QgsFeature() f1.setAttributes(["foo", 246, 321]) f2 = QgsFeature() f2.setAttributes(["bar", 456, 654]) self.assertTrue(pr.addFeatures([f1, f2])) joinLayer.addExpressionField('"y"/2', QgsField('exp1', QVariant.LongLong)) layer = QgsVectorLayer("Point?field=fldtxt:string&field=fldint:integer", "addfeat", "memory") pr = layer.dataProvider() f = QgsFeature() f.setAttributes(["test", 123]) self.assertTrue(pr.addFeatures([f])) QgsProject.instance().addMapLayers([layer, joinLayer]) join = QgsVectorLayerJoinInfo() join.setTargetFieldName("fldint") join.setJoinLayer(joinLayer) join.setJoinFieldName("exp1") join.setUsingMemoryCache(True) layer.addJoin(join) f = QgsFeature() fi = layer.getFeatures() self.assertTrue(fi.nextFeature(f)) attrs = f.attributes() self.assertEqual(attrs[0], "test") self.assertEqual(attrs[1], 123) self.assertEqual(attrs[2], "foo") self.assertEqual(attrs[3], 246) self.assertEqual(attrs[4], 321) self.assertFalse(fi.nextFeature(f)) QgsProject.instance().removeMapLayers([layer.id(), joinLayer.id()])
def test_JoinUsingExpression(self): """ test joining a layer using a virtual field """ joinLayer = QgsVectorLayer( "Point?field=x:string&field=y:integer&field=z:integer", "joinlayer", "memory") pr = joinLayer.dataProvider() f1 = QgsFeature() f1.setAttributes(["foo", 246, 321]) f2 = QgsFeature() f2.setAttributes(["bar", 456, 654]) self.assertTrue(pr.addFeatures([f1, f2])) layer = QgsVectorLayer("Point?field=fldtxt:string&field=fldint:integer", "addfeat", "memory") pr = layer.dataProvider() f = QgsFeature() f.setAttributes(["test", 123]) self.assertTrue(pr.addFeatures([f])) layer.addExpressionField('"fldint"*2', QgsField('exp1', QVariant.LongLong)) QgsProject.instance().addMapLayers([layer, joinLayer]) join = QgsVectorLayerJoinInfo() join.setTargetFieldName("exp1") join.setJoinLayer(joinLayer) join.setJoinFieldName("y") join.setUsingMemoryCache(True) layer.addJoin(join) f = QgsFeature() fi = layer.getFeatures() self.assertTrue(fi.nextFeature(f)) attrs = f.attributes() self.assertEqual(attrs[0], "test") self.assertEqual(attrs[1], 123) self.assertEqual(attrs[2], "foo") self.assertEqual(attrs[3], 321) self.assertEqual(attrs[4], 246) self.assertFalse(fi.nextFeature(f)) QgsProject.instance().removeMapLayers([layer.id(), joinLayer.id()])
def setUp(self): myShpFile = os.path.join(TEST_DATA_DIR, 'rectangles.shp') layer = QgsVectorLayer(myShpFile, 'Rectangles', 'ogr') vfield = QgsField('fa_cy-fie+ld', QVariant.Int) layer.addExpressionField('"id"', vfield) QgsProject.instance().addMapLayer(layer) # Create rulebased style sym1 = QgsFillSymbol.createSimple({ 'color': '#fdbf6f', 'outline_color': 'black' }) sym2 = QgsFillSymbol.createSimple({ 'color': '#71bd6c', 'outline_color': 'black' }) sym3 = QgsFillSymbol.createSimple({ 'color': '#1f78b4', 'outline_color': 'black' }) self.r1 = QgsRuleBasedRenderer.Rule(sym1, 0, 0, '"id" = 1') self.r2 = QgsRuleBasedRenderer.Rule(sym2, 0, 0, '"id" = 2') self.r3 = QgsRuleBasedRenderer.Rule(sym3, 0, 0, 'ELSE') rootrule = QgsRuleBasedRenderer.Rule(None) rootrule.appendChild(self.r1) rootrule.appendChild(self.r2) rootrule.appendChild(self.r3) layer.setRenderer(QgsRuleBasedRenderer(rootrule)) self.mapsettings = QgsMapSettings() self.mapsettings.setOutputSize(QSize(400, 400)) self.mapsettings.setOutputDpi(96) self.mapsettings.setExtent(QgsRectangle(-163, 22, -70, 52)) rendered_layers = [layer] self.mapsettings.setLayers(rendered_layers) self.mapsettings_archive.append(self.mapsettings)
def test_ExpressionFieldNestedCircular(self): """ test circular virtual field definitions """ myShpFile = os.path.join(TEST_DATA_DIR, 'points.shp') layer = QgsVectorLayer(myShpFile, 'Points', 'ogr') self.assertTrue(layer.isValid()) cnt = layer.fields().count() # NOQA idx = layer.addExpressionField('"exp3"*2', QgsField('exp1', QVariant.LongLong)) # NOQA idx = layer.addExpressionField('"exp1"-1', QgsField('exp2', QVariant.LongLong)) # NOQA idx = layer.addExpressionField('"exp2"*3', QgsField('exp3', QVariant.LongLong)) # NOQA # really just testing that this doesn't hang/crash... there's no good result here! fet = next( layer.getFeatures(QgsFeatureRequest().setSubsetOfAttributes( ['exp2'], layer.fields()))) self.assertEqual(fet['Class'], NULL)
def test_ExpressionFieldDependingOnOtherFields(self): myShpFile = os.path.join(TEST_DATA_DIR, 'points.shp') layer = QgsVectorLayer(myShpFile, 'Points', 'ogr') self.assertTrue(layer.isValid()) idx = layer.addExpressionField("eval('Class')", QgsField('exp1', QVariant.String)) # NOQA fet = next( layer.getFeatures(QgsFeatureRequest().setFlags( QgsFeatureRequest.NoGeometry).setSubsetOfAttributes( ['exp1'], layer.fields()))) self.assertEqual(fet['exp1'], 'Jet')
def test_ExpressionFieldEllipsoidAreaCalculation(self): #create a temporary layer temp_layer = QgsVectorLayer("Polygon?crs=epsg:3111&field=pk:int", "vl", "memory") self.assertTrue(temp_layer.isValid()) f1 = QgsFeature(temp_layer.dataProvider().fields(), 1) f1.setAttribute("pk", 1) f1.setGeometry( QgsGeometry.fromPolygon([[ QgsPoint(2484588, 2425722), QgsPoint(2482767, 2398853), QgsPoint(2520109, 2397715), QgsPoint(2520792, 2425494), QgsPoint(2484588, 2425722) ]])) temp_layer.dataProvider().addFeatures([f1]) # set project CRS and ellipsoid srs = QgsCoordinateReferenceSystem( 3111, QgsCoordinateReferenceSystem.EpsgCrsId) QgsProject.instance().writeEntry("SpatialRefSys", "/ProjectCRSProj4String", srs.toProj4()) QgsProject.instance().writeEntry("SpatialRefSys", "/ProjectCRSID", srs.srsid()) QgsProject.instance().writeEntry("SpatialRefSys", "/ProjectCrs", srs.authid()) QgsProject.instance().writeEntry("Measure", "/Ellipsoid", "WGS84") QgsProject.instance().writeEntry( "Measurement", "/AreaUnits", QgsUnitTypes.encodeUnit(QgsUnitTypes.SquareMeters)) idx = temp_layer.addExpressionField('$area', QgsField('area', QVariant.Double)) # NOQA # check value f = next(temp_layer.getFeatures()) expected = 1009089817.0 self.assertAlmostEqual(f['area'], expected, delta=1.0) # change project area unit, check calculation respects unit QgsProject.instance().writeEntry( "Measurement", "/AreaUnits", QgsUnitTypes.encodeUnit(QgsUnitTypes.SquareMiles)) f = next(temp_layer.getFeatures()) expected = 389.6117565069 self.assertAlmostEqual(f['area'], expected, 3)
def test_ExpressionFieldEllipsoidLengthCalculation(self): #create a temporary layer temp_layer = QgsVectorLayer("LineString?crs=epsg:3111&field=pk:int", "vl", "memory") self.assertTrue(temp_layer.isValid()) f1 = QgsFeature(temp_layer.dataProvider().fields(), 1) f1.setAttribute("pk", 1) f1.setGeometry( QgsGeometry.fromPolyline( [QgsPoint(2484588, 2425722), QgsPoint(2482767, 2398853)])) temp_layer.dataProvider().addFeatures([f1]) # set project CRS and ellipsoid srs = QgsCoordinateReferenceSystem( 3111, QgsCoordinateReferenceSystem.EpsgCrsId) QgsProject.instance().writeEntry("SpatialRefSys", "/ProjectCRSProj4String", srs.toProj4()) QgsProject.instance().writeEntry("SpatialRefSys", "/ProjectCRSID", srs.srsid()) QgsProject.instance().writeEntry("SpatialRefSys", "/ProjectCrs", srs.authid()) QgsProject.instance().writeEntry("Measure", "/Ellipsoid", "WGS84") QgsProject.instance().writeEntry("Measurement", "/DistanceUnits", QgsUnitTypes.encodeUnit(QGis.Meters)) idx = temp_layer.addExpressionField('$length', QgsField('length', QVariant.Double)) # NOQA # check value f = next(temp_layer.getFeatures()) expected = 26932.156 self.assertAlmostEqual(f['length'], expected, 3) # change project length unit, check calculation respects unit QgsProject.instance().writeEntry("Measurement", "/DistanceUnits", QgsUnitTypes.encodeUnit(QGis.Feet)) f = next(temp_layer.getFeatures()) expected = 88360.0918635 self.assertAlmostEqual(f['length'], expected, 3)