QGIS API Documentation  3.18.1-Zürich (202f1bf7e5)
qgsgml.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsgml.h
3  ---------------------
4  begin : February 2013
5  copyright : (C) 2013 by Radim Blazek
6  email : radim dot blazek 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 QGSGML_H
16 #define QGSGML_H
17 
18 #include "qgis_core.h"
19 #include <expat.h>
20 #include "qgis_sip.h"
21 #include "qgsfields.h"
22 #include "qgsrectangle.h"
23 #include "qgswkbptr.h"
24 #include "qgsfeature.h"
25 
26 #include <QPair>
27 #include <QByteArray>
28 #include <QDomElement>
29 #include <QStringList>
30 #include <QStack>
31 #include <QVector>
32 
33 #include <string>
34 
36 
37 #ifndef SIP_RUN
38 
50 class CORE_EXPORT QgsGmlStreamingParser
51 {
52  public:
53 
54  typedef QPair<QgsFeature *, QString> QgsGmlFeaturePtrGmlIdPair;
55 
61  {
62  public:
64  LayerProperties() = default;
65 
67  QString mName;
70  };
71 
73  typedef enum
74  {
81  } AxisOrientationLogic;
82 
84  QgsGmlStreamingParser( const QString &typeName,
85  const QString &geometryAttribute,
86  const QgsFields &fields,
87  AxisOrientationLogic axisOrientationLogic = Honour_EPSG_if_urn,
88  bool invertAxisOrientation = false );
89 
91  QgsGmlStreamingParser( const QList<LayerProperties> &layerProperties,
92  const QgsFields &fields,
93  const QMap< QString, QPair<QString, QString> > &mapFieldNameToSrcLayerNameFieldName,
94  AxisOrientationLogic axisOrientationLogic = Honour_EPSG_if_urn,
95  bool invertAxisOrientation = false );
97 
99  QgsGmlStreamingParser( const QgsGmlStreamingParser &other ) = delete;
102 
107  bool processData( const QByteArray &data, bool atEnd, QString &errorMsg );
108 
113  bool processData( const QByteArray &data, bool atEnd );
114 
121  QVector<QgsGmlFeaturePtrGmlIdPair> getAndStealReadyFeatures();
122 
124  int getEPSGCode() const { return mEpsg; }
125 
127  QString srsName() const { return mSrsName; }
128 
130  const QgsRectangle &layerExtent() const { return mLayerExtent; }
131 
133  QgsWkbTypes::Type wkbType() const { return mWkbType; }
134 
136  int numberMatched() const { return mNumberMatched; }
137 
139  int numberReturned() const { return mNumberReturned; }
140 
142  bool isException() const { return mIsException; }
143 
145  QString exceptionText() const { return mExceptionText; }
146 
148  bool isTruncatedResponse() const { return mTruncatedResponse; }
149 
150  private:
151 
152  enum ParseMode
153  {
154  None,
155  BoundingBox,
156  Null,
157  Envelope,
158  LowerCorner,
159  UpperCorner,
160  Feature, // feature element containing attrs and geo (inside gml:featureMember)
161  Attribute,
162  Tuple, // wfs:Tuple of a join layer
163  FeatureTuple,
164  AttributeTuple,
165  Geometry,
166  Coordinate,
167  PosList,
168  MultiPoint,
169  MultiLine,
170  MultiPolygon,
171  ExceptionReport,
172  ExceptionText
173  };
174 
176  void startElement( const XML_Char *el, const XML_Char **attr );
177  void endElement( const XML_Char *el );
178  void characters( const XML_Char *chars, int len );
179  static void start( void *data, const XML_Char *el, const XML_Char **attr )
180  {
181  static_cast<QgsGmlStreamingParser *>( data )->startElement( el, attr );
182  }
183  static void end( void *data, const XML_Char *el )
184  {
185  static_cast<QgsGmlStreamingParser *>( data )->endElement( el );
186  }
187  static void chars( void *data, const XML_Char *chars, int len )
188  {
189  static_cast<QgsGmlStreamingParser *>( data )->characters( chars, len );
190  }
191 
192  // Set current feature attribute
193  void setAttribute( const QString &name, const QString &value );
194 
195  //helper routines
196 
203  int readEpsgFromAttribute( int &epsgNr, const XML_Char **attr );
204 
211  QString readAttribute( const QString &attributeName, const XML_Char **attr ) const;
213  bool createBBoxFromCoordinateString( QgsRectangle &bb, const QString &coordString ) const;
214 
221  int pointsFromCoordinateString( QList<QgsPointXY> &points, const QString &coordString ) const;
222 
230  int pointsFromPosListString( QList<QgsPointXY> &points, const QString &coordString, int dimension ) const;
231 
232  int pointsFromString( QList<QgsPointXY> &points, const QString &coordString ) const;
233  int getPointWKB( QgsWkbPtr &wkbPtr, const QgsPointXY & ) const;
234  int getLineWKB( QgsWkbPtr &wkbPtr, const QList<QgsPointXY> &lineCoordinates ) const;
235  int getRingWKB( QgsWkbPtr &wkbPtr, const QList<QgsPointXY> &ringCoordinates ) const;
236 
243  int createMultiLineFromFragments();
244  int createMultiPointFromFragments();
245  int createPolygonFromFragments();
246  int createMultiPolygonFromFragments();
248  int totalWKBFragmentSize() const;
249 
251  ParseMode modeStackTop() { return mParseModeStack.isEmpty() ? None : mParseModeStack.top(); }
252 
254  ParseMode modeStackPop() { return mParseModeStack.isEmpty() ? None : mParseModeStack.pop(); }
255 
257  XML_Parser mParser;
258 
260  QVector<QgsGmlFeaturePtrGmlIdPair> mFeatureList;
261 
263  QList<LayerProperties> mLayerProperties;
264  QMap< QString, LayerProperties > mMapTypeNameToProperties;
265 
267  QString mTypeName;
268  QByteArray mTypeNameBA;
269  const char *mTypeNamePtr = nullptr;
270  size_t mTypeNameUTF8Len;
271 
272  QgsWkbTypes::Type mWkbType;
273 
274  //results are members such that handler routines are able to manipulate them
275 
277  QString mGeometryAttribute;
278  QByteArray mGeometryAttributeBA;
279  const char *mGeometryAttributePtr = nullptr;
280  size_t mGeometryAttributeUTF8Len;
281 
282  QgsFields mFields;
283  QMap<QString, QPair<int, QgsField> > mThematicAttributes;
284 
285  bool mIsException;
286  QString mExceptionText;
287  bool mTruncatedResponse;
289  int mParseDepth;
290  int mFeatureTupleDepth;
291  QString mCurrentTypename;
293  QStack<ParseMode> mParseModeStack;
295  QString mStringCash;
296  QgsFeature *mCurrentFeature = nullptr;
297  QVector<QVariant> mCurrentAttributes; //attributes of current feature
298  QString mCurrentFeatureId;
299  int mFeatureCount;
301  QgsWkbPtr mCurrentWKB;
302  QgsRectangle mCurrentExtent;
303  bool mBoundedByNullFound;
304 
311  QList< QList<QgsWkbPtr> > mCurrentWKBFragments;
312  QString mAttributeName;
313  char mEndian;
315  QString mCoordinateSeparator;
317  QString mTupleSeparator;
319  QStack<int> mDimensionStack;
321  int mDimension;
323  ParseMode mCoorMode;
325  int mEpsg;
327  QString mSrsName;
329  QgsRectangle mLayerExtent;
331  QString mGMLNameSpaceURI;
332  const char *mGMLNameSpaceURIPtr = nullptr;
334  AxisOrientationLogic mAxisOrientationLogic;
336  bool mInvertAxisOrientationRequest;
338  bool mInvertAxisOrientation;
340  int mNumberReturned;
342  int mNumberMatched;
344  std::string mGeometryString;
346  bool mFoundUnhandledGeometryElement;
347 };
348 
349 #endif
350 
360 class CORE_EXPORT QgsGml : public QObject
361 {
362  Q_OBJECT
363  public:
364  QgsGml(
365  const QString &typeName,
366  const QString &geometryAttribute,
367  const QgsFields &fields );
368 
381  int getFeatures( const QString &uri,
382  QgsWkbTypes::Type *wkbType,
383  QgsRectangle *extent = nullptr,
384  const QString &userName = QString(),
385  const QString &password = QString(),
386  const QString &authcfg = QString() ) SIP_PYNAME( getFeaturesUri );
387 
392  int getFeatures( const QByteArray &data, QgsWkbTypes::Type *wkbType, QgsRectangle *extent = nullptr );
393 
395  QMap<QgsFeatureId, QgsFeature * > featuresMap() const { return mFeatures; }
396 
398  QMap<QgsFeatureId, QString > idsMap() const { return mIdMap; }
399 
405 
406  signals:
407  void dataReadProgress( int progress );
408  void totalStepsUpdate( int totalSteps );
410  void dataProgressAndSteps( int progress, int totalSteps );
411 
412  private slots:
413 
414  void setFinished();
415 
417  void handleProgressEvent( qint64 progress, qint64 totalSteps );
418 
419  private:
420 
427  void calculateExtentFromFeatures();
428 
429  void fillMapsFromParser();
430 
431  QgsGmlStreamingParser mParser;
432 
434  QString mTypeName;
435 
437  bool mFinished;
438 
440  //QMap<QgsFeatureId, QgsFeature* > &mFeatures;
441  QMap<QgsFeatureId, QgsFeature * > mFeatures;
442  //QMap<QString, QMap<QgsFeatureId, QgsFeature* > > mFeatures;
443 
445  //QMap<QgsFeatureId, QString > &mIdMap;
446  QMap<QgsFeatureId, QString > mIdMap;
447  //QMap<QString, QMap<QgsFeatureId, QString > > mIdMap;
448 
450  QgsRectangle mExtent;
451 };
452 
453 #endif
This class represents a coordinate reference system (CRS).
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:56
Container of fields for a vector layer.
Definition: qgsfields.h:45
QString mGeometryAttribute
Geometry attribute name.
Definition: qgsgml.h:69
LayerProperties()=default
Constructor.
This class builds features from GML data in a streaming way.
Definition: qgsgml.h:51
QgsWkbTypes::Type wkbType() const
Returns the geometry type.
Definition: qgsgml.h:133
QPair< QgsFeature *, QString > QgsGmlFeaturePtrGmlIdPair
Definition: qgsgml.h:54
bool isException() const
Returns whether the document parser is a OGC exception.
Definition: qgsgml.h:142
int numberReturned() const
Returns WFS 2.0 "numberReturned" or WFS 1.1 "numberOfFeatures" attribute, or -1 if invalid/not found.
Definition: qgsgml.h:139
@ Ignore_EPSG
Ignore EPSG axis order.
Definition: qgsgml.h:80
@ Honour_EPSG
Honour EPSG axis order.
Definition: qgsgml.h:78
@ Honour_EPSG_if_urn
Honour EPSG axis order only if srsName is of the form urn:ogc:def:crs:EPSG:
Definition: qgsgml.h:76
QgsGmlStreamingParser & operator=(const QgsGmlStreamingParser &other)=delete
QgsGmlStreamingParser cannot be copied.
int numberMatched() const
Returns WFS 2.0 "numberMatched" attribute, or -1 if invalid/not found.
Definition: qgsgml.h:136
QgsGmlStreamingParser(const QgsGmlStreamingParser &other)=delete
QgsGmlStreamingParser cannot be copied.
bool isTruncatedResponse() const
Returns whether a "truncatedResponse" element is found.
Definition: qgsgml.h:148
QString exceptionText() const
Returns the exception text.
Definition: qgsgml.h:145
int getEPSGCode() const
Returns the EPSG code, or 0 if unknown.
Definition: qgsgml.h:124
const QgsRectangle & layerExtent() const
Returns layer bounding box.
Definition: qgsgml.h:130
QString srsName() const
Returns the value of the srsName attribute.
Definition: qgsgml.h:127
This class reads data from a WFS server or alternatively from a GML file.
Definition: qgsgml.h:361
QMap< QgsFeatureId, QgsFeature * > featuresMap() const
Gets parsed features for given type name.
Definition: qgsgml.h:395
void totalStepsUpdate(int totalSteps)
void dataReadProgress(int progress)
QMap< QgsFeatureId, QString > idsMap() const
Gets feature ids map.
Definition: qgsgml.h:398
void dataProgressAndSteps(int progress, int totalSteps)
Also emit signal with progress and totalSteps together (this is better for the status message)
A class to represent a 2D point.
Definition: qgspointxy.h:44
A rectangle specified with double values.
Definition: qgsrectangle.h:42
WKB pointer handler.
Definition: qgswkbptr.h:44
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:70
#define SIP_PYNAME(name)
Definition: qgis_sip.h:81
const QgsCoordinateReferenceSystem & crs
const QString & typeName