QGIS API Documentation  2.99.0-Master (0cba29c)
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 
37 #ifndef SIP_RUN
38 
47 class CORE_EXPORT QgsGmlStreamingParser
48 {
49  public:
50 
51  typedef QPair<QgsFeature *, QString> QgsGmlFeaturePtrGmlIdPair;
52 
57  {
58  public:
61 
63  QString mName;
66  };
67 
69  typedef enum
70  {
78 
80  QgsGmlStreamingParser( const QString &typeName,
81  const QString &geometryAttribute,
82  const QgsFields &fields,
83  AxisOrientationLogic axisOrientationLogic = Honour_EPSG_if_urn,
84  bool invertAxisOrientation = false );
85 
87  QgsGmlStreamingParser( const QList<LayerProperties> &layerProperties,
88  const QgsFields &fields,
89  const QMap< QString, QPair<QString, QString> > &mapFieldNameToSrcLayerNameFieldName,
90  AxisOrientationLogic axisOrientationLogic = Honour_EPSG_if_urn,
91  bool invertAxisOrientation = false );
93 
95  QgsGmlStreamingParser( const QgsGmlStreamingParser &other ) = delete;
97  QgsGmlStreamingParser &operator=( const QgsGmlStreamingParser &other ) = delete;
98 
101  bool processData( const QByteArray &data, bool atEnd, QString &errorMsg );
102 
105  bool processData( const QByteArray &data, bool atEnd );
106 
111  QVector<QgsGmlFeaturePtrGmlIdPair> getAndStealReadyFeatures();
112 
114  int getEPSGCode() const { return mEpsg; }
115 
117  QString srsName() const { return mSrsName; }
118 
120  const QgsRectangle &layerExtent() const { return mLayerExtent; }
121 
123  QgsWkbTypes::Type wkbType() const { return mWkbType; }
124 
126  int numberMatched() const { return mNumberMatched; }
127 
129  int numberReturned() const { return mNumberReturned; }
130 
132  bool isException() const { return mIsException; }
133 
135  QString exceptionText() const { return mExceptionText; }
136 
138  bool isTruncatedResponse() const { return mTruncatedResponse; }
139 
140  private:
141 
142  enum ParseMode
143  {
144  None,
145  BoundingBox,
146  Null,
147  Envelope,
148  LowerCorner,
149  UpperCorner,
150  Feature, // feature element containing attrs and geo (inside gml:featureMember)
151  Attribute,
152  Tuple, // wfs:Tuple of a join layer
153  FeatureTuple,
154  AttributeTuple,
155  Geometry,
156  Coordinate,
157  PosList,
158  MultiPoint,
159  MultiLine,
160  MultiPolygon,
161  ExceptionReport,
162  ExceptionText
163  };
164 
166  void startElement( const XML_Char *el, const XML_Char **attr );
167  void endElement( const XML_Char *el );
168  void characters( const XML_Char *chars, int len );
169  static void start( void *data, const XML_Char *el, const XML_Char **attr )
170  {
171  static_cast<QgsGmlStreamingParser *>( data )->startElement( el, attr );
172  }
173  static void end( void *data, const XML_Char *el )
174  {
175  static_cast<QgsGmlStreamingParser *>( data )->endElement( el );
176  }
177  static void chars( void *data, const XML_Char *chars, int len )
178  {
179  static_cast<QgsGmlStreamingParser *>( data )->characters( chars, len );
180  }
181 
182  // Set current feature attribute
183  void setAttribute( const QString &name, const QString &value );
184 
185  //helper routines
186 
192  int readEpsgFromAttribute( int &epsgNr, const XML_Char **attr );
193 
199  QString readAttribute( const QString &attributeName, const XML_Char **attr ) const;
201  bool createBBoxFromCoordinateString( QgsRectangle &bb, const QString &coordString ) const;
202 
208  int pointsFromCoordinateString( QList<QgsPointXY> &points, const QString &coordString ) const;
209 
216  int pointsFromPosListString( QList<QgsPointXY> &points, const QString &coordString, int dimension ) const;
217 
218  int pointsFromString( QList<QgsPointXY> &points, const QString &coordString ) const;
219  int getPointWKB( QgsWkbPtr &wkbPtr, const QgsPointXY & ) const;
220  int getLineWKB( QgsWkbPtr &wkbPtr, const QList<QgsPointXY> &lineCoordinates ) const;
221  int getRingWKB( QgsWkbPtr &wkbPtr, const QList<QgsPointXY> &ringCoordinates ) const;
222 
228  int createMultiLineFromFragments();
229  int createMultiPointFromFragments();
230  int createPolygonFromFragments();
231  int createMultiPolygonFromFragments();
233  int totalWKBFragmentSize() const;
234 
236  ParseMode modeStackTop() { return mParseModeStack.isEmpty() ? None : mParseModeStack.top(); }
237 
239  ParseMode modeStackPop() { return mParseModeStack.isEmpty() ? None : mParseModeStack.pop(); }
240 
242  XML_Parser mParser;
243 
245  QVector<QgsGmlFeaturePtrGmlIdPair> mFeatureList;
246 
248  QList<LayerProperties> mLayerProperties;
249  QMap< QString, LayerProperties > mMapTypeNameToProperties;
250 
252  QString mTypeName;
253  QByteArray mTypeNameBA;
254  const char *mTypeNamePtr = nullptr;
255  size_t mTypeNameUTF8Len;
256 
257  QgsWkbTypes::Type mWkbType;
258 
259  //results are members such that handler routines are able to manipulate them
260 
262  QString mGeometryAttribute;
263  QByteArray mGeometryAttributeBA;
264  const char *mGeometryAttributePtr = nullptr;
265  size_t mGeometryAttributeUTF8Len;
266 
267  QgsFields mFields;
268  QMap<QString, QPair<int, QgsField> > mThematicAttributes;
269 
270  bool mIsException;
271  QString mExceptionText;
272  bool mTruncatedResponse;
274  int mParseDepth;
275  int mFeatureTupleDepth;
276  QString mCurrentTypename;
277  QStack<ParseMode> mParseModeStack;
280  QString mStringCash;
281  QgsFeature *mCurrentFeature = nullptr;
282  QVector<QVariant> mCurrentAttributes; //attributes of current feature
283  QString mCurrentFeatureId;
284  int mFeatureCount;
286  QgsWkbPtr mCurrentWKB;
287  QgsRectangle mCurrentExtent;
288  bool mBoundedByNullFound;
289 
294  QList< QList<QgsWkbPtr> > mCurrentWKBFragments;
295  QString mAttributeName;
296  char mEndian;
298  QString mCoordinateSeparator;
300  QString mTupleSeparator;
302  int mDimension;
304  ParseMode mCoorMode;
306  int mEpsg;
308  QString mSrsName;
310  QgsRectangle mLayerExtent;
312  QString mGMLNameSpaceURI;
313  const char *mGMLNameSpaceURIPtr = nullptr;
315  AxisOrientationLogic mAxisOrientationLogic;
317  bool mInvertAxisOrientationRequest;
319  bool mInvertAxisOrientation;
321  int mNumberReturned;
323  int mNumberMatched;
325  std::string mGeometryString;
327  bool mFoundUnhandledGeometryElement;
328 };
329 
330 #endif
331 
337 class CORE_EXPORT QgsGml : public QObject
338 {
339  Q_OBJECT
340  public:
341  QgsGml(
342  const QString &typeName,
343  const QString &geometryAttribute,
344  const QgsFields &fields );
345 
357  int getFeatures( const QString &uri,
358  QgsWkbTypes::Type *wkbType,
359  QgsRectangle *extent = nullptr,
360  const QString &userName = QString(),
361  const QString &password = QString(),
362  const QString &authcfg = QString() ) SIP_PYNAME( getFeaturesUri );
363 
367  int getFeatures( const QByteArray &data, QgsWkbTypes::Type *wkbType, QgsRectangle *extent = nullptr );
368 
370  QMap<QgsFeatureId, QgsFeature * > featuresMap() const { return mFeatures; }
371 
373  QMap<QgsFeatureId, QString > idsMap() const { return mIdMap; }
374 
377  QgsCoordinateReferenceSystem crs() const;
378 
379  signals:
380  void dataReadProgress( int progress );
381  void totalStepsUpdate( int totalSteps );
383  void dataProgressAndSteps( int progress, int totalSteps );
384 
385  private slots:
386 
387  void setFinished();
388 
390  void handleProgressEvent( qint64 progress, qint64 totalSteps );
391 
392  private:
393 
399  void calculateExtentFromFeatures();
400 
401  void fillMapsFromParser();
402 
403  QgsGmlStreamingParser mParser;
404 
406  QString mTypeName;
407 
409  bool mFinished;
410 
412  //QMap<QgsFeatureId, QgsFeature* > &mFeatures;
413  QMap<QgsFeatureId, QgsFeature * > mFeatures;
414  //QMap<QString, QMap<QgsFeatureId, QgsFeature* > > mFeatures;
415 
417  //QMap<QgsFeatureId, QString > &mIdMap;
418  QMap<QgsFeatureId, QString > mIdMap;
419  //QMap<QString, QMap<QgsFeatureId, QString > > mIdMap;
420 
422  QgsRectangle mExtent;
423 };
424 
425 #endif
int getEPSGCode() const
Return the EPSG code, or 0 if unknown.
Definition: qgsgml.h:114
A rectangle specified with double values.
Definition: qgsrectangle.h:38
AxisOrientationLogic
Axis orientation logic.
Definition: qgsgml.h:69
int numberReturned() const
Return WFS 2.0 "numberReturned" or WFS 1.1 "numberOfFeatures" attribute, or -1 if invalid/not found...
Definition: qgsgml.h:129
QString srsName() const
Return the value of the srsName attribute.
Definition: qgsgml.h:117
A class to represent a 2D point.
Definition: qgspointxy.h:42
int numberMatched() const
Return WFS 2.0 "numberMatched" attribute, or -1 if invalid/not found.
Definition: qgsgml.h:126
Container of fields for a vector layer.
Definition: qgsfields.h:41
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:370
bool isException() const
Return whether the document parser is a OGC exception.
Definition: qgsgml.h:132
Ignore EPSG axis order.
Definition: qgsgml.h:76
Honour EPSG axis order.
Definition: qgsgml.h:74
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:66
This class reads data from a WFS server or alternatively from a GML file.
Definition: qgsgml.h:337
QString exceptionText() const
Return the exception text.
Definition: qgsgml.h:135
QMap< QgsFeatureId, QString > idsMap() const
Get feature ids map.
Definition: qgsgml.h:373
Honour EPSG axis order only if srsName is of the form urn:ogc:def:crs:EPSG: *.
Definition: qgsgml.h:72
QString mGeometryAttribute
Geometry attribute name.
Definition: qgsgml.h:65
const QgsRectangle & layerExtent() const
Return layer bounding box.
Definition: qgsgml.h:120
QPair< QgsFeature *, QString > QgsGmlFeaturePtrGmlIdPair
Definition: qgsgml.h:51
bool isTruncatedResponse() const
Return whether a "truncatedResponse" element is found.
Definition: qgsgml.h:138
This class represents a coordinate reference system (CRS).
QgsWkbTypes::Type wkbType() const
Return the geometry type.
Definition: qgsgml.h:123
This class builds features from GML data in a streaming way.
Definition: qgsgml.h:47
#define SIP_PYNAME(name)
Definition: qgis_sip.h:74