Skip to content

Commit

Permalink
Merge pull request #5899 from peppsac/fix_17668
Browse files Browse the repository at this point in the history
[server] Fix layers group handling
  • Loading branch information
pblottiere authored Jan 18, 2018
2 parents dd03981 + ac27347 commit 172b217
Show file tree
Hide file tree
Showing 5 changed files with 4,507 additions and 24 deletions.
79 changes: 55 additions & 24 deletions src/server/services/wms/qgswmsrenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -504,30 +504,7 @@ namespace QgsWms
}
else
{
QList<QgsMapLayer *> layerSet;
const QList<QgsWmsParametersLayer> layers = cMapParams.mLayers;
for ( const auto &layer : layers )
{
QString nickname = layer.mNickname;
QString style = layer.mStyle;
if ( mNicknameLayers.contains( nickname ) && !mRestrictedLayers.contains( nickname ) )
{
if ( !style.isEmpty() )
{
bool rc = mNicknameLayers[nickname]->styleManager()->setCurrentStyle( style );
if ( ! rc )
{
throw QgsMapServiceException( QStringLiteral( "StyleNotDefined" ), QStringLiteral( "Style \"%1\" does not exist for layer \"%2\"" ).arg( style, nickname ) );
}
}
layerSet << mNicknameLayers[nickname];
}
else
{
throw QgsBadRequestException( QStringLiteral( "LayerNotDefined" ),
QStringLiteral( "Layer \"%1\" does not exist" ).arg( nickname ) );
}
}
QList<QgsMapLayer *> layerSet = stylizedLayers( cMapParams.mLayers );
layerSet << highlightLayers( cMapParams.mHighlightLayers );
std::reverse( layerSet.begin(), layerSet.end() );
map->setLayers( layerSet );
Expand Down Expand Up @@ -2389,6 +2366,30 @@ namespace QgsWms
{
mNicknameLayers[ layerNickname( *ml ) ] = ml;
}

// init groups
const QgsLayerTreeGroup *root = mProject->layerTreeRoot();
initLayerGroupsRecursive( root, mProject->title() );
}

void QgsRenderer::initLayerGroupsRecursive( const QgsLayerTreeGroup *group, const QString &groupName )
{
if ( !groupName.isEmpty() )
{
mLayerGroups[groupName] = QList<QgsMapLayer *>();
for ( QgsLayerTreeLayer *layer : group->findLayers() )
{
mLayerGroups[groupName].append( layer->layer() );
}
}

for ( const QgsLayerTreeNode *child : group->children() )
{
if ( child->nodeType() == QgsLayerTreeNode::NodeGroup )
{
initLayerGroupsRecursive( static_cast<const QgsLayerTreeGroup *>( child ), child->name() );
}
}
}

QString QgsRenderer::layerNickname( const QgsMapLayer &layer ) const
Expand Down Expand Up @@ -2602,6 +2603,18 @@ namespace QgsWms
mNicknameLayers[lname]->setCustomProperty( "readSLD", true );
layers.append( mNicknameLayers[lname] );
}
else if ( mLayerGroups.contains( lname ) )
{
for ( QgsMapLayer *layer : mLayerGroups[lname] )
{
if ( !mRestrictedLayers.contains( layerNickname( *layer ) ) )
{
layer->readSld( namedElem, err );
layer->setCustomProperty( "readSLD", true );
layers.append( layer );
}
}
}
else
{
throw QgsBadRequestException( QStringLiteral( "LayerNotDefined" ),
Expand Down Expand Up @@ -2648,6 +2661,24 @@ namespace QgsWms

layers.append( mNicknameLayers[nickname] );
}
else if ( mLayerGroups.contains( nickname ) )
{
for ( QgsMapLayer *layer : mLayerGroups[nickname] )
{
if ( !mRestrictedLayers.contains( layerNickname( *layer ) ) )
{
if ( !style.isEmpty() )
{
bool rc = layer->styleManager()->setCurrentStyle( style );
if ( ! rc )
{
throw QgsMapServiceException( QStringLiteral( "StyleNotDefined" ), QStringLiteral( "Style \"%1\" does not exist for layer \"%2\"" ).arg( style, layerNickname( *layer ) ) );
}
}
layers.append( layer );
}
}
}
else
{
throw QgsBadRequestException( QStringLiteral( "LayerNotDefined" ),
Expand Down
4 changes: 4 additions & 0 deletions src/server/services/wms/qgswmsrenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class QImage;
class QPaintDevice;
class QPainter;
class QStandardItem;
class QgsLayerTreeGroup;

/**
* This class handles requestsi that share rendering:
Expand Down Expand Up @@ -136,6 +137,8 @@ namespace QgsWms
// Init a map with nickname for layers' project
void initNicknameLayers();

void initLayerGroupsRecursive( const QgsLayerTreeGroup *group, const QString &groupName );

// Return the nickname of the layer (short name, id or name according to
// the project configuration)
QString layerNickname( const QgsMapLayer &layer ) const;
Expand Down Expand Up @@ -297,6 +300,7 @@ namespace QgsWms
QgsWmsParameters mWmsParameters;
QStringList mRestrictedLayers;
QMap<QString, QgsMapLayer *> mNicknameLayers;
QMap<QString, QList<QgsMapLayer *> > mLayerGroups;
QList<QgsMapLayer *> mTemporaryLayers;

public:
Expand Down
1 change: 1 addition & 0 deletions tests/src/python/test_qgsserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ def setUp(self):
self.projectAnnotationPath = os.path.join(d, "project_with_annotations.qgs")
self.projectStatePath = os.path.join(d, "project_state.qgs")
self.projectUseLayerIdsPath = os.path.join(d, "project_use_layerids.qgs")
self.projectGroupsPath = os.path.join(d, "project_groups.qgs")

# Clean env just to be sure
env_vars = ['QUERY_STRING', 'QGIS_PROJECT_FILE']
Expand Down
51 changes: 51 additions & 0 deletions tests/src/python/test_qgsserver_wms_getmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,57 @@ def test_wms_getmap_sld(self):
r, h = self._result(self._execute_request(qs))
self._img_diff_error(r, h, "WMS_GetMap_SLDRestored")

def test_wms_getmap_group(self):
qs = "?" + "&".join(["%s=%s" % i for i in list({
"MAP": urllib.parse.quote(self.projectGroupsPath),
"SERVICE": "WMS",
"VERSION": "1.1.1",
"REQUEST": "GetMap",
"LAYERS": "Country,Country_Labels,Country_Diagrams",
"STYLES": "",
"FORMAT": "image/png",
"BBOX": "-16817707,-4710778,5696513,14587125",
"HEIGHT": "500",
"WIDTH": "500",
"CRS": "EPSG:3857"
}.items())])

r_individual, _ = self._result(self._execute_request(qs))

qs = "?" + "&".join(["%s=%s" % i for i in list({
"MAP": urllib.parse.quote(self.projectGroupsPath),
"SERVICE": "WMS",
"VERSION": "1.1.1",
"REQUEST": "GetMap",
"LAYERS": "CountryGroup",
"STYLES": "",
"FORMAT": "image/png",
"BBOX": "-16817707,-4710778,5696513,14587125",
"HEIGHT": "500",
"WIDTH": "500",
"CRS": "EPSG:3857"
}.items())])

r_group, _ = self._result(self._execute_request(qs))
self.assertEqual(r_individual, r_group, 'Individual layers query and group layers query results should be identical')

qs = "?" + "&".join(["%s=%s" % i for i in list({
"MAP": urllib.parse.quote(self.projectGroupsPath),
"SERVICE": "WMS",
"VERSION": "1.1.1",
"REQUEST": "GetMap",
"SLD": "<?xml version=\"1.0\" encoding=\"UTF-8\"?><StyledLayerDescriptor xmlns=\"http://www.opengis.net/sld\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:ogc=\"http://www.opengis.net/ogc\" xsi:schemaLocation=\"http://www.opengis.net/sld http://schemas.opengis.net/sld/1.1.0/StyledLayerDescriptor.xsd\" version=\"1.1.0\" xmlns:se=\"http://www.opengis.net/se\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"> <NamedLayer> <se:Name>CountryGroup</se:Name></NamedLayer> </StyledLayerDescriptor>",
"STYLES": "",
"FORMAT": "image/png",
"BBOX": "-16817707,-4710778,5696513,14587125",
"HEIGHT": "500",
"WIDTH": "500",
"CRS": "EPSG:3857"
}.items())])

r_group_sld, _ = self._result(self._execute_request(qs))
self.assertEqual(r_individual, r_group_sld, 'Individual layers query and SLD group layers query results should be identical')


if __name__ == '__main__':
unittest.main()
Loading

0 comments on commit 172b217

Please sign in to comment.