Skip to content

Commit

Permalink
Add QgsCoordinateTransform::conversionFactor
Browse files Browse the repository at this point in the history
directly copied from QgsMapSettings
  • Loading branch information
m-kuhn committed Sep 10, 2018
1 parent 85ff90f commit 89526a0
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 8 deletions.
11 changes: 11 additions & 0 deletions python/core/auto_generated/qgscoordinatetransform.sip.in
Expand Up @@ -356,6 +356,17 @@ This should be called whenever the srs database has
been modified in order to ensure that outdated CRS transforms are not created.

.. versionadded:: 3.0
%End

double conversionFactor( const QgsRectangle &referenceExtent ) const;
%Docstring
Computes an *estimated* conversion factor between source and destination units:

sourceUnits * conversionFactor = destinationUnits

:param referenceExtent: A reference extent based on which to perform the computation

.. versionadded:: 3.4
%End

};
Expand Down
11 changes: 11 additions & 0 deletions src/core/qgscoordinatetransform.cpp
Expand Up @@ -798,3 +798,14 @@ void QgsCoordinateTransform::invalidateCache()
sTransforms.clear();
sCacheLock.unlock();
}

double QgsCoordinateTransform::conversionFactor( const QgsRectangle &ReferenceExtent ) const
{
QgsPointXY source1( ReferenceExtent.xMinimum(), ReferenceExtent.yMinimum() );
QgsPointXY source2( ReferenceExtent.xMaximum(), ReferenceExtent.yMaximum() );
double distSourceUnits = std::sqrt( source1.sqrDist( source2 ) );
QgsPointXY dest1 = transform( source1 );
QgsPointXY dest2 = transform( source2 );
double distDestUnits = std::sqrt( dest1.sqrDist( dest2 ) );
return distDestUnits / distSourceUnits;
}
11 changes: 11 additions & 0 deletions src/core/qgscoordinatetransform.h
Expand Up @@ -405,6 +405,17 @@ class CORE_EXPORT QgsCoordinateTransform
*/
static void invalidateCache();

/**
* Computes an *estimated* conversion factor between source and destination units:
*
* sourceUnits * conversionFactor = destinationUnits
*
* \param referenceExtent A reference extent based on which to perform the computation
*
* \since QGIS 3.4
*/
double conversionFactor( const QgsRectangle &referenceExtent ) const;

private:

mutable QExplicitlySharedDataPointer<QgsCoordinateTransformPrivate> d;
Expand Down
9 changes: 1 addition & 8 deletions src/core/qgsmapsettings.cpp
Expand Up @@ -396,14 +396,7 @@ QgsCoordinateTransform QgsMapSettings::layerTransform( const QgsMapLayer *layer

double QgsMapSettings::layerToMapUnits( const QgsMapLayer *layer, const QgsRectangle &referenceExtent ) const
{
QgsRectangle extent = referenceExtent.isEmpty() ? layer->extent() : referenceExtent;
QgsPointXY l1( extent.xMinimum(), extent.yMinimum() );
QgsPointXY l2( extent.xMaximum(), extent.yMaximum() );
double distLayerUnits = std::sqrt( l1.sqrDist( l2 ) );
QgsPointXY m1 = layerToMapCoordinates( layer, l1 );
QgsPointXY m2 = layerToMapCoordinates( layer, l2 );
double distMapUnits = std::sqrt( m1.sqrDist( m2 ) );
return distMapUnits / distLayerUnits;
return layerTransform( layer ).conversionFactor( referenceExtent );
}


Expand Down

0 comments on commit 89526a0

Please sign in to comment.