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 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 = 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 = 150000000.0 mup = c.computeMapUnitsPerPixel(r) self.assertAlmostEqual(mup, 1.0276160, places=5) c.minScale = 50000000.0 mup = c.computeMapUnitsPerPixel(r) self.assertAlmostEqual(mup, 0.3425386, places=5) c.minScale = 350000000.0 # add a maximum scale greater than the renderer scale, so should be no change c.maxScale = 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 = 350000000.0 mup = c.computeMapUnitsPerPixel(r) self.assertAlmostEqual(mup, 2.3977706, places=5) c.maxScale = 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 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 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 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 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 testEncodeDecode(self): # test encoding and decoding QgsMapUnitScale s = QgsMapUnitScale() s.minScale = 100 s.maxScale = 50 s.minSizeMMEnabled = True s.minSizeMM = 3 s.maxSizeMMEnabled = False s.maxSizeMM = 99 encode = QgsSymbolLayerUtils.encodeMapUnitScale(s) r = QgsSymbolLayerUtils.decodeMapUnitScale(encode) self.assertEqual(s, r) # check old style encoding encode = '9,78.3' r = QgsSymbolLayerUtils.decodeMapUnitScale(encode) self.assertAlmostEqual(r.minScale, 1.0 / 9, 3) self.assertAlmostEqual(r.maxScale, 1.0 / 78.3, 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 = 1 / 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 = 1 / 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 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 testConvertToMapUnits(self): # test QgsSymbolLayerV2Utils::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 = QgsSymbolLayerV2Utils.convertToMapUnits(r, 2, QgsSymbolV2.MapUnit, c) self.assertEqual(size, 2.0) size = QgsSymbolLayerV2Utils.convertToMapUnits(r, 2, QgsSymbolV2.MM, c) self.assertAlmostEqual(size, 47.244094, places=5) size = QgsSymbolLayerV2Utils.convertToMapUnits(r, 2, QgsSymbolV2.Pixel, 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 = QgsSymbolLayerV2Utils.convertToMapUnits(r, 2, QgsSymbolV2.MapUnit, c) self.assertAlmostEqual(size, 118.1102362, places=5) # only conversion from mapunits should be affected size = QgsSymbolLayerV2Utils.convertToMapUnits(r, 2, QgsSymbolV2.MM, c) self.assertAlmostEqual(size, 47.244094, places=5) size = QgsSymbolLayerV2Utils.convertToMapUnits(r, 2, QgsSymbolV2.Pixel, 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 = QgsSymbolLayerV2Utils.convertToMapUnits(r, 2, QgsSymbolV2.MapUnit, c) self.assertAlmostEqual(size, 1.1811023622047245, places=5) # only conversion from mapunits should be affected size = QgsSymbolLayerV2Utils.convertToMapUnits(r, 2, QgsSymbolV2.MM, c) self.assertAlmostEqual(size, 47.244094, places=5) size = QgsSymbolLayerV2Utils.convertToMapUnits(r, 2, QgsSymbolV2.Pixel, c) self.assertAlmostEqual(size, 4.0, places=5) c.maxSizeMMEnabled = False # test with minimum scale set c.minScale = 1 / 150000000.0 size = QgsSymbolLayerV2Utils.convertToMapUnits(r, 2, QgsSymbolV2.MapUnit, c) self.assertAlmostEqual(size, 15.57001821, places=5) # only conversion from mapunits should be affected size = QgsSymbolLayerV2Utils.convertToMapUnits(r, 2, QgsSymbolV2.MM, c) self.assertAlmostEqual(size, 47.244094, places=5) size = QgsSymbolLayerV2Utils.convertToMapUnits(r, 2, QgsSymbolV2.Pixel, c) self.assertAlmostEqual(size, 4.0, places=5) c.minScale = 0 # test with maximum scale set c.maxScale = 1 / 1550000000.0 size = QgsSymbolLayerV2Utils.convertToMapUnits(r, 2, QgsSymbolV2.MapUnit, c) self.assertAlmostEqual(size, 1.50677595625, places=5) # only conversion from mapunits should be affected size = QgsSymbolLayerV2Utils.convertToMapUnits(r, 2, QgsSymbolV2.MM, c) self.assertAlmostEqual(size, 47.244094, places=5) size = QgsSymbolLayerV2Utils.convertToMapUnits(r, 2, QgsSymbolV2.Pixel, c) self.assertAlmostEqual(size, 4.0, places=5) c.maxScale = 0
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)