Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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 e49f357
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/core/qgsrendercontext.cpp
Expand Up @@ -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()
Expand Down
5 changes: 5 additions & 0 deletions tests/src/python/test_qgsrendercontext.py
Expand Up @@ -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))
Expand Down

0 comments on commit e49f357

Please sign in to comment.