void writeGetMap( QgsServerInterface* serverIface, const QString& version, const QgsServerRequest& request, QgsServerResponse& response ) { Q_UNUSED( version ); QgsServerRequest::Parameters params = request.parameters(); QgsWmsConfigParser* parser = getConfigParser( serverIface ); QgsWmsServer server( serverIface->configFilePath(), *serverIface->serverSettings(), params, parser, serverIface->accessControls() ); QScopedPointer<QImage> result( server.getMap() ); if ( !result.isNull() ) { QString format = params.value( QStringLiteral( "FORMAT" ), QStringLiteral( "PNG" ) ); writeImage( response, *result, format, server.getImageQuality() ); } else { throw QgsServiceException( QStringLiteral( "UnknownError" ), QStringLiteral( "Failed to compute GetMap image" ) ); } }
// Write image response void writeImage( QgsServerResponse &response, QImage &img, const QString &formatStr, int imageQuality ) { ImageOutputFormat outputFormat = parseImageFormat( formatStr ); QImage result; QString saveFormat; QString contentType; switch ( outputFormat ) { case PNG: result = img; contentType = "image/png"; saveFormat = "PNG"; break; case PNG8: { QVector<QRgb> colorTable; medianCut( colorTable, 256, img ); result = img.convertToFormat( QImage::Format_Indexed8, colorTable, Qt::ColorOnly | Qt::ThresholdDither | Qt::ThresholdAlphaDither | Qt::NoOpaqueDetection ); } contentType = "image/png"; saveFormat = "PNG"; break; case PNG16: result = img.convertToFormat( QImage::Format_ARGB4444_Premultiplied ); contentType = "image/png"; saveFormat = "PNG"; break; case PNG1: result = img.convertToFormat( QImage::Format_Mono, Qt::MonoOnly | Qt::ThresholdDither | Qt::ThresholdAlphaDither | Qt::NoOpaqueDetection ); contentType = "image/png"; saveFormat = "PNG"; break; case JPEG: result = img; contentType = "image/jpeg"; saveFormat = "JPEG"; break; default: QgsMessageLog::logMessage( QString( "Unsupported format string %1" ).arg( formatStr ) ); saveFormat = UNKN; break; } if ( outputFormat != UNKN ) { response.setHeader( "Content-Type", contentType ); result.save( response.io(), qPrintable( saveFormat ), imageQuality ); } else { throw QgsServiceException( "InvalidFormat", QString( "Output format '%1' is not supported in the GetMap request" ).arg( formatStr ) ); } }
void executeRequest( const QgsServerRequest& request, QgsServerResponse& response, const QgsProject* project ) { QgsServerRequest::Parameters params = request.parameters(); QString versionString = params.value( "VERSION" ); // Set the default version if ( versionString.isEmpty() ) { versionString = version(); // defined in qgswfsutils.h } // Get the request QString req = params.value( QStringLiteral( "REQUEST" ) ); if ( req.isEmpty() ) { throw QgsServiceException( QStringLiteral( "OperationNotSupported" ), QStringLiteral( "Please check the value of the REQUEST parameter" ) ); } if ( QSTR_COMPARE( req, "GetCapabilities" ) ) { writeGetCapabilities( mServerIface, project, versionString, request, response ); } else if ( QSTR_COMPARE( req, "GetFeature" ) ) { writeGetFeature( mServerIface, project, versionString, request, response ); } else if ( QSTR_COMPARE( req, "DescribeFeatureType" ) ) { writeDescribeFeatureType( mServerIface, versionString, request, response ); } else if ( QSTR_COMPARE( req, "Transaction" ) ) { writeTransaction( mServerIface, versionString, request, response ); } else { // Operation not supported throw QgsServiceException( QStringLiteral( "OperationNotSupported" ), QStringLiteral( "Request %1 is not supported" ).arg( req ) ); } }
void executeRequest( const QgsServerRequest &request, QgsServerResponse &response, const QgsProject *project ) override { Q_UNUSED( project ); const QgsWmtsParameters params( QUrlQuery( request.url() ) ); // Set the default version QString versionString = params.version(); if ( versionString.isEmpty() ) { versionString = version(); // defined in qgswfsutils.h } // Get the request QString req = params.value( QgsServerParameter::name( QgsServerParameter::REQUEST ) ); if ( req.isEmpty() ) { throw QgsServiceException( QStringLiteral( "OperationNotSupported" ), QStringLiteral( "Please check the value of the REQUEST parameter" ), 501 ); } if ( QSTR_COMPARE( req, "GetCapabilities" ) ) { writeGetCapabilities( mServerIface, project, versionString, request, response ); } else if ( QSTR_COMPARE( req, "GetTile" ) ) { writeGetTile( mServerIface, project, versionString, request, response ); } else if ( QSTR_COMPARE( req, "GetFeatureInfo" ) ) { writeGetFeatureInfo( mServerIface, project, versionString, request, response ); } else { // Operation not supported throw QgsServiceException( QStringLiteral( "OperationNotSupported" ), QStringLiteral( "Request %1 is not supported" ).arg( req ), 501 ); } }
// Return the wms config parser (Transitional) QgsWmsConfigParser *getConfigParser( QgsServerInterface *serverIface ) { QString configFilePath = serverIface->configFilePath(); QgsWmsConfigParser *parser = QgsConfigCache::instance()->wmsConfiguration( configFilePath, serverIface->accessControls() ); if ( !parser ) { throw QgsServiceException( QStringLiteral( "WMS configuration error" ), QStringLiteral( "There was an error reading the project file or the SLD configuration" ) ); } return parser; }
void writeGetPrint( QgsServerInterface *serverIface, const QgsProject *project, const QString &version, const QgsServerRequest &request, QgsServerResponse &response ) { QgsServerRequest::Parameters params = request.parameters(); Q_UNUSED( version ); QgsWmsParameters wmsParameters( QUrlQuery( request.url() ) ); QgsRenderer renderer( serverIface, project, wmsParameters ); QString format = params.value( "FORMAT" ); QString contentType; // GetPrint supports svg/png/pdf if ( format.compare( QLatin1String( "image/png" ), Qt::CaseInsensitive ) == 0 || format.compare( QLatin1String( "png" ), Qt::CaseInsensitive ) == 0 ) { format = "png"; contentType = "image/png"; } else if ( format.compare( QLatin1String( "image/svg" ), Qt::CaseInsensitive ) == 0 || format.compare( QLatin1String( "image/svg+xml" ), Qt::CaseInsensitive ) == 0 || format.compare( QLatin1String( "svg" ), Qt::CaseInsensitive ) == 0 ) { format = "svg"; contentType = "image/svg+xml"; } else if ( format.compare( QLatin1String( "application/pdf" ), Qt::CaseInsensitive ) == 0 || format.compare( QLatin1String( "pdf" ), Qt::CaseInsensitive ) == 0 ) { format = "pdf"; contentType = "application/pdf"; } else { throw QgsServiceException( QStringLiteral( "InvalidFormat" ), QString( "Output format %1 is not supported by the GetPrint request" ).arg( format ) ); } response.setHeader( QStringLiteral( "Content-Type" ), contentType ); response.write( renderer.getPrint( format ) ); }
void writeGetLegendGraphics( QgsServerInterface* serverIface, const QString& version, const QgsServerRequest& request, QgsServerResponse& response ) { Q_UNUSED( version ); QgsServerRequest::Parameters params = request.parameters(); QgsRenderer renderer( serverIface, params, getConfigParser( serverIface ) ); QScopedPointer<QImage> result( renderer.getLegendGraphics() ); if ( !result.isNull() ) { QString format = params.value( QStringLiteral( "FORMAT" ), QStringLiteral( "PNG" ) ); writeImage( response, *result, format, renderer.getImageQuality() ); } else { throw QgsServiceException( QStringLiteral( "UnknownError" ), QStringLiteral( "Failed to compute GetLegendGraphics image" ) ); } }
void writeGetLegendGraphics( QgsServerInterface *serverIface, const QgsProject *project, const QString &version, const QgsServerRequest &request, QgsServerResponse &response ) { Q_UNUSED( version ); QgsServerRequest::Parameters params = request.parameters(); QgsWmsParameters wmsParameters( QUrlQuery( request.url() ) ); QgsRenderer renderer( serverIface, project, wmsParameters ); std::unique_ptr<QImage> result( renderer.getLegendGraphics() ); if ( result ) { QString format = params.value( QStringLiteral( "FORMAT" ), QStringLiteral( "PNG" ) ); writeImage( response, *result, format, renderer.getImageQuality() ); } else { throw QgsServiceException( QStringLiteral( "UnknownError" ), QStringLiteral( "Failed to compute GetLegendGraphics image" ) ); } }
// DescribeLayer is defined for WMS1.1.1/SLD1.0 and in WMS 1.3.0 SLD Extension QDomDocument describeLayer( QgsServerInterface *serverIface, const QgsProject *project, const QString &version, const QgsServerRequest &request ) { Q_UNUSED( version ); QgsServerRequest::Parameters parameters = request.parameters(); if ( !parameters.contains( QStringLiteral( "SLD_VERSION" ) ) ) { throw QgsServiceException( QStringLiteral( "MissingParameterValue" ), QStringLiteral( "SLD_VERSION is mandatory for DescribeLayer operation" ), 400 ); } if ( parameters[ QStringLiteral( "SLD_VERSION" )] != QLatin1String( "1.1.0" ) ) { throw QgsServiceException( QStringLiteral( "InvalidParameterValue" ), QStringLiteral( "SLD_VERSION = %1 is not supported" ).arg( parameters[ QStringLiteral( "SLD_VERSION" )] ), 400 ); } if ( !parameters.contains( QStringLiteral( "LAYERS" ) ) ) { throw QgsServiceException( QStringLiteral( "MissingParameterValue" ), QStringLiteral( "LAYERS is mandatory for DescribeLayer operation" ), 400 ); } QStringList layersList = parameters[ QStringLiteral( "LAYERS" )].split( ',', QString::SkipEmptyParts ); if ( layersList.isEmpty() ) { throw QgsServiceException( QStringLiteral( "InvalidParameterValue" ), QStringLiteral( "Layers is empty" ), 400 ); } QDomDocument myDocument = QDomDocument(); QDomNode header = myDocument.createProcessingInstruction( QStringLiteral( "xml" ), QStringLiteral( "version=\"1.0\" encoding=\"UTF-8\"" ) ); myDocument.appendChild( header ); // Create the root element QDomElement root = myDocument.createElementNS( QStringLiteral( "http://www.opengis.net/sld" ), QStringLiteral( "DescribeLayerResponse" ) ); root.setAttribute( QStringLiteral( "xsi:schemaLocation" ), QStringLiteral( "http://www.opengis.net/sld http://schemas.opengis.net/sld/1.1.0/DescribeLayer.xsd" ) ); root.setAttribute( QStringLiteral( "xmlns:ows" ), QStringLiteral( "http://www.opengis.net/ows" ) ); root.setAttribute( QStringLiteral( "xmlns:se" ), QStringLiteral( "http://www.opengis.net/se" ) ); root.setAttribute( QStringLiteral( "xmlns:xlink" ), QStringLiteral( "http://www.w3.org/1999/xlink" ) ); root.setAttribute( QStringLiteral( "xmlns:xsi" ), QStringLiteral( "http://www.w3.org/2001/XMLSchema-instance" ) ); myDocument.appendChild( root ); // store the Version element QDomElement versionNode = myDocument.createElement( QStringLiteral( "Version" ) ); versionNode.appendChild( myDocument.createTextNode( QStringLiteral( "1.1.0" ) ) ); root.appendChild( versionNode ); // get the wms service url defined in project or keep the one from the // request url QString wmsHrefString = serviceUrl( request, project ).toString(); // get the wfs service url defined in project or take the same as the // wms service url QString wfsHrefString = QgsServerProjectUtils::wfsServiceUrl( *project ); if ( wfsHrefString.isEmpty() ) { wfsHrefString = wmsHrefString; } // get the wcs service url defined in project or take the same as the // wms service url QString wcsHrefString = QgsServerProjectUtils::wcsServiceUrl( *project ); if ( wcsHrefString.isEmpty() ) { wcsHrefString = wmsHrefString; } // access control #ifdef HAVE_SERVER_PYTHON_PLUGINS QgsAccessControl *accessControl = serverIface->accessControls(); #endif // Use layer ids bool useLayerIds = QgsServerProjectUtils::wmsUseLayerIds( *project ); // WMS restricted layers QStringList restrictedLayers = QgsServerProjectUtils::wmsRestrictedLayers( *project ); // WFS layers QStringList wfsLayerIds = QgsServerProjectUtils::wfsLayerIds( *project ); // WCS layers QStringList wcsLayerIds = QgsServerProjectUtils::wcsLayerIds( *project ); for ( QgsMapLayer *layer : project->mapLayers() ) { QString name = layer->name(); if ( useLayerIds ) name = layer->id(); else if ( !layer->shortName().isEmpty() ) name = layer->shortName(); if ( !layersList.contains( name ) ) { continue; } //unpublished layer if ( restrictedLayers.contains( layer->name() ) ) { throw QgsSecurityException( QStringLiteral( "You are not allowed to access to this layer" ) ); } #ifdef HAVE_SERVER_PYTHON_PLUGINS if ( accessControl && !accessControl->layerReadPermission( layer ) ) { throw QgsSecurityException( QStringLiteral( "You are not allowed to access to this layer" ) ); } #endif // Create the NamedLayer element QDomElement layerNode = myDocument.createElement( QStringLiteral( "LayerDescription" ) ); root.appendChild( layerNode ); // store the owsType element QDomElement typeNode = myDocument.createElement( QStringLiteral( "owsType" ) ); // store the se:OnlineResource element QDomElement oResNode = myDocument.createElement( QStringLiteral( "se:OnlineResource" ) ); oResNode.setAttribute( QStringLiteral( "xlink:type" ), QStringLiteral( "simple" ) ); // store the TypeName element QDomElement nameNode = myDocument.createElement( QStringLiteral( "TypeName" ) ); switch ( layer->type() ) { case QgsMapLayer::VectorLayer: { typeNode.appendChild( myDocument.createTextNode( QStringLiteral( "wfs" ) ) ); if ( wfsLayerIds.indexOf( layer->id() ) != -1 ) { oResNode.setAttribute( QStringLiteral( "xlink:href" ), wfsHrefString ); } // store the se:FeatureTypeName element QDomElement typeNameNode = myDocument.createElement( QStringLiteral( "se:FeatureTypeName" ) ); typeNameNode.appendChild( myDocument.createTextNode( name ) ); nameNode.appendChild( typeNameNode ); break; } case QgsMapLayer::RasterLayer: { typeNode.appendChild( myDocument.createTextNode( QStringLiteral( "wcs" ) ) ); if ( wcsLayerIds.indexOf( layer->id() ) != -1 ) { oResNode.setAttribute( QStringLiteral( "xlink:href" ), wcsHrefString ); } // store the se:CoverageTypeName element QDomElement typeNameNode = myDocument.createElement( QStringLiteral( "se:CoverageTypeName" ) ); typeNameNode.appendChild( myDocument.createTextNode( name ) ); nameNode.appendChild( typeNameNode ); break; } case QgsMapLayer::MeshLayer: case QgsMapLayer::PluginLayer: break; } layerNode.appendChild( typeNode ); layerNode.appendChild( oResNode ); layerNode.appendChild( nameNode ); } return myDocument; }