Navigation Menu

Skip to content

Commit

Permalink
Fix python serialization for geometry processing parameter
Browse files Browse the repository at this point in the history
    Fix regexp for referenced geometry parsing
    Fix suggested doc strings
  • Loading branch information
dmarteau authored and nyalldawson committed Sep 14, 2020
1 parent fc67ddc commit 518e48c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 19 deletions.
Expand Up @@ -1578,7 +1578,7 @@ A geometry parameter for processing algorithms.
%Docstring
Constructor for QgsProcessingParameterGeometry.

The ``geometryTypes`` argument allows for specifying a list of geometry types acceptable for this
The ``geometryTypes`` argument allows for specifying a list of geometry types (see QgsWkbTypes.GeometryType) acceptable for this
parameter. Passing a empty list will allow for any type of geometry.
%End

Expand All @@ -1604,14 +1604,14 @@ Returns the type name for the parameter class.

QList<int> geometryTypes() const;
%Docstring
Returns the parameter allowed geometries.
Returns the parameter allowed geometries, as a list of QgsWkbTypes.GeometryType values.

.. seealso:: :py:func:`setGeometryTypes`
%End

void setGeometryTypes( const QList<int> &geometryTypes );
%Docstring
Sets the allowed ``geometryTypes``.
Sets the allowed ``geometryTypes``, as a list of QgsWkbTypes.GeometryType values.

