def testConvertSingleUnit(self): ms = QgsMapSettings() ms.setExtent(QgsRectangle(0, 0, 100, 100)) ms.setOutputSize(QSize(100, 50)) ms.setOutputDpi(300) r = QgsRenderContext.fromMapSettings(ms) # renderer scale should be about 1:291937841 # start with no min/max scale c = QgsMapUnitScale() #self.assertEqual(r.scaleFactor(),666) sf = r.convertToPainterUnits(1, QgsUnitTypes.RenderMapUnits, c) self.assertAlmostEqual(sf, 0.5, places=5) sf = r.convertToPainterUnits(1, QgsUnitTypes.RenderMillimeters, c) self.assertAlmostEqual(sf, 11.8110236, places=5) sf = r.convertToPainterUnits(1, QgsUnitTypes.RenderPoints, c) self.assertAlmostEqual(sf, 4.166666665625, places=5) sf = r.convertToPainterUnits(1, QgsUnitTypes.RenderInches, c) self.assertAlmostEqual(sf, 300.0, places=5) sf = r.convertToPainterUnits(1, QgsUnitTypes.RenderPixels, c) self.assertAlmostEqual(sf, 1.0, places=5) # minimum scale greater than the renderer scale, so should be limited to minScale c.minScale = 150000000.0 sf = r.convertToPainterUnits(1, QgsUnitTypes.RenderMapUnits, c) self.assertAlmostEqual(sf, 3.89250455, places=5) # only conversion from mapunits should be affected sf = r.convertToPainterUnits(1, QgsUnitTypes.RenderMillimeters, c) self.assertAlmostEqual(sf, 11.8110236, places=5) sf = r.convertToPainterUnits(1, QgsUnitTypes.RenderPoints, c) self.assertAlmostEqual(sf, 4.166666665625, places=5) sf = r.convertToPainterUnits(1, QgsUnitTypes.RenderInches, c) self.assertAlmostEqual(sf, 300.0, places=5) sf = r.convertToPainterUnits(1, QgsUnitTypes.RenderPixels, c) self.assertAlmostEqual(sf, 1.0, places=5) c.minScale = 0 # maximum scale less than the renderer scale, so should be limited to maxScale c.maxScale = 350000000.0 sf = r.convertToPainterUnits(1, QgsUnitTypes.RenderMapUnits, c) self.assertAlmostEqual(sf, 0.5, places=5) # only conversion from mapunits should be affected sf = r.convertToPainterUnits(1, QgsUnitTypes.RenderMillimeters, c) self.assertAlmostEqual(sf, 11.8110236, places=5) sf = r.convertToPainterUnits(1, QgsUnitTypes.RenderPoints, c) self.assertAlmostEqual(sf, 4.166666665625, places=5) sf = r.convertToPainterUnits(1, QgsUnitTypes.RenderInches, c) self.assertAlmostEqual(sf, 300.0, places=5) sf = r.convertToPainterUnits(1, QgsUnitTypes.RenderPixels, c) self.assertAlmostEqual(sf, 1.0, places=5)
def testPixelSizeScaleFactor(self): # test QgsSymbolLayerUtils::pixelSizeScaleFactor() using QgsMapUnitScale ms = QgsMapSettings() ms.setExtent(QgsRectangle(0, 0, 100, 100)) ms.setOutputSize(QSize(100, 50)) ms.setOutputDpi(300) r = QgsRenderContext.fromMapSettings(ms) # renderer scale should be about 1:291937841 # start with no min/max scale c = QgsMapUnitScale() sf = QgsSymbolLayerUtils.pixelSizeScaleFactor(r, QgsUnitTypes.RenderMapUnits, c) self.assertAlmostEqual(sf, 0.5, places=5) sf = QgsSymbolLayerUtils.pixelSizeScaleFactor(r, QgsUnitTypes.RenderMillimeters, c) self.assertAlmostEqual(sf, 11.8110236, places=5) sf = QgsSymbolLayerUtils.pixelSizeScaleFactor(r, QgsUnitTypes.RenderPoints, c) self.assertAlmostEqual(sf, 33.48006689762, places=5) sf = QgsSymbolLayerUtils.pixelSizeScaleFactor(r, QgsUnitTypes.RenderPixels, c) self.assertAlmostEqual(sf, 1.0, places=5) # minimum scale greater than the renderer scale, so should be limited to minScale c.minScale = 1 / 150000000.0 sf = QgsSymbolLayerUtils.pixelSizeScaleFactor(r, QgsUnitTypes.RenderMapUnits, c) self.assertAlmostEqual(sf, 3.8925045, places=5) # only conversion from mapunits should be affected sf = QgsSymbolLayerUtils.pixelSizeScaleFactor(r, QgsUnitTypes.RenderMillimeters, c) self.assertAlmostEqual(sf, 11.811023, places=5) sf = QgsSymbolLayerUtils.pixelSizeScaleFactor(r, QgsUnitTypes.RenderPoints, c) self.assertAlmostEqual(sf, 33.48006689762, places=5) sf = QgsSymbolLayerUtils.pixelSizeScaleFactor(r, QgsUnitTypes.RenderPixels, c) self.assertAlmostEqual(sf, 1.0, places=5) c.minScale = 0 # maximum scale less than the renderer scale, so should be limited to maxScale c.maxScale = 1 / 350000000.0 sf = QgsSymbolLayerUtils.pixelSizeScaleFactor(r, QgsUnitTypes.RenderMapUnits, c) self.assertAlmostEqual(sf, 0.5, places=5) # only conversion from mapunits should be affected sf = QgsSymbolLayerUtils.pixelSizeScaleFactor(r, QgsUnitTypes.RenderMillimeters, c) self.assertAlmostEqual(sf, 11.8110236, places=5) sf = QgsSymbolLayerUtils.pixelSizeScaleFactor(r, QgsUnitTypes.RenderPoints, c) self.assertAlmostEqual(sf, 33.48006689762, places=5) sf = QgsSymbolLayerUtils.pixelSizeScaleFactor(r, QgsUnitTypes.RenderPixels, c) self.assertAlmostEqual(sf, 1.0, places=5)
def testConvertToPainterUnits(self): ms = QgsMapSettings() ms.setExtent(QgsRectangle(0, 0, 100, 100)) ms.setOutputSize(QSize(100, 50)) ms.setOutputDpi(300) r = QgsRenderContext.fromMapSettings(ms) # renderer scale should be about 1:291937841 # start with no min/max scale c = QgsMapUnitScale() size = r.convertToPainterUnits(2, QgsUnitTypes.RenderMapUnits, c) self.assertAlmostEqual(size, 1.0, places=5) size = r.convertToPainterUnits(2, QgsUnitTypes.RenderMillimeters, c) self.assertAlmostEqual(size, 23.622047, places=5) size = r.convertToPainterUnits(2, QgsUnitTypes.RenderPoints, c) self.assertAlmostEqual(size, 8.33333333125, places=5) size = r.convertToPainterUnits(2, QgsUnitTypes.RenderInches, c) self.assertAlmostEqual(size, 600.0, places=5) size = r.convertToPainterUnits(2, QgsUnitTypes.RenderPixels, c) self.assertAlmostEqual(size, 2.0, places=5) # minimum size greater than the calculated size, so size should be limited to minSizeMM c.minSizeMM = 5 c.minSizeMMEnabled = True size = r.convertToPainterUnits(2, QgsUnitTypes.RenderMapUnits, c) self.assertAlmostEqual(size, 59.0551181, places=5) # only conversion from mapunits should be affected size = r.convertToPainterUnits(2, QgsUnitTypes.RenderMillimeters, c) self.assertAlmostEqual(size, 23.622047, places=5) size = r.convertToPainterUnits(2, QgsUnitTypes.RenderPoints, c) self.assertAlmostEqual(size, 8.33333333125, places=5) size = r.convertToPainterUnits(2, QgsUnitTypes.RenderInches, c) self.assertAlmostEqual(size, 600.0, places=5) size = r.convertToPainterUnits(2, QgsUnitTypes.RenderPixels, c) self.assertAlmostEqual(size, 2.0, places=5) c.minSizeMMEnabled = False # maximum size less than the calculated size, so size should be limited to maxSizeMM c.maxSizeMM = 0.1 c.maxSizeMMEnabled = True size = r.convertToPainterUnits(2, QgsUnitTypes.RenderMapUnits, c) self.assertAlmostEqual(size, 1.0, places=5) # only conversion from mapunits should be affected size = r.convertToPainterUnits(2, QgsUnitTypes.RenderMillimeters, c) self.assertAlmostEqual(size, 23.622047, places=5) size = r.convertToPainterUnits(2, QgsUnitTypes.RenderPoints, c) self.assertAlmostEqual(size, 8.33333333125, places=5) size = r.convertToPainterUnits(2, QgsUnitTypes.RenderInches, c) self.assertAlmostEqual(size, 600.0, places=5) size = r.convertToPainterUnits(2, QgsUnitTypes.RenderPixels, c) self.assertAlmostEqual(size, 2.0, places=5)
def testConvertToPainterUnits(self): # test QgsSymbolLayerV2Utils::convertToPainterUnits() using QgsMapUnitScale ms = QgsMapSettings() ms.setExtent(QgsRectangle(0, 0, 100, 100)) ms.setOutputSize(QSize(100, 50)) ms.setOutputDpi(300) r = QgsRenderContext.fromMapSettings(ms) # renderer scale should be about 1:291937841 # start with no min/max scale c = QgsMapUnitScale() size = QgsSymbolLayerV2Utils.convertToPainterUnits(r, 2, QgsSymbolV2.MapUnit, c) self.assertAlmostEqual(size, 1.0, places=5) size = QgsSymbolLayerV2Utils.convertToPainterUnits(r, 2, QgsSymbolV2.MM, c) self.assertAlmostEqual(size, 23.622047, places=5) size = QgsSymbolLayerV2Utils.convertToPainterUnits(r, 2, QgsSymbolV2.Pixel, c) self.assertAlmostEqual(size, 2.0, places=5) # minimum size greater than the calculated size, so size should be limited to minSizeMM c.minSizeMM = 5 c.minSizeMMEnabled = True size = QgsSymbolLayerV2Utils.convertToPainterUnits(r, 2, QgsSymbolV2.MapUnit, c) self.assertAlmostEqual(size, 59.0551181, places=5) # only conversion from mapunits should be affected size = QgsSymbolLayerV2Utils.convertToPainterUnits(r, 2, QgsSymbolV2.MM, c) self.assertAlmostEqual(size, 23.622047, places=5) size = QgsSymbolLayerV2Utils.convertToPainterUnits(r, 2, QgsSymbolV2.Pixel, c) self.assertAlmostEqual(size, 2.0, places=5) c.minSizeMMEnabled = False # maximum size less than the calculated size, so size should be limited to maxSizeMM c.maxSizeMM = 0.1 c.maxSizeMMEnabled = True size = QgsSymbolLayerV2Utils.convertToPainterUnits(r, 2, QgsSymbolV2.MapUnit, c) self.assertAlmostEqual(size, 1.0, places=5) # only conversion from mapunits should be affected size = QgsSymbolLayerV2Utils.convertToPainterUnits(r, 2, QgsSymbolV2.MM, c) self.assertAlmostEqual(size, 23.622047, places=5) size = QgsSymbolLayerV2Utils.convertToPainterUnits(r, 2, QgsSymbolV2.Pixel, c) self.assertAlmostEqual(size, 2.0, places=5)
def testEncodeDecode(self): # test encoding and decoding QgsMapUnitScale s = QgsMapUnitScale() s.minScale = 50 s.maxScale = 100 s.minSizeMMEnabled = True s.minSizeMM = 3 s.maxSizeMMEnabled = False s.maxSizeMM = 99 encode = QgsSymbolLayerV2Utils.encodeMapUnitScale(s) r = QgsSymbolLayerV2Utils.decodeMapUnitScale(encode) self.assertEqual(s, r) # check old style encoding encode = '9,78.3' r = QgsSymbolLayerV2Utils.decodeMapUnitScale(encode) self.assertEqual(r.minScale, 9) self.assertEqual(r.maxScale, 78.3) self.assertFalse(r.minSizeMMEnabled) self.assertEqual(r.minSizeMM, 0) self.assertFalse(r.maxSizeMMEnabled) self.assertEqual(r.maxSizeMM, 0)
def testConvertToMapUnits(self): ms = QgsMapSettings() ms.setExtent(QgsRectangle(0, 0, 100, 100)) ms.setOutputSize(QSize(100, 50)) ms.setOutputDpi(300) r = QgsRenderContext.fromMapSettings(ms) # renderer scale should be about 1:291937841 # start with no min/max scale c = QgsMapUnitScale() size = r.convertToMapUnits(2, QgsUnitTypes.RenderMapUnits, c) self.assertEqual(size, 2.0) size = r.convertToMapUnits(2, QgsUnitTypes.RenderMillimeters, c) self.assertAlmostEqual(size, 47.244094, places=5) size = r.convertToMapUnits(5.66929, QgsUnitTypes.RenderPoints, c) self.assertAlmostEqual(size, 47.2440833, places=5) size = r.convertToMapUnits(5.66929, QgsUnitTypes.RenderInches, c) self.assertAlmostEqual(size, 3401.574, places=5) size = r.convertToMapUnits(2, QgsUnitTypes.RenderPixels, c) self.assertAlmostEqual(size, 4.0, places=5) # minimum size greater than the calculated size, so size should be limited to minSizeMM c.minSizeMM = 5 c.minSizeMMEnabled = True size = r.convertToMapUnits(2, QgsUnitTypes.RenderMapUnits, c) self.assertAlmostEqual(size, 118.1102362, places=5) # only conversion from mapunits should be affected size = r.convertToMapUnits(2, QgsUnitTypes.RenderMillimeters, c) self.assertAlmostEqual(size, 47.244094, places=5) size = r.convertToMapUnits(5.66929, QgsUnitTypes.RenderPoints, c) self.assertAlmostEqual(size, 47.2440833, places=5) size = r.convertToMapUnits(5.66929, QgsUnitTypes.RenderInches, c) self.assertAlmostEqual(size, 3401.574, places=5) size = r.convertToMapUnits(2, QgsUnitTypes.RenderPixels, c) self.assertAlmostEqual(size, 4.0, places=5) c.minSizeMMEnabled = False # maximum size less than the calculated size, so size should be limited to maxSizeMM c.maxSizeMM = 0.05 c.maxSizeMMEnabled = True size = r.convertToMapUnits(2, QgsUnitTypes.RenderMapUnits, c) self.assertAlmostEqual(size, 1.1811023622047245, places=5) # only conversion from mapunits should be affected size = r.convertToMapUnits(2, QgsUnitTypes.RenderMillimeters, c) self.assertAlmostEqual(size, 47.244094, places=5) size = r.convertToMapUnits(5.66929, QgsUnitTypes.RenderPoints, c) self.assertAlmostEqual(size, 47.2440833, places=5) size = r.convertToMapUnits(5.66929, QgsUnitTypes.RenderInches, c) self.assertAlmostEqual(size, 3401.574, places=5) size = r.convertToMapUnits(2, QgsUnitTypes.RenderPixels, c) self.assertAlmostEqual(size, 4.0, places=5) c.maxSizeMMEnabled = False # test with minimum scale set c.minScale = 150000000.0 size = r.convertToMapUnits(2, QgsUnitTypes.RenderMapUnits, c) self.assertAlmostEqual(size, 15.57001821, places=5) # only conversion from mapunits should be affected size = r.convertToMapUnits(2, QgsUnitTypes.RenderMillimeters, c) self.assertAlmostEqual(size, 47.244094, places=5) size = r.convertToMapUnits(5.66929, QgsUnitTypes.RenderPoints, c) self.assertAlmostEqual(size, 47.2440833, places=5) size = r.convertToMapUnits(5.66929, QgsUnitTypes.RenderInches, c) self.assertAlmostEqual(size, 3401.574, places=5) size = r.convertToMapUnits(2, QgsUnitTypes.RenderPixels, c) self.assertAlmostEqual(size, 4.0, places=5) c.minScale = 0 # test with maximum scale set c.maxScale = 1550000000.0 size = r.convertToMapUnits(2, QgsUnitTypes.RenderMapUnits, c) self.assertAlmostEqual(size, 1.50677595625, places=5) # only conversion from mapunits should be affected size = r.convertToMapUnits(2, QgsUnitTypes.RenderMillimeters, c) self.assertAlmostEqual(size, 47.244094, places=5) size = r.convertToMapUnits(5.66929, QgsUnitTypes.RenderPoints, c) self.assertAlmostEqual(size, 47.2440833, places=5) size = r.convertToMapUnits(5.66929, QgsUnitTypes.RenderInches, c) self.assertAlmostEqual(size, 3401.574, places=5) size = r.convertToMapUnits(2, QgsUnitTypes.RenderPixels, c) self.assertAlmostEqual(size, 4.0, places=5) c.maxScale = 0
def testEquality(self): # test equality operator c1 = QgsMapUnitScale(0.0001, 0.005) c1.minSizeMMEnabled = True c1.minSizeMM = 3 c1.maxSizeMMEnabled = True c1.maxSizeMM = 8 c2 = QgsMapUnitScale(0.0001, 0.005) c2.minSizeMMEnabled = True c2.minSizeMM = 3 c2.maxSizeMMEnabled = True c2.maxSizeMM = 8 self.assertEqual(c1, c2) c2.minScale = 0.0004 self.assertNotEqual(c1, c2) c2.minScale = 0.0001 c2.maxScale = 0.007 self.assertNotEqual(c1, c2) c2.maxScale = 0.005 c2.minSizeMMEnabled = False self.assertNotEqual(c1, c2) c2.minSizeMMEnabled = True c2.maxSizeMMEnabled = False self.assertNotEqual(c1, c2) c2.maxSizeMMEnabled = True c2.minSizeMM = 1 self.assertNotEqual(c1, c2) c2.minSizeMM = 3 c2.maxSizeMM = 100 self.assertNotEqual(c1, c2) c2.maxSizeMM = 8 self.assertEqual(c1, c2)
def testConvertToMapUnits(self): ms = QgsMapSettings() ms.setExtent(QgsRectangle(0, 0, 100, 100)) ms.setOutputSize(QSize(100, 50)) ms.setOutputDpi(300) r = QgsRenderContext.fromMapSettings(ms) # renderer scale should be about 1:291937841 # start with no min/max scale c = QgsMapUnitScale() size = r.convertToMapUnits(2, QgsUnitTypes.RenderMapUnits, c) self.assertEqual(size, 2.0) self.assertEqual( r.convertFromMapUnits(size, QgsUnitTypes.RenderMapUnits), 2) size = r.convertToMapUnits(2, QgsUnitTypes.RenderMillimeters, c) self.assertAlmostEqual(size, 47.244094, places=5) self.assertEqual( r.convertFromMapUnits(size, QgsUnitTypes.RenderMillimeters), 2) size = r.convertToMapUnits(5.66929, QgsUnitTypes.RenderPoints, c) self.assertAlmostEqual(size, 47.2440833, places=5) self.assertAlmostEqual( r.convertFromMapUnits(size, QgsUnitTypes.RenderPoints), 5.66929, 4) size = r.convertToMapUnits(5.66929, QgsUnitTypes.RenderInches, c) self.assertAlmostEqual(size, 3401.574, places=5) self.assertAlmostEqual( r.convertFromMapUnits(size, QgsUnitTypes.RenderInches), 5.66929, 4) size = r.convertToMapUnits(2, QgsUnitTypes.RenderPixels, c) self.assertAlmostEqual(size, 4.0, places=5) self.assertAlmostEqual( r.convertFromMapUnits(size, QgsUnitTypes.RenderPixels), 2, 4) # minimum size greater than the calculated size, so size should be limited to minSizeMM c.minSizeMM = 5 c.minSizeMMEnabled = True size = r.convertToMapUnits(2, QgsUnitTypes.RenderMapUnits, c) self.assertAlmostEqual(size, 118.1102362, places=5) # only conversion from mapunits should be affected size = r.convertToMapUnits(2, QgsUnitTypes.RenderMillimeters, c) self.assertAlmostEqual(size, 47.244094, places=5) size = r.convertToMapUnits(5.66929, QgsUnitTypes.RenderPoints, c) self.assertAlmostEqual(size, 47.2440833, places=5) size = r.convertToMapUnits(5.66929, QgsUnitTypes.RenderInches, c) self.assertAlmostEqual(size, 3401.574, places=5) size = r.convertToMapUnits(2, QgsUnitTypes.RenderPixels, c) self.assertAlmostEqual(size, 4.0, places=5) c.minSizeMMEnabled = False # maximum size less than the calculated size, so size should be limited to maxSizeMM c.maxSizeMM = 0.05 c.maxSizeMMEnabled = True size = r.convertToMapUnits(2, QgsUnitTypes.RenderMapUnits, c) self.assertAlmostEqual(size, 1.1811023622047245, places=5) # only conversion from mapunits should be affected size = r.convertToMapUnits(2, QgsUnitTypes.RenderMillimeters, c) self.assertAlmostEqual(size, 47.244094, places=5) size = r.convertToMapUnits(5.66929, QgsUnitTypes.RenderPoints, c) self.assertAlmostEqual(size, 47.2440833, places=5) size = r.convertToMapUnits(5.66929, QgsUnitTypes.RenderInches, c) self.assertAlmostEqual(size, 3401.574, places=5) size = r.convertToMapUnits(2, QgsUnitTypes.RenderPixels, c) self.assertAlmostEqual(size, 4.0, places=5) c.maxSizeMMEnabled = False # test with minimum scale set c.minScale = 150000000.0 size = r.convertToMapUnits(2, QgsUnitTypes.RenderMapUnits, c) self.assertAlmostEqual(size, 15.57001821, places=5) # only conversion from mapunits should be affected size = r.convertToMapUnits(2, QgsUnitTypes.RenderMillimeters, c) self.assertAlmostEqual(size, 47.244094, places=5) size = r.convertToMapUnits(5.66929, QgsUnitTypes.RenderPoints, c) self.assertAlmostEqual(size, 47.2440833, places=5) size = r.convertToMapUnits(5.66929, QgsUnitTypes.RenderInches, c) self.assertAlmostEqual(size, 3401.574, places=5) size = r.convertToMapUnits(2, QgsUnitTypes.RenderPixels, c) self.assertAlmostEqual(size, 4.0, places=5) c.minScale = 0 # test with maximum scale set c.maxScale = 1550000000.0 size = r.convertToMapUnits(2, QgsUnitTypes.RenderMapUnits, c) self.assertAlmostEqual(size, 1.50677595625, places=5) # only conversion from mapunits should be affected size = r.convertToMapUnits(2, QgsUnitTypes.RenderMillimeters, c) self.assertAlmostEqual(size, 47.244094, places=5) size = r.convertToMapUnits(5.66929, QgsUnitTypes.RenderPoints, c) self.assertAlmostEqual(size, 47.2440833, places=5) size = r.convertToMapUnits(5.66929, QgsUnitTypes.RenderInches, c) self.assertAlmostEqual(size, 3401.574, places=5) size = r.convertToMapUnits(2, QgsUnitTypes.RenderPixels, c) self.assertAlmostEqual(size, 4.0, places=5) c.maxScale = 0 # with symbology reference scale c = QgsMapUnitScale() r.setSymbologyReferenceScale(1000) r.setRendererScale(1000) size = r.convertToMapUnits(2, QgsUnitTypes.RenderMapUnits, c) self.assertEqual(size, 2.0) self.assertEqual( r.convertFromMapUnits(size, QgsUnitTypes.RenderMapUnits), 2) size = r.convertToMapUnits(2, QgsUnitTypes.RenderMillimeters, c) self.assertAlmostEqual(size, 47.244094, places=5) self.assertEqual( r.convertFromMapUnits(size, QgsUnitTypes.RenderMillimeters), 2) size = r.convertToMapUnits(5.66929, QgsUnitTypes.RenderPoints, c) self.assertAlmostEqual(size, 47.2440833, places=5) self.assertAlmostEqual( r.convertFromMapUnits(size, QgsUnitTypes.RenderPoints), 5.66929, 4) size = r.convertToMapUnits(5.66929, QgsUnitTypes.RenderInches, c) self.assertAlmostEqual(size, 3401.574, places=5) self.assertAlmostEqual( r.convertFromMapUnits(size, QgsUnitTypes.RenderInches), 5.66929, 4) size = r.convertToMapUnits(2, QgsUnitTypes.RenderPixels, c) self.assertAlmostEqual(size, 4.0, places=5) self.assertAlmostEqual( r.convertFromMapUnits(size, QgsUnitTypes.RenderPixels), 2, 4) r.setRendererScale(2000) size = r.convertToMapUnits(2, QgsUnitTypes.RenderMapUnits, c) self.assertEqual(size, 2.0) self.assertEqual( r.convertFromMapUnits(size, QgsUnitTypes.RenderMapUnits), 2) size = r.convertToMapUnits(2, QgsUnitTypes.RenderMillimeters, c) self.assertAlmostEqual(size, 47.244094 * 2, places=5) self.assertEqual( r.convertFromMapUnits(size, QgsUnitTypes.RenderMillimeters), 2) size = r.convertToMapUnits(5.66929, QgsUnitTypes.RenderPoints, c) self.assertAlmostEqual(size, 47.2440833 * 2, places=5) self.assertAlmostEqual( r.convertFromMapUnits(size, QgsUnitTypes.RenderPoints), 5.66929, 4) size = r.convertToMapUnits(5.66929, QgsUnitTypes.RenderInches, c) self.assertAlmostEqual(size, 3401.574 * 2, places=5) self.assertAlmostEqual( r.convertFromMapUnits(size, QgsUnitTypes.RenderInches), 5.66929, 4) size = r.convertToMapUnits(2, QgsUnitTypes.RenderPixels, c) self.assertAlmostEqual(size, 4.0 * 2, places=5) self.assertAlmostEqual( r.convertFromMapUnits(size, QgsUnitTypes.RenderPixels), 2, 4) r.setRendererScale(500) size = r.convertToMapUnits(2, QgsUnitTypes.RenderMapUnits, c) self.assertEqual(size, 2.0) self.assertEqual( r.convertFromMapUnits(size, QgsUnitTypes.RenderMapUnits), 2) size = r.convertToMapUnits(2, QgsUnitTypes.RenderMillimeters, c) self.assertAlmostEqual(size, 47.244094 / 2, places=5) self.assertEqual( r.convertFromMapUnits(size, QgsUnitTypes.RenderMillimeters), 2) size = r.convertToMapUnits(5.66929, QgsUnitTypes.RenderPoints, c) self.assertAlmostEqual(size, 47.2440833 / 2, places=5) self.assertAlmostEqual( r.convertFromMapUnits(size, QgsUnitTypes.RenderPoints), 5.66929, 4) size = r.convertToMapUnits(5.66929, QgsUnitTypes.RenderInches, c) self.assertAlmostEqual(size, 3401.574 / 2, places=5) self.assertAlmostEqual( r.convertFromMapUnits(size, QgsUnitTypes.RenderInches), 5.66929, 4) size = r.convertToMapUnits(2, QgsUnitTypes.RenderPixels, c) self.assertAlmostEqual(size, 4.0 / 2, places=5) self.assertAlmostEqual( r.convertFromMapUnits(size, QgsUnitTypes.RenderPixels), 2, 4)
def testEquality(self): # test equality operator c1 = QgsMapUnitScale(10000, 200) c1.minSizeMMEnabled = True c1.minSizeMM = 3 c1.maxSizeMMEnabled = True c1.maxSizeMM = 8 c2 = QgsMapUnitScale(10000, 200) c2.minSizeMMEnabled = True c2.minSizeMM = 3 c2.maxSizeMMEnabled = True c2.maxSizeMM = 8 self.assertEqual(c1, c2) c2.minScale = 2500.0 self.assertNotEqual(c1, c2) c2.minScale = 10000 c2.maxScale = 142.857 self.assertNotEqual(c1, c2) c2.maxScale = 200 c2.minSizeMMEnabled = False self.assertNotEqual(c1, c2) c2.minSizeMMEnabled = True c2.maxSizeMMEnabled = False self.assertNotEqual(c1, c2) c2.maxSizeMMEnabled = True c2.minSizeMM = 1 self.assertNotEqual(c1, c2) c2.minSizeMM = 3 c2.maxSizeMM = 100 self.assertNotEqual(c1, c2) c2.maxSizeMM = 8 self.assertEqual(c1, c2)
def testBasic(self): renderer = QgsPointCloudRgbRenderer() renderer.setBlueAttribute('b') self.assertEqual(renderer.blueAttribute(), 'b') renderer.setGreenAttribute('g') self.assertEqual(renderer.greenAttribute(), 'g') renderer.setRedAttribute('r') self.assertEqual(renderer.redAttribute(), 'r') redce = QgsContrastEnhancement() redce.setMinimumValue(100) redce.setMaximumValue(120) redce.setContrastEnhancementAlgorithm( QgsContrastEnhancement.StretchAndClipToMinimumMaximum) renderer.setRedContrastEnhancement(redce) greence = QgsContrastEnhancement() greence.setMinimumValue(130) greence.setMaximumValue(150) greence.setContrastEnhancementAlgorithm( QgsContrastEnhancement.StretchToMinimumMaximum) renderer.setGreenContrastEnhancement(greence) bluece = QgsContrastEnhancement() bluece.setMinimumValue(170) bluece.setMaximumValue(190) bluece.setContrastEnhancementAlgorithm( QgsContrastEnhancement.ClipToMinimumMaximum) renderer.setBlueContrastEnhancement(bluece) renderer.setMaximumScreenError(18) renderer.setMaximumScreenErrorUnit(QgsUnitTypes.RenderInches) renderer.setPointSize(13) renderer.setPointSizeUnit(QgsUnitTypes.RenderPoints) renderer.setPointSizeMapUnitScale(QgsMapUnitScale(1000, 2000)) rr = renderer.clone() self.assertEqual(rr.maximumScreenError(), 18) self.assertEqual(rr.maximumScreenErrorUnit(), QgsUnitTypes.RenderInches) self.assertEqual(rr.pointSize(), 13) self.assertEqual(rr.pointSizeUnit(), QgsUnitTypes.RenderPoints) self.assertEqual(rr.pointSizeMapUnitScale().minScale, 1000) self.assertEqual(rr.pointSizeMapUnitScale().maxScale, 2000) self.assertEqual(rr.blueAttribute(), 'b') self.assertEqual(rr.greenAttribute(), 'g') self.assertEqual(rr.redAttribute(), 'r') self.assertEqual(rr.redContrastEnhancement().minimumValue(), 100) self.assertEqual(rr.redContrastEnhancement().maximumValue(), 120) self.assertEqual( rr.redContrastEnhancement().contrastEnhancementAlgorithm(), QgsContrastEnhancement.StretchAndClipToMinimumMaximum) self.assertEqual(rr.greenContrastEnhancement().minimumValue(), 130) self.assertEqual(rr.greenContrastEnhancement().maximumValue(), 150) self.assertEqual( rr.greenContrastEnhancement().contrastEnhancementAlgorithm(), QgsContrastEnhancement.StretchToMinimumMaximum) self.assertEqual(rr.blueContrastEnhancement().minimumValue(), 170) self.assertEqual(rr.blueContrastEnhancement().maximumValue(), 190) self.assertEqual( rr.blueContrastEnhancement().contrastEnhancementAlgorithm(), QgsContrastEnhancement.ClipToMinimumMaximum) doc = QDomDocument("testdoc") elem = renderer.save(doc, QgsReadWriteContext()) r2 = QgsPointCloudRgbRenderer.create(elem, QgsReadWriteContext()) self.assertEqual(r2.maximumScreenError(), 18) self.assertEqual(r2.maximumScreenErrorUnit(), QgsUnitTypes.RenderInches) self.assertEqual(r2.pointSize(), 13) self.assertEqual(r2.pointSizeUnit(), QgsUnitTypes.RenderPoints) self.assertEqual(r2.pointSizeMapUnitScale().minScale, 1000) self.assertEqual(r2.pointSizeMapUnitScale().maxScale, 2000) self.assertEqual(r2.blueAttribute(), 'b') self.assertEqual(r2.greenAttribute(), 'g') self.assertEqual(r2.redAttribute(), 'r') self.assertEqual(r2.redContrastEnhancement().minimumValue(), 100) self.assertEqual(r2.redContrastEnhancement().maximumValue(), 120) self.assertEqual( r2.redContrastEnhancement().contrastEnhancementAlgorithm(), QgsContrastEnhancement.StretchAndClipToMinimumMaximum) self.assertEqual(r2.greenContrastEnhancement().minimumValue(), 130) self.assertEqual(r2.greenContrastEnhancement().maximumValue(), 150) self.assertEqual( r2.greenContrastEnhancement().contrastEnhancementAlgorithm(), QgsContrastEnhancement.StretchToMinimumMaximum) self.assertEqual(r2.blueContrastEnhancement().minimumValue(), 170) self.assertEqual(r2.blueContrastEnhancement().maximumValue(), 190) self.assertEqual( r2.blueContrastEnhancement().contrastEnhancementAlgorithm(), QgsContrastEnhancement.ClipToMinimumMaximum)
def testCondenseFillAndOutline(self): """ Test QgsSymbolLayerUtils.condenseFillAndOutline """ self.assertFalse(QgsSymbolLayerUtils.condenseFillAndOutline(None, None)) # not simple fill or line self.assertFalse(QgsSymbolLayerUtils.condenseFillAndOutline(QgsShapeburstFillSymbolLayer(), QgsSimpleLineSymbolLayer())) self.assertFalse(QgsSymbolLayerUtils.condenseFillAndOutline(QgsSimpleFillSymbolLayer(), QgsMarkerLineSymbolLayer())) # simple fill/line fill = QgsSimpleFillSymbolLayer() line = QgsSimpleLineSymbolLayer() # set incompatible settings on outline line.setUseCustomDashPattern(True) self.assertFalse(QgsSymbolLayerUtils.condenseFillAndOutline(fill, line)) line = QgsSimpleLineSymbolLayer() line.setDashPatternOffset(1) self.assertFalse(QgsSymbolLayerUtils.condenseFillAndOutline(fill, line)) line = QgsSimpleLineSymbolLayer() line.setAlignDashPattern(True) self.assertFalse(QgsSymbolLayerUtils.condenseFillAndOutline(fill, line)) line = QgsSimpleLineSymbolLayer() line.setTweakDashPatternOnCorners(True) self.assertFalse(QgsSymbolLayerUtils.condenseFillAndOutline(fill, line)) line = QgsSimpleLineSymbolLayer() line.setTrimDistanceStart(1) self.assertFalse(QgsSymbolLayerUtils.condenseFillAndOutline(fill, line)) line = QgsSimpleLineSymbolLayer() line.setTrimDistanceEnd(1) self.assertFalse(QgsSymbolLayerUtils.condenseFillAndOutline(fill, line)) line = QgsSimpleLineSymbolLayer() line.setDrawInsidePolygon(True) self.assertFalse(QgsSymbolLayerUtils.condenseFillAndOutline(fill, line)) line = QgsSimpleLineSymbolLayer() line.setRingFilter(QgsSimpleLineSymbolLayer.ExteriorRingOnly) self.assertFalse(QgsSymbolLayerUtils.condenseFillAndOutline(fill, line)) line = QgsSimpleLineSymbolLayer() line.setOffset(1) self.assertFalse(QgsSymbolLayerUtils.condenseFillAndOutline(fill, line)) line = QgsSimpleLineSymbolLayer() line.setDataDefinedProperty(QgsSymbolLayer.PropertyTrimEnd, QgsProperty.fromValue(4)) self.assertFalse(QgsSymbolLayerUtils.condenseFillAndOutline(fill, line)) # compatible! line = QgsSimpleLineSymbolLayer() line.setColor(QColor(255, 0, 0)) line.setWidth(1.2) line.setWidthUnit(QgsUnitTypes.RenderPoints) line.setWidthMapUnitScale(QgsMapUnitScale(1, 2)) line.setPenJoinStyle(Qt.MiterJoin) line.setPenStyle(Qt.DashDotDotLine) self.assertTrue(QgsSymbolLayerUtils.condenseFillAndOutline(fill, line)) self.assertEqual(fill.strokeColor(), QColor(255, 0, 0)) self.assertEqual(fill.strokeWidth(), 1.2) self.assertEqual(fill.strokeWidthUnit(), QgsUnitTypes.RenderPoints) self.assertEqual(fill.strokeWidthMapUnitScale(), QgsMapUnitScale(1, 2)) self.assertEqual(fill.penJoinStyle(), Qt.MiterJoin) self.assertEqual(fill.strokeStyle(), Qt.DashDotDotLine)
def test_line_settings(self): """ Test line settings """ settings = QgsLabelLineSettings() settings.setPlacementFlags(QgsLabeling.LinePlacementFlag.OnLine) self.assertEqual(settings.placementFlags(), QgsLabeling.LinePlacementFlag.OnLine) settings.setPlacementFlags( QgsLabeling.LinePlacementFlag.OnLine | QgsLabeling.LinePlacementFlag.MapOrientation) self.assertEqual( settings.placementFlags(), QgsLabeling.LinePlacementFlag.OnLine | QgsLabeling.LinePlacementFlag.MapOrientation) settings.setMergeLines(True) self.assertTrue(settings.mergeLines()) settings.setMergeLines(False) self.assertFalse(settings.mergeLines()) settings.setAddDirectionSymbol(True) self.assertTrue(settings.addDirectionSymbol()) settings.setAddDirectionSymbol(False) self.assertFalse(settings.addDirectionSymbol()) settings.setLeftDirectionSymbol('left') self.assertEqual(settings.leftDirectionSymbol(), 'left') settings.setRightDirectionSymbol('right') self.assertEqual(settings.rightDirectionSymbol(), 'right') settings.setReverseDirectionSymbol(True) self.assertTrue(settings.reverseDirectionSymbol()) settings.setReverseDirectionSymbol(False) self.assertFalse(settings.reverseDirectionSymbol()) settings.setDirectionSymbolPlacement( QgsLabelLineSettings.DirectionSymbolPlacement.SymbolBelow) self.assertEqual( settings.directionSymbolPlacement(), QgsLabelLineSettings.DirectionSymbolPlacement.SymbolBelow) settings.setDirectionSymbolPlacement( QgsLabelLineSettings.DirectionSymbolPlacement.SymbolAbove) self.assertEqual( settings.directionSymbolPlacement(), QgsLabelLineSettings.DirectionSymbolPlacement.SymbolAbove) settings.setOverrunDistance(5.6) self.assertEqual(settings.overrunDistance(), 5.6) settings.setOverrunDistanceUnit(QgsUnitTypes.RenderInches) self.assertEqual(settings.overrunDistanceUnit(), QgsUnitTypes.RenderInches) scale = QgsMapUnitScale(1, 2) settings.setOverrunDistanceMapUnitScale(scale) self.assertEqual(settings.overrunDistanceMapUnitScale().minScale, 1) self.assertEqual(settings.overrunDistanceMapUnitScale().maxScale, 2) settings.setLineAnchorPercent(0.3) self.assertEqual(settings.lineAnchorPercent(), 0.3) # check that compatibility code works pal_settings = QgsPalLayerSettings() pal_settings.placementFlags = QgsPalLayerSettings.OnLine | QgsPalLayerSettings.MapOrientation self.assertEqual(pal_settings.placementFlags, 9) self.assertTrue( pal_settings.lineSettings().placementFlags(), QgsLabeling.LinePlacementFlag.OnLine | QgsLabeling.LinePlacementFlag.MapOrientation) pal_settings.mergeLines = True self.assertTrue(pal_settings.mergeLines) self.assertTrue(pal_settings.lineSettings().mergeLines()) pal_settings.mergeLines = False self.assertFalse(pal_settings.mergeLines) self.assertFalse(pal_settings.lineSettings().mergeLines()) pal_settings.addDirectionSymbol = True self.assertTrue(pal_settings.addDirectionSymbol) self.assertTrue(pal_settings.lineSettings().addDirectionSymbol()) pal_settings.addDirectionSymbol = False self.assertFalse(pal_settings.addDirectionSymbol) self.assertFalse(pal_settings.lineSettings().addDirectionSymbol()) pal_settings.leftDirectionSymbol = 'l' self.assertEqual(pal_settings.leftDirectionSymbol, 'l') self.assertEqual(pal_settings.lineSettings().leftDirectionSymbol(), 'l') pal_settings.rightDirectionSymbol = 'r' self.assertEqual(pal_settings.rightDirectionSymbol, 'r') self.assertEqual(pal_settings.lineSettings().rightDirectionSymbol(), 'r') pal_settings.reverseDirectionSymbol = True self.assertTrue(pal_settings.reverseDirectionSymbol) self.assertTrue(pal_settings.lineSettings().reverseDirectionSymbol()) pal_settings.reverseDirectionSymbol = False self.assertFalse(pal_settings.reverseDirectionSymbol) self.assertFalse(pal_settings.lineSettings().reverseDirectionSymbol()) pal_settings.placeDirectionSymbol = QgsPalLayerSettings.SymbolAbove self.assertEqual(pal_settings.placeDirectionSymbol, 1) self.assertTrue( pal_settings.lineSettings().directionSymbolPlacement(), QgsLabelLineSettings.DirectionSymbolPlacement.SymbolAbove) pal_settings.overrunDistance = 4.2 self.assertEqual(pal_settings.overrunDistance, 4.2) self.assertTrue(pal_settings.lineSettings().overrunDistance(), 4.2) pal_settings.overrunDistanceUnit = QgsUnitTypes.RenderInches self.assertEqual(pal_settings.overrunDistanceUnit, QgsUnitTypes.RenderInches) self.assertTrue(pal_settings.lineSettings().overrunDistanceUnit(), QgsUnitTypes.RenderInches) pal_settings.overrunDistanceMapUnitScale = scale self.assertEqual(pal_settings.overrunDistanceMapUnitScale.minScale, 1) self.assertEqual(pal_settings.overrunDistanceMapUnitScale.maxScale, 2) self.assertTrue( pal_settings.lineSettings().overrunDistanceMapUnitScale().minScale, 1) self.assertTrue( pal_settings.lineSettings().overrunDistanceMapUnitScale().maxScale, 2)
def testConvertToPainterUnits(self): # test QgsSymbolLayerUtils::convertToPainterUnits() using QgsMapUnitScale ms = QgsMapSettings() ms.setExtent(QgsRectangle(0, 0, 100, 100)) ms.setOutputSize(QSize(100, 50)) ms.setOutputDpi(300) r = QgsRenderContext.fromMapSettings(ms) # renderer scale should be about 1:291937841 # start with no min/max scale c = QgsMapUnitScale() size = QgsSymbolLayerUtils.convertToPainterUnits( r, 2, QgsUnitTypes.RenderMapUnits, c) self.assertAlmostEqual(size, 1.0, places=5) size = QgsSymbolLayerUtils.convertToPainterUnits( r, 2, QgsUnitTypes.RenderMillimeters, c) self.assertAlmostEqual(size, 23.622047, places=5) size = QgsSymbolLayerUtils.convertToPainterUnits( r, 2, QgsUnitTypes.RenderPoints, c) self.assertAlmostEqual(size, 66.9601332, places=5) size = QgsSymbolLayerUtils.convertToPainterUnits( r, 2, QgsUnitTypes.RenderPixels, c) self.assertAlmostEqual(size, 2.0, places=5) # minimum size greater than the calculated size, so size should be limited to minSizeMM c.minSizeMM = 5 c.minSizeMMEnabled = True size = QgsSymbolLayerUtils.convertToPainterUnits( r, 2, QgsUnitTypes.RenderMapUnits, c) self.assertAlmostEqual(size, 59.0551181, places=5) # only conversion from mapunits should be affected size = QgsSymbolLayerUtils.convertToPainterUnits( r, 2, QgsUnitTypes.RenderMillimeters, c) self.assertAlmostEqual(size, 23.622047, places=5) size = QgsSymbolLayerUtils.convertToPainterUnits( r, 2, QgsUnitTypes.RenderPoints, c) self.assertAlmostEqual(size, 66.9601332, places=5) size = QgsSymbolLayerUtils.convertToPainterUnits( r, 2, QgsUnitTypes.RenderPixels, c) self.assertAlmostEqual(size, 2.0, places=5) c.minSizeMMEnabled = False # maximum size less than the calculated size, so size should be limited to maxSizeMM c.maxSizeMM = 0.1 c.maxSizeMMEnabled = True size = QgsSymbolLayerUtils.convertToPainterUnits( r, 2, QgsUnitTypes.RenderMapUnits, c) self.assertAlmostEqual(size, 1.0, places=5) # only conversion from mapunits should be affected size = QgsSymbolLayerUtils.convertToPainterUnits( r, 2, QgsUnitTypes.RenderMillimeters, c) self.assertAlmostEqual(size, 23.622047, places=5) size = QgsSymbolLayerUtils.convertToPainterUnits( r, 2, QgsUnitTypes.RenderPoints, c) self.assertAlmostEqual(size, 66.9601332, places=5) size = QgsSymbolLayerUtils.convertToPainterUnits( r, 2, QgsUnitTypes.RenderPixels, c) self.assertAlmostEqual(size, 2.0, places=5)
def testBasic(self): renderer = QgsPointCloudAttributeByRampRenderer() renderer.setAttribute('attr') self.assertEqual(renderer.attribute(), 'attr') renderer.setMinimum(5) self.assertEqual(renderer.minimum(), 5) renderer.setMaximum(15) self.assertEqual(renderer.maximum(), 15) ramp = QgsStyle.defaultStyle().colorRamp("Viridis") shader = QgsColorRampShader(20, 30, ramp) renderer.setColorRampShader(shader) self.assertEqual(renderer.colorRampShader().minimumValue(), 20) self.assertEqual(renderer.colorRampShader().maximumValue(), 30) renderer.setMaximumScreenError(18) renderer.setMaximumScreenErrorUnit(QgsUnitTypes.RenderInches) renderer.setPointSize(13) renderer.setPointSizeUnit(QgsUnitTypes.RenderPoints) renderer.setPointSizeMapUnitScale(QgsMapUnitScale(1000, 2000)) rr = renderer.clone() self.assertEqual(rr.maximumScreenError(), 18) self.assertEqual(rr.maximumScreenErrorUnit(), QgsUnitTypes.RenderInches) self.assertEqual(rr.pointSize(), 13) self.assertEqual(rr.pointSizeUnit(), QgsUnitTypes.RenderPoints) self.assertEqual(rr.pointSizeMapUnitScale().minScale, 1000) self.assertEqual(rr.pointSizeMapUnitScale().maxScale, 2000) self.assertEqual(rr.attribute(), 'attr') self.assertEqual(rr.minimum(), 5) self.assertEqual(rr.maximum(), 15) self.assertEqual(rr.colorRampShader().minimumValue(), 20) self.assertEqual(rr.colorRampShader().maximumValue(), 30) self.assertEqual( rr.colorRampShader().sourceColorRamp().color1().name(), renderer.colorRampShader().sourceColorRamp().color1().name()) self.assertEqual( rr.colorRampShader().sourceColorRamp().color2().name(), renderer.colorRampShader().sourceColorRamp().color2().name()) doc = QDomDocument("testdoc") elem = renderer.save(doc, QgsReadWriteContext()) r2 = QgsPointCloudAttributeByRampRenderer.create( elem, QgsReadWriteContext()) self.assertEqual(r2.maximumScreenError(), 18) self.assertEqual(r2.maximumScreenErrorUnit(), QgsUnitTypes.RenderInches) self.assertEqual(r2.pointSize(), 13) self.assertEqual(r2.pointSizeUnit(), QgsUnitTypes.RenderPoints) self.assertEqual(r2.pointSizeMapUnitScale().minScale, 1000) self.assertEqual(r2.pointSizeMapUnitScale().maxScale, 2000) self.assertEqual(r2.attribute(), 'attr') self.assertEqual(r2.minimum(), 5) self.assertEqual(r2.maximum(), 15) self.assertEqual(r2.colorRampShader().minimumValue(), 20) self.assertEqual(r2.colorRampShader().maximumValue(), 30) self.assertEqual( r2.colorRampShader().sourceColorRamp().color1().name(), renderer.colorRampShader().sourceColorRamp().color1().name()) self.assertEqual( r2.colorRampShader().sourceColorRamp().color2().name(), renderer.colorRampShader().sourceColorRamp().color2().name())
def testMapUnitsPerPixel(self): # test computeMapUnitsPerPixel ms = QgsMapSettings() ms.setExtent(QgsRectangle(0, 0, 100, 100)) ms.setOutputSize(QSize(100, 50)) ms.setOutputDpi(75) r = QgsRenderContext.fromMapSettings(ms) # renderer scale should be about 1:291937841 # start with no min/max scale c = QgsMapUnitScale() mup = c.computeMapUnitsPerPixel(r) self.assertAlmostEqual(mup, 2.0, places=5) # add a minimum scale less than the renderer scale, so should be no change c.minScale = 1 / 350000000.0 mup = c.computeMapUnitsPerPixel(r) self.assertAlmostEqual(mup, 2.0, places=5) # minimum scale greater than the renderer scale, so should be limited to minScale c.minScale = 1 / 150000000.0 mup = c.computeMapUnitsPerPixel(r) self.assertAlmostEqual(mup, 1.0276160, places=5) c.minScale = 1 / 50000000.0 mup = c.computeMapUnitsPerPixel(r) self.assertAlmostEqual(mup, 0.3425386, places=5) c.minScale = 1 / 350000000.0 # add a maximum scale greater than the renderer scale, so should be no change c.maxScale = 1 / 150000000.0 mup = c.computeMapUnitsPerPixel(r) self.assertAlmostEqual(mup, 2.0, places=5) # maximum scale less than the renderer scale, so should be limited to maxScale c.maxScale = 1 / 350000000.0 mup = c.computeMapUnitsPerPixel(r) self.assertAlmostEqual(mup, 2.3977706, places=5) c.maxScale = 1 / 500000000.0 mup = c.computeMapUnitsPerPixel(r) self.assertAlmostEqual(mup, 3.4253867, places=5) # test resetting to min/max c.minScale = 0 c.maxScale = 0 mup = c.computeMapUnitsPerPixel(r) self.assertAlmostEqual(mup, 2.0, places=5)
def testSizeMapUnitScale(self): # test sizeMapUnitScale and setSizeMapUnitScale # create a marker symbol with a single layer markerSymbol = QgsMarkerSymbol() markerSymbol.deleteSymbolLayer(0) markerSymbol.appendSymbolLayer( QgsSimpleMarkerSymbolLayer(QgsSimpleMarkerSymbolLayerBase.Star, color=QColor(255, 0, 0), strokeColor=QColor(0, 255, 0), size=10)) markerSymbol.symbolLayer(0).setSizeMapUnitScale( QgsMapUnitScale(10000, 20000)) self.assertEqual(markerSymbol.sizeMapUnitScale(), QgsMapUnitScale(10000, 20000)) markerSymbol.setSizeMapUnitScale(QgsMapUnitScale(1000, 2000)) self.assertEqual(markerSymbol.sizeMapUnitScale(), QgsMapUnitScale(1000, 2000)) self.assertEqual( markerSymbol.symbolLayer(0).sizeMapUnitScale(), QgsMapUnitScale(1000, 2000)) # add additional layers markerSymbol.appendSymbolLayer( QgsSimpleMarkerSymbolLayer(QgsSimpleMarkerSymbolLayerBase.Star, color=QColor(255, 0, 0), strokeColor=QColor(0, 255, 0), size=10)) markerSymbol.appendSymbolLayer( QgsSimpleMarkerSymbolLayer(QgsSimpleMarkerSymbolLayerBase.Star, color=QColor(255, 0, 0), strokeColor=QColor(0, 255, 0), size=30)) # should take first layer's map unit scale self.assertEqual(markerSymbol.sizeMapUnitScale(), QgsMapUnitScale(1000, 2000)) markerSymbol.setSizeMapUnitScale(QgsMapUnitScale(3000, 4000)) self.assertEqual(markerSymbol.sizeMapUnitScale(), QgsMapUnitScale(3000, 4000)) # all layers should have size unit set self.assertEqual( markerSymbol.symbolLayer(0).sizeMapUnitScale(), QgsMapUnitScale(3000, 4000)) self.assertEqual( markerSymbol.symbolLayer(1).sizeMapUnitScale(), QgsMapUnitScale(3000, 4000)) self.assertEqual( markerSymbol.symbolLayer(2).sizeMapUnitScale(), QgsMapUnitScale(3000, 4000))
def testConvertToMapUnits(self): # test QgsSymbolLayerUtils::convertToMapUnits() using QgsMapUnitScale ms = QgsMapSettings() ms.setExtent(QgsRectangle(0, 0, 100, 100)) ms.setOutputSize(QSize(100, 50)) ms.setOutputDpi(300) r = QgsRenderContext.fromMapSettings(ms) # renderer scale should be about 1:291937841 # start with no min/max scale c = QgsMapUnitScale() size = QgsSymbolLayerUtils.convertToMapUnits(r, 2, QgsUnitTypes.RenderMapUnits, c) self.assertEqual(size, 2.0) size = QgsSymbolLayerUtils.convertToMapUnits(r, 2, QgsUnitTypes.RenderMillimeters, c) self.assertAlmostEqual(size, 47.244094, places=5) size = QgsSymbolLayerUtils.convertToMapUnits(r, 5.66929, QgsUnitTypes.RenderPoints, c) self.assertAlmostEqual(size, 47.2440833, places=5) size = QgsSymbolLayerUtils.convertToMapUnits(r, 2, QgsUnitTypes.RenderPixels, c) self.assertAlmostEqual(size, 4.0, places=5) # minimum size greater than the calculated size, so size should be limited to minSizeMM c.minSizeMM = 5 c.minSizeMMEnabled = True size = QgsSymbolLayerUtils.convertToMapUnits(r, 2, QgsUnitTypes.RenderMapUnits, c) self.assertAlmostEqual(size, 118.1102362, places=5) # only conversion from mapunits should be affected size = QgsSymbolLayerUtils.convertToMapUnits(r, 2, QgsUnitTypes.RenderMillimeters, c) self.assertAlmostEqual(size, 47.244094, places=5) size = QgsSymbolLayerUtils.convertToMapUnits(r, 5.66929, QgsUnitTypes.RenderPoints, c) self.assertAlmostEqual(size, 47.2440833, places=5) size = QgsSymbolLayerUtils.convertToMapUnits(r, 2, QgsUnitTypes.RenderPixels, c) self.assertAlmostEqual(size, 4.0, places=5) c.minSizeMMEnabled = False # maximum size less than the calculated size, so size should be limited to maxSizeMM c.maxSizeMM = 0.05 c.maxSizeMMEnabled = True size = QgsSymbolLayerUtils.convertToMapUnits(r, 2, QgsUnitTypes.RenderMapUnits, c) self.assertAlmostEqual(size, 1.1811023622047245, places=5) # only conversion from mapunits should be affected size = QgsSymbolLayerUtils.convertToMapUnits(r, 2, QgsUnitTypes.RenderMillimeters, c) self.assertAlmostEqual(size, 47.244094, places=5) size = QgsSymbolLayerUtils.convertToMapUnits(r, 5.66929, QgsUnitTypes.RenderPoints, c) self.assertAlmostEqual(size, 47.2440833, places=5) size = QgsSymbolLayerUtils.convertToMapUnits(r, 2, QgsUnitTypes.RenderPixels, c) self.assertAlmostEqual(size, 4.0, places=5) c.maxSizeMMEnabled = False # test with minimum scale set c.minScale = 1 / 150000000.0 size = QgsSymbolLayerUtils.convertToMapUnits(r, 2, QgsUnitTypes.RenderMapUnits, c) self.assertAlmostEqual(size, 15.57001821, places=5) # only conversion from mapunits should be affected size = QgsSymbolLayerUtils.convertToMapUnits(r, 2, QgsUnitTypes.RenderMillimeters, c) self.assertAlmostEqual(size, 47.244094, places=5) size = QgsSymbolLayerUtils.convertToMapUnits(r, 5.66929, QgsUnitTypes.RenderPoints, c) self.assertAlmostEqual(size, 47.2440833, places=5) size = QgsSymbolLayerUtils.convertToMapUnits(r, 2, QgsUnitTypes.RenderPixels, c) self.assertAlmostEqual(size, 4.0, places=5) c.minScale = 0 # test with maximum scale set c.maxScale = 1 / 1550000000.0 size = QgsSymbolLayerUtils.convertToMapUnits(r, 2, QgsUnitTypes.RenderMapUnits, c) self.assertAlmostEqual(size, 1.50677595625, places=5) # only conversion from mapunits should be affected size = QgsSymbolLayerUtils.convertToMapUnits(r, 2, QgsUnitTypes.RenderMillimeters, c) self.assertAlmostEqual(size, 47.244094, places=5) size = QgsSymbolLayerUtils.convertToMapUnits(r, 5.66929, QgsUnitTypes.RenderPoints, c) self.assertAlmostEqual(size, 47.2440833, places=5) size = QgsSymbolLayerUtils.convertToMapUnits(r, 2, QgsUnitTypes.RenderPixels, c) self.assertAlmostEqual(size, 4.0, places=5) c.maxScale = 0
def testMapUnitsPerPixel(self): # test computeMapUnitsPerPixel ms = QgsMapSettings() ms.setExtent(QgsRectangle(0, 0, 100, 100)) ms.setOutputSize(QSize(100, 50)) r = QgsRenderContext.fromMapSettings(ms) # renderer scale should be about 1:291937841 # start with no min/max scale c = QgsMapUnitScale() mup = c.computeMapUnitsPerPixel(r) self.assertAlmostEqual(mup, 2.0, places=5) # add a minimum scale less than the renderer scale, so should be no change c.minScale = 1 / 350000000.0 mup = c.computeMapUnitsPerPixel(r) self.assertAlmostEqual(mup, 2.0, places=5) # minimum scale greater than the renderer scale, so should be limited to minScale c.minScale = 1 / 150000000.0 mup = c.computeMapUnitsPerPixel(r) self.assertAlmostEqual(mup, 1.0276160, places=5) c.minScale = 1 / 50000000.0 mup = c.computeMapUnitsPerPixel(r) self.assertAlmostEqual(mup, 0.3425386, places=5) c.minScale = 1 / 350000000.0 # add a maximum scale greater than the renderer scale, so should be no change c.maxScale = 1 / 150000000.0 mup = c.computeMapUnitsPerPixel(r) self.assertAlmostEqual(mup, 2.0, places=5) # maximum scale less than the renderer scale, so should be limited to maxScale c.maxScale = 1 / 350000000.0 mup = c.computeMapUnitsPerPixel(r) self.assertAlmostEqual(mup, 2.3977706, places=5) c.maxScale = 1 / 500000000.0 mup = c.computeMapUnitsPerPixel(r) self.assertAlmostEqual(mup, 3.4253867, places=5) # test resetting to min/max c.minScale = 0 c.maxScale = 0 mup = c.computeMapUnitsPerPixel(r) self.assertAlmostEqual(mup, 2.0, places=5)
def testConvertSingleUnit(self): ms = QgsMapSettings() ms.setExtent(QgsRectangle(0, 0, 100, 100)) ms.setOutputSize(QSize(100, 50)) ms.setOutputDpi(300) r = QgsRenderContext.fromMapSettings(ms) # renderer scale should be about 1:291937841 # start with no min/max scale c = QgsMapUnitScale() # self.assertEqual(r.scaleFactor(),666) sf = r.convertToPainterUnits(1, QgsUnitTypes.RenderMapUnits, c) self.assertAlmostEqual(sf, 0.5, places=5) sf = r.convertToPainterUnits(1, QgsUnitTypes.RenderMillimeters, c) self.assertAlmostEqual(sf, 11.8110236, places=5) sf = r.convertToPainterUnits(1, QgsUnitTypes.RenderPoints, c) self.assertAlmostEqual(sf, 4.166666665625, places=5) sf = r.convertToPainterUnits(1, QgsUnitTypes.RenderInches, c) self.assertAlmostEqual(sf, 300.0, places=5) sf = r.convertToPainterUnits(1, QgsUnitTypes.RenderPixels, c) self.assertAlmostEqual(sf, 1.0, places=5) # minimum scale greater than the renderer scale, so should be limited to minScale c.minScale = 150000000.0 sf = r.convertToPainterUnits(1, QgsUnitTypes.RenderMapUnits, c) self.assertAlmostEqual(sf, 3.89250455, places=5) # only conversion from mapunits should be affected sf = r.convertToPainterUnits(1, QgsUnitTypes.RenderMillimeters, c) self.assertAlmostEqual(sf, 11.8110236, places=5) sf = r.convertToPainterUnits(1, QgsUnitTypes.RenderPoints, c) self.assertAlmostEqual(sf, 4.166666665625, places=5) sf = r.convertToPainterUnits(1, QgsUnitTypes.RenderInches, c) self.assertAlmostEqual(sf, 300.0, places=5) sf = r.convertToPainterUnits(1, QgsUnitTypes.RenderPixels, c) self.assertAlmostEqual(sf, 1.0, places=5) c.minScale = 0 # maximum scale less than the renderer scale, so should be limited to maxScale c.maxScale = 350000000.0 sf = r.convertToPainterUnits(1, QgsUnitTypes.RenderMapUnits, c) self.assertAlmostEqual(sf, 0.5, places=5) # only conversion from mapunits should be affected sf = r.convertToPainterUnits(1, QgsUnitTypes.RenderMillimeters, c) self.assertAlmostEqual(sf, 11.8110236, places=5) sf = r.convertToPainterUnits(1, QgsUnitTypes.RenderPoints, c) self.assertAlmostEqual(sf, 4.166666665625, places=5) sf = r.convertToPainterUnits(1, QgsUnitTypes.RenderInches, c) self.assertAlmostEqual(sf, 300.0, places=5) sf = r.convertToPainterUnits(1, QgsUnitTypes.RenderPixels, c) self.assertAlmostEqual(sf, 1.0, places=5) # with symbologyReferenceScale set c = QgsMapUnitScale() r.setSymbologyReferenceScale(1000) r.setRendererScale(1000) sf = r.convertToPainterUnits(1, QgsUnitTypes.RenderMapUnits, c) self.assertAlmostEqual(sf, 0.5, places=5) sf = r.convertToPainterUnits(1, QgsUnitTypes.RenderMillimeters, c) self.assertAlmostEqual(sf, 11.8110236, places=5) sf = r.convertToPainterUnits(1, QgsUnitTypes.RenderPoints, c) self.assertAlmostEqual(sf, 4.166666665625, places=5) sf = r.convertToPainterUnits(1, QgsUnitTypes.RenderInches, c) self.assertAlmostEqual(sf, 300.0, places=5) sf = r.convertToPainterUnits(1, QgsUnitTypes.RenderPixels, c) self.assertAlmostEqual(sf, 1.0, places=5) r.setRendererScale(2000) sf = r.convertToPainterUnits(1, QgsUnitTypes.RenderMapUnits, c) self.assertAlmostEqual(sf, 0.5 / 2, places=5) sf = r.convertToPainterUnits(1, QgsUnitTypes.RenderMillimeters, c) self.assertAlmostEqual(sf, 11.8110236 / 2, places=5) sf = r.convertToPainterUnits(1, QgsUnitTypes.RenderPoints, c) self.assertAlmostEqual(sf, 4.166666665625 / 2, places=5) sf = r.convertToPainterUnits(1, QgsUnitTypes.RenderInches, c) self.assertAlmostEqual(sf, 300.0 / 2, places=5) sf = r.convertToPainterUnits(1, QgsUnitTypes.RenderPixels, c) self.assertAlmostEqual(sf, 1.0 / 2, places=5) r.setRendererScale(500) sf = r.convertToPainterUnits(1, QgsUnitTypes.RenderMapUnits, c) self.assertAlmostEqual(sf, 0.5 * 2, places=5) sf = r.convertToPainterUnits(1, QgsUnitTypes.RenderMillimeters, c) self.assertAlmostEqual(sf, 11.8110236 * 2, places=5) sf = r.convertToPainterUnits(1, QgsUnitTypes.RenderPoints, c) self.assertAlmostEqual(sf, 4.166666665625 * 2, places=5) sf = r.convertToPainterUnits(1, QgsUnitTypes.RenderInches, c) self.assertAlmostEqual(sf, 300.0 * 2, places=5) sf = r.convertToPainterUnits(1, QgsUnitTypes.RenderPixels, c) self.assertAlmostEqual(sf, 1.0 * 2, places=5)