Skip to content

Commit 8f3532f

Browse files
Marcel Dancakmhugent
authored andcommittedMar 9, 2014
Mapserver: fixed inconsistencies int WMS GetFeatureInfo response
1 parent d0df28d commit 8f3532f

File tree

1 file changed

+123
-53
lines changed

1 file changed

+123
-53
lines changed
 

‎src/mapserver/qgswmsserver.cpp

Lines changed: 123 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,13 +1052,38 @@ int QgsWMSServer::getFeatureInfo( QDomDocument& result, QString version )
10521052

10531053
if ( featuresRect )
10541054
{
1055-
QDomElement bBoxElem = result.createElement( "BoundingBox" );
1056-
bBoxElem.setAttribute( "CRS", mMapRenderer->destinationCrs().authid() );
1057-
bBoxElem.setAttribute( "minx", QString::number( featuresRect->xMinimum() ) );
1058-
bBoxElem.setAttribute( "maxx", QString::number( featuresRect->xMaximum() ) );
1059-
bBoxElem.setAttribute( "miny", QString::number( featuresRect->yMinimum() ) );
1060-
bBoxElem.setAttribute( "maxy", QString::number( featuresRect->yMaximum() ) );
1061-
getFeatureInfoElement.insertBefore( bBoxElem, QDomNode() ); //insert as first child
1055+
if ( infoFormat.startsWith( "application/vnd.ogc.gml" ) )
1056+
{
1057+
QDomElement bBoxElem = result.createElement( "gml:boundedBy" );
1058+
QDomElement boxElem;
1059+
int gmlVersion = infoFormat.startsWith( "application/vnd.ogc.gml/3" ) ? 3 : 2;
1060+
if ( gmlVersion < 3 )
1061+
{
1062+
boxElem = QgsOgcUtils::rectangleToGMLBox( featuresRect, result );
1063+
}
1064+
else
1065+
{
1066+
boxElem = QgsOgcUtils::rectangleToGMLEnvelope( featuresRect, result );
1067+
}
1068+
1069+
QgsCoordinateReferenceSystem crs = mMapRenderer->destinationCrs();
1070+
if ( crs.isValid() )
1071+
{
1072+
boxElem.setAttribute( "srsName", crs.authid() );
1073+
}
1074+
bBoxElem.appendChild( boxElem );
1075+
getFeatureInfoElement.insertBefore( bBoxElem, QDomNode() ); //insert as first child
1076+
}
1077+
else
1078+
{
1079+
QDomElement bBoxElem = result.createElement( "BoundingBox" );
1080+
bBoxElem.setAttribute( "CRS", mMapRenderer->destinationCrs().authid() );
1081+
bBoxElem.setAttribute( "minx", QString::number( featuresRect->xMinimum() ) );
1082+
bBoxElem.setAttribute( "maxx", QString::number( featuresRect->xMaximum() ) );
1083+
bBoxElem.setAttribute( "miny", QString::number( featuresRect->yMinimum() ) );
1084+
bBoxElem.setAttribute( "maxy", QString::number( featuresRect->yMaximum() ) );
1085+
getFeatureInfoElement.insertBefore( bBoxElem, QDomNode() ); //insert as first child
1086+
}
10621087
}
10631088

10641089
if ( sia2045 && infoFormat.compare( "text/xml", Qt::CaseInsensitive ) == 0 )
@@ -1460,7 +1485,8 @@ int QgsWMSServer::featureInfoFromVectorLayer( QgsVectorLayer* layer,
14601485
const QSet<QString>& excludedAttributes = layer->excludeAttributesWMS();
14611486

14621487
QgsFeatureRequest fReq;
1463-
fReq.setFlags((( addWktGeometry || featureBBox ) ? QgsFeatureRequest::NoFlags : QgsFeatureRequest::NoGeometry ) | QgsFeatureRequest::ExactIntersect );
1488+
bool hasGeometry = addWktGeometry || featureBBox;
1489+
fReq.setFlags((( hasGeometry ) ? QgsFeatureRequest::NoFlags : QgsFeatureRequest::NoGeometry ) | QgsFeatureRequest::ExactIntersect );
14641490
if ( !searchRect.isEmpty() )
14651491
{
14661492
fReq.setFilterRect( searchRect );
@@ -1491,12 +1517,34 @@ int QgsWMSServer::featureInfoFromVectorLayer( QgsVectorLayer* layer,
14911517
continue;
14921518
}
14931519

1520+
QgsRectangle box;
1521+
if ( hasGeometry )
1522+
{
1523+
box = mapRender->layerExtentToOutputExtent( layer, feature.geometry()->boundingBox() );
1524+
if ( featureBBox ) //extend feature info bounding box if requested
1525+
{
1526+
if ( featureBBox->isEmpty() )
1527+
{
1528+
*featureBBox = box;
1529+
}
1530+
else
1531+
{
1532+
featureBBox->combineExtentWith( &box );
1533+
}
1534+
}
1535+
}
1536+
1537+
QgsCoordinateReferenceSystem outputCrs = layer->crs();
1538+
if ( hasGeometry && layer->crs() != mapRender->destinationCrs() && mapRender->hasCrsTransformEnabled() )
1539+
{
1540+
outputCrs = mapRender->destinationCrs();
1541+
}
1542+
14941543
if ( infoFormat == "application/vnd.ogc.gml" )
14951544
{
1496-
QgsCoordinateReferenceSystem layerCrs = layer->crs();
1497-
bool withGeom = layer->wkbType() != QGis::WKBNoGeometry;
1545+
bool withGeom = layer->wkbType() != QGis::WKBNoGeometry && addWktGeometry;
14981546
int version = infoFormat.startsWith( "application/vnd.ogc.gml/3" ) ? 3 : 2;
1499-
QDomElement elem = createFeatureGML( &feature, layer, infoDocument, layerCrs, layer->name(), withGeom, version );
1547+
QDomElement elem = createFeatureGML( &feature, layer, infoDocument, outputCrs, layer->name(), withGeom, version );
15001548
QDomElement featureMemberElem = infoDocument.createElement( "gml:featureMember"/*wfs:FeatureMember*/ );
15011549
featureMemberElem.appendChild( elem );
15021550
layerElement.appendChild( featureMemberElem );
@@ -1527,37 +1575,36 @@ int QgsWMSServer::featureInfoFromVectorLayer( QgsVectorLayer* layer,
15271575
featureElement.appendChild( attributeElement );
15281576
}
15291577

1530-
//also append the wkt geometry as an attribute
1531-
QgsGeometry* geom = feature.geometry();
1532-
if ( addWktGeometry && geom )
1533-
{
1534-
QDomElement geometryElement = infoDocument.createElement( "Attribute" );
1535-
geometryElement.setAttribute( "name", "geometry" );
1536-
geometryElement.setAttribute( "value", geom->exportToWkt() );
1537-
geometryElement.setAttribute( "type", "derived" );
1538-
featureElement.appendChild( geometryElement );
1539-
}
1540-
if ( featureBBox && geom && mapRender ) //extend feature info bounding box if requested
1578+
//append feature bounding box to feature info xml
1579+
if ( hasGeometry && mapRender )
15411580
{
1542-
QgsRectangle box = mapRender->layerExtentToOutputExtent( layer, geom->boundingBox() );
1543-
if ( featureBBox->isEmpty() )
1544-
{
1545-
*featureBBox = box;
1546-
}
1547-
else
1548-
{
1549-
featureBBox->combineExtentWith( &box );
1550-
}
1551-
1552-
//append feature bounding box to feature info xml
15531581
QDomElement bBoxElem = infoDocument.createElement( "BoundingBox" );
1554-
bBoxElem.setAttribute( version == "1.1.1" ? "SRS" : "CRS", mapRender->destinationCrs().authid() );
1582+
bBoxElem.setAttribute( version == "1.1.1" ? "SRS" : "CRS", outputCrs.authid() );
15551583
bBoxElem.setAttribute( "minx", QString::number( box.xMinimum() ) );
15561584
bBoxElem.setAttribute( "maxx", QString::number( box.xMaximum() ) );
15571585
bBoxElem.setAttribute( "miny", QString::number( box.yMinimum() ) );
15581586
bBoxElem.setAttribute( "maxy", QString::number( box.yMaximum() ) );
15591587
featureElement.appendChild( bBoxElem );
15601588
}
1589+
1590+
//also append the wkt geometry as an attribute
1591+
if ( addWktGeometry && hasGeometry )
1592+
{
1593+
QgsGeometry* geom = feature.geometry();
1594+
if ( layer->crs() != outputCrs )
1595+
{
1596+
const QgsCoordinateTransform *transform = mapRender->transformation( layer );
1597+
if ( transform )
1598+
{
1599+
geom->transform( *transform );
1600+
}
1601+
}
1602+
QDomElement geometryElement = infoDocument.createElement( "Attribute" );
1603+
geometryElement.setAttribute( "name", "geometry" );
1604+
geometryElement.setAttribute( "value", geom->exportToWkt() );
1605+
geometryElement.setAttribute( "type", "derived" );
1606+
featureElement.appendChild( geometryElement );
1607+
}
15611608
}
15621609
}
15631610

@@ -2510,10 +2557,50 @@ QDomElement QgsWMSServer::createFeatureGML(
25102557
QDomElement typeNameElement = doc.createElement( "qgs:" + typeName /*qgs:%TYPENAME%*/ );
25112558
typeNameElement.setAttribute( "fid", typeName + "." + QString::number( feat->id() ) );
25122559

2513-
if ( withGeom )
2560+
const QgsCoordinateTransform* transform = 0;
2561+
if ( layer && layer->crs() != crs)
2562+
{
2563+
transform = mMapRenderer->transformation( layer );
2564+
}
2565+
2566+
QgsGeometry* geom = feat->geometry();
2567+
2568+
// always add bounding box info if feature contains geometry
2569+
if ( geom && geom->type() != QGis::UnknownGeometry && geom->type() != QGis::NoGeometry)
2570+
{
2571+
QgsRectangle box = feat->geometry()->boundingBox();
2572+
if ( transform )
2573+
{
2574+
box = transform->transformBoundingBox( box );
2575+
}
2576+
2577+
QDomElement bbElem = doc.createElement( "gml:boundedBy" );
2578+
QDomElement boxElem;
2579+
if ( version < 3 )
2580+
{
2581+
boxElem = QgsOgcUtils::rectangleToGMLBox( &box, doc );
2582+
}
2583+
else
2584+
{
2585+
boxElem = QgsOgcUtils::rectangleToGMLEnvelope( &box, doc );
2586+
}
2587+
2588+
if ( crs.isValid() )
2589+
{
2590+
boxElem.setAttribute( "srsName", crs.authid() );
2591+
}
2592+
bbElem.appendChild( boxElem );
2593+
typeNameElement.appendChild( bbElem );
2594+
}
2595+
2596+
if ( withGeom)
25142597
{
25152598
//add geometry column (as gml)
2516-
QgsGeometry* geom = feat->geometry();
2599+
2600+
if ( transform )
2601+
{
2602+
geom->transform( *transform );
2603+
}
25172604

25182605
QDomElement geomElem = doc.createElement( "qgs:geometry" );
25192606
QDomElement gmlElem;
@@ -2528,27 +2615,10 @@ QDomElement QgsWMSServer::createFeatureGML(
25282615

25292616
if ( !gmlElem.isNull() )
25302617
{
2531-
QgsRectangle box = geom->boundingBox();
2532-
QDomElement bbElem = doc.createElement( "gml:boundedBy" );
2533-
QDomElement boxElem;
2534-
if ( version < 3 )
2535-
{
2536-
boxElem = QgsOgcUtils::rectangleToGMLBox( &box, doc );
2537-
}
2538-
else
2539-
{
2540-
boxElem = QgsOgcUtils::rectangleToGMLEnvelope( &box, doc );
2541-
}
2542-
25432618
if ( crs.isValid() )
25442619
{
2545-
boxElem.setAttribute( "srsName", crs.authid() );
25462620
gmlElem.setAttribute( "srsName", crs.authid() );
25472621
}
2548-
2549-
bbElem.appendChild( boxElem );
2550-
typeNameElement.appendChild( bbElem );
2551-
25522622
geomElem.appendChild( gmlElem );
25532623
typeNameElement.appendChild( geomElem );
25542624
}

0 commit comments

Comments
 (0)
Please sign in to comment.