Skip to content

Commit

Permalink
Merge pull request #1058 from elpaso/master
Browse files Browse the repository at this point in the history
GetStyles split from GetStyle
  • Loading branch information
mhugent committed Jan 9, 2014
2 parents 9b0652f + 1f9c378 commit 1b02969
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 4 deletions.
24 changes: 23 additions & 1 deletion src/mapserver/qgis_map_serv.cpp
Expand Up @@ -692,7 +692,7 @@ int main( int argc, char * argv[] )
delete theServer;
continue;
}
else if ( request.compare( "GetStyles", Qt::CaseInsensitive ) == 0 || request.compare( "GetStyle", Qt::CaseInsensitive ) == 0 ) // GetStyle for compatibility with earlier QGIS versions
else if ( request.compare( "GetStyle", Qt::CaseInsensitive ) == 0 ) // GetStyle for compatibility with earlier QGIS versions
{
try
{
Expand All @@ -708,6 +708,27 @@ int main( int argc, char * argv[] )
delete theServer;
continue;
}
else if ( request.compare( "GetStyles", Qt::CaseInsensitive ) == 0 )
{
// GetStyles is only defined for WMS1.1.1/SLD1.0
if ( version != "1.1.1") {
theRequestHandler->sendServiceException( QgsMapServiceException( "OperationNotSupported", "GetStyles method is only available in WMS version 1.1.1" ) );
} else {
try
{
QDomDocument doc = theServer->getStyles();
theRequestHandler->sendGetStyleResponse( doc );
}
catch ( QgsMapServiceException& ex )
{
theRequestHandler->sendServiceException( ex );
}
}

delete theRequestHandler;
delete theServer;
continue;
}
else if ( request.compare( "GetLegendGraphic", Qt::CaseInsensitive ) == 0 ||
request.compare( "GetLegendGraphics", Qt::CaseInsensitive ) == 0 )
// GetLegendGraphics for compatibility with earlier QGIS versions
Expand Down Expand Up @@ -774,3 +795,4 @@ int main( int argc, char * argv[] )
QgsDebugMsg( "************* all done ***************" );
return 0;
}

2 changes: 2 additions & 0 deletions src/mapserver/qgsconfigparser.h
Expand Up @@ -78,6 +78,8 @@ class QgsConfigParser

/**Returns the xml fragment of a style*/
virtual QDomDocument getStyle( const QString& styleName, const QString& layerName ) const = 0;
/**Returns the xml fragment of layer styles*/
virtual QDomDocument getStyles( QStringList& layerList ) const = 0;