.. seealso:: :py:func:`geometryTypes`
%End
Expand Down
26 changes: 13 additions & 13 deletions src/core/processing/qgsprocessingparameters.cpp
Expand Up @@ -1509,15 +1509,15 @@ QgsGeometry QgsProcessingParameters::parameterAsGeometry( const QgsProcessingPar
if ( valueAsString.isEmpty() )
return QgsGeometry();

QRegularExpression rx( QStringLiteral( "^\\s*(?:CRS=(.*);)?(.*)$" ) );
QRegularExpression rx( QStringLiteral( "^\\s*(?:CRS=(.*);)?(.*?)$" ) );

QRegularExpressionMatch match = rx.match( valueAsString );
if ( match.hasMatch() )
{
QgsGeometry g = QgsGeometry::fromWkt( match.captured( 2 ) );
if ( !g.isNull() )
{
QgsCoordinateReferenceSystem geomCrs( QStringLiteral( "%1" ).arg( match.captured( 1 ) ) );
QgsCoordinateReferenceSystem geomCrs( match.captured( 1 ) );
if ( crs.isValid() && geomCrs.isValid() && crs != geomCrs )
{
QgsCoordinateTransform ct( geomCrs, crs, context.project() );
Expand Down Expand Up @@ -1574,13 +1574,13 @@ QgsCoordinateReferenceSystem QgsProcessingParameters::parameterAsGeometryCrs( co
}

// Match against EWKT
QRegularExpression rx( QStringLiteral( "^\\s*(?:CRS=(.*);)?(.*)$" ) );
QRegularExpression rx( QStringLiteral( "^\\s*(?:CRS=(.*);)?(.*?)$" ) );

QString valueAsString = parameterAsString( definition, value, context );
QRegularExpressionMatch match = rx.match( valueAsString );
if ( match.hasMatch() )
{
QgsCoordinateReferenceSystem crs( QStringLiteral( "%1" ).arg( match.captured( 1 ) ) );
QgsCoordinateReferenceSystem crs( match.captured( 1 ) );
if ( crs.isValid() )
return crs;
}
Expand Down Expand Up @@ -3011,7 +3011,7 @@ bool QgsProcessingParameterGeometry::checkValueIsAcceptable( const QVariant &inp
}

// Match against EWKT
QRegularExpression rx( QStringLiteral( "^\\s*(?:CRS=(.*);)?(.*)$" ) );
QRegularExpression rx( QStringLiteral( "^\\s*(?:CRS=(.*);)?(.*?)$" ) );

QRegularExpressionMatch match = rx.match( input.toString() );
if ( match.hasMatch() )
Expand All @@ -3031,19 +3031,19 @@ bool QgsProcessingParameterGeometry::checkValueIsAcceptable( const QVariant &inp

QString QgsProcessingParameterGeometry::valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const
{
auto asPythonString = []( const QgsGeometry & g, QString authid = QString() )
auto asPythonString = []( const QgsGeometry & g, const QgsCoordinateReferenceSystem &crs = QgsCoordinateReferenceSystem() )
{
if ( authid.isEmpty() )
return QStringLiteral( "'%1'" ).arg( g.asWkt() );
if ( !crs.isValid() )
return QgsProcessingUtils::stringToPythonLiteral( g.asWkt() );
else
return QStringLiteral( "'CRS=%2;%1'" ).arg( g.asWkt(), authid );
return QgsProcessingUtils::stringToPythonLiteral( QStringLiteral( "CRS=%1;%2" ).arg( crs.authid().isEmpty() ? crs.toWkt( QgsCoordinateReferenceSystem::WKT_PREFERRED ) : crs.authid(), g.asWkt() ) );
};

if ( !value.isValid() )
return QStringLiteral( "None" );

if ( value.canConvert<QgsProperty>() )
return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
return QStringLiteral( "QgsProperty.fromExpression(%1)" ).arg( QgsProcessingUtils::stringToPythonLiteral( value.value< QgsProperty >().asExpression() ) );

if ( value.canConvert< QgsGeometry >() )
{
Expand All @@ -3056,7 +3056,7 @@ QString QgsProcessingParameterGeometry::valueAsPythonString( const QVariant &val
{
const QgsReferencedGeometry g = value.value<QgsReferencedGeometry>();
if ( !g.isNull() )
return asPythonString( g, g.crs().authid() );
return asPythonString( g, g.crs() );
}

if ( value.canConvert< QgsPointXY >() )
Expand All @@ -3070,7 +3070,7 @@ QString QgsProcessingParameterGeometry::valueAsPythonString( const QVariant &val
{
const QgsReferencedGeometry g = QgsReferencedGeometry::fromReferencedPointXY( value.value<QgsReferencedPointXY>() );
if ( !g.isNull() )
return asPythonString( g, g.crs().authid() );
return asPythonString( g, g.crs() );
}

if ( value.canConvert< QgsRectangle >() )
Expand All @@ -3084,7 +3084,7 @@ QString QgsProcessingParameterGeometry::valueAsPythonString( const QVariant &val
{
const QgsReferencedGeometry g = QgsReferencedGeometry::fromReferencedRect( value.value<QgsReferencedRectangle>() );
if ( !g.isNull() )
return asPythonString( g, g.crs().authid() );
return asPythonString( g, g.crs() );
}

return QgsProcessingParameterDefinition::valueAsPythonString( value, context );
Expand Down
6 changes: 3 additions & 3 deletions src/core/processing/qgsprocessingparameters.h
Expand Up @@ -1613,7 +1613,7 @@ class CORE_EXPORT QgsProcessingParameterGeometry : public QgsProcessingParameter
/**
* Constructor for QgsProcessingParameterGeometry.
*
* The \a geometryTypes argument allows for specifying a list of geometry types acceptable for this
* The \a geometryTypes argument allows for specifying a list of geometry types (see QgsWkbTypes::GeometryType) acceptable for this
* parameter. Passing a empty list will allow for any type of geometry.
*/
QgsProcessingParameterGeometry( const QString &name, const QString &description = QString(), const QVariant &defaultValue = QVariant(), bool optional = false, const QList< int > &geometryTypes = QList< int >() );
Expand All @@ -1632,13 +1632,13 @@ class CORE_EXPORT QgsProcessingParameterGeometry : public QgsProcessingParameter
bool fromVariantMap( const QVariantMap &map ) override;

/**
* Returns the parameter allowed geometries.
* Returns the parameter allowed geometries, as a list of QgsWkbTypes::GeometryType values.
* \see setGeometryTypes()
*/
QList<int> geometryTypes() const { return mGeomTypes; }

/**
* Sets the allowed \a geometryTypes.
* Sets the allowed \a geometryTypes, as a list of QgsWkbTypes::GeometryType values.
* \see geometryTypes()
*/
void setGeometryTypes( const QList<int> &geometryTypes ) { mGeomTypes = geometryTypes; }
Expand Down

0 comments on commit 518e48c

Please sign in to comment.