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
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -155,26 +155,34 @@ class ProjectionSettingRestorer


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

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


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


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

0 comments on commit fb4e84a

Please sign in to comment.