QDomDocument getStyle( QgsServerInterface *serverIface, const QgsProject *project, const QString &version, const QgsServerRequest &request ) { Q_UNUSED( version ); QgsServerRequest::Parameters parameters = request.parameters(); QDomDocument doc; QString styleName = parameters.value( QStringLiteral( "STYLE" ) ); QString layerName = parameters.value( QStringLiteral( "LAYER" ) ); if ( styleName.isEmpty() ) { throw QgsBadRequestException( QgsServiceException::QGIS_MissingParameterValue, QgsWmsParameter::STYLE ); } if ( layerName.isEmpty() ) { throw QgsBadRequestException( QgsServiceException::QGIS_MissingParameterValue, QgsWmsParameter::LAYERS ); } QStringList layerList; layerList.append( layerName ); return getStyledLayerDescriptorDocument( serverIface, project, layerList ); }
QDomDocument getStyles( QgsServerInterface *serverIface, const QgsProject *project, const QString &version, const QgsServerRequest &request ) { Q_UNUSED( version ); QgsServerRequest::Parameters parameters = request.parameters(); QString layersName = parameters.value( "LAYERS" ); if ( layersName.isEmpty() ) { throw QgsBadRequestException( QgsServiceException::QGIS_MissingParameterValue, QgsWmsParameter::LAYERS ); } QStringList layerList = layersName.split( ',', QString::SkipEmptyParts ); if ( layerList.isEmpty() ) { throw QgsBadRequestException( QgsServiceException::QGIS_MissingParameterValue, QgsWmsParameter::LAYERS ); } return getStyledLayerDescriptorDocument( serverIface, project, layerList ); }
// 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 ); if ( saveFormat == "JPEG" ) { result.save( response.io(), qPrintable( saveFormat ), imageQuality ); } else { result.save( response.io(), qPrintable( saveFormat ) ); } } else { QgsWmsParameter parameter( QgsWmsParameter::FORMAT ); parameter.mValue = formatStr; throw QgsBadRequestException( QgsServiceException::OGC_InvalidFormat, parameter ); } }
QDomDocument createDescribeFeatureTypeDocument( QgsServerInterface *serverIface, const QgsProject *project, const QString &version, const QgsServerRequest &request ) { Q_UNUSED( version ); QDomDocument doc; QgsServerRequest::Parameters parameters = request.parameters(); QgsWfsParameters wfsParameters( parameters ); QgsWfsParameters::Format oFormat = wfsParameters.outputFormat(); // test oFormat if ( oFormat == QgsWfsParameters::Format::NONE ) throw QgsBadRequestException( QStringLiteral( "Invalid WFS Parameter" ), "OUTPUTFORMAT " + wfsParameters.outputFormatAsString() + "is not supported" ); QgsAccessControl *accessControl = serverIface->accessControls(); //xsd:schema QDomElement schemaElement = doc.createElement( QStringLiteral( "schema" )/*xsd:schema*/ ); schemaElement.setAttribute( QStringLiteral( "xmlns" ), QStringLiteral( "http://www.w3.org/2001/XMLSchema" ) ); schemaElement.setAttribute( QStringLiteral( "xmlns:xsd" ), QStringLiteral( "http://www.w3.org/2001/XMLSchema" ) ); schemaElement.setAttribute( QStringLiteral( "xmlns:ogc" ), OGC_NAMESPACE ); schemaElement.setAttribute( QStringLiteral( "xmlns:gml" ), GML_NAMESPACE ); schemaElement.setAttribute( QStringLiteral( "xmlns:qgs" ), QGS_NAMESPACE ); schemaElement.setAttribute( QStringLiteral( "targetNamespace" ), QGS_NAMESPACE ); schemaElement.setAttribute( QStringLiteral( "elementFormDefault" ), QStringLiteral( "qualified" ) ); schemaElement.setAttribute( QStringLiteral( "version" ), QStringLiteral( "1.0" ) ); doc.appendChild( schemaElement ); //xsd:import QDomElement importElement = doc.createElement( QStringLiteral( "import" )/*xsd:import*/ ); importElement.setAttribute( QStringLiteral( "namespace" ), GML_NAMESPACE ); if ( oFormat == QgsWfsParameters::Format::GML2 ) importElement.setAttribute( QStringLiteral( "schemaLocation" ), QStringLiteral( "http://schemas.opengis.net/gml/2.1.2/feature.xsd" ) ); else if ( oFormat == QgsWfsParameters::Format::GML3 ) importElement.setAttribute( QStringLiteral( "schemaLocation" ), QStringLiteral( "http://schemas.opengis.net/gml/3.1.1/base/gml.xsd" ) ); schemaElement.appendChild( importElement ); QStringList typeNameList; QDomDocument queryDoc; QString errorMsg; if ( queryDoc.setContent( parameters.value( QStringLiteral( "REQUEST_BODY" ) ), true, &errorMsg ) ) { //read doc QDomElement queryDocElem = queryDoc.documentElement(); QDomNodeList docChildNodes = queryDocElem.childNodes(); if ( docChildNodes.size() ) { for ( int i = 0; i < docChildNodes.size(); i++ ) { QDomElement docChildElem = docChildNodes.at( i ).toElement(); if ( docChildElem.tagName() == QLatin1String( "TypeName" ) ) { QString typeName = docChildElem.text().trimmed(); if ( typeName.contains( ':' ) ) typeNameList << typeName.section( ':', 1, 1 ); else typeNameList << typeName; } } } } else { QString typeNames = request.parameter( QStringLiteral( "TYPENAME" ) ); if ( !typeNames.isEmpty() ) { QStringList typeNameSplit = typeNames.split( ',' ); for ( int i = 0; i < typeNameSplit.size(); ++i ) { QString typeName = typeNameSplit.at( i ).trimmed(); if ( typeName.contains( ':' ) ) typeNameList << typeName.section( ':', 1, 1 ); else typeNameList << typeName; } } } QStringList wfsLayerIds = QgsServerProjectUtils::wfsLayerIds( *project ); for ( int i = 0; i < wfsLayerIds.size(); ++i ) { QgsMapLayer *layer = project->mapLayer( wfsLayerIds.at( i ) ); if ( layer->type() != QgsMapLayer::LayerType::VectorLayer ) { continue; } QString name = layer->name(); if ( !layer->shortName().isEmpty() ) name = layer->shortName(); name = name.replace( ' ', '_' ); if ( !typeNameList.isEmpty() && !typeNameList.contains( name ) ) { continue; } if ( accessControl && !accessControl->layerReadPermission( layer ) ) { if ( !typeNameList.isEmpty() ) { throw QgsSecurityAccessException( QStringLiteral( "Feature access permission denied" ) ); } else { continue; } } QgsVectorLayer *vLayer = qobject_cast<QgsVectorLayer *>( layer ); QgsVectorDataProvider *provider = vLayer->dataProvider(); if ( !provider ) { continue; } setSchemaLayer( schemaElement, doc, const_cast<QgsVectorLayer *>( vLayer ) ); } return doc; }
QUrlQuery translateWmtsParamToWmsQueryItem( const QString &request, const QgsWmtsParameters ¶ms, const QgsProject *project, QgsServerInterface *serverIface ) { #ifndef HAVE_SERVER_PYTHON_PLUGINS ( void )serverIface; #endif //defining Layer QString layer = params.layer(); //read Layer if ( layer.isEmpty() ) { throw QgsRequestNotWellFormedException( QStringLiteral( "Layer is mandatory" ) ); } //check layer value bool wmtsProject = project->readBoolEntry( QStringLiteral( "WMTSLayers" ), QStringLiteral( "Project" ) ); QStringList wmtsGroupNameList = project->readListEntry( QStringLiteral( "WMTSLayers" ), QStringLiteral( "Group" ) ); QStringList wmtsLayerIdList = project->readListEntry( QStringLiteral( "WMTSLayers" ), QStringLiteral( "Layer" ) ); QStringList wmtsLayerIds; if ( wmtsProject ) { // Root Layer name QString rootLayerId = QgsServerProjectUtils::wmsRootName( *project ); if ( rootLayerId.isEmpty() ) { rootLayerId = project->title(); } if ( !rootLayerId.isEmpty() ) { wmtsLayerIds << rootLayerId; } } if ( !wmtsGroupNameList.isEmpty() ) { QgsLayerTreeGroup *treeRoot = project->layerTreeRoot(); for ( const QString &gName : wmtsGroupNameList ) { QgsLayerTreeGroup *treeGroup = treeRoot->findGroup( gName ); if ( !treeGroup ) { continue; } QString groupLayerId = treeGroup->customProperty( QStringLiteral( "wmsShortName" ) ).toString(); if ( groupLayerId.isEmpty() ) { groupLayerId = gName; } wmtsLayerIds << groupLayerId; } } if ( !wmtsLayerIdList.isEmpty() ) { #ifdef HAVE_SERVER_PYTHON_PLUGINS QgsAccessControl *accessControl = serverIface->accessControls(); #endif for ( const QString &lId : wmtsLayerIdList ) { QgsMapLayer *l = project->mapLayer( lId ); if ( !l ) { continue; } #ifdef HAVE_SERVER_PYTHON_PLUGINS if ( !accessControl->layerReadPermission( l ) ) { continue; } #endif QString layerLayerId = l->shortName(); if ( layerLayerId.isEmpty() ) { layerLayerId = l->name(); } wmtsLayerIds << layerLayerId; } } if ( !wmtsLayerIds.contains( layer ) ) { QString msg = QObject::tr( "Layer '%1' not found" ).arg( layer ); throw QgsBadRequestException( QStringLiteral( "LayerNotDefined" ), msg ); } //defining Format QString format = params.formatAsString(); //read Format if ( format.isEmpty() ) { throw QgsRequestNotWellFormedException( QStringLiteral( "Format is mandatory" ) ); } //defining TileMatrixSet ref QString tms_ref = params.tileMatrixSet(); //read TileMatrixSet if ( tms_ref.isEmpty() ) { throw QgsRequestNotWellFormedException( QStringLiteral( "TileMatrixSet is mandatory" ) ); } // verifying TileMatrixSet value QList< tileMatrixSetDef > tmsList = getTileMatrixSetList( project, tms_ref ); if ( tmsList.isEmpty() ) { throw QgsRequestNotWellFormedException( QStringLiteral( "TileMatrixSet is unknown" ) ); } tileMatrixSetDef tms = tmsList[0]; if ( tms.ref != tms_ref ) { throw QgsRequestNotWellFormedException( QStringLiteral( "TileMatrixSet is unknown" ) ); } //defining TileMatrix idx int tm_idx = params.tileMatrixAsInt(); //read TileMatrix if ( tm_idx < 0 || tms.tileMatrixList.count() < tm_idx ) { throw QgsRequestNotWellFormedException( QStringLiteral( "TileMatrix is unknown" ) ); } tileMatrixDef tm = tms.tileMatrixList.at( tm_idx ); //defining TileRow int tr = params.tileRowAsInt(); //read TileRow if ( tr < 0 || tm.row <= tr ) { throw QgsRequestNotWellFormedException( QStringLiteral( "TileRow is unknown" ) ); } //defining TileCol int tc = params.tileColAsInt(); //read TileCol if ( tc < 0 || tm.col <= tc ) { throw QgsRequestNotWellFormedException( QStringLiteral( "TileCol is unknown" ) ); } double res = tm.resolution; double minx = tm.left + tc * ( tileSize * res ); double miny = tm.top - ( tr + 1 ) * ( tileSize * res ); double maxx = tm.left + ( tc + 1 ) * ( tileSize * res ); double maxy = tm.top - tr * ( tileSize * res ); QString bbox; if ( tms.hasAxisInverted ) { bbox = qgsDoubleToString( miny, 6 ) + ',' + qgsDoubleToString( minx, 6 ) + ',' + qgsDoubleToString( maxy, 6 ) + ',' + qgsDoubleToString( maxx, 6 ); } else { bbox = qgsDoubleToString( minx, 6 ) + ',' + qgsDoubleToString( miny, 6 ) + ',' + qgsDoubleToString( maxx, 6 ) + ',' + qgsDoubleToString( maxy, 6 ); } QUrlQuery query; if ( !params.value( QStringLiteral( "MAP" ) ).isEmpty() ) { query.addQueryItem( QgsServerParameter::name( QgsServerParameter::MAP ), params.value( QStringLiteral( "MAP" ) ) ); } query.addQueryItem( QgsServerParameter::name( QgsServerParameter::SERVICE ), QStringLiteral( "WMS" ) ); query.addQueryItem( QgsServerParameter::name( QgsServerParameter::VERSION_SERVICE ), QStringLiteral( "1.3.0" ) ); query.addQueryItem( QgsServerParameter::name( QgsServerParameter::REQUEST ), request ); query.addQueryItem( QgsWmsParameterForWmts::name( QgsWmsParameterForWmts::LAYERS ), layer ); query.addQueryItem( QgsWmsParameterForWmts::name( QgsWmsParameterForWmts::STYLES ), QString() ); query.addQueryItem( QgsWmsParameterForWmts::name( QgsWmsParameterForWmts::CRS ), tms.ref ); query.addQueryItem( QgsWmsParameterForWmts::name( QgsWmsParameterForWmts::BBOX ), bbox ); query.addQueryItem( QgsWmsParameterForWmts::name( QgsWmsParameterForWmts::WIDTH ), QStringLiteral( "256" ) ); query.addQueryItem( QgsWmsParameterForWmts::name( QgsWmsParameterForWmts::HEIGHT ), QStringLiteral( "256" ) ); query.addQueryItem( QgsWmsParameterForWmts::name( QgsWmsParameterForWmts::FORMAT ), format ); if ( params.format() == QgsWmtsParameters::Format::PNG ) { query.addQueryItem( QgsWmsParameterForWmts::name( QgsWmsParameterForWmts::TRANSPARENT ), QStringLiteral( "true" ) ); } query.addQueryItem( QgsWmsParameterForWmts::name( QgsWmsParameterForWmts::DPI ), QStringLiteral( "96" ) ); return query; }
void QgsServerParameterDefinition::raiseError( const QString &msg ) { throw QgsBadRequestException( QStringLiteral( "Invalid Parameter" ), msg ); }