Skip to content

Commit

Permalink
allow pipe character in filepath in processing
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustry committed Oct 24, 2017
1 parent 386eef2 commit fb4e84a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 17 deletions.
42 changes: 25 additions & 17 deletions src/core/processing/qgsprocessingutils.cpp
Expand Up @@ -155,26 +155,34 @@ class ProjectionSettingRestorer

QgsMapLayer *QgsProcessingUtils::loadMapLayerFromString( const QString &string )
{
QStringList components = string.split( '|' );
if ( components.isEmpty() )
return nullptr;

QFileInfo fi;
if ( QFileInfo::exists( string ) )
{
// TODO - remove when there is a cleaner way to block the unknown projection dialog!
ProjectionSettingRestorer restorer;
( void )restorer; // no warnings
fi = QFileInfo( string );
else if ( QFileInfo::exists( components.at( 0 ) ) )
fi = QFileInfo( components.at( 0 ) );
else
return nullptr;

QFileInfo fi( string );
QString name = fi.baseName();
// TODO - remove when there is a cleaner way to block the unknown projection dialog!
ProjectionSettingRestorer restorer;
( void )restorer; // no warnings

// brute force attempt to load a matching layer
std::unique_ptr< QgsVectorLayer > layer( new QgsVectorLayer( string, name, QStringLiteral( "ogr" ), false ) );
if ( layer->isValid() )
{
return layer.release();
}
std::unique_ptr< QgsRasterLayer > rasterLayer( new QgsRasterLayer( string, name, QStringLiteral( "gdal" ), false ) );
if ( rasterLayer->isValid() )
{
return rasterLayer.release();
}
QString name = fi.baseName();

// brute force attempt to load a matching layer
std::unique_ptr< QgsVectorLayer > layer( new QgsVectorLayer( string, name, QStringLiteral( "ogr" ), false ) );
if ( layer->isValid() )
{
return layer.release();
}
std::unique_ptr< QgsRasterLayer > rasterLayer( new QgsRasterLayer( string, name, QStringLiteral( "gdal" ), false ) );
if ( rasterLayer->isValid() )
{
return rasterLayer.release();
}
return nullptr;
}
Expand Down
15 changes: 15 additions & 0 deletions tests/src/analysis/testqgsprocessing.cpp
Expand Up @@ -709,6 +709,21 @@ void TestQgsProcessing::mapLayers()
QCOMPARE( l->type(), QgsMapLayer::VectorLayer );
QCOMPARE( l->name(), QStringLiteral( "multipoint" ) );
delete l;

// Test layers from a string with parameters
QString osmFilePath = testDataDir + "openstreetmap/testdata.xml";
QgsVectorLayer *osm = qobject_cast< QgsVectorLayer *>( QgsProcessingUtils::loadMapLayerFromString( osmFilePath ) );
QVERIFY( osm->isValid() );
QCOMPARE( osm->geometryType(), QgsWkbTypes::PointGeometry );

osm = qobject_cast< QgsVectorLayer *>( QgsProcessingUtils::loadMapLayerFromString( osmFilePath + "|layerid=3" ) );
QVERIFY( osm->isValid() );
QCOMPARE( osm->geometryType(), QgsWkbTypes::PolygonGeometry );

osm = qobject_cast< QgsVectorLayer *>( QgsProcessingUtils::loadMapLayerFromString( osmFilePath + "|layerid=3|subset=\"building\" is not null" ) );
QVERIFY( osm->isValid() );
QCOMPARE( osm->geometryType(), QgsWkbTypes::PolygonGeometry );
QCOMPARE( osm->subsetString(), QStringLiteral( "\"building\" is not null" ) );
}

void TestQgsProcessing::mapLayerFromStore()
Expand Down

0 comments on commit fb4e84a

Please sign in to comment.