Skip to content
Permalink
Browse files

Add QgsCoordinateTransform::conversionFactor

directly copied from QgsMapSettings
  • Loading branch information
m-kuhn committed Sep 10, 2018
1 parent 85ff90f commit 89526a02e023598e85a9f5d8b45ae0e7deb19340
@@ -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

};
@@ -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;
}
@@ -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;
@@ -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 );
}


0 comments on commit 89526a0

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