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)