/**Returns the names of the published wfs layers (not the ids as in wfsLayers() )*/
virtual QStringList wfsLayerNames() const { return QStringList(); }
Expand Down
63 changes: 60 additions & 3 deletions src/mapserver/qgsprojectparser.cpp
Expand Up @@ -1543,9 +1543,7 @@ void QgsProjectParser::addLayerProjectSettings( QDomElement& layerElem, QDomDocu
{
QgsVectorLayer* vLayer = static_cast<QgsVectorLayer*>( currentLayer );
const QSet<QString>& excludedAttributes = vLayer->excludeAttributesWMS();

//displayfield
layerElem.setAttribute( "displayField", vLayer->displayField() );
QString displayField = vLayer->displayField();

//attributes
QDomElement attributesElem = doc.createElement( "Attributes" );
Expand All @@ -1557,6 +1555,11 @@ void QgsProjectParser::addLayerProjectSettings( QDomElement& layerElem, QDomDocu
{
continue;
}
// field alias in case of displayField
if ( field.name() == displayField )
{
displayField = vLayer->attributeDisplayName( idx );
}
QDomElement attributeElem = doc.createElement( "Attribute" );
attributeElem.setAttribute( "name", vLayer->attributeDisplayName( idx ) );
attributeElem.setAttribute( "type", QVariant::typeToName( field.type() ) );
Expand All @@ -1569,6 +1572,8 @@ void QgsProjectParser::addLayerProjectSettings( QDomElement& layerElem, QDomDocu
attributeElem.setAttribute( "precision", field.precision() );
attributesElem.appendChild( attributeElem );
}
//displayfield
layerElem.setAttribute( "displayField", displayField );
layerElem.appendChild( attributesElem );
}
}
Expand Down Expand Up @@ -1932,6 +1937,58 @@ QDomDocument QgsProjectParser::getStyle( const QString& styleName, const QString
return myDocument;
}

QDomDocument QgsProjectParser::getStyles( QStringList& layerList ) const
{
QDomDocument myDocument = QDomDocument();

QDomNode header = myDocument.createProcessingInstruction( "xml", "version=\"1.0\" encoding=\"UTF-8\"" );
myDocument.appendChild( header );

// Create the root element
QDomElement root = myDocument.createElementNS( "http://www.opengis.net/sld", "StyledLayerDescriptor" );
root.setAttribute( "version", "1.1.0" );
root.setAttribute( "xsi:schemaLocation", "http://www.opengis.net/sld http://schemas.opengis.net/sld/1.1.0/StyledLayerDescriptor.xsd" );
root.setAttribute( "xmlns:ogc", "http://www.opengis.net/ogc" );
root.setAttribute( "xmlns:se", "http://www.opengis.net/se" );
root.setAttribute( "xmlns:xlink", "http://www.w3.org/1999/xlink" );
root.setAttribute( "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance" );
myDocument.appendChild( root );

for ( int i = 0; i < layerList.size(); i++)
{
QString layerName;
QString typeName;
layerName = layerList.at( i );
typeName = layerName.replace(" ", "_");
QList<QgsMapLayer*> currentLayerList = mapLayerFromTypeName( typeName );
if ( currentLayerList.size() < 1 )
{
throw QgsMapServiceException( "Error", QString( "The layer for the TypeName '%1' is not found" ).arg( layerName ) );
}
for ( int j = 0; j < currentLayerList.size(); j++)
{
QgsMapLayer* currentLayer = currentLayerList.at( j );
QgsVectorLayer* layer = dynamic_cast<QgsVectorLayer*>( currentLayer );
if ( !layer )
{
throw QgsMapServiceException( "Error", QString( "Could not get style because:\n%1" ).arg( "Non-vector layers not supported yet" ) );
}
// Create the NamedLayer element
QDomElement namedLayerNode = myDocument.createElement( "NamedLayer" );
root.appendChild( namedLayerNode );

QString errorMsg;
if ( !layer->writeSld( namedLayerNode, myDocument, errorMsg ) )
{
throw QgsMapServiceException( "Error", QString( "Could not get style because:\n%1" ).arg( errorMsg ) );
}
}
}
return myDocument;
}



QgsMapRenderer::OutputUnits QgsProjectParser::outputUnits() const
{
return QgsMapRenderer::Millimeters;
Expand Down
3 changes: 3 additions & 0 deletions src/mapserver/qgsprojectparser.h
Expand Up @@ -71,6 +71,9 @@ class QgsProjectParser: public QgsConfigParser

/**Returns the xml fragment of a style*/
virtual QDomDocument getStyle( const QString& styleName, const QString& layerName ) const;
/**Returns the xml fragment of layers styles*/
virtual QDomDocument getStyles( QStringList& layerList ) const;


/**Returns if output are MM or PIXEL*/
virtual QgsMapRenderer::OutputUnits outputUnits() const;
Expand Down
23 changes: 23 additions & 0 deletions src/mapserver/qgssldparser.cpp
Expand Up @@ -1412,6 +1412,29 @@ QDomDocument QgsSLDParser::getStyle( const QString& styleName, const QString& la
return styleDoc;
}

QDomDocument QgsSLDParser::getStyles( QStringList& layerList ) const
{
QDomDocument styleDoc;
for ( int i = 0; i < layerList.size(); i++)
{
QString layerName;
QString typeName;
layerName = layerList.at( i );
QDomElement userLayerElement = findUserLayerElement( layerName );
if ( userLayerElement.isNull() )
{
throw QgsMapServiceException( "LayerNotDefined", "Operation request is for a Layer not offered by the server." );
}
QDomNodeList userStyleList = userLayerElement.elementsByTagName( "UserStyle" );
for ( int j = 0; j < userStyleList.size(); j++)
{
QDomElement userStyleElement = userStyleList.item( i ).toElement();
styleDoc.appendChild( styleDoc.importNode( userStyleElement, true ) );
}
}
return styleDoc;
}

QString QgsSLDParser::layerNameFromUri( const QString& uri ) const
{
//file based?
Expand Down
2 changes: 2 additions & 0 deletions src/mapserver/qgssldparser.h
Expand Up @@ -77,6 +77,8 @@ class QgsSLDParser: public QgsConfigParser

/**Returns the xml fragment of a style*/
QDomDocument getStyle( const QString& styleName, const QString& layerName ) const;
/**Returns the xml fragment of layers styles*/
QDomDocument getStyles( QStringList& layerList ) const;

virtual void setParameterMap( const QMap<QString, QString>& parameterMap ) { mParameterMap = parameterMap; }

Expand Down
18 changes: 18 additions & 0 deletions src/mapserver/qgswmsserver.cpp
Expand Up @@ -601,6 +601,24 @@ QDomDocument QgsWMSServer::getStyle()
return mConfigParser->getStyle( styleName, layerName );
}

// GetStyles is only defined for WMS1.1.1/SLD1.0
QDomDocument QgsWMSServer::getStyles()
{
QDomDocument doc;
if ( !mParameterMap.contains( "LAYERS" ) )
{
throw QgsMapServiceException( "LayerNotSpecified", "Layers is mandatory for GetStyles operation" );
}

QStringList layersList = mParameterMap[ "LAYERS" ].split( ",", QString::SkipEmptyParts );
if ( layersList.size() < 1 )
{
throw QgsMapServiceException( "LayerNotSpecified", "Layers is mandatory for GetStyles operation" );
}

return mConfigParser->getStyles( layersList );
}

QByteArray* QgsWMSServer::getPrint( const QString& formatString )
{
QStringList layersList, stylesList, layerIdList;
Expand Down
2 changes: 2 additions & 0 deletions src/mapserver/qgswmsserver.h
Expand Up @@ -72,6 +72,8 @@ class QgsWMSServer
QImage* getMap();
/**Returns an SLD file with the style of the requested layer. Exception is raised in case of troubles :-)*/
QDomDocument getStyle();
/**Returns an SLD file with the styles of the requested layers. Exception is raised in case of troubles :-)*/
QDomDocument getStyles();

/**Returns printed page as binary
@param formatString out: format of the print output (e.g. pdf, svg, png, ...)
Expand Down

0 comments on commit 1b02969

Please sign in to comment.