Skip to content
Permalink
Browse files

Fix transform of extent parameter set to match layer's extent

  • Loading branch information
nyalldawson committed Sep 24, 2017
1 parent 83a8a8d commit d72309e42b9c3358aa26ced6089f4105dc1c8a84
Showing with 63 additions and 3 deletions.
  1. +43 −2 src/core/processing/qgsprocessingparameters.cpp
  2. +20 −1 tests/src/core/testqgsprocessing.cpp
@@ -558,8 +558,22 @@ QgsRectangle QgsProcessingParameters::parameterAsExtent( const QgsProcessingPara

// try as layer extent
if ( QgsMapLayer *layer = QgsProcessingUtils::mapLayerFromString( rectText, context ) )
return layer->extent();

{
QgsRectangle rect = layer->extent();
if ( crs.isValid() && layer->crs().isValid() && crs != layer->crs() )
{
QgsCoordinateTransform ct( layer->crs(), crs );
try
{
return ct.transformBoundingBox( rect );
}
catch ( QgsCsException & )
{
QgsMessageLog::logMessage( QObject::tr( "Error transforming extent geometry" ) );
}
}
return rect;
}
return QgsRectangle();
}

@@ -633,6 +647,27 @@ QgsGeometry QgsProcessingParameters::parameterAsExtentGeometry( const QgsProcess
}
}

// try as layer extent
if ( QgsMapLayer *layer = QgsProcessingUtils::mapLayerFromString( rectText, context ) )
{
QgsRectangle rect = layer->extent();
QgsGeometry g = QgsGeometry::fromRect( rect );
if ( crs.isValid() && layer->crs().isValid() && crs != layer->crs() )
{
g = g.densifyByCount( 20 );
QgsCoordinateTransform ct( layer->crs(), crs );
try
{
g.transform( ct );
}
catch ( QgsCsException & )
{
QgsMessageLog::logMessage( QObject::tr( "Error transforming extent geometry" ) );
}
}
return g;
}

return QgsGeometry::fromRect( parameterAsExtent( definition, parameters, context, crs ) );
}

@@ -660,6 +695,12 @@ QgsCoordinateReferenceSystem QgsProcessingParameters::parameterAsExtentCrs( cons
return crs;
}

// try as layer crs
if ( QgsMapLayer *layer = QgsProcessingUtils::mapLayerFromString( valueAsString, context ) )
{
return layer->crs();
}

if ( context.project() )
return context.project()->crs();
else
@@ -1883,15 +1883,34 @@ void TestQgsProcessing::parameterExtent()
params.insert( "non_optional", raster1 );
QVERIFY( def->checkValueIsAcceptable( raster1 ) );
QCOMPARE( QgsProcessingParameters::parameterAsExtent( def.get(), params, context ), r1->extent() );
QCOMPARE( QgsProcessingParameters::parameterAsExtentCrs( def.get(), params, context ).authid(), QStringLiteral( "EPSG:4326" ) );
ext = QgsProcessingParameters::parameterAsExtent( def.get(), params, context, QgsCoordinateReferenceSystem( "EPSG:4326" ) );
QGSCOMPARENEAR( ext.xMinimum(), 1535375, 100 );
QGSCOMPARENEAR( ext.xMaximum(), 1535475, 100 );
QGSCOMPARENEAR( ext.yMinimum(), 5083255, 100 );
QGSCOMPARENEAR( ext.yMaximum(), 5083355, 100 );

// string representing a non-project layer source
params.insert( "non_optional", raster2 );
QVERIFY( def->checkValueIsAcceptable( raster2 ) );
QCOMPARE( QgsProcessingParameters::parameterAsExtentCrs( def.get(), params, context ).authid(), QStringLiteral( "EPSG:32633" ) );
ext = QgsProcessingParameters::parameterAsExtent( def.get(), params, context );
QGSCOMPARENEAR( ext.xMinimum(), 781662.375000, 10 );
QGSCOMPARENEAR( ext.xMaximum(), 793062.375000, 10 );
QGSCOMPARENEAR( ext.yMinimum(), 3339523.125000, 10 );
QGSCOMPARENEAR( ext.yMaximum(), 3350923.125000, 10 );
ext = QgsProcessingParameters::parameterAsExtent( def.get(), params, context, QgsCoordinateReferenceSystem( "EPSG:4326" ) );
QGSCOMPARENEAR( ext.xMinimum(), 17.924273, 0.01 );
QGSCOMPARENEAR( ext.xMaximum(), 18.045658, 0.01 );
QGSCOMPARENEAR( ext.yMinimum(), 30.151856, 0.01 );
QGSCOMPARENEAR( ext.yMaximum(), 30.257289, 0.01 );
QgsGeometry gExt = QgsProcessingParameters::parameterAsExtentGeometry( def.get(), params, context, QgsCoordinateReferenceSystem( "EPSG:4326" ) );
QCOMPARE( gExt.geometry()->vertexCount(), 85 );
ext = gExt.boundingBox();
QGSCOMPARENEAR( ext.xMinimum(), 17.924273, 0.01 );
QGSCOMPARENEAR( ext.xMaximum(), 18.045658, 0.01 );
QGSCOMPARENEAR( ext.yMinimum(), 30.151856, 0.01 );
QGSCOMPARENEAR( ext.yMaximum(), 30.257289, 0.01 );

// string representation of an extent
params.insert( "non_optional", QString( "1.1,2.2,3.3,4.4" ) );
@@ -1922,7 +1941,7 @@ void TestQgsProcessing::parameterExtent()
QGSCOMPARENEAR( ext.xMaximum(), 367354, 100 );
QGSCOMPARENEAR( ext.yMinimum(), 244963, 100 );
QGSCOMPARENEAR( ext.yMaximum(), 490287, 100 );
QgsGeometry gExt = QgsProcessingParameters::parameterAsExtentGeometry( def.get(), params, context, QgsCoordinateReferenceSystem( "EPSG:3785" ) );
gExt = QgsProcessingParameters::parameterAsExtentGeometry( def.get(), params, context, QgsCoordinateReferenceSystem( "EPSG:3785" ) );
QCOMPARE( gExt.geometry()->vertexCount(), 85 );
ext = gExt.boundingBox();
QGSCOMPARENEAR( ext.xMinimum(), 122451, 100 );

0 comments on commit d72309e

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