Skip to content
Permalink
Browse files

Merge pull request #5899 from peppsac/fix_17668

[server] Fix layers group handling
  • Loading branch information
pblottiere committed Jan 18, 2018
2 parents dd03981 + ac27347 commit 172b21742e3dec3be45dd8964c24ec03987a8d53
@@ -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 );
@@ -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
@@ -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" ),
@@ -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" ),
@@ -57,6 +57,7 @@ class QImage;
class QPaintDevice;
class QPainter;
class QStandardItem;
class QgsLayerTreeGroup;

/**
* This class handles requestsi that share rendering:
@@ -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;
@@ -297,6 +300,7 @@ namespace QgsWms
QgsWmsParameters mWmsParameters;
QStringList mRestrictedLayers;
QMap<QString, QgsMapLayer *> mNicknameLayers;
QMap<QString, QList<QgsMapLayer *> > mLayerGroups;
QList<QgsMapLayer *> mTemporaryLayers;

public:
@@ -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']
@@ -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()

0 comments on commit 172b217

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