QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 
323  static QDomElement createGMLCoordinates( const QgsPolylineXY &points, QDomDocument &doc );
324 
330  static QDomElement createGMLPositions( const QgsPolylineXY &points, QDomDocument &doc );
331 
333  static QgsExpressionNode *nodeFromOgcFilter( QDomElement &element, QString &errorMessage, QgsVectorLayer *layer = nullptr );
335  static QgsExpressionNodeBinaryOperator *nodeBinaryOperatorFromOgcFilter( QDomElement &element, QString &errorMessage, QgsVectorLayer *layer = nullptr );
337  static QgsExpressionNodeFunction *nodeSpatialOperatorFromOgcFilter( QDomElement &element, QString &errorMessage );
339  static QgsExpressionNodeUnaryOperator *nodeNotFromOgcFilter( QDomElement &element, QString &errorMessage );
341  static QgsExpressionNodeFunction *nodeFunctionFromOgcFilter( QDomElement &element, QString &errorMessage );
343  static QgsExpressionNode *nodeLiteralFromOgcFilter( QDomElement &element, QString &errorMessage, QgsVectorLayer *layer = nullptr );
345  static QgsExpressionNodeColumnRef *nodeColumnRefFromOgcFilter( QDomElement &element, QString &errorMessage );
347  static QgsExpressionNode *nodeIsBetweenFromOgcFilter( QDomElement &element, QString &errorMessage );
349  static QgsExpressionNodeBinaryOperator *nodePropertyIsNullFromOgcFilter( QDomElement &element, QString &errorMessage );
350 };
351 
352 #ifndef SIP_RUN
353 
360 {
361  public:
363  QgsOgcUtilsExprToFilter( QDomDocument &doc,
364  QgsOgcUtils::GMLVersion gmlVersion,
365  QgsOgcUtils::FilterVersion filterVersion,
366  const QString &geometryName,
367  const QString &srsName,
368  bool honourAxisOrientation,
369  bool invertAxisOrientation );
370 
372  QDomElement expressionNodeToOgcFilter( const QgsExpressionNode *node, QgsExpression *expression, const QgsExpressionContext *context );
373 
375  bool GMLNamespaceUsed() const { return mGMLUsed; }
376 
378  QString errorMessage() const { return mErrorMessage; }
379 
380  private:
381  QDomDocument &mDoc;
382  bool mGMLUsed;
383  QgsOgcUtils::GMLVersion mGMLVersion;
384  QgsOgcUtils::FilterVersion mFilterVersion;
385  const QString &mGeometryName;
386  const QString &mSrsName;
387  bool mInvertAxisOrientation;
388  QString mErrorMessage;
389  QString mFilterPrefix;
390  QString mPropertyName;
391  int mGeomId;
392 
393  QDomElement expressionUnaryOperatorToOgcFilter( const QgsExpressionNodeUnaryOperator *node, QgsExpression *expression, const QgsExpressionContext *context );
394  QDomElement expressionBinaryOperatorToOgcFilter( const QgsExpressionNodeBinaryOperator *node, QgsExpression *expression, const QgsExpressionContext *context );
395  QDomElement expressionLiteralToOgcFilter( const QgsExpressionNodeLiteral *node, QgsExpression *expression, const QgsExpressionContext *context );
396  QDomElement expressionColumnRefToOgcFilter( const QgsExpressionNodeColumnRef *node, QgsExpression *expression, const QgsExpressionContext *context );
397  QDomElement expressionInOperatorToOgcFilter( const QgsExpressionNodeInOperator *node, QgsExpression *expression, const QgsExpressionContext *context );
398  QDomElement expressionFunctionToOgcFilter( const QgsExpressionNodeFunction *node, QgsExpression *expression, const QgsExpressionContext *context );
399 };
400 
408 {
409  public:
410 
417  const QgsVectorLayer *layer = nullptr );
418 
424  QgsExpressionNode *nodeFromOgcFilter( const QDomElement &element );
425 
430  QString errorMessage() const;
431 
437  QgsExpressionNodeBinaryOperator *nodeBinaryOperatorFromOgcFilter( const QDomElement &element );
438 
443  QgsExpressionNodeFunction *nodeSpatialOperatorFromOgcFilter( const QDomElement &element );
444 
449  QgsExpressionNodeColumnRef *nodeColumnRefFromOgcFilter( const QDomElement &element );
450 
455  QgsExpressionNode *nodeLiteralFromOgcFilter( const QDomElement &element );
456 
461  QgsExpressionNodeUnaryOperator *nodeNotFromOgcFilter( const QDomElement &element );
462 
467  QgsExpressionNodeBinaryOperator *nodePropertyIsNullFromOgcFilter( const QDomElement &element );
468 
473  QgsExpressionNodeFunction *nodeFunctionFromOgcFilter( const QDomElement &element );
474 
479  QgsExpressionNode *nodeIsBetweenFromOgcFilter( const QDomElement &element );
480 
481  private:
482  const QgsVectorLayer *mLayer = nullptr;
483  QString mErrorMessage;
484  QString mPropertyName;
485  QString mPrefix;
486 };
487 
494 {
495  public:
497  QgsOgcUtilsSQLStatementToFilter( QDomDocument &doc,
498  QgsOgcUtils::GMLVersion gmlVersion,
499  QgsOgcUtils::FilterVersion filterVersion,
500  const QList<QgsOgcUtils::LayerProperties> &layerProperties,
501  bool honourAxisOrientation,
502  bool invertAxisOrientation,
503  const QMap< QString, QString> &mapUnprefixedTypenameToPrefixedTypename );
504 
506  QDomElement toOgcFilter( const QgsSQLStatement::Node *node );
507 
509  bool GMLNamespaceUsed() const { return mGMLUsed; }
510 
512  QString errorMessage() const { return mErrorMessage; }
513 
514  private:
515  QDomDocument &mDoc;
516  bool mGMLUsed;
517  QgsOgcUtils::GMLVersion mGMLVersion;
518  QgsOgcUtils::FilterVersion mFilterVersion;
519  const QList<QgsOgcUtils::LayerProperties> &mLayerProperties;
520  bool mHonourAxisOrientation;
521  bool mInvertAxisOrientation;
522  QString mErrorMessage;
523  QString mFilterPrefix;
524  QString mPropertyName;
525  int mGeomId;
526  QString mCurrentSRSName;
527  QMap<QString, QString> mMapTableAliasToNames;
528  const QMap< QString, QString> &mMapUnprefixedTypenameToPrefixedTypename;
529 
530  QDomElement toOgcFilter( const QgsSQLStatement::NodeUnaryOperator *node );
531  QDomElement toOgcFilter( const QgsSQLStatement::NodeBinaryOperator *node );
532  QDomElement toOgcFilter( const QgsSQLStatement::NodeLiteral *node );
533  QDomElement toOgcFilter( const QgsSQLStatement::NodeColumnRef *node );
534  QDomElement toOgcFilter( const QgsSQLStatement::NodeInOperator *node );
535  QDomElement toOgcFilter( const QgsSQLStatement::NodeBetweenOperator *node );
536  QDomElement toOgcFilter( const QgsSQLStatement::NodeFunction *node );
537  QDomElement toOgcFilter( const QgsSQLStatement::NodeJoin *node, const QString &leftTable );
538  QDomElement toOgcFilter( const QgsSQLStatement::NodeSelect *node );
539 
540  void visit( const QgsSQLStatement::NodeTableDef *node );
541  QString getGeometryColumnSRSName( const QgsSQLStatement::Node *node );
542  bool processSRSName( const QgsSQLStatement::NodeFunction *mainNode,
543  QList<QgsSQLStatement::Node *> args,
544  bool lastArgIsSRSName,
545  QString &srsName,
546  bool &axisInversion );
547 };
548 #endif // #ifndef SIP_RUN
549 
550 #endif // QGSOGCUTILS_H
QgsSQLStatement::Node
Definition: qgssqlstatement.h:229
QgsOgcUtils::GML_3_2_1
@ GML_3_2_1
Definition: qgsogcutils.h:80
QgsSQLStatement::NodeJoin
Definition: qgssqlstatement.h:664
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:369
QgsCoordinateTransformContext
Definition: qgscoordinatetransformcontext.h:57
QgsOgcUtils::LayerProperties::mGeometryAttribute
QString mGeometryAttribute
Geometry attribute name.
Definition: qgsogcutils.h:250
QgsExpressionNodeLiteral
An expression node for literal values.
Definition: qgsexpressionnodeimpl.h:364
QgsExpressionNodeBinaryOperator
A binary expression operator, which operates on two values.
Definition: qgsexpressionnodeimpl.h:91
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:400
QgsPolylineXY
QVector< QgsPointXY > QgsPolylineXY
Polyline as represented as a vector of two-dimensional points.
Definition: qgsgeometry.h:50
QgsSQLStatement::NodeTableDef
Definition: qgssqlstatement.h:633
QgsOgcUtils::LayerProperties::mName
QString mName
Layer name.
Definition: qgsogcutils.h:248
QgsExpressionNodeUnaryOperator
Definition: qgsexpressionnodeimpl.h:27
QgsSQLStatement::NodeFunction
Definition: qgssqlstatement.h:478
QgsSQLStatement::NodeSelect
Definition: qgssqlstatement.h:734
QgsOgcUtils
The QgsOgcUtils class provides various utility functions for conversion between OGC (Open Geospatial ...
Definition: qgsogcutils.h:50
QgsSQLStatement::NodeUnaryOperator
Definition: qgssqlstatement.h:338
QgsRectangle
Definition: qgsrectangle.h:41
geometryName
const QString & geometryName
Definition: qgswfsgetfeature.cpp:113
QgsOgcUtilsExprToFilter::GMLNamespaceUsed
bool GMLNamespaceUsed() const
Returns whether the gml: namespace is used.
Definition: qgsogcutils.h:375
QgsExpressionNodeInOperator
An expression node for value IN or NOT IN clauses.
Definition: qgsexpressionnodeimpl.h:264
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:509
QgsOgcUtilsExprToFilter
Definition: qgsogcutils.h:359
QgsOgcUtilsExprToFilter::errorMessage
QString errorMessage() const
Returns the error message.
Definition: qgsogcutils.h:378
precision
int precision
Definition: qgswfsgetfeature.cpp:103
SIP_SKIP
#define SIP_SKIP
Definition: qgis_sip.h:126
qgis_sip.h
qgsexpressionnode.h
QgsSQLStatement
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:316
QgsOgcUtilsExpressionFromFilter
Internal use by QgsOgcUtils.
Definition: qgsogcutils.h:407
QgsOgcUtilsSQLStatementToFilter::errorMessage
QString errorMessage() const
Returns the error message.
Definition: qgsogcutils.h:512
QgsOgcUtils::LayerProperties
Definition: qgsogcutils.h:241
QgsOgcUtilsSQLStatementToFilter
Definition: qgsogcutils.h:493
QgsSQLStatement::NodeInOperator
Definition: qgssqlstatement.h:409
QgsSQLStatement::NodeBinaryOperator
Definition: qgssqlstatement.h:366
QgsOgcUtils::FilterVersion
FilterVersion
OGC filter version.
Definition: qgsogcutils.h:177
QgsPointXY
Definition: qgspointxy.h:43
QgsExpressionNode
Definition: qgsexpressionnode.h:34
QgsSQLStatement::NodeColumnRef
Definition: qgssqlstatement.h:530
qgsgeometry.h
qgsexpressionnodeimpl.h
QgsGeometry
Definition: qgsgeometry.h:122
QgsVectorLayer
Definition: qgsvectorlayer.h:385
QgsMapLayer
Definition: qgsmaplayer.h:81
QgsOgcUtils::GMLVersion
GMLVersion
GML version.
Definition: qgsogcutils.h:76
QgsOgcUtils::Context::Context
Context(const QgsMapLayer *layer=nullptr, const QgsCoordinateTransformContext &transformContext=QgsCoordinateTransformContext())
Constructs a Context from layer and transformContext.
Definition: qgsogcutils.h:64
QgsExpression
Definition: qgsexpression.h:113
QgsOgcUtils::LayerProperties::mSRSName
QString mSRSName
SRS name.
Definition: qgsogcutils.h:252
QgsSQLStatement::NodeBetweenOperator
Definition: qgssqlstatement.h:441
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:58
QgsOgcUtils::FILTER_OGC_1_0
@ FILTER_OGC_1_0
Definition: qgsogcutils.h:179
QgsSQLStatement::NodeLiteral
Definition: qgssqlstatement.h:507