Skip to content

Commit

Permalink
Output bounding boxes of wms groups in project CRS instead of WGS84
Browse files Browse the repository at this point in the history
  • Loading branch information
marco committed Sep 2, 2011
1 parent 901485f commit 372696e
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/mapserver/qgsconfigparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void QgsConfigParser::addExternalGMLData( const QString& layerName, QDomDocument
mExternalGMLDatasets.insert( layerName, gmlDoc );
}

void QgsConfigParser::appendExGeographicBoundingBox( QDomElement& layerElem,
void QgsConfigParser::appendLayerBoundingBoxes( QDomElement& layerElem,
QDomDocument& doc,
const QgsRectangle& layerExtent,
const QgsCoordinateReferenceSystem& layerCRS ) const
Expand Down
4 changes: 2 additions & 2 deletions src/mapserver/qgsconfigparser.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ class QgsConfigParser
double mLegendSymbolWidth;
double mLegendSymbolHeight;

/**Transforms layer extent to epsg 4326 and appends ExGeographicBoundingBox element to layer element*/
void appendExGeographicBoundingBox( QDomElement& layerElem, QDomDocument& doc, const QgsRectangle& layerExtent, const QgsCoordinateReferenceSystem& layerCRS ) const;
/**Transforms layer extent to epsg 4326 and appends ExGeographicBoundingBox and BoundingBox elements to the layer element*/
void appendLayerBoundingBoxes( QDomElement& layerElem, QDomDocument& doc, const QgsRectangle& layerExtent, const QgsCoordinateReferenceSystem& layerCRS ) const;
/**Returns the <Ex_GeographicalBoundingBox of a layer element as a rectangle
@param layerElement <Layer> element in capabilities
@param rect out: bounding box as rectangle
Expand Down
103 changes: 95 additions & 8 deletions src/mapserver/qgsprojectparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ void QgsProjectParser::addLayers( QDomDocument &doc,
appendCRSElementsToLayer( layerElem, doc, crsList );

//Ex_GeographicBoundingBox
appendExGeographicBoundingBox( layerElem, doc, currentLayer->extent(), currentLayer->crs() );
appendLayerBoundingBoxes( layerElem, doc, currentLayer->extent(), currentLayer->crs() );

//only one default style in project file mode
QDomElement styleElem = doc.createElement( "Style" );
Expand All @@ -250,7 +250,7 @@ void QgsProjectParser::addLayers( QDomDocument &doc,

void QgsProjectParser::combineExtentAndCrsOfGroupChildren( QDomElement& groupElem, QDomDocument& doc ) const
{
QgsRectangle combinedGeographicBBox;
QgsRectangle combinedBBox;
QSet<QString> combinedCRSSet;
bool firstBBox = true;
bool firstCRSSet = true;
Expand All @@ -263,17 +263,17 @@ void QgsProjectParser::combineExtentAndCrsOfGroupChildren( QDomElement& groupEle
if ( childElem.tagName() != "Layer" )
continue;

QgsRectangle bbox;
if ( exGeographicBoundingBox( childElem, bbox ) )
QgsRectangle bbox = layerBoundingBoxInProjectCRS( childElem );
if ( !bbox.isEmpty() )
{
if ( firstBBox )
{
combinedGeographicBBox = bbox;
combinedBBox = bbox;
firstBBox = false;
}
else
{
combinedGeographicBBox.combineExtentWith( &bbox );
combinedBBox.combineExtentWith( &bbox );
}
}

Expand All @@ -295,8 +295,8 @@ void QgsProjectParser::combineExtentAndCrsOfGroupChildren( QDomElement& groupEle

appendCRSElementsToLayer( groupElem, doc, combinedCRSSet.toList() );

const QgsCoordinateReferenceSystem& groupCRS = QgsEPSGCache::instance()->searchCRS( GEO_EPSG_CRS_ID );
appendExGeographicBoundingBox( groupElem, doc, combinedGeographicBBox, groupCRS );
const QgsCoordinateReferenceSystem& groupCRS = projectCRS();
appendLayerBoundingBoxes( groupElem, doc, combinedBBox, groupCRS );
}

QList<QgsMapLayer*> QgsProjectParser::mapLayerFromStyle( const QString& lName, const QString& styleName, bool allowCaching ) const
Expand Down Expand Up @@ -1441,3 +1441,90 @@ void QgsProjectParser::setSelectionColor()
QgsRenderer::setSelectionColor( QColor( red, green, blue, alpha ) );
}

const QgsCoordinateReferenceSystem& QgsProjectParser::projectCRS() const
{
//mapcanvas->destinationsrs->spatialrefsys->authid
if ( mXMLDoc )
{
QDomElement authIdElem = mXMLDoc->documentElement().firstChildElement( "mapcanvas" ).firstChildElement( "destinationsrs" ).
firstChildElement( "spatialrefsys" ).firstChildElement( "authid" );
if ( !authIdElem.isNull() )
{
QString authId = authIdElem.text();
QStringList authIdSplit = authId.split( ":" );
if ( authIdSplit.size() == 2 && authIdSplit.at( 0 ).compare( "EPSG", Qt::CaseInsensitive ) == 0 )
{
bool ok;
int id = authIdSplit.at( 1 ).toInt( &ok );
if ( ok )
{
return QgsEPSGCache::instance()->searchCRS( id );
}
}
}
}
return QgsEPSGCache::instance()->searchCRS( GEO_EPSG_CRS_ID );
}

QgsRectangle QgsProjectParser::layerBoundingBoxInProjectCRS( const QDomElement& layerElem ) const
{
QgsRectangle BBox;
if ( layerElem.isNull() )
{
return BBox;
}

//read box coordinates and layer auth. id
QDomElement boundingBoxElem = layerElem.firstChildElement( "BoundingBox" );
if ( boundingBoxElem.isNull() )
{
return BBox;
}

double minx, miny, maxx, maxy;
bool conversionOk;
minx = boundingBoxElem.attribute( "minx" ).toDouble( &conversionOk );
if ( !conversionOk )
{
return BBox;
}
miny = boundingBoxElem.attribute( "miny" ).toDouble( &conversionOk );
if ( !conversionOk )
{
return BBox;
}
maxx = boundingBoxElem.attribute( "maxx" ).toDouble( &conversionOk );
if ( !conversionOk )
{
return BBox;
}
maxy = boundingBoxElem.attribute( "maxy" ).toDouble( &conversionOk );
if ( !conversionOk )
{
return BBox;
}

int authId;
QString authIdString = boundingBoxElem.attribute( "CRS" );
QStringList authIdSplit = authIdString.split( ":" );
if ( authIdSplit.size() < 2 )
{
return BBox;
}
authId = authIdSplit.at( 1 ).toInt( &conversionOk );
if ( !conversionOk )
{
return BBox;
}

//create layer crs
const QgsCoordinateReferenceSystem& layerCrs = QgsEPSGCache::instance()->searchCRS( authId );

//get project crs
const QgsCoordinateReferenceSystem& projectCrs = projectCRS();
QgsCoordinateTransform t( layerCrs, projectCrs );

//transform
BBox = t.transformBoundingBox( QgsRectangle( minx, miny, maxx, maxy ) );
return BBox;
}
6 changes: 6 additions & 0 deletions src/mapserver/qgsprojectparser.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,12 @@ class QgsProjectParser: public QgsConfigParser

/**Sets global selection color from the project or yellow if not defined in project*/
void setSelectionColor();

/**Returns mapcanvas output CRS from project file*/
const QgsCoordinateReferenceSystem& projectCRS() const;

/**Returns bbox of layer in project CRS (or empty rectangle in case of error)*/
QgsRectangle layerBoundingBoxInProjectCRS( const QDomElement& layerElem ) const;
};

#endif // QGSPROJECTPARSER_H
2 changes: 1 addition & 1 deletion src/mapserver/qgssldparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ void QgsSLDParser::layersAndStylesCapabilities( QDomElement& parentElement, QDom
//append geographic bbox and the CRS elements
QStringList crsNumbers = createCRSListForLayer( theMapLayer );
appendCRSElementsToLayer( layerElement, doc, crsNumbers );
appendExGeographicBoundingBox( layerElement, doc, theMapLayer->extent(), theMapLayer->crs() );
appendLayerBoundingBoxes( layerElement, doc, theMapLayer->extent(), theMapLayer->crs() );

//iterate over all <UserStyle> nodes within a user layer
QDomNodeList userStyleList = layerNodeList.item( i ).toElement().elementsByTagName( "UserStyle" );
Expand Down

0 comments on commit 372696e

Please sign in to comment.