QGIS API Documentation  3.14.0-Pi (9f7028fd23)
qgswmsgetfeatureinfo.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgswmsgetfeatureinfo.cpp
3  -------------------------
4  begin : December 20 , 2016
5  copyright : (C) 2007 by Marco Hugentobler (original code)
6  (C) 2014 by Alessandro Pasotti (original code)
7  (C) 2016 by David Marteau
8  email : marco dot hugentobler at karto dot baug dot ethz dot ch
9  a dot pasotti at itopen dot it
10  david dot marteau at 3liz dot com
11  ***************************************************************************/
12 
13 /***************************************************************************
14  * *
15  * This program is free software; you can redistribute it and/or modify *
16  * it under the terms of the GNU General Public License as published by *
17  * the Free Software Foundation; either version 2 of the License, or *
18  * (at your option) any later version. *
19  * *
20  ***************************************************************************/
21 #include "qgswmsutils.h"
22 #include "qgswmsgetfeatureinfo.h"
23 #include "qgswmsrenderer.h"
24 
25 namespace QgsWms
26 {
27  void writeGetFeatureInfo( QgsServerInterface *serverIface, const QgsProject *project,
28  const QString &version, const QgsServerRequest &request,
29  QgsServerResponse &response )
30  {
31  // get wms parameters from query
32  QgsWmsParameters parameters( QUrlQuery( request.url() ) );
33 
34  // WIDTH and HEIGHT are not mandatory, but we need to set a default size
35  if ( ( parameters.widthAsInt() <= 0
36  || parameters.heightAsInt() <= 0 )
37  && ! parameters.infoFormatIsImage() )
38  {
39  QSize size( 10, 10 );
40 
41  if ( ! parameters.filterGeom().isEmpty() )
42  {
43  const QgsRectangle bbox = QgsGeometry::fromWkt( parameters.filterGeom() ).boundingBox();
44  const int defaultWidth = 800;
45  size.setWidth( defaultWidth );
46  size.setHeight( defaultWidth * bbox.height() / bbox.width() );
47  }
48 
49  parameters.set( QgsWmsParameter::WIDTH, size.width() );
50  parameters.set( QgsWmsParameter::HEIGHT, size.height() );
51  }
52 
53  // prepare render context
54  QgsWmsRenderContext context( project, serverIface );
55  context.setFlag( QgsWmsRenderContext::AddQueryLayers );
56  context.setFlag( QgsWmsRenderContext::UseFilter );
58  context.setFlag( QgsWmsRenderContext::SetAccessControl );
59  context.setParameters( parameters );
60 
61  const QString infoFormat = request.parameters().value( QStringLiteral( "INFO_FORMAT" ), QStringLiteral( "text/plain" ) );
62  response.setHeader( QStringLiteral( "Content-Type" ), infoFormat + QStringLiteral( "; charset=utf-8" ) );
63 
64  QgsRenderer renderer( context );
65  response.write( renderer.getFeatureInfo( version ) );
66  }
67 } // namespace QgsWms
QgsServerRequest::parameters
QgsServerRequest::Parameters parameters() const
Returns a map of query parameters with keys converted to uppercase.
Definition: qgsserverrequest.cpp:85
qgswmsrenderer.h
qgswmsutils.h
QgsWms::QgsWmsRenderContext::UseFilter
@ UseFilter
Definition: qgswmsrendercontext.h:55
QgsServerRequest
Definition: qgsserverrequest.h:38
QgsWms::QgsWmsRenderContext::AddQueryLayers
@ AddQueryLayers
Definition: qgswmsrendercontext.h:60
QgsWms::QgsWmsParameter::WIDTH
@ WIDTH
Definition: qgswmsparameters.h:123
QgsRectangle
Definition: qgsrectangle.h:41
QgsWms::writeGetFeatureInfo
void writeGetFeatureInfo(QgsServerInterface *serverIface, const QgsProject *project, const QString &version, const QgsServerRequest &request, QgsServerResponse &response)
Output GetFeatureInfo response.
Definition: qgswmsgetfeatureinfo.cpp:44
QgsProject
Definition: qgsproject.h:92
qgswmsgetfeatureinfo.h
QgsServerResponse::write
virtual void write(const QString &data)
Write string This is a convenient method that will write directly to the underlying I/O device.
Definition: qgsserverresponse.cpp:25
QgsWms::QgsWmsRenderContext::SetAccessControl
@ SetAccessControl
Definition: qgswmsrendercontext.h:59
QgsGeometry::fromWkt
static QgsGeometry fromWkt(const QString &wkt)
Creates a new geometry from a WKT string.
Definition: qgsgeometry.cpp:154
QgsWms
WMS implementation.
Definition: qgsdxfwriter.cpp:22
QgsRectangle::set
void set(const QgsPointXY &p1, const QgsPointXY &p2)
Sets the rectangle from two QgsPoints.
Definition: qgsrectangle.h:105
QgsRectangle::height
double height() const
Returns the height of the rectangle.
Definition: qgsrectangle.h:209
QgsServerRequest::url
QUrl url() const
Definition: qgsserverrequest.cpp:65
QgsGeometry::boundingBox
QgsRectangle boundingBox() const
Returns the bounding box of the geometry.
Definition: qgsgeometry.cpp:962
QgsWms::QgsWmsParameter::HEIGHT
@ HEIGHT
Definition: qgswmsparameters.h:124
QgsWms::QgsWmsRenderContext::UseScaleDenominator
@ UseScaleDenominator
Definition: qgswmsrendercontext.h:53
QgsServerInterface
Definition: qgsserverinterface.h:60
QgsRectangle::width
double width() const
Returns the width of the rectangle.
Definition: qgsrectangle.h:202
QgsServerResponse
Definition: qgsserverresponse.h:43
QgsServerResponse::setHeader
virtual void setHeader(const QString &key, const QString &value)=0
Set Header entry Add Header entry to the response Note that it is usually an error to set Header afte...