Skip to content
Permalink
Browse files

Attempting to convert Meters in Map Units based sizes when no

map extent is available should fallback to a very approximate
degrees to meters conversion only

We can't do better in this situation!

Refs #28690
  • Loading branch information
nyalldawson committed Jun 16, 2020
1 parent b7eac07 commit e49f3575505902913c33b7db06618f8e830a78ef
Showing with 11 additions and 0 deletions.
  1. +6 −0 src/core/qgsrendercontext.cpp
  2. +5 −0 tests/src/python/test_qgsrendercontext.py
@@ -457,6 +457,12 @@ double QgsRenderContext::convertMetersToMapUnits( double meters ) const
return meters;
case QgsUnitTypes::DistanceDegrees:
{
if ( mExtent.isNull() )
{
// we don't have an extent to calculate exactly -- so just use a very rough approximation
return meters * QgsUnitTypes::fromUnitToUnitFactor( QgsUnitTypes::DistanceMeters, QgsUnitTypes::DistanceDegrees );
}

QgsPointXY pointCenter = mExtent.center();
// The Extent is in the sourceCrs(), when different from destinationCrs()
// - the point must be transformed, since DistanceArea uses the destinationCrs()
@@ -215,6 +215,11 @@ def testRenderMetersInMapUnits(self):
self.assertEqual(QgsDistanceArea.formatDistance(result_test_meters, 1, QgsUnitTypes.DistanceMeters, True),
QgsDistanceArea.formatDistance(meters_test, 1, QgsUnitTypes.DistanceMeters, True))

# attempting to convert to meters in map units when no extent is available should fallback to a very
# approximate degrees -> meters conversion
r.setExtent(QgsRectangle())
self.assertAlmostEqual(r.convertToPainterUnits(5555, QgsUnitTypes.RenderMetersInMapUnits), 0.0499, 3)

def testConvertSingleUnit(self):
ms = QgsMapSettings()
ms.setExtent(QgsRectangle(0, 0, 100, 100))

0 comments on commit e49f357

Please sign in to comment.
You can’t perform that action at this time.