QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
qgsogcutils.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsogcutils.h
3  ---------------------
4  begin : March 2013
5  copyright : (C) 2013 by Martin Dobias
6  email : wonder dot sk at gmail dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 #ifndef QGSOGCUTILS_H
16 #define QGSOGCUTILS_H
17 
18 class QColor;
19 class QDomNode;
20 class QDomElement;
21 class QDomDocument;
22 class QString;
23 
24 #include "qgis_core.h"
25 #include "qgis_sip.h"
26 #include <list>
27 #include <QVector>
28 
29 class QgsExpression;
30 class QgsGeometry;
31 class QgsPointXY;
32 class QgsRectangle;
33 class QgsVectorLayer;
34 
35 #include "qgsgeometry.h"
36 #include "qgsexpression.h"
37 #include "qgsexpressionnode.h"
38 #include "qgsexpressionnodeimpl.h"
39 #include "qgssqlstatement.h"
40 
50 class CORE_EXPORT QgsOgcUtils
51 {
52  public:
53 
58  struct Context
59  {
60 
64  Context( const QgsMapLayer *layer = nullptr, const QgsCoordinateTransformContext &transformContext = QgsCoordinateTransformContext() )
65  : layer( layer )
66  , transformContext( transformContext )
67  {
68  }
69  const QgsMapLayer *layer = nullptr;
71  };
72 
77  {
81  };
82 
90  static QgsGeometry geometryFromGML( const QString &xmlString, const QgsOgcUtils::Context &context = QgsOgcUtils::Context() );
91 
95  static QgsGeometry geometryFromGML( const QDomNode &geometryNode, const QgsOgcUtils::Context &context = QgsOgcUtils::Context() );
96 
98  static QgsRectangle rectangleFromGMLBox( const QDomNode &boxNode );
99 
101  static QgsRectangle rectangleFromGMLEnvelope( const QDomNode &envelopeNode );
102 
108  static QDomElement geometryToGML( const QgsGeometry &geometry, QDomDocument &doc,
109  QgsOgcUtils::GMLVersion gmlVersion,
110  const QString &srsName,
111  bool invertAxisOrientation,
112  const QString &gmlIdBase,
113  int precision = 17 );
114 
119  static QDomElement geometryToGML( const QgsGeometry &geometry, QDomDocument &doc, const QString &format, int precision = 17 );
120 
125  static QDomElement geometryToGML( const QgsGeometry &geometry, QDomDocument &doc, int precision = 17 );
126 
131  static QDomElement rectangleToGMLBox( QgsRectangle *box, QDomDocument &doc, int precision = 17 );
132 
138  static QDomElement rectangleToGMLBox( QgsRectangle *box, QDomDocument &doc,
139  const QString &srsName,
140  bool invertAxisOrientation,
141  int precision = 17 );
142 
147  static QDomElement rectangleToGMLEnvelope( QgsRectangle *env, QDomDocument &doc, int precision = 17 );
148 
154  static QDomElement rectangleToGMLEnvelope( QgsRectangle *env, QDomDocument &doc,
155  const QString &srsName,
156  bool invertAxisOrientation,
157  int precision = 17 );
158 
159 
161  static QColor colorFromOgcFill( const QDomElement &fillElement );
162 
164  static QgsExpression *expressionFromOgcFilter( const QDomElement &element, QgsVectorLayer *layer = nullptr ) SIP_FACTORY;
165 
172  static QDomElement expressionToOgcFilter( const QgsExpression &exp, QDomDocument &doc, QString *errorMessage = nullptr );
173 
178  {
181  FILTER_FES_2_0
182  };
183 
191  static QgsExpression *expressionFromOgcFilter( const QDomElement &element, FilterVersion version, QgsVectorLayer *layer = nullptr ) SIP_FACTORY;
192 
201  static QDomElement expressionToOgcFilter( const QgsExpression &exp,
202  QDomDocument &doc,
203  QgsOgcUtils::GMLVersion gmlVersion,
204  FilterVersion filterVersion,
205  const QString &geometryName,
206  const QString &srsName,
207  bool honourAxisOrientation,
208  bool invertAxisOrientation,
209  QString *errorMessage = nullptr ) SIP_SKIP;
210 
216  static QDomElement expressionToOgcExpression( const QgsExpression &exp, QDomDocument &doc, QString *errorMessage = nullptr );
217 
223  static QDomElement expressionToOgcExpression( const QgsExpression &exp,
224  QDomDocument &doc,
225  QgsOgcUtils::GMLVersion gmlVersion,
226  FilterVersion filterVersion,
227  const QString &geometryName,
228  const QString &srsName,
229  bool honourAxisOrientation,
230  bool invertAxisOrientation,
231  QString *errorMessage = nullptr );
232 
233 #ifndef SIP_RUN
234 
242  {
243  public:
245  LayerProperties() = default;
246 
248  QString mName;
252  QString mSRSName;
253  };
254 #endif
255 
274  static QDomElement SQLStatementToOgcFilter( const QgsSQLStatement &statement,
275  QDomDocument &doc,
276  QgsOgcUtils::GMLVersion gmlVersion,
277  FilterVersion filterVersion,
278  const QList<LayerProperties> &layerProperties,
279  bool honourAxisOrientation,
280  bool invertAxisOrientation,
281  const QMap< QString, QString> &mapUnprefixedTypenameToPrefixedTypename,
282  QString *errorMessage = nullptr ) SIP_SKIP;
283 
284  private:
285 
287  static QgsGeometry geometryFromGMLPoint( const QDomElement &geometryElement );
289  static QgsGeometry geometryFromGMLLineString( const QDomElement &geometryElement );
291  static QgsGeometry geometryFromGMLPolygon( const QDomElement &geometryElement );
293  static QgsGeometry geometryFromGMLMultiPoint( const QDomElement &geometryElement );
295  static QgsGeometry geometryFromGMLMultiLineString( const QDomElement &geometryElement );
297  static QgsGeometry geometryFromGMLMultiPolygon( const QDomElement &geometryElement );
298 
305  static bool readGMLCoordinates( QgsPolylineXY &coords, const QDomElement &elem );
306 
315  static bool readGMLPositions( QgsPolylineXY &coords, const QDomElement &elem );
316 
317 
324  static QDomElement createGMLCoordinates( const QgsPolylineXY &points, QDomDocument &doc );
325 
332  static QDomElement createGMLPositions( const QgsPolylineXY &points, QDomDocument &doc );
333 
335  static QgsExpressionNode *nodeFromOgcFilter( QDomElement &element, QString &errorMessage, QgsVectorLayer *layer = nullptr );
337  static QgsExpressionNodeBinaryOperator *nodeBinaryOperatorFromOgcFilter( QDomElement &element, QString &errorMessage, QgsVectorLayer *layer = nullptr );
339  static QgsExpressionNodeFunction *nodeSpatialOperatorFromOgcFilter( QDomElement &element, QString &errorMessage );
341  static QgsExpressionNodeUnaryOperator *nodeNotFromOgcFilter( QDomElement &element, QString &errorMessage );
343  static QgsExpressionNodeFunction *nodeFunctionFromOgcFilter( QDomElement &element, QString &errorMessage );
345  static QgsExpressionNode *nodeLiteralFromOgcFilter( QDomElement &element, QString &errorMessage, QgsVectorLayer *layer = nullptr );
347  static QgsExpressionNodeColumnRef *nodeColumnRefFromOgcFilter( QDomElement &element, QString &errorMessage );
349  static QgsExpressionNode *nodeIsBetweenFromOgcFilter( QDomElement &element, QString &errorMessage );
351  static QgsExpressionNodeBinaryOperator *nodePropertyIsNullFromOgcFilter( QDomElement &element, QString &errorMessage );
352 };
353 
354 #ifndef SIP_RUN
355 
362 {
363  public:
365  QgsOgcUtilsExprToFilter( QDomDocument &doc,
366  QgsOgcUtils::GMLVersion gmlVersion,
367  QgsOgcUtils::FilterVersion filterVersion,
368  const QString &geometryName,
369  const QString &srsName,
370  bool honourAxisOrientation,
371  bool invertAxisOrientation );
372 
374  QDomElement expressionNodeToOgcFilter( const QgsExpressionNode *node, QgsExpression *expression, const QgsExpressionContext *context );
375 
377  bool GMLNamespaceUsed() const { return mGMLUsed; }
378 
380  QString errorMessage() const { return mErrorMessage; }
381 
382  private:
383  QDomDocument &mDoc;
384  bool mGMLUsed;
385  QgsOgcUtils::GMLVersion mGMLVersion;
386  QgsOgcUtils::FilterVersion mFilterVersion;
387  const QString &mGeometryName;
388  const QString &mSrsName;
389  bool mInvertAxisOrientation;
390  QString mErrorMessage;
391  QString mFilterPrefix;
392  QString mPropertyName;
393  int mGeomId;
394 
395  QDomElement expressionUnaryOperatorToOgcFilter( const QgsExpressionNodeUnaryOperator *node, QgsExpression *expression, const QgsExpressionContext *context );
396  QDomElement expressionBinaryOperatorToOgcFilter( const QgsExpressionNodeBinaryOperator *node, QgsExpression *expression, const QgsExpressionContext *context );
397  QDomElement expressionLiteralToOgcFilter( const QgsExpressionNodeLiteral *node, QgsExpression *expression, const QgsExpressionContext *context );
398  QDomElement expressionColumnRefToOgcFilter( const QgsExpressionNodeColumnRef *node, QgsExpression *expression, const QgsExpressionContext *context );
399  QDomElement expressionInOperatorToOgcFilter( const QgsExpressionNodeInOperator *node, QgsExpression *expression, const QgsExpressionContext *context );
400  QDomElement expressionFunctionToOgcFilter( const QgsExpressionNodeFunction *node, QgsExpression *expression, const QgsExpressionContext *context );
401 };
402 
410 {
411  public:
412 
419  const QgsVectorLayer *layer = nullptr );
420 
426  QgsExpressionNode *nodeFromOgcFilter( const QDomElement &element );
427 
432  QString errorMessage() const;
433 
439  QgsExpressionNodeBinaryOperator *nodeBinaryOperatorFromOgcFilter( const QDomElement &element );
440 
445  QgsExpressionNodeFunction *nodeSpatialOperatorFromOgcFilter( const QDomElement &element );
446 
451  QgsExpressionNodeColumnRef *nodeColumnRefFromOgcFilter( const QDomElement &element );
452 
457  QgsExpressionNode *nodeLiteralFromOgcFilter( const QDomElement &element );
458 
463  QgsExpressionNodeUnaryOperator *nodeNotFromOgcFilter( const QDomElement &element );
464 
469  QgsExpressionNodeBinaryOperator *nodePropertyIsNullFromOgcFilter( const QDomElement &element );
470 
475  QgsExpressionNodeFunction *nodeFunctionFromOgcFilter( const QDomElement &element );
476 
481  QgsExpressionNode *nodeIsBetweenFromOgcFilter( const QDomElement &element );
482 
483  private:
484  const QgsVectorLayer *mLayer = nullptr;
485  QString mErrorMessage;
486  QString mPropertyName;
487  QString mPrefix;
488 };
489 
496 {
497  public:
499  QgsOgcUtilsSQLStatementToFilter( QDomDocument &doc,
500  QgsOgcUtils::GMLVersion gmlVersion,
501  QgsOgcUtils::FilterVersion filterVersion,
502  const QList<QgsOgcUtils::LayerProperties> &layerProperties,
503  bool honourAxisOrientation,
504  bool invertAxisOrientation,
505  const QMap< QString, QString> &mapUnprefixedTypenameToPrefixedTypename );
506 
508  QDomElement toOgcFilter( const QgsSQLStatement::Node *node );
509 
511  bool GMLNamespaceUsed() const { return mGMLUsed; }
512 
514  QString errorMessage() const { return mErrorMessage; }
515 
516  private:
517  QDomDocument &mDoc;
518  bool mGMLUsed;
519  QgsOgcUtils::GMLVersion mGMLVersion;
520  QgsOgcUtils::FilterVersion mFilterVersion;
521  const QList<QgsOgcUtils::LayerProperties> &mLayerProperties;
522  bool mHonourAxisOrientation;
523  bool mInvertAxisOrientation;
524  QString mErrorMessage;
525  QString mFilterPrefix;
526  QString mPropertyName;
527  int mGeomId;
528  QString mCurrentSRSName;
529  QMap<QString, QString> mMapTableAliasToNames;
530  const QMap< QString, QString> &mMapUnprefixedTypenameToPrefixedTypename;
531 
532  QDomElement toOgcFilter( const QgsSQLStatement::NodeUnaryOperator *node );
533  QDomElement toOgcFilter( const QgsSQLStatement::NodeBinaryOperator *node );
534  QDomElement toOgcFilter( const QgsSQLStatement::NodeLiteral *node );
535  QDomElement toOgcFilter( const QgsSQLStatement::NodeColumnRef *node );
536  QDomElement toOgcFilter( const QgsSQLStatement::NodeInOperator *node );
537  QDomElement toOgcFilter( const QgsSQLStatement::NodeBetweenOperator *node );
538  QDomElement toOgcFilter( const QgsSQLStatement::NodeFunction *node );
539  QDomElement toOgcFilter( const QgsSQLStatement::NodeJoin *node, const QString &leftTable );
540  QDomElement toOgcFilter( const QgsSQLStatement::NodeSelect *node );
541 
542  void visit( const QgsSQLStatement::NodeTableDef *node );
543  QString getGeometryColumnSRSName( const QgsSQLStatement::Node *node );
544  bool processSRSName( const QgsSQLStatement::NodeFunction *mainNode,
545  QList<QgsSQLStatement::Node *> args,
546  bool lastArgIsSRSName,
547  QString &srsName,
548  bool &axisInversion );
549 };
550 #endif // #ifndef SIP_RUN
551 
552 #endif // QGSOGCUTILS_H
QgsSQLStatement::Node
Abstract node class.
Definition: qgssqlstatement.h:221
QgsOgcUtils::GML_3_2_1
@ GML_3_2_1
Definition: qgsogcutils.h:80
QgsSQLStatement::NodeJoin
Join definition.
Definition: qgssqlstatement.h:656
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:370
QgsCoordinateTransformContext
Contains information about the context in which a coordinate transform is executed.
Definition: qgscoordinatetransformcontext.h:58
QgsOgcUtils::LayerProperties::mGeometryAttribute
QString mGeometryAttribute
Geometry attribute name.
Definition: qgsogcutils.h:250
QgsExpressionNodeLiteral
An expression node for literal values.
Definition: qgsexpressionnodeimpl.h:365
QgsOgcUtils::LayerProperties::LayerProperties
LayerProperties()=default
Constructor.
QgsExpressionNodeBinaryOperator
A binary expression operator, which operates on two values.
Definition: qgsexpressionnodeimpl.h:92
QgsOgcUtils::FILTER_OGC_1_1
@ FILTER_OGC_1_1
Definition: qgsogcutils.h:180
qgsexpression.h
QgsExpressionNodeColumnRef
An expression node which takes it value from a feature's field.
Definition: qgsexpressionnodeimpl.h:401
QgsPolylineXY
QVector< QgsPointXY > QgsPolylineXY
Polyline as represented as a vector of two-dimensional points.
Definition: qgsgeometry.h:51
QgsSQLStatement::NodeTableDef
Table definition.
Definition: qgssqlstatement.h:625
QgsOgcUtils::LayerProperties::mName
QString mName
Layer name.
Definition: qgsogcutils.h:248
QgsExpressionNodeUnaryOperator
A unary node is either negative as in boolean (not) or as in numbers (minus).
Definition: qgsexpressionnodeimpl.h:28
QgsSQLStatement::NodeFunction
Function with a name and arguments node.
Definition: qgssqlstatement.h:470
QgsSQLStatement::NodeSelect
SELECT node.
Definition: qgssqlstatement.h:726
QgsOgcUtils
The QgsOgcUtils class provides various utility functions for conversion between OGC (Open Geospatial ...
Definition: qgsogcutils.h:51
QgsSQLStatement::NodeUnaryOperator
Unary logicial/arithmetical operator ( NOT, - )
Definition: qgssqlstatement.h:330
QgsRectangle
A rectangle specified with double values.
Definition: qgsrectangle.h:42
geometryName
const QString & geometryName
Definition: qgswfsgetfeature.cpp:59
QgsOgcUtilsExprToFilter::GMLNamespaceUsed
bool GMLNamespaceUsed() const
Returns whether the gml: namespace is used.
Definition: qgsogcutils.h:377
QgsExpressionNodeInOperator
An expression node for value IN or NOT IN clauses.
Definition: qgsexpressionnodeimpl.h:265
QgsOgcUtils::Context::transformContext
QgsCoordinateTransformContext transformContext
Definition: qgsogcutils.h:70
SIP_FACTORY
#define SIP_FACTORY
Definition: qgis_sip.h:76
QgsOgcUtilsSQLStatementToFilter::GMLNamespaceUsed
bool GMLNamespaceUsed() const
Returns whether the gml: namespace is used.
Definition: qgsogcutils.h:511
QgsOgcUtilsExprToFilter
Internal use by QgsOgcUtils.
Definition: qgsogcutils.h:362
QgsOgcUtilsExprToFilter::errorMessage
QString errorMessage() const
Returns the error message.
Definition: qgsogcutils.h:380
precision
int precision
Definition: qgswfsgetfeature.cpp:49
SIP_SKIP
#define SIP_SKIP
Definition: qgis_sip.h:126
qgis_sip.h
qgsexpressionnode.h
QgsSQLStatement
Class for parsing SQL statements.
Definition: qgssqlstatement.h:36
QgsOgcUtils::GML_3_1_0
@ GML_3_1_0
Definition: qgsogcutils.h:79
QgsExpressionNodeFunction
An expression node for expression functions.
Definition: qgsexpressionnodeimpl.h:317
QgsOgcUtilsExpressionFromFilter
Internal use by QgsOgcUtils.
Definition: qgsogcutils.h:410
QgsOgcUtilsSQLStatementToFilter::errorMessage
QString errorMessage() const
Returns the error message.
Definition: qgsogcutils.h:514
QgsOgcUtils::LayerProperties
Layer properties.
Definition: qgsogcutils.h:242
QgsOgcUtilsSQLStatementToFilter
Internal use by QgsOgcUtils.
Definition: qgsogcutils.h:496
QgsSQLStatement::NodeInOperator
'x IN (y, z)' operator
Definition: qgssqlstatement.h:401
QgsSQLStatement::NodeBinaryOperator
Binary logical/arithmetical operator (AND, OR, =, +, ...)
Definition: qgssqlstatement.h:358
QgsOgcUtils::FilterVersion
FilterVersion
OGC filter version.
Definition: qgsogcutils.h:178
QgsPointXY
A class to represent a 2D point.
Definition: qgspointxy.h:44
QgsExpressionNode
Abstract base class for all nodes that can appear in an expression.
Definition: qgsexpressionnode.h:35
QgsSQLStatement::NodeColumnRef
Reference to a column.
Definition: qgssqlstatement.h:522
qgsgeometry.h
qgsexpressionnodeimpl.h
QgsGeometry
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:124
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:387
QgsMapLayer
Base class for all map layer types.
Definition: qgsmaplayer.h:83
QgsOgcUtils::GMLVersion
GMLVersion
GML version.
Definition: qgsogcutils.h:77
QgsOgcUtils::Context::Context
Context(const QgsMapLayer *layer=nullptr, const QgsCoordinateTransformContext &transformContext=QgsCoordinateTransformContext())
Constructs a Context from layer and transformContext.
Definition: qgsogcutils.h:64
QgsExpression
Class for parsing and evaluation of expressions (formerly called "search strings").
Definition: qgsexpression.h:105
QgsOgcUtils::LayerProperties::mSRSName
QString mSRSName
SRS name.
Definition: qgsogcutils.h:252
QgsSQLStatement::NodeBetweenOperator
'X BETWEEN y and z' operator
Definition: qgssqlstatement.h:433
qgssqlstatement.h
QgsOgcUtils::GML_2_1_2
@ GML_2_1_2
Definition: qgsogcutils.h:78
QgsOgcUtils::Context
The Context struct stores the current layer and coordinate transform context.
Definition: qgsogcutils.h:59
QgsOgcUtils::FILTER_OGC_1_0
@ FILTER_OGC_1_0
Definition: qgsogcutils.h:179
QgsSQLStatement::NodeLiteral
Literal value (integer, integer64, double, string)
Definition: qgssqlstatement.h:499