QGIS API Documentation  3.20.0-Odense (decaadbb31)
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 {
28  const QgsProject *project,
29  const QgsWmsRequest &request,
30  QgsServerResponse &response )
31  {
32  QgsWmsParameters parameters = request.wmsParameters();
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 );
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( parameters.version() ) );
66  }
67 } // namespace QgsWms
static QgsGeometry fromWkt(const QString &wkt)
Creates a new geometry from a WKT string.
QgsRectangle boundingBox() const
Returns the bounding box of the geometry.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition: qgsproject.h:99
A rectangle specified with double values.
Definition: qgsrectangle.h:42
double height() const SIP_HOLDGIL
Returns the height of the rectangle.
Definition: qgsrectangle.h:230
double width() const SIP_HOLDGIL
Returns the width of the rectangle.
Definition: qgsrectangle.h:223
QgsServerInterface Class defining interfaces exposed by QGIS Server and made available to plugins.
QgsServerRequest::Parameters parameters() const
Returns a map of query parameters with keys converted to uppercase.
QgsServerResponse Class defining response interface passed to services QgsService::executeRequest() m...
virtual void write(const QString &data)
Write string This is a convenient method that will write directly to the underlying I/O device.
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...
Map renderer for WMS requests.
QByteArray getFeatureInfo(const QString &version="1.3.0")
Creates an xml document that describes the result of the getFeatureInfo request.
Provides an interface to retrieve and manipulate WMS parameters received from the client.
void set(QgsWmsParameter::Name name, const QVariant &value)
Sets a parameter value thanks to its name.
QString filterGeom() const
Returns the filter geometry found in FILTER_GEOM parameter.
int heightAsInt() const
Returns HEIGHT parameter as an int or its default value if not defined.
int widthAsInt() const
Returns WIDTH parameter as an int or its default value if not defined.
QString version() const override
Returns VERSION parameter as a string or an empty string if not defined.
bool infoFormatIsImage() const
Checks if INFO_FORMAT parameter is one of the image formats (PNG, JPG).
Rendering context for the WMS renderer.
void setParameters(const QgsWmsParameters &parameters)
Sets WMS parameters.
void setFlag(Flag flag, bool on=true)
Sets or unsets a rendering flag according to the on value.
Class defining request interface passed to WMS service.
Definition: qgswmsrequest.h:35
const QgsWmsParameters & wmsParameters() const
Returns the parameters interpreted for the WMS service.
Median cut implementation.
void writeGetFeatureInfo(QgsServerInterface *serverIface, const QgsProject *project, const QgsWmsRequest &request, QgsServerResponse &response)
Output GetFeatureInfo response.