QGIS API Documentation  2.99.0-Master (716ff6c)
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.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 
45 class CORE_EXPORT QgsGmlStreamingParser
46 {
47  public:
48 
49  typedef QPair<QgsFeature *, QString> QgsGmlFeaturePtrGmlIdPair;
50 
55  {
56  public:
59 
61  QString mName;
64  };
65 
67  typedef enum
68  {
76 
78  QgsGmlStreamingParser( const QString &typeName,
79  const QString &geometryAttribute,
80  const QgsFields &fields,
81  AxisOrientationLogic axisOrientationLogic = Honour_EPSG_if_urn,
82  bool invertAxisOrientation = false );
83 
85  QgsGmlStreamingParser( const QList<LayerProperties> &layerProperties,
86  const QgsFields &fields,
87  const QMap< QString, QPair<QString, QString> > &mapFieldNameToSrcLayerNameFieldName,
88  AxisOrientationLogic axisOrientationLogic = Honour_EPSG_if_urn,
89  bool invertAxisOrientation = false );
91 
93  QgsGmlStreamingParser( const QgsGmlStreamingParser &other ) = delete;
95  QgsGmlStreamingParser &operator=( const QgsGmlStreamingParser &other ) = delete;
96 
99  bool processData( const QByteArray &data, bool atEnd, QString &errorMsg );
100 
103  bool processData( const QByteArray &data, bool atEnd );
104 
109  QVector<QgsGmlFeaturePtrGmlIdPair> getAndStealReadyFeatures();
110 
112  int getEPSGCode() const { return mEpsg; }
113 
115  QString srsName() const { return mSrsName; }
116 
118  const QgsRectangle &layerExtent() const { return mLayerExtent; }
119 
121  QgsWkbTypes::Type wkbType() const { return mWkbType; }
122 
124  int numberMatched() const { return mNumberMatched; }
125 
127  int numberReturned() const { return mNumberReturned; }
128 
130  bool isException() const { return mIsException; }
131 
133  QString exceptionText() const { return mExceptionText; }
134 
136  bool isTruncatedResponse() const { return mTruncatedResponse; }
137 
138  private:
139 
140  enum ParseMode
141  {
142  None,
143  BoundingBox,
144  Null,
145  Envelope,
146  LowerCorner,
147  UpperCorner,
148  Feature, // feature element containing attrs and geo (inside gml:featureMember)
149  Attribute,
150  Tuple, // wfs:Tuple of a join layer
151  FeatureTuple,
152  AttributeTuple,
153  Geometry,
154  Coordinate,
155  PosList,
156  MultiPoint,
157  MultiLine,
158  MultiPolygon,
159  ExceptionReport,
160  ExceptionText
161  };
162 
164  void startElement( const XML_Char *el, const XML_Char **attr );
165  void endElement( const XML_Char *el );
166  void characters( const XML_Char *chars, int len );
167  static void start( void *data, const XML_Char *el, const XML_Char **attr )
168  {
169  static_cast<QgsGmlStreamingParser *>( data )->startElement( el, attr );
170  }
171  static void end( void *data, const XML_Char *el )
172  {
173  static_cast<QgsGmlStreamingParser *>( data )->endElement( el );
174  }
175  static void chars( void *data, const XML_Char *chars, int len )
176  {
177  static_cast<QgsGmlStreamingParser *>( data )->characters( chars, len );
178  }
179 
180  // Set current feature attribute
181  void setAttribute( const QString &name, const QString &value );
182 
183  //helper routines
184 
190  int readEpsgFromAttribute( int &epsgNr, const XML_Char **attr );
191 
197  QString readAttribute( const QString &attributeName, const XML_Char **attr ) const;
199  bool createBBoxFromCoordinateString( QgsRectangle &bb, const QString &coordString ) const;
200 
206  int pointsFromCoordinateString( QList<QgsPoint> &points, const QString &coordString ) const;
207 
214  int pointsFromPosListString( QList<QgsPoint> &points, const QString &coordString, int dimension ) const;
215 
216  int pointsFromString( QList<QgsPoint> &points, const QString &coordString ) const;
217  int getPointWKB( QgsWkbPtr &wkbPtr, const QgsPoint & ) const;
218  int getLineWKB( QgsWkbPtr &wkbPtr, const QList<QgsPoint> &lineCoordinates ) const;
219  int getRingWKB( QgsWkbPtr &wkbPtr, const QList<QgsPoint> &ringCoordinates ) const;
220 
226  int createMultiLineFromFragments();
227  int createMultiPointFromFragments();
228  int createPolygonFromFragments();
229  int createMultiPolygonFromFragments();
231  int totalWKBFragmentSize() const;
232 
234  ParseMode modeStackTop() { return mParseModeStack.isEmpty() ? None : mParseModeStack.top(); }
235 
237  ParseMode modeStackPop() { return mParseModeStack.isEmpty() ? None : mParseModeStack.pop(); }
238 
240  XML_Parser mParser;
241 
243  QVector<QgsGmlFeaturePtrGmlIdPair> mFeatureList;
244 
246  QList<LayerProperties> mLayerProperties;
247  QMap< QString, LayerProperties > mMapTypeNameToProperties;
248 
250  QString mTypeName;
251  QByteArray mTypeNameBA;
252  const char *mTypeNamePtr = nullptr;
253  size_t mTypeNameUTF8Len;
254 
255  QgsWkbTypes::Type mWkbType;
256 
257  //results are members such that handler routines are able to manipulate them
258 
260  QString mGeometryAttribute;
261  QByteArray mGeometryAttributeBA;
262  const char *mGeometryAttributePtr = nullptr;
263  size_t mGeometryAttributeUTF8Len;
264 
265  QgsFields mFields;
266  QMap<QString, QPair<int, QgsField> > mThematicAttributes;
267 
268  bool mIsException;
269  QString mExceptionText;
270  bool mTruncatedResponse;
272  int mParseDepth;
273  int mFeatureTupleDepth;
274  QString mCurrentTypename;
275  QStack<ParseMode> mParseModeStack;
278  QString mStringCash;
279  QgsFeature *mCurrentFeature = nullptr;
280  QVector<QVariant> mCurrentAttributes; //attributes of current feature
281  QString mCurrentFeatureId;
282  int mFeatureCount;
284  QgsWkbPtr mCurrentWKB;
285  QgsRectangle mCurrentExtent;
286  bool mBoundedByNullFound;
287 
292  QList< QList<QgsWkbPtr> > mCurrentWKBFragments;
293  QString mAttributeName;
294  char mEndian;
296  QString mCoordinateSeparator;
298  QString mTupleSeparator;
300  int mDimension;
302  ParseMode mCoorMode;
304  int mEpsg;
306  QString mSrsName;
308  QgsRectangle mLayerExtent;
310  QString mGMLNameSpaceURI;
311  const char *mGMLNameSpaceURIPtr = nullptr;
313  AxisOrientationLogic mAxisOrientationLogic;
315  bool mInvertAxisOrientationRequest;
317  bool mInvertAxisOrientation;
319  int mNumberReturned;
321  int mNumberMatched;
323  std::string mGeometryString;
325  bool mFoundUnhandledGeometryElement;
326 };
327 
328 
334 class CORE_EXPORT QgsGml : public QObject
335 {
336  Q_OBJECT
337  public:
338  QgsGml(
339  const QString &typeName,
340  const QString &geometryAttribute,
341  const QgsFields &fields );
342 
354  int getFeatures( const QString &uri,
355  QgsWkbTypes::Type *wkbType,
356  QgsRectangle *extent = nullptr,
357  const QString &userName = QString(),
358  const QString &password = QString(),
359  const QString &authcfg = QString() );
360 
364  int getFeatures( const QByteArray &data, QgsWkbTypes::Type *wkbType, QgsRectangle *extent = nullptr );
365 
367  QMap<QgsFeatureId, QgsFeature * > featuresMap() const { return mFeatures; }
368 
370  QMap<QgsFeatureId, QString > idsMap() const { return mIdMap; }
371 
374  QgsCoordinateReferenceSystem crs() const;
375 
376  private slots:
377 
378  void setFinished();
379 
381  void handleProgressEvent( qint64 progress, qint64 totalSteps );
382 
383  signals:
384  void dataReadProgress( int progress );
385  void totalStepsUpdate( int totalSteps );
386  //also emit signal with progress and totalSteps together (this is better for the status message)
387  void dataProgressAndSteps( int progress, int totalSteps );
388 
389  private:
390 
396  void calculateExtentFromFeatures();
397 
398  void fillMapsFromParser();
399 
400  QgsGmlStreamingParser mParser;
401 
403  QString mTypeName;
404 
406  bool mFinished;
407 
409  //QMap<QgsFeatureId, QgsFeature* > &mFeatures;
410  QMap<QgsFeatureId, QgsFeature * > mFeatures;
411  //QMap<QString, QMap<QgsFeatureId, QgsFeature* > > mFeatures;
412 
414  //QMap<QgsFeatureId, QString > &mIdMap;
415  QMap<QgsFeatureId, QString > mIdMap;
416  //QMap<QString, QMap<QgsFeatureId, QString > > mIdMap;
417 
419  QgsRectangle mExtent;
420 };
421 
422 #endif
int getEPSGCode() const
Return the EPSG code, or 0 if unknown.
Definition: qgsgml.h:112
A rectangle specified with double values.
Definition: qgsrectangle.h:38
AxisOrientationLogic
Axis orientation logic.
Definition: qgsgml.h:67
int numberReturned() const
Return WFS 2.0 "numberReturned" or WFS 1.1 "numberOfFeatures" attribute, or -1 if invalid/not found...
Definition: qgsgml.h:127
QString srsName() const
Return the value of the srsName attribute.
Definition: qgsgml.h:115
int numberMatched() const
Return WFS 2.0 "numberMatched" attribute, or -1 if invalid/not found.
Definition: qgsgml.h:124
Container of fields for a vector layer.
Definition: qgsfields.h:39
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:61
QMap< QgsFeatureId, QgsFeature *> featuresMap() const
Get parsed features for given type name.
Definition: qgsgml.h:367
bool isException() const
Return whether the document parser is a OGC exception.
Definition: qgsgml.h:130
Ignore EPSG axis order.
Definition: qgsgml.h:74
Honour EPSG axis order.
Definition: qgsgml.h:72
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:65
This class reads data from a WFS server or alternatively from a GML file.
Definition: qgsgml.h:334
QString exceptionText() const
Return the exception text.
Definition: qgsgml.h:133
QMap< QgsFeatureId, QString > idsMap() const
Get feature ids map.
Definition: qgsgml.h:370
Honour EPSG axis order only if srsName is of the form urn:ogc:def:crs:EPSG: *.
Definition: qgsgml.h:70
A class to represent a point.
Definition: qgspoint.h:37
QString mGeometryAttribute
Geometry attribute name.
Definition: qgsgml.h:63
const QgsRectangle & layerExtent() const
Return layer bounding box.
Definition: qgsgml.h:118
QPair< QgsFeature *, QString > QgsGmlFeaturePtrGmlIdPair
Definition: qgsgml.h:49
bool isTruncatedResponse() const
Return whether a "truncatedResponse" element is found.
Definition: qgsgml.h:136
This class represents a coordinate reference system (CRS).
QgsWkbTypes::Type wkbType() const
Return the geometry type.
Definition: qgsgml.h:121
This class builds features from GML data in a streaming way.
Definition: qgsgml.h:45