Skip to content

Commit

Permalink
Merge pull request #3812 from mhugent/wms_1_3_compliance
Browse files Browse the repository at this point in the history
Wms 1 3 compliance
  • Loading branch information
mhugent committed Dec 1, 2016
2 parents 3bd8a29 + 898ca57 commit d2e2110
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 26 deletions.
12 changes: 11 additions & 1 deletion src/server/qgsconfigparserutils.cpp
Expand Up @@ -65,6 +65,9 @@ void QgsConfigParserUtils::appendCrsElementsToLayer( QDomElement& layerElement,
appendCrsElementToLayer( layerElement, CRSPrecedingElement, crs, doc );
}
}

//Support for CRS:84 is mandatory (equals EPSG:4326 with reversed axis)
appendCrsElementToLayer( layerElement, CRSPrecedingElement, QString( "CRS:84" ), doc );
}

void QgsConfigParserUtils::appendCrsElementToLayer( QDomElement& layerElement, const QDomElement& precedingElement,
Expand All @@ -77,14 +80,21 @@ void QgsConfigParserUtils::appendCrsElementToLayer( QDomElement& layerElement, c
layerElement.insertAfter( crsElement, precedingElement );
}

void QgsConfigParserUtils::appendLayerBoundingBoxes( QDomElement& layerElem, QDomDocument& doc, const QgsRectangle& layerExtent,
void QgsConfigParserUtils::appendLayerBoundingBoxes( QDomElement& layerElem, QDomDocument& doc, const QgsRectangle& lExtent,
const QgsCoordinateReferenceSystem& layerCRS, const QStringList &crsList, const QStringList& constrainedCrsList )
{
if ( layerElem.isNull() )
{
return;
}

QgsRectangle layerExtent = lExtent;
if ( qgsDoubleNear( layerExtent.xMinimum(), layerExtent.xMaximum() ) || qgsDoubleNear( layerExtent.yMinimum(), layerExtent.yMaximum() ) )
{
//layer bbox cannot be empty
layerExtent.grow( 0.000001 );
}

QgsCoordinateReferenceSystem wgs84 = QgsCoordinateReferenceSystem::fromOgcWmsCrs( GEO_EPSG_CRS_AUTHID );

QString version = doc.documentElement().attribute( QStringLiteral( "version" ) );
Expand Down
2 changes: 1 addition & 1 deletion src/server/qgsserverprojectparser.cpp
Expand Up @@ -586,7 +586,7 @@ void QgsServerProjectParser::serviceCapabilities( QDomElement& parentElement, QD
//Fees
QDomElement feesElem = propertiesElement.firstChildElement( QStringLiteral( "WMSFees" ) );
QDomElement wmsFeesElem = doc.createElement( QStringLiteral( "Fees" ) );
QDomText wmsFeesText = doc.createTextNode( QStringLiteral( "conditions unknown" ) ); // default value if access conditions are unknown
QDomText wmsFeesText = doc.createTextNode( QStringLiteral( "None" ) ); // default value if access conditions are unknown
if ( !feesElem.isNull() && !feesElem.text().isEmpty() )
{
wmsFeesText = doc.createTextNode( feesElem.text() );
Expand Down
97 changes: 92 additions & 5 deletions src/server/qgswmsserver.cpp
Expand Up @@ -427,6 +427,12 @@ QDomDocument QgsWmsServer::getCapabilities( const QString& version, bool fullPro
hrefString = serviceUrl();
}

//href needs to be a prefix
if ( !hrefString.endsWith( "?" ) && !hrefString.endsWith( "&" ) )
{
hrefString.append( hrefString.contains( "?" ) ? "&" : "?" );
}

if ( version == QLatin1String( "1.1.1" ) )
{
doc = QDomDocument( QStringLiteral( "WMT_MS_Capabilities SYSTEM 'http://schemas.opengis.net/wms/1.1.1/WMS_MS_Capabilities.dtd'" ) ); //WMS 1.1.1 needs DOCTYPE "SYSTEM http://schemas.opengis.net/wms/1.1.1/WMS_MS_Capabilities.dtd"
Expand Down Expand Up @@ -560,7 +566,7 @@ QDomDocument QgsWmsServer::getCapabilities( const QString& version, bool fullPro

//Exception element is mandatory
elem = doc.createElement( QStringLiteral( "Exception" ) );
appendFormats( doc, elem, QStringList() << ( version == QLatin1String( "1.1.1" ) ? "application/vnd.ogc.se_xml" : "text/xml" ) );
appendFormats( doc, elem, QStringList() << ( version == QLatin1String( "1.1.1" ) ? "application/vnd.ogc.se_xml" : "XML" ) );
capabilityElement.appendChild( elem );

//UserDefinedSymbolization element
Expand Down Expand Up @@ -694,6 +700,10 @@ static QgsRectangle _parseBBOX( const QString &bboxStr, bool &ok )
}

ok = true;
if ( d[2] <= d[0] || d[3] <= d[1] )
{
throw QgsMapServiceException( "InvalidParameterValue", "BBOX is empty" );
}
return QgsRectangle( d[0], d[1], d[2], d[3] );
}

Expand Down Expand Up @@ -1460,6 +1470,17 @@ QImage* QgsWmsServer::getMap( HitTest* hitTest )
// theImage->save( QDir::tempPath() + QDir::separator() + "lastrender.png" );
//#endif

thePainter.end();

//test if width / height ratio of image is the same as the ratio of WIDTH / HEIGHT parameters. If not, the image has to be scaled (required by WMS spec)
int widthParam = mParameters.value( "WIDTH", "0" ).toInt();
int heightParam = mParameters.value( "HEIGHT", "0" ).toInt();
if ( widthParam != theImage->width() || heightParam != theImage->height() )
{
//scale image
*theImage = theImage->scaled( widthParam, heightParam, Qt::IgnoreAspectRatio, Qt::SmoothTransformation );
}

return theImage;
}

Expand Down Expand Up @@ -1581,7 +1602,6 @@ int QgsWmsServer::getFeatureInfo( QDomDocument& result, const QString& version )
QgsRectangle mapExtent = mMapRenderer->extent();
double scaleDenominator = scaleCalc.calculate( mapExtent, outputImage->width() );
mConfigParser->setScaleDenominator( scaleDenominator );
delete outputImage; //no longer needed for feature info

//read FEATURE_COUNT
int featureCount = 1;
Expand Down Expand Up @@ -1621,6 +1641,16 @@ int QgsWmsServer::getFeatureInfo( QDomDocument& result, const QString& version )
j = -1;
}

//In case the output image is distorted (WIDTH/HEIGHT ratio not equal to BBOX width/height), I and J need to be adapted as well
int widthParam = mParameters.value( "WIDTH", "-1" ).toInt();
int heightParam = mParameters.value( "HEIGHT", "-1" ).toInt();
if (( i != -1 && j != -1 && widthParam != -1 && heightParam != -1 ) && ( widthParam != outputImage->width() || heightParam != outputImage->height() ) )
{
i *= ( outputImage->width() / ( double )widthParam );
j *= ( outputImage->height() / ( double )heightParam );
}
delete outputImage; //no longer needed for feature info

//Normally, I/J or X/Y are mandatory parameters.
//However, in order to make attribute only queries via the FILTER parameter, it is allowed to skip them if the FILTER parameter is there

Expand Down Expand Up @@ -1962,6 +1992,29 @@ QImage* QgsWmsServer::createImage( int width, int height ) const
}
}

//Adapt width / height if the aspect ratio does not correspond with the BBOX.
//Required by WMS spec. 1.3.
bool bboxOk;
QgsRectangle mapExtent = _parseBBOX( mParameters.value( "BBOX" ), bboxOk );
if ( bboxOk )
{
double mapWidthHeightRatio = mapExtent.width() / mapExtent.height();
double imageWidthHeightRatio = ( double )width / ( double )height;
if ( !qgsDoubleNear( mapWidthHeightRatio, imageWidthHeightRatio, 0.0001 ) )
{
if ( mapWidthHeightRatio >= imageWidthHeightRatio )
{
//decrease image height
height = width / mapWidthHeightRatio;
}
else
{
//decrease image width
width = height * mapWidthHeightRatio;
}
}
}

if ( width < 0 || height < 0 )
{
return nullptr;
Expand All @@ -1978,6 +2031,19 @@ QImage* QgsWmsServer::createImage( int width, int height ) const
//transparent parameter
bool transparent = mParameters.value( QStringLiteral( "TRANSPARENT" ) ).compare( QLatin1String( "true" ), Qt::CaseInsensitive ) == 0;

//background color
QString bgColorString = mParameters.value( "BGCOLOR" );
if ( bgColorString.startsWith( "0x", Qt::CaseInsensitive ) )
{
bgColorString.replace( 0, 2, "#" );
}
QColor backgroundColor;
backgroundColor.setNamedColor( bgColorString );
if ( !backgroundColor.isValid() )
{
backgroundColor = QColor( Qt::white );
}

//use alpha channel only if necessary because it slows down performance
if ( transparent && !jpeg )
{
Expand All @@ -1987,7 +2053,7 @@ QImage* QgsWmsServer::createImage( int width, int height ) const
else
{
theImage = new QImage( width, height, QImage::Format_RGB32 );
theImage->fill( qRgb( 255, 255, 255 ) );
theImage->fill( backgroundColor );
}

if ( !theImage )
Expand Down Expand Up @@ -2023,17 +2089,32 @@ int QgsWmsServer::configureMapRender( const QPaintDevice* paintDevice ) const
mMapRenderer->setOutputSize( QSize( paintDevice->width(), paintDevice->height() ), paintDevice->logicalDpiX() );

//map extent
bool bboxOk;
QgsRectangle mapExtent = _parseBBOX( mParameters.value( QStringLiteral( "BBOX" ), QStringLiteral( "0,0,0,0" ) ), bboxOk );
bool bboxOk = true;
QgsRectangle mapExtent;
if ( mParameters.contains( "BBOX" ) )
{
mapExtent = _parseBBOX( mParameters.value( QStringLiteral( "BBOX" ), QStringLiteral( "0,0,0,0" ) ), bboxOk );
}

if ( !bboxOk )
{
//throw a service exception
throw QgsMapServiceException( QStringLiteral( "InvalidParameterValue" ), QStringLiteral( "Invalid BBOX parameter" ) );
}

if ( mParameters.contains( "BBOX" ) && mapExtent.isEmpty() )
{
throw QgsMapServiceException( "InvalidParameterValue", "BBOX is empty" );
}

QgsUnitTypes::DistanceUnit mapUnits = QgsUnitTypes::DistanceDegrees;

QString crs = mParameters.value( QStringLiteral( "CRS" ), mParameters.value( QStringLiteral( "SRS" ) ) );
if ( crs.compare( "CRS:84", Qt::CaseInsensitive ) == 0 )
{
crs = QString( "EPSG:4326" );
mapExtent.invert();
}

QgsCoordinateReferenceSystem outputCRS;

Expand Down Expand Up @@ -2160,6 +2241,12 @@ bool QgsWmsServer::infoPointToMapCoordinates( int i, int j, QgsPoint* infoPoint,
return false;
}

//check if i, j are in the pixel range of the image
if ( i < 0 || i > mapRenderer->width() || j < 0 || j > mapRenderer->height() )
{
throw QgsMapServiceException( "InvalidPoint", "I/J parameters not within the pixel range" );
}

double xRes = mapRenderer->extent().width() / mapRenderer->width();
double yRes = mapRenderer->extent().height() / mapRenderer->height();
infoPoint->setX( mapRenderer->extent().xMinimum() + i * xRes + xRes / 2.0 );
Expand Down
32 changes: 16 additions & 16 deletions tests/src/python/test_qgsserver_accesscontrol.py
Expand Up @@ -306,7 +306,7 @@ def test_wms_getmap(self):
"LAYERS": "Country,Hello",
"STYLES": "",
"FORMAT": "image/png",
"BBOX": "-16817707,-4710778,5696513,14587125",
"BBOX": "-16817707,-6318936.5,5696513,16195283.5",
"HEIGHT": "500",
"WIDTH": "500",
"SRS": "EPSG:3857"
Expand All @@ -323,7 +323,7 @@ def test_wms_getmap(self):
"LAYERS": "Hello",
"STYLES": "",
"FORMAT": "image/png",
"BBOX": "-16817707,-4710778,5696513,14587125",
"BBOX": "-16817707,-6318936.5,5696513,16195283.5",
"HEIGHT": "500",
"WIDTH": "500",
"SRS": "EPSG:3857"
Expand All @@ -339,7 +339,7 @@ def test_wms_getmap(self):
"LAYERS": "Country",
"STYLES": "",
"FORMAT": "image/png",
"BBOX": "-16817707,-4710778,5696513,14587125",
"BBOX": "-16817707,-6318936.5,5696513,16195283.5",
"HEIGHT": "500",
"WIDTH": "500",
"SRS": "EPSG:3857"
Expand All @@ -363,7 +363,7 @@ def test_wms_getfeatureinfo_hello(self):
"QUERY_LAYERS": "Hello",
"STYLES": "",
"FORMAT": "image/png",
"BBOX": "-16817707,-4710778,5696513,14587125",
"BBOX": "-16817707,-6318936.5,5696513,16195283.5",
"HEIGHT": "500",
"WIDTH": "500",
"SRS": "EPSG:3857",
Expand Down Expand Up @@ -402,7 +402,7 @@ def test_wms_getfeatureinfo_hello2(self):
"QUERY_LAYERS": "Hello",
"STYLES": "",
"FORMAT": "image/png",
"BBOX": "-16817707,-4710778,5696513,14587125",
"BBOX": "-16817707,-6318936.5,5696513,16195283.5",
"HEIGHT": "500",
"WIDTH": "500",
"SRS": "EPSG:3857",
Expand Down Expand Up @@ -432,7 +432,7 @@ def test_wms_getfeatureinfo_country(self):
"QUERY_LAYERS": "Country",
"STYLES": "",
"FORMAT": "image/png",
"BBOX": "-16817707,-4710778,5696513,14587125",
"BBOX": "-16817707,-6318936.5,5696513,16195283.5",
"HEIGHT": "500",
"WIDTH": "500",
"SRS": "EPSG:3857",
Expand Down Expand Up @@ -854,7 +854,7 @@ def test_wms_getmap_subsetstring(self):
"LAYERS": "Country,Hello_SubsetString",
"STYLES": "",
"FORMAT": "image/png",
"BBOX": "-16817707,-4710778,5696513,14587125",
"BBOX": "-16817707,-6318936.5,5696513,16195283.5",
"HEIGHT": "500",
"WIDTH": "500",
"SRS": "EPSG:3857"
Expand All @@ -871,7 +871,7 @@ def test_wms_getmap_subsetstring(self):
"LAYERS": "Hello_SubsetString",
"STYLES": "",
"FORMAT": "image/png",
"BBOX": "-16817707,-4710778,5696513,14587125",
"BBOX": "-16817707,-6318936.5,5696513,16195283.5",
"HEIGHT": "500",
"WIDTH": "500",
"SRS": "EPSG:3857"
Expand All @@ -894,7 +894,7 @@ def test_wms_getmap_subsetstring_with_filter(self):
"FILTER": "Hello_Filter_SubsetString:\"pkuid\" IN ( 7 , 8 )",
"STYLES": "",
"FORMAT": "image/png",
"BBOX": "-16817707,-4710778,5696513,14587125",
"BBOX": "-16817707,-6318936.5,5696513,16195283.5",
"HEIGHT": "500",
"WIDTH": "500",
"SRS": "EPSG:3857"
Expand All @@ -912,7 +912,7 @@ def test_wms_getmap_subsetstring_with_filter(self):
"FILTER": "Hello_Filter_SubsetString:\"pkuid\" IN ( 7 , 8 )",
"STYLES": "",
"FORMAT": "image/png",
"BBOX": "-16817707,-4710778,5696513,14587125",
"BBOX": "-16817707,-6318936.5,5696513,16195283.5",
"HEIGHT": "500",
"WIDTH": "500",
"SRS": "EPSG:3857"
Expand All @@ -931,7 +931,7 @@ def test_wms_getmap_projectsubsetstring(self):
"LAYERS": "Hello_Project_SubsetString",
"STYLES": "",
"FORMAT": "image/png",
"BBOX": "-16817707,-4710778,5696513,14587125",
"BBOX": "-16817707,-6318936.5,5696513,16195283.5",
"HEIGHT": "500",
"WIDTH": "500",
"SRS": "EPSG:3857"
Expand All @@ -948,7 +948,7 @@ def test_wms_getmap_projectsubsetstring(self):
"LAYERS": "Hello_Project_SubsetString",
"STYLES": "",
"FORMAT": "image/png",
"BBOX": "-16817707,-4710778,5696513,14587125",
"BBOX": "-16817707,-6318936.5,5696513,16195283.5",
"HEIGHT": "500",
"WIDTH": "500",
"SRS": "EPSG:3857"
Expand All @@ -966,7 +966,7 @@ def test_wms_getfeatureinfo_subsetstring(self):
"QUERY_LAYERS": "Hello_SubsetString",
"STYLES": "",
"FORMAT": "image/png",
"BBOX": "-16817707,-4710778,5696513,14587125",
"BBOX": "-16817707,-6318936.5,5696513,16195283.5",
"HEIGHT": "500",
"WIDTH": "500",
"SRS": "EPSG:3857",
Expand Down Expand Up @@ -1002,7 +1002,7 @@ def test_wms_getfeatureinfo_subsetstring2(self):
"QUERY_LAYERS": "Hello_SubsetString",
"STYLES": "",
"FORMAT": "image/png",
"BBOX": "-16817707,-4710778,5696513,14587125",
"BBOX": "-16817707,-6318936.5,5696513,16195283.5",
"HEIGHT": "500",
"WIDTH": "500",
"SRS": "EPSG:3857",
Expand Down Expand Up @@ -1038,7 +1038,7 @@ def test_wms_getfeatureinfo_projectsubsetstring(self):
"QUERY_LAYERS": "Hello_Project_SubsetString",
"STYLES": "",
"FORMAT": "image/png",
"BBOX": "-16817707,-4710778,5696513,14587125",
"BBOX": "-16817707,-6318936.5,5696513,16195283.5",
"HEIGHT": "500",
"WIDTH": "500",
"SRS": "EPSG:3857",
Expand Down Expand Up @@ -1147,7 +1147,7 @@ def test_wms_getfeatureinfo_subsetstring_with_filter(self):
"FILTER": "Hello_Filter_SubsetString:\"pkuid\" IN ( 7 , 8 )",
"STYLES": "",
"FORMAT": "image/png",
"BBOX": "-16817707,-4710778,5696513,14587125",
"BBOX": "-16817707,-6318936.5,5696513,16195283.5",
"HEIGHT": "500",
"WIDTH": "500",
"SRS": "EPSG:3857",
Expand Down
4 changes: 3 additions & 1 deletion tests/testdata/qgis_server/getcapabilities.txt
Expand Up @@ -97,11 +97,12 @@ Content-Type: text/xml; charset=utf-8
</qgs:GetStyles>
</Request>
<Exception>
<Format>text/xml</Format>
<Format>XML</Format>
</Exception>
<Layer queryable="1">
<Name>QGIS Test Project</Name>
<Title>QGIS Test Project</Title>
<CRS>CRS:84</CRS>
<CRS>EPSG:4326</CRS>
<CRS>EPSG:3857</CRS>
<EX_GeographicBoundingBox>
Expand All @@ -116,6 +117,7 @@ Content-Type: text/xml; charset=utf-8
<Name>testlayer èé</Name>
<Title>A test vector layer</Title>
<Abstract>A test vector layer with unicode òà</Abstract>
<CRS>CRS:84</CRS>
<CRS>EPSG:4326</CRS>
<CRS>EPSG:3857</CRS>
<EX_GeographicBoundingBox>
Expand Down

0 comments on commit d2e2110

Please sign in to comment.