QGIS API Documentation  3.18.1-Zürich (202f1bf7e5)
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
Contains information about the context in which a coordinate transform is executed.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
A binary expression operator, which operates on two values.
An expression node which takes it value from a feature's field.
An expression node for expression functions.
An expression node for value IN or NOT IN clauses.
An expression node for literal values.
A unary node is either negative as in boolean (not) or as in numbers (minus).
Abstract base class for all nodes that can appear in an expression.
Class for parsing and evaluation of expressions (formerly called "search strings").
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:124
Base class for all map layer types.
Definition: qgsmaplayer.h:85
Internal use by QgsOgcUtils.
Definition: qgsogcutils.h:362
bool GMLNamespaceUsed() const
Returns whether the gml: namespace is used.
Definition: qgsogcutils.h:377
QString errorMessage() const
Returns the error message.
Definition: qgsogcutils.h:380
Internal use by QgsOgcUtils.
Definition: qgsogcutils.h:410
Internal use by QgsOgcUtils.
Definition: qgsogcutils.h:496
bool GMLNamespaceUsed() const
Returns whether the gml: namespace is used.
Definition: qgsogcutils.h:511
QString errorMessage() const
Returns the error message.
Definition: qgsogcutils.h:514
QString mSRSName
SRS name.
Definition: qgsogcutils.h:252
LayerProperties()=default
Constructor.
QString mGeometryAttribute
Geometry attribute name.
Definition: qgsogcutils.h:250
QString mName
Layer name.
Definition: qgsogcutils.h:248
The QgsOgcUtils class provides various utility functions for conversion between OGC (Open Geospatial ...
Definition: qgsogcutils.h:51
GMLVersion
GML version.
Definition: qgsogcutils.h:77
FilterVersion
OGC filter version.
Definition: qgsogcutils.h:178
A class to represent a 2D point.
Definition: qgspointxy.h:44
A rectangle specified with double values.
Definition: qgsrectangle.h:42
'X BETWEEN y and z' operator
Binary logical/arithmetical operator (AND, OR, =, +, ...)
Reference to a column.
Function with a name and arguments node.
Literal value (integer, integer64, double, string)
Unary logicial/arithmetical operator ( NOT, - )
Abstract node class.
Class for parsing SQL statements.
Represents a vector layer which manages a vector based data sets.
#define SIP_SKIP
Definition: qgis_sip.h:126
#define SIP_FACTORY
Definition: qgis_sip.h:76
QVector< QgsPointXY > QgsPolylineXY
Polyline as represented as a vector of two-dimensional points.
Definition: qgsgeometry.h:51
const QString & geometryName
int precision
The Context struct stores the current layer and coordinate transform context.
Definition: qgsogcutils.h:59
Context(const QgsMapLayer *layer=nullptr, const QgsCoordinateTransformContext &transformContext=QgsCoordinateTransformContext())
Constructs a Context from layer and transformContext.
Definition: qgsogcutils.h:64
QgsCoordinateTransformContext transformContext
Definition: qgsogcutils.h:70