Skip to content
Permalink
Browse files
GetStyles implementation
  • Loading branch information
elpaso committed Jan 7, 2014
2 parents 6eb72c7 + f8247fa commit 5c307b7
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 1 deletion.
@@ -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
{
@@ -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
@@ -774,3 +795,4 @@ int main( int argc, char * argv[] )
QgsDebugMsg( "************* all done ***************" );
return 0;
}

@@ -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(); }
@@ -1932,6 +1932,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;
@@ -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;
@@ -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?
@@ -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; }

@@ -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;
@@ -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, ...)

0 comments on commit 5c307b7

Please sign in to comment.