QGIS API Documentation  3.22.4-Białowieża (ce8e65e95e)
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"
41 
51 class CORE_EXPORT QgsOgcUtils
52 {
53  public:
54 
59  struct Context
60  {
61 
65  Context( const QgsMapLayer *layer = nullptr, const QgsCoordinateTransformContext &transformContext = QgsCoordinateTransformContext() )
66  : layer( layer )
67  , transformContext( transformContext )
68  {
69  }
70  const QgsMapLayer *layer = nullptr;
72  };
73 
78  {
82  };
83 
91  static QgsGeometry geometryFromGML( const QString &xmlString, const QgsOgcUtils::Context &context = QgsOgcUtils::Context() );
92 
96  static QgsGeometry geometryFromGML( const QDomNode &geometryNode, const QgsOgcUtils::Context &context = QgsOgcUtils::Context() );
97 
99  static QgsRectangle rectangleFromGMLBox( const QDomNode &boxNode );
100 
102  static QgsRectangle rectangleFromGMLEnvelope( const QDomNode &envelopeNode );
103 
109  static QDomElement geometryToGML( const QgsGeometry &geometry, QDomDocument &doc,
110  QgsOgcUtils::GMLVersion gmlVersion,
111  const QString &srsName,
112  bool invertAxisOrientation,
113  const QString &gmlIdBase,
114  int precision = 17 );
115 
120  static QDomElement geometryToGML( const QgsGeometry &geometry, QDomDocument &doc, const QString &format, int precision = 17 );
121 
126  static QDomElement geometryToGML( const QgsGeometry &geometry, QDomDocument &doc, int precision = 17 );
127 
132  static QDomElement rectangleToGMLBox( QgsRectangle *box, QDomDocument &doc, int precision = 17 );
133 
139  static QDomElement rectangleToGMLBox( QgsRectangle *box, QDomDocument &doc,
140  const QString &srsName,
141  bool invertAxisOrientation,
142  int precision = 17 );
143 
148  static QDomElement rectangleToGMLEnvelope( QgsRectangle *env, QDomDocument &doc, int precision = 17 );
149 
155  static QDomElement rectangleToGMLEnvelope( QgsRectangle *env, QDomDocument &doc,
156  const QString &srsName,
157  bool invertAxisOrientation,
158  int precision = 17 );
159 
160 
162  static QColor colorFromOgcFill( const QDomElement &fillElement );
163 
165  static QgsExpression *expressionFromOgcFilter( const QDomElement &element, QgsVectorLayer *layer = nullptr ) SIP_FACTORY;
166 
173  static QDomElement expressionToOgcFilter( const QgsExpression &exp, QDomDocument &doc, QString *errorMessage = nullptr );
174 
179  {
182  FILTER_FES_2_0
183  };
184 
192  static QgsExpression *expressionFromOgcFilter( const QDomElement &element, FilterVersion version, QgsVectorLayer *layer = nullptr ) SIP_FACTORY;
193 
202  static QDomElement expressionToOgcFilter( const QgsExpression &exp,
203  QDomDocument &doc,
204  QgsOgcUtils::GMLVersion gmlVersion,
205  FilterVersion filterVersion,
206  const QString &geometryName,
207  const QString &srsName,
208  bool honourAxisOrientation,
209  bool invertAxisOrientation,
210  QString *errorMessage = nullptr ) SIP_SKIP;
211 
217  static QDomElement expressionToOgcExpression( const QgsExpression &exp, QDomDocument &doc, QString *errorMessage = nullptr );
218 
224  static QDomElement expressionToOgcExpression( const QgsExpression &exp,
225  QDomDocument &doc,
226  QgsOgcUtils::GMLVersion gmlVersion,
227  FilterVersion filterVersion,
228  const QString &geometryName,
229  const QString &srsName,
230  bool honourAxisOrientation,
231  bool invertAxisOrientation,
232  QString *errorMessage = nullptr );
233 
234 #ifndef SIP_RUN
235 
243  {
244  public:
246  LayerProperties() = default;
247 
249  QString mName;
253  QString mSRSName;
257  QString mNamespaceURI;
258  };
259 #endif
260 
279  static QDomElement SQLStatementToOgcFilter( const QgsSQLStatement &statement,
280  QDomDocument &doc,
281  QgsOgcUtils::GMLVersion gmlVersion,
282  FilterVersion filterVersion,
283  const QList<LayerProperties> &layerProperties,
284  bool honourAxisOrientation,
285  bool invertAxisOrientation,
286  const QMap< QString, QString> &mapUnprefixedTypenameToPrefixedTypename,
287  QString *errorMessage = nullptr ) SIP_SKIP;
288 
289  private:
290 
292  static QgsGeometry geometryFromGMLPoint( const QDomElement &geometryElement );
294  static QgsGeometry geometryFromGMLLineString( const QDomElement &geometryElement );
296  static QgsGeometry geometryFromGMLPolygon( const QDomElement &geometryElement );
298  static QgsGeometry geometryFromGMLMultiPoint( const QDomElement &geometryElement );
300  static QgsGeometry geometryFromGMLMultiLineString( const QDomElement &geometryElement );
302  static QgsGeometry geometryFromGMLMultiPolygon( const QDomElement &geometryElement );
303 
310  static bool readGMLCoordinates( QgsPolylineXY &coords, const QDomElement &elem );
311 
320  static bool readGMLPositions( QgsPolylineXY &coords, const QDomElement &elem );
321 
322 
329  static QDomElement createGMLCoordinates( const QgsPolylineXY &points, QDomDocument &doc );
330 
337  static QDomElement createGMLPositions( const QgsPolylineXY &points, QDomDocument &doc );
338 
340  static QgsExpressionNode *nodeFromOgcFilter( QDomElement &element, QString &errorMessage, QgsVectorLayer *layer = nullptr );
342  static QgsExpressionNodeBinaryOperator *nodeBinaryOperatorFromOgcFilter( QDomElement &element, QString &errorMessage, QgsVectorLayer *layer = nullptr );
344  static QgsExpressionNodeFunction *nodeSpatialOperatorFromOgcFilter( QDomElement &element, QString &errorMessage );
346  static QgsExpressionNodeUnaryOperator *nodeNotFromOgcFilter( QDomElement &element, QString &errorMessage );
348  static QgsExpressionNodeFunction *nodeFunctionFromOgcFilter( QDomElement &element, QString &errorMessage );
350  static QgsExpressionNode *nodeLiteralFromOgcFilter( QDomElement &element, QString &errorMessage, QgsVectorLayer *layer = nullptr );
352  static QgsExpressionNodeColumnRef *nodeColumnRefFromOgcFilter( QDomElement &element, QString &errorMessage );
354  static QgsExpressionNode *nodeIsBetweenFromOgcFilter( QDomElement &element, QString &errorMessage );
356  static QgsExpressionNodeBinaryOperator *nodePropertyIsNullFromOgcFilter( QDomElement &element, QString &errorMessage );
357 };
358 
359 #ifndef SIP_RUN
360 
367 {
368  public:
370  QgsOgcUtilsExprToFilter( QDomDocument &doc,
371  QgsOgcUtils::GMLVersion gmlVersion,
372  QgsOgcUtils::FilterVersion filterVersion,
373  const QString &geometryName,
374  const QString &srsName,
375  bool honourAxisOrientation,
376  bool invertAxisOrientation );
377 
379  QDomElement expressionNodeToOgcFilter( const QgsExpressionNode *node, QgsExpression *expression, const QgsExpressionContext *context );
380 
382  bool GMLNamespaceUsed() const { return mGMLUsed; }
383 
385  QString errorMessage() const { return mErrorMessage; }
386 
387  private:
388  QDomDocument &mDoc;
389  bool mGMLUsed;
390  QgsOgcUtils::GMLVersion mGMLVersion;
391  QgsOgcUtils::FilterVersion mFilterVersion;
392  const QString &mGeometryName;
393  const QString &mSrsName;
394  bool mInvertAxisOrientation;
395  QString mErrorMessage;
396  QString mFilterPrefix;
397  QString mPropertyName;
398  int mGeomId;
399 
400  QDomElement expressionUnaryOperatorToOgcFilter( const QgsExpressionNodeUnaryOperator *node, QgsExpression *expression, const QgsExpressionContext *context );
401  QDomElement expressionBinaryOperatorToOgcFilter( const QgsExpressionNodeBinaryOperator *node, QgsExpression *expression, const QgsExpressionContext *context );
402  QDomElement expressionLiteralToOgcFilter( const QgsExpressionNodeLiteral *node, QgsExpression *expression, const QgsExpressionContext *context );
403  QDomElement expressionColumnRefToOgcFilter( const QgsExpressionNodeColumnRef *node, QgsExpression *expression, const QgsExpressionContext *context );
404  QDomElement expressionInOperatorToOgcFilter( const QgsExpressionNodeInOperator *node, QgsExpression *expression, const QgsExpressionContext *context );
405  QDomElement expressionFunctionToOgcFilter( const QgsExpressionNodeFunction *node, QgsExpression *expression, const QgsExpressionContext *context );
406 };
407 
415 {
416  public:
417 
424  const QgsVectorLayer *layer = nullptr );
425 
431  QgsExpressionNode *nodeFromOgcFilter( const QDomElement &element );
432 
437  QString errorMessage() const;
438 
444  QgsExpressionNodeBinaryOperator *nodeBinaryOperatorFromOgcFilter( const QDomElement &element );
445 
450  QgsExpressionNodeFunction *nodeSpatialOperatorFromOgcFilter( const QDomElement &element );
451 
456  QgsExpressionNodeColumnRef *nodeColumnRefFromOgcFilter( const QDomElement &element );
457 
462  QgsExpressionNode *nodeLiteralFromOgcFilter( const QDomElement &element );
463 
468  QgsExpressionNodeUnaryOperator *nodeNotFromOgcFilter( const QDomElement &element );
469 
474  QgsExpressionNodeBinaryOperator *nodePropertyIsNullFromOgcFilter( const QDomElement &element );
475 
480  QgsExpressionNodeFunction *nodeFunctionFromOgcFilter( const QDomElement &element );
481 
486  QgsExpressionNode *nodeIsBetweenFromOgcFilter( const QDomElement &element );
487 
488  private:
489  const QgsVectorLayer *mLayer = nullptr;
490  QString mErrorMessage;
491  QString mPropertyName;
492  QString mPrefix;
493 };
494 
501 {
502  public:
504  QgsOgcUtilsSQLStatementToFilter( QDomDocument &doc,
505  QgsOgcUtils::GMLVersion gmlVersion,
506  QgsOgcUtils::FilterVersion filterVersion,
507  const QList<QgsOgcUtils::LayerProperties> &layerProperties,
508  bool honourAxisOrientation,
509  bool invertAxisOrientation,
510  const QMap< QString, QString> &mapUnprefixedTypenameToPrefixedTypename );
511 
513  QDomElement toOgcFilter( const QgsSQLStatement::Node *node );
514 
516  bool GMLNamespaceUsed() const { return mGMLUsed; }
517 
519  QString errorMessage() const { return mErrorMessage; }
520 
521  private:
522  QDomDocument &mDoc;
523  bool mGMLUsed;
524  QgsOgcUtils::GMLVersion mGMLVersion;
525  QgsOgcUtils::FilterVersion mFilterVersion;
526  const QList<QgsOgcUtils::LayerProperties> &mLayerProperties;
527  bool mHonourAxisOrientation;
528  bool mInvertAxisOrientation;
529  QString mErrorMessage;
530  QString mFilterPrefix;
531  QString mPropertyName;
532  int mGeomId;
533  QString mCurrentSRSName;
534  QMap<QString, QString> mMapTableAliasToNames;
535  const QMap< QString, QString> &mMapUnprefixedTypenameToPrefixedTypename;
536 
537  QDomElement toOgcFilter( const QgsSQLStatement::NodeUnaryOperator *node );
538  QDomElement toOgcFilter( const QgsSQLStatement::NodeBinaryOperator *node );
539  QDomElement toOgcFilter( const QgsSQLStatement::NodeLiteral *node );
540  QDomElement toOgcFilter( const QgsSQLStatement::NodeColumnRef *node );
541  QDomElement toOgcFilter( const QgsSQLStatement::NodeInOperator *node );
542  QDomElement toOgcFilter( const QgsSQLStatement::NodeBetweenOperator *node );
543  QDomElement toOgcFilter( const QgsSQLStatement::NodeFunction *node );
544  QDomElement toOgcFilter( const QgsSQLStatement::NodeJoin *node, const QString &leftTable );
545  QDomElement toOgcFilter( const QgsSQLStatement::NodeSelect *node );
546 
547  void visit( const QgsSQLStatement::NodeTableDef *node );
548  QString getGeometryColumnSRSName( const QgsSQLStatement::Node *node );
549  bool processSRSName( const QgsSQLStatement::NodeFunction *mainNode,
550  QList<QgsSQLStatement::Node *> args,
551  bool lastArgIsSRSName,
552  QString &srsName,
553  bool &axisInversion );
554 };
555 #endif // #ifndef SIP_RUN
556 
557 #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:125
Base class for all map layer types.
Definition: qgsmaplayer.h:73
Internal use by QgsOgcUtils.
Definition: qgsogcutils.h:367
bool GMLNamespaceUsed() const
Returns whether the gml: namespace is used.
Definition: qgsogcutils.h:382
QString errorMessage() const
Returns the error message.
Definition: qgsogcutils.h:385
Internal use by QgsOgcUtils.
Definition: qgsogcutils.h:415
Internal use by QgsOgcUtils.
Definition: qgsogcutils.h:501
bool GMLNamespaceUsed() const
Returns whether the gml: namespace is used.
Definition: qgsogcutils.h:516
QString errorMessage() const
Returns the error message.
Definition: qgsogcutils.h:519
QString mSRSName
SRS name.
Definition: qgsogcutils.h:253
LayerProperties()=default
Constructor.
QString mNamespaceURI
Namespace URI.
Definition: qgsogcutils.h:257
QString mNamespacePrefix
Namespace prefix.
Definition: qgsogcutils.h:255
QString mGeometryAttribute
Geometry attribute name.
Definition: qgsogcutils.h:251
QString mName
Layer name.
Definition: qgsogcutils.h:249
The QgsOgcUtils class provides various utility functions for conversion between OGC (Open Geospatial ...
Definition: qgsogcutils.h:52
GMLVersion
GML version.
Definition: qgsogcutils.h:78
FilterVersion
OGC filter version.
Definition: qgsogcutils.h:179
A class to represent a 2D point.
Definition: qgspointxy.h:59
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:52
const QString & srsName
const QString & geometryName
int precision
The Context struct stores the current layer and coordinate transform context.
Definition: qgsogcutils.h:60
Context(const QgsMapLayer *layer=nullptr, const QgsCoordinateTransformContext &transformContext=QgsCoordinateTransformContext())
Constructs a Context from layer and transformContext.
Definition: qgsogcutils.h:65
QgsCoordinateTransformContext transformContext
Definition: qgsogcutils.h:71