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_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 testEncodeDecodeLayoutUnits(self): """Test encoding and decoding layout units""" units = [QgsUnitTypes.LayoutMillimeters, QgsUnitTypes.LayoutCentimeters, QgsUnitTypes.LayoutMeters, QgsUnitTypes.LayoutInches, QgsUnitTypes.LayoutFeet, QgsUnitTypes.LayoutPoints, QgsUnitTypes.LayoutPicas, QgsUnitTypes.LayoutPixels] for u in units: res, ok = QgsUnitTypes.decodeLayoutUnit(QgsUnitTypes.encodeUnit(u)) assert ok self.assertEqual(res, u) # Test decoding bad units res, ok = QgsUnitTypes.decodeLayoutUnit('bad') self.assertFalse(ok) # default units should be MM self.assertEqual(res, QgsUnitTypes.LayoutMillimeters) # Test that string is cleaned before decoding res, ok = QgsUnitTypes.decodeLayoutUnit(' px ') assert ok self.assertEqual(res, QgsUnitTypes.LayoutPixels)
def testEncodeDecodeDistanceUnits(self): """Test encoding and decoding distance units""" units = [QgsUnitTypes.DistanceMeters, QgsUnitTypes.DistanceKilometers, QgsUnitTypes.DistanceFeet, QgsUnitTypes.DistanceYards, QgsUnitTypes.DistanceMiles, QgsUnitTypes.DistanceDegrees, QgsUnitTypes.DistanceCentimeters, QgsUnitTypes.DistanceMillimeters, QgsUnitTypes.DistanceUnknownUnit, QgsUnitTypes.DistanceNauticalMiles] for u in units: res, ok = QgsUnitTypes.decodeDistanceUnit(QgsUnitTypes.encodeUnit(u)) assert ok self.assertEqual(res, u) # Test decoding bad units res, ok = QgsUnitTypes.decodeDistanceUnit('bad') self.assertFalse(ok) self.assertEqual(res, QgsUnitTypes.DistanceUnknownUnit) # Test that string is cleaned before decoding res, ok = QgsUnitTypes.decodeDistanceUnit(' FeEt ') assert ok self.assertEqual(res, QgsUnitTypes.DistanceFeet)
def testEncodeDecodeRenderUnits(self): """Test encoding and decoding render units""" units = [QgsUnitTypes.RenderMillimeters, QgsUnitTypes.RenderMapUnits, QgsUnitTypes.RenderPixels, QgsUnitTypes.RenderPercentage, QgsUnitTypes.RenderPoints] for u in units: res, ok = QgsUnitTypes.decodeRenderUnit(QgsUnitTypes.encodeUnit(u)) assert ok self.assertEqual(res, u) # Test decoding bad units res, ok = QgsUnitTypes.decodeRenderUnit('bad') self.assertFalse(ok) # default units should be MM self.assertEqual(res, QgsUnitTypes.RenderMillimeters) # Test that string is cleaned before decoding res, ok = QgsUnitTypes.decodeRenderUnit(' PiXeL ') assert ok self.assertEqual(res, QgsUnitTypes.RenderPixels) # check some aliases - used in data defined labeling res, ok = QgsUnitTypes.decodeRenderUnit('MapUnits') assert ok self.assertEqual(res, QgsUnitTypes.RenderMapUnits) res, ok = QgsUnitTypes.decodeRenderUnit('Percent') assert ok self.assertEqual(res, QgsUnitTypes.RenderPercentage) res, ok = QgsUnitTypes.decodeRenderUnit('Points') assert ok self.assertEqual(res, QgsUnitTypes.RenderPoints)
def testEncodeDecodeAreaUnits(self): """Test encoding and decoding area units""" units = [QgsUnitTypes.AreaSquareMeters, QgsUnitTypes.AreaSquareKilometers, QgsUnitTypes.AreaSquareFeet, QgsUnitTypes.AreaSquareYards, QgsUnitTypes.AreaSquareMiles, QgsUnitTypes.AreaHectares, QgsUnitTypes.AreaAcres, QgsUnitTypes.AreaSquareNauticalMiles, QgsUnitTypes.AreaSquareDegrees, QgsUnitTypes.AreaSquareCentimeters, QgsUnitTypes.AreaSquareMillimeters, QgsUnitTypes.AreaUnknownUnit] for u in units: res, ok = QgsUnitTypes.decodeAreaUnit(QgsUnitTypes.encodeUnit(u)) assert ok self.assertEqual(res, u) # Test decoding bad units res, ok = QgsUnitTypes.decodeAreaUnit('bad') self.assertFalse(ok) self.assertEqual(res, QgsUnitTypes.AreaUnknownUnit) # Test that string is cleaned before decoding res, ok = QgsUnitTypes.decodeAreaUnit(' Ha ') assert ok self.assertEqual(res, QgsUnitTypes.AreaHectares)
def testEncodeDecodeAngleUnits(self): """Test encoding and decoding angle units""" units = [ QgsUnitTypes.AngleDegrees, QgsUnitTypes.Radians, QgsUnitTypes.Gon, QgsUnitTypes.MinutesOfArc, QgsUnitTypes.SecondsOfArc, QgsUnitTypes.Turn, QgsUnitTypes.UnknownAngleUnit, ] for u in units: res, ok = QgsUnitTypes.decodeAngleUnit(QgsUnitTypes.encodeUnit(u)) assert ok, "could not decode unit {}".format(QgsUnitTypes.toString(u)) self.assertEqual(res, u) # Test decoding bad units res, ok = QgsUnitTypes.decodeAngleUnit("bad") self.assertFalse(ok) self.assertEqual(res, QgsUnitTypes.UnknownAngleUnit) # Test that string is cleaned before decoding res, ok = QgsUnitTypes.decodeAngleUnit(" MoA ") assert ok self.assertEqual(res, QgsUnitTypes.MinutesOfArc)
def createBackgroundSettings(self): s = QgsTextBackgroundSettings() s.setEnabled(True) s.setType(QgsTextBackgroundSettings.ShapeEllipse) s.setSvgFile('svg.svg') s.setSizeType(QgsTextBackgroundSettings.SizeFixed) s.setSize(QSizeF(1, 2)) s.setSizeUnit(QgsUnitTypes.RenderPixels) s.setSizeMapUnitScale(QgsMapUnitScale(1, 2)) s.setRotationType(QgsTextBackgroundSettings.RotationFixed) s.setRotation(45) s.setOffset(QPointF(3, 4)) s.setOffsetUnit(QgsUnitTypes.RenderMapUnits) s.setOffsetMapUnitScale(QgsMapUnitScale(5, 6)) s.setRadii(QSizeF(11, 12)) s.setRadiiUnit(QgsUnitTypes.RenderPixels) s.setRadiiMapUnitScale(QgsMapUnitScale(15, 16)) s.setFillColor(QColor(255, 0, 0)) s.setStrokeColor(QColor(0, 255, 0)) s.setOpacity(0.5) s.setJoinStyle(Qt.RoundJoin) s.setBlendMode(QPainter.CompositionMode_Difference) s.setStrokeWidth(7) s.setStrokeWidthUnit(QgsUnitTypes.RenderMapUnits) s.setStrokeWidthMapUnitScale(QgsMapUnitScale(QgsMapUnitScale(25, 26))) s.setPaintEffect(QgsBlurEffect.create({'blur_level': '6.0', 'blur_unit': QgsUnitTypes.encodeUnit(QgsUnitTypes.RenderMillimeters), 'enabled': '1'})) return s
def createBufferSettings(self): s = QgsTextBufferSettings() s.setEnabled(True) s.setSize(5) s.setSizeUnit(QgsUnitTypes.RenderPixels) s.setSizeMapUnitScale(QgsMapUnitScale(1, 2)) s.setColor(QColor(255, 0, 0)) s.setFillBufferInterior(True) s.setOpacity(0.5) s.setJoinStyle(Qt.RoundJoin) s.setBlendMode(QPainter.CompositionMode_Difference) s.setPaintEffect(QgsBlurEffect.create({'blur_level': '2.0', 'blur_unit': QgsUnitTypes.encodeUnit(QgsUnitTypes.RenderMillimeters), 'enabled': '1'})) return s
def testEncodeDecodeDistanceUnits(self): """Test encoding and decoding distance units""" units = [QGis.Meters, QGis.Feet, QGis.Degrees, QGis.UnknownUnit, QGis.NauticalMiles] for u in units: res, ok = QgsUnitTypes.decodeDistanceUnit(QgsUnitTypes.encodeUnit(u)) assert ok self.assertEqual(res, u) # Test decoding bad units res, ok = QgsUnitTypes.decodeDistanceUnit("bad") self.assertFalse(ok) self.assertEqual(res, QGis.UnknownUnit) # Test that string is cleaned before decoding res, ok = QgsUnitTypes.decodeDistanceUnit(" FeEt ") assert ok self.assertEqual(res, QGis.Feet)
def testEncodeDecodeSymbolUnits(self): """Test encoding and decoding symbol units""" units = [QgsSymbolV2.MM, QgsSymbolV2.MapUnit, QgsSymbolV2.Pixel, QgsSymbolV2.Percentage] for u in units: res, ok = QgsUnitTypes.decodeSymbolUnit(QgsUnitTypes.encodeUnit(u)) assert ok self.assertEqual(res, u) # Test decoding bad units res, ok = QgsUnitTypes.decodeSymbolUnit("bad") self.assertFalse(ok) # default units should be MM self.assertEqual(res, QgsSymbolV2.MM) # Test that string is cleaned before decoding res, ok = QgsUnitTypes.decodeSymbolUnit(" PiXeL ") assert ok self.assertEqual(res, QgsSymbolV2.Pixel)
def testEncodeDecodeRenderUnits(self): """Test encoding and decoding render units""" units = [QgsUnitTypes.RenderMillimeters, QgsUnitTypes.RenderMapUnits, QgsUnitTypes.RenderPixels, QgsUnitTypes.RenderPercentage] for u in units: res, ok = QgsUnitTypes.decodeRenderUnit(QgsUnitTypes.encodeUnit(u)) assert ok self.assertEqual(res, u) # Test decoding bad units res, ok = QgsUnitTypes.decodeRenderUnit('bad') self.assertFalse(ok) # default units should be MM self.assertEqual(res, QgsUnitTypes.RenderMillimeters) # Test that string is cleaned before decoding res, ok = QgsUnitTypes.decodeRenderUnit(' PiXeL ') assert ok self.assertEqual(res, QgsUnitTypes.RenderPixels)
def testEncodeDecodeAreaUnits(self): """Test encoding and decoding area units""" units = [ QgsUnitTypes.SquareMeters, QgsUnitTypes.SquareKilometers, QgsUnitTypes.SquareFeet, QgsUnitTypes.SquareYards, QgsUnitTypes.SquareMiles, QgsUnitTypes.Hectares, QgsUnitTypes.Acres, QgsUnitTypes.SquareNauticalMiles, QgsUnitTypes.SquareDegrees, QgsUnitTypes.UnknownAreaUnit ] for u in units: res, ok = QgsUnitTypes.decodeAreaUnit(QgsUnitTypes.encodeUnit(u)) assert ok self.assertEqual(res, u) # Test decoding bad units res, ok = QgsUnitTypes.decodeAreaUnit('bad') self.assertFalse(ok) self.assertEqual(res, QgsUnitTypes.UnknownAreaUnit) # Test that string is cleaned before decoding res, ok = QgsUnitTypes.decodeAreaUnit(' Ha ') assert ok self.assertEqual(res, QgsUnitTypes.Hectares)
def testEncodeDecodeAngleUnits(self): """Test encoding and decoding angle units""" units = [QgsUnitTypes.AngleDegrees, QgsUnitTypes.AngleRadians, QgsUnitTypes.AngleGon, QgsUnitTypes.AngleMinutesOfArc, QgsUnitTypes.AngleSecondsOfArc, QgsUnitTypes.AngleTurn, QgsUnitTypes.AngleUnknownUnit] for u in units: res, ok = QgsUnitTypes.decodeAngleUnit(QgsUnitTypes.encodeUnit(u)) assert ok, 'could not decode unit {}'.format(QgsUnitTypes.toString(u)) self.assertEqual(res, u) # Test decoding bad units res, ok = QgsUnitTypes.decodeAngleUnit('bad') self.assertFalse(ok) self.assertEqual(res, QgsUnitTypes.AngleUnknownUnit) # Test that string is cleaned before decoding res, ok = QgsUnitTypes.decodeAngleUnit(' MoA ') assert ok self.assertEqual(res, QgsUnitTypes.AngleMinutesOfArc)
def testEncodeDecodeLayoutUnits(self): """Test encoding and decoding layout units""" units = [ QgsUnitTypes.LayoutMillimeters, QgsUnitTypes.LayoutCentimeters, QgsUnitTypes.LayoutMeters, QgsUnitTypes.LayoutInches, QgsUnitTypes.LayoutFeet, QgsUnitTypes.LayoutPoints, QgsUnitTypes.LayoutPicas, QgsUnitTypes.LayoutPixels ] for u in units: res, ok = QgsUnitTypes.decodeLayoutUnit(QgsUnitTypes.encodeUnit(u)) assert ok self.assertEqual(res, u) # Test decoding bad units res, ok = QgsUnitTypes.decodeLayoutUnit('bad') self.assertFalse(ok) # default units should be MM self.assertEqual(res, QgsUnitTypes.LayoutMillimeters) # Test that string is cleaned before decoding res, ok = QgsUnitTypes.decodeLayoutUnit(' px ') assert ok self.assertEqual(res, QgsUnitTypes.LayoutPixels)
def testEncodeDecodeAngleUnits(self): """Test encoding and decoding angle units""" units = [ QgsUnitTypes.AngleDegrees, QgsUnitTypes.AngleRadians, QgsUnitTypes.AngleGon, QgsUnitTypes.AngleMinutesOfArc, QgsUnitTypes.AngleSecondsOfArc, QgsUnitTypes.AngleTurn, QgsUnitTypes.AngleUnknownUnit ] for u in units: res, ok = QgsUnitTypes.decodeAngleUnit(QgsUnitTypes.encodeUnit(u)) assert ok, 'could not decode unit {}'.format( QgsUnitTypes.toString(u)) self.assertEqual(res, u) # Test decoding bad units res, ok = QgsUnitTypes.decodeAngleUnit('bad') self.assertFalse(ok) self.assertEqual(res, QgsUnitTypes.AngleUnknownUnit) # Test that string is cleaned before decoding res, ok = QgsUnitTypes.decodeAngleUnit(' MoA ') assert ok self.assertEqual(res, QgsUnitTypes.AngleMinutesOfArc)
def on_cmdAdd_clicked(self): if self.mcboRasterLayer.currentLayer() is None: self.cmdAdd.setStyleSheet('color:red') self.tabList.horizontalHeader().setStyleSheet('color:red') self.lblRasterLayer.setStyleSheet('color:red') self.send_to_messagebar( 'No raster layers to process. Please add a RASTER layer into QGIS', level=QgsMessageBar.WARNING, duration=5) return rowPosition = self.tabList.rowCount() self.tabList.insertRow(rowPosition) ## Save the id of the layer to a column used to get a layer object later on. ## adapted from https://gis.stackexchange.com/questions/165415/activating-layer-by-its-name-in-pyqgis self.tabList.setItem( rowPosition, 0, QtGui.QTableWidgetItem(self.mcboRasterLayer.currentLayer().id())) self.tabList.setItem( rowPosition, 1, QtGui.QTableWidgetItem(self.mcboRasterLayer.currentLayer().name())) if rowPosition == 0: # get the pixel units from the coordinate systems as a string ie degrees, metres etc. # for QGIS 3 see the following functions # .toAbbreviatedString() https://www.qgis.org/api/classQgsUnitTypes.html#a7d09b9df11b6dcc2fe29928f5de296a4 # and /or DistanceValue https://www.qgis.org/api/structQgsUnitTypes_1_1DistanceValue.html pixel_units = QgsUnitTypes.encodeUnit( self.mcboRasterLayer.currentLayer().crs().mapUnits()) # Adjust for Aust/UK spelling pixel_units = pixel_units.replace('meters', 'metres') if self.mcboRasterLayer.currentLayer().crs().geographicFlag(): ft = 'f' # this will convert 1.99348e-05 to 0.000020 else: ft = 'g' # this will convert 2.0 to 2 or 0.5, '0.5' self.pixel_size = format( self.mcboRasterLayer.currentLayer().rasterUnitsPerPixelX(), ft) self.lblPixelFilter.setText( 'Only allow processing of rasters with a pixel size of {} {}'. format(self.pixel_size, pixel_units)) if not self.mcboRasterLayer.currentLayer().crs().geographicFlag(): for obj in [ self.opt3x3, self.opt5x5, self.opt7x7, self.opt9x9 ]: pixel_len = format( float(self.pixel_size) * float(obj.text()[0]), ft) obj.setText('{0} ({1}x{1}{2})'.format( obj.text(), pixel_len, pixel_units[0])) # remove layer from picklist. self.setMapLayers() self.lblRasterLayer.setStyleSheet('color:black') self.cmdAdd.setStyleSheet('color:black') self.cmdAdd.setEnabled(len(self.mcboRasterLayer) > 0) self.cmdDel.setEnabled(self.tabList.rowCount() > 0) self.cmdDown.setEnabled(self.tabList.rowCount() > 1) self.cmdUp.setEnabled(self.tabList.rowCount() > 1)