QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 <expat.h>
19 #include "qgis.h"
20 #include "qgsapplication.h"
22 #include "qgsdataprovider.h"
23 #include "qgsfeature.h"
24 #include "qgsfield.h"
25 #include "qgslogger.h"
26 #include "qgspoint.h"
27 #include "qgsrectangle.h"
28 #include "qgswkbptr.h"
29 
30 #include <QPair>
31 #include <QByteArray>
32 #include <QDomElement>
33 #include <QStringList>
34 #include <QStack>
35 #include <QVector>
36 
37 #include <string>
38 
47 class CORE_EXPORT QgsGmlStreamingParser
48 {
49  public:
50 
52 
57  {
58  public:
61 
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 
96  bool processData( const QByteArray& data, bool atEnd, QString& errorMsg );
97 
100  bool processData( const QByteArray& data, bool atEnd );
101 
106  QVector<QgsGmlFeaturePtrGmlIdPair> getAndStealReadyFeatures();
107 
109  int getEPSGCode() const { return mEpsg; }
110 
112  const QString& srsName() const { return mSrsName; }
113 
115  const QgsRectangle& layerExtent() const { return mLayerExtent; }
116 
118  QGis::WkbType wkbType() const { return mWkbType; }
119 
121  int numberMatched() const { return mNumberMatched; }
122 
124  int numberReturned() const { return mNumberReturned; }
125 
127  bool isException() const { return mIsException; }
128 
130  const QString& exceptionText() const { return mExceptionText; }
131 
133  bool isTruncatedResponse() const { return mTruncatedResponse; }
134 
135  private:
136 
137  enum ParseMode
138  {
139  none,
140  boundingBox,
141  null,
142  envelope,
143  lowerCorner,
144  upperCorner,
145  feature, // feature element containing attrs and geo (inside gml:featureMember)
146  attribute,
147  tuple, // wfs:Tuple of a join layer
148  featureTuple,
149  attributeTuple,
150  geometry,
151  coordinate,
152  posList,
153  multiPoint,
154  multiLine,
155  multiPolygon,
156  ExceptionReport,
157  ExceptionText
158  };
159 
161  void startElement( const XML_Char* el, const XML_Char** attr );
162  void endElement( const XML_Char* el );
163  void characters( const XML_Char* chars, int len );
164  static void start( void* data, const XML_Char* el, const XML_Char** attr )
165  {
166  static_cast<QgsGmlStreamingParser*>( data )->startElement( el, attr );
167  }
168  static void end( void* data, const XML_Char* el )
169  {
170  static_cast<QgsGmlStreamingParser*>( data )->endElement( el );
171  }
172  static void chars( void* data, const XML_Char* chars, int len )
173  {
174  static_cast<QgsGmlStreamingParser*>( data )->characters( chars, len );
175  }
176 
177  // Set current feature attribute
178  void setAttribute( const QString& name, const QString& value );
179 
180  //helper routines
181 
187  int readEpsgFromAttribute( int& epsgNr, const XML_Char** attr );
193  QString readAttribute( const QString& attributeName, const XML_Char** attr ) const;
195  bool createBBoxFromCoordinateString( QgsRectangle &bb, const QString& coordString ) const;
201  int pointsFromCoordinateString( QList<QgsPoint>& points, const QString& coordString ) const;
202 
209  int pointsFromPosListString( QList<QgsPoint>& points, const QString& coordString, int dimension ) const;
210 
211  int pointsFromString( QList<QgsPoint>& points, const QString& coordString ) const;
212  int getPointWKB( QgsWkbPtr &wkbPtr, const QgsPoint& ) const;
213  int getLineWKB( QgsWkbPtr &wkbPtr, const QList<QgsPoint>& lineCoordinates ) const;
214  int getRingWKB( QgsWkbPtr &wkbPtr, const QList<QgsPoint>& ringCoordinates ) const;
220  int createMultiLineFromFragments();
221  int createMultiPointFromFragments();
222  int createPolygonFromFragments();
223  int createMultiPolygonFromFragments();
225  int totalWKBFragmentSize() const;
226 
228  ParseMode modeStackTop() { return mParseModeStack.isEmpty() ? none : mParseModeStack.top(); }
229 
231  ParseMode modeStackPop() { return mParseModeStack.isEmpty() ? none : mParseModeStack.pop(); }
232 
234  XML_Parser mParser;
235 
238 
240  QList<LayerProperties> mLayerProperties;
241  QMap< QString, LayerProperties > mMapTypeNameToProperties;
242 
244  QString mTypeName;
245  QByteArray mTypeNameBA;
246  const char* mTypeNamePtr;
247  size_t mTypeNameUTF8Len;
248 
249  QGis::WkbType mWkbType;
250 
251  //results are members such that handler routines are able to manipulate them
252 
254  QString mGeometryAttribute;
255  QByteArray mGeometryAttributeBA;
256  const char* mGeometryAttributePtr;
257  size_t mGeometryAttributeUTF8Len;
258 
259  QgsFields mFields;
260  QMap<QString, QPair<int, QgsField> > mThematicAttributes;
261 
262  bool mIsException;
263  QString mExceptionText;
264  bool mTruncatedResponse;
266  int mParseDepth;
267  int mFeatureTupleDepth;
268  QString mCurrentTypename;
270  QStack<ParseMode> mParseModeStack;
272  QString mStringCash;
273  QgsFeature* mCurrentFeature;
274  QVector<QVariant> mCurrentAttributes; //attributes of current feature
275  QString mCurrentFeatureId;
276  int mFeatureCount;
278  QgsWkbPtr mCurrentWKB;
279  QgsRectangle mCurrentExtent;
280  bool mBoundedByNullFound;
285  QList< QList<QgsWkbPtr> > mCurrentWKBFragments;
286  QString mAttributeName;
287  char mEndian;
289  QString mCoordinateSeparator;
291  QString mTupleSeparator;
293  QStack<int> mDimensionStack;
295  int mDimension;
297  ParseMode mCoorMode;
299  int mEpsg;
301  QString mSrsName;
303  QgsRectangle mLayerExtent;
305  QString mGMLNameSpaceURI;
306  const char* mGMLNameSpaceURIPtr;
308  AxisOrientationLogic mAxisOrientationLogic;
310  bool mInvertAxisOrientationRequest;
312  bool mInvertAxisOrientation;
314  int mNumberReturned;
316  int mNumberMatched;
318  std::string mGeometryString;
320  bool mFoundUnhandledGeometryElement;
321 };
322 
323 
329 class CORE_EXPORT QgsGml : public QObject
330 {
331  Q_OBJECT
332  public:
333  QgsGml(
334  const QString& typeName,
335  const QString& geometryAttribute,
336  const QgsFields & fields );
337 
338  ~QgsGml();
339 
351  int getFeatures( const QString& uri,
352  QGis::WkbType* wkbType,
353  QgsRectangle* extent = nullptr,
354  const QString& userName = QString(),
355  const QString& password = QString(),
356  const QString& authcfg = QString() );
357 
361  int getFeatures( const QByteArray &data, QGis::WkbType* wkbType, QgsRectangle* extent = nullptr );
362 
364  QMap<QgsFeatureId, QgsFeature* > featuresMap() const { return mFeatures; }
365 
367  QMap<QgsFeatureId, QString > idsMap() const { return mIdMap; }
368 
371  QgsCoordinateReferenceSystem crs() const;
372 
373  private slots:
374 
375  void setFinished();
376 
378  void handleProgressEvent( qint64 progress, qint64 totalSteps );
379 
380  signals:
381  void dataReadProgress( int progress );
382  void totalStepsUpdate( int totalSteps );
383  //also emit signal with progress and totalSteps together (this is better for the status message)
384  void dataProgressAndSteps( int progress, int totalSteps );
385 
386  private:
387 
393  void calculateExtentFromFeatures();
394 
395  void fillMapsFromParser();
396 
397  QgsGmlStreamingParser mParser;
398 
400  QString mTypeName;
401 
403  bool mFinished;
404 
406  //QMap<QgsFeatureId, QgsFeature* > &mFeatures;
408  //QMap<QString, QMap<QgsFeatureId, QgsFeature* > > mFeatures;
409 
411  //QMap<QgsFeatureId, QString > &mIdMap;
413  //QMap<QString, QMap<QgsFeatureId, QString > > mIdMap;
414 
416  QgsRectangle mExtent;
417 };
418 
419 #endif
const QString & srsName() const
Return the value of the srsName attribute.
Definition: qgsgml.h:112
int getEPSGCode() const
Return the EPSG code, or 0 if unknown.
Definition: qgsgml.h:109
A rectangle specified with double values.
Definition: qgsrectangle.h:35
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:124
int numberMatched() const
Return WFS 2.0 "numberMatched" attribute, or -1 if invalid/not found.
Definition: qgsgml.h:121
Container of fields for a vector layer.
Definition: qgsfield.h:252
WkbType
Used for symbology operations.
Definition: qgis.h:61
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:187
bool isException() const
Return whether the document parser is a OGC exception.
Definition: qgsgml.h:127
Ignore EPSG axis order.
Definition: qgsgml.h:76
Honour EPSG axis order.
Definition: qgsgml.h:74
This class reads data from a WFS server or alternatively from a GML file.
Definition: qgsgml.h:329
QMap< QgsFeatureId, QgsFeature *> featuresMap() const
Get parsed features for given type name.
Definition: qgsgml.h:364
QMap< QgsFeatureId, QString > idsMap() const
Get feature ids map.
Definition: qgsgml.h:367
Honour EPSG axis order only if srsName is of the form urn:ogc:def:crs:EPSG:
Definition: qgsgml.h:72
QPair< QgsFeature *, QString > QgsGmlFeaturePtrGmlIdPair
Definition: qgsgml.h:51
A class to represent a point.
Definition: qgspoint.h:117
QString mGeometryAttribute
Geometry attribute name.
Definition: qgsgml.h:65
const QgsRectangle & layerExtent() const
Return layer bounding box.
Definition: qgsgml.h:115
bool isTruncatedResponse() const
Return whether a "truncatedResponse" element is found.
Definition: qgsgml.h:133
Class for storing a coordinate reference system (CRS)
const QString & exceptionText() const
Return the exception text.
Definition: qgsgml.h:130
QGis::WkbType wkbType() const
Return the geometry type.
Definition: qgsgml.h:118
This class builds features from GML data in a streaming way.
Definition: qgsgml.h:47