QGIS API Documentation  2.17.0-Master (3a3b9ab7)
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 
248  QGis::WkbType mWkbType;
249 
250  //results are members such that handler routines are able to manipulate them
251 
253  QString mGeometryAttribute;
254  QByteArray mGeometryAttributeBA;
255  const char* mGeometryAttributePtr;
256 
257  QgsFields mFields;
258  QMap<QString, QPair<int, QgsField> > mThematicAttributes;
259 
260  bool mIsException;
261  QString mExceptionText;
262  bool mTruncatedResponse;
264  int mParseDepth;
265  int mFeatureTupleDepth;
266  QString mCurrentTypename;
268  QStack<ParseMode> mParseModeStack;
270  QString mStringCash;
271  QgsFeature* mCurrentFeature;
272  QVector<QVariant> mCurrentAttributes; //attributes of current feature
273  QString mCurrentFeatureId;
274  int mFeatureCount;
276  QgsWkbPtr mCurrentWKB;
277  QgsRectangle mCurrentExtent;
278  bool mBoundedByNullFound;
283  QList< QList<QgsWkbPtr> > mCurrentWKBFragments;
284  QString mAttributeName;
285  char mEndian;
287  QString mCoordinateSeparator;
289  QString mTupleSeparator;
291  int mDimension;
293  ParseMode mCoorMode;
295  int mEpsg;
297  QString mSrsName;
299  QgsRectangle mLayerExtent;
301  QString mGMLNameSpaceURI;
302  const char* mGMLNameSpaceURIPtr;
304  AxisOrientationLogic mAxisOrientationLogic;
306  bool mInvertAxisOrientationRequest;
308  bool mInvertAxisOrientation;
310  int mNumberReturned;
312  int mNumberMatched;
314  std::string mGeometryString;
316  bool mFoundUnhandledGeometryElement;
317 };
318 
319 
325 class CORE_EXPORT QgsGml : public QObject
326 {
327  Q_OBJECT
328  public:
329  QgsGml(
330  const QString& typeName,
331  const QString& geometryAttribute,
332  const QgsFields & fields );
333 
334  ~QgsGml();
335 
347  int getFeatures( const QString& uri,
348  QGis::WkbType* wkbType,
349  QgsRectangle* extent = nullptr,
350  const QString& userName = QString(),
351  const QString& password = QString(),
352  const QString& authcfg = QString() );
353 
357  int getFeatures( const QByteArray &data, QGis::WkbType* wkbType, QgsRectangle* extent = nullptr );
358 
360  QMap<QgsFeatureId, QgsFeature* > featuresMap() const { return mFeatures; }
361 
363  QMap<QgsFeatureId, QString > idsMap() const { return mIdMap; }
364 
367  QgsCoordinateReferenceSystem crs() const;
368 
369  private slots:
370 
371  void setFinished();
372 
374  void handleProgressEvent( qint64 progress, qint64 totalSteps );
375 
376  signals:
377  void dataReadProgress( int progress );
378  void totalStepsUpdate( int totalSteps );
379  //also emit signal with progress and totalSteps together (this is better for the status message)
380  void dataProgressAndSteps( int progress, int totalSteps );
381 
382  private:
383 
389  void calculateExtentFromFeatures();
390 
391  void fillMapsFromParser();
392 
393  QgsGmlStreamingParser mParser;
394 
396  QString mTypeName;
397 
399  bool mFinished;
400 
402  //QMap<QgsFeatureId, QgsFeature* > &mFeatures;
404  //QMap<QString, QMap<QgsFeatureId, QgsFeature* > > mFeatures;
405 
407  //QMap<QgsFeatureId, QString > &mIdMap;
409  //QMap<QString, QMap<QgsFeatureId, QString > > mIdMap;
410 
412  QgsRectangle mExtent;
413 };
414 
415 #endif
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
const QString & exceptionText() const
Return the exception text.
Definition: qgsgml.h:130
AxisOrientationLogic
Axis orientation logic.
Definition: qgsgml.h:69
const QgsRectangle & layerExtent() const
Return layer bounding box.
Definition: qgsgml.h:115
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:209
WkbType
Used for symbology operations.
Definition: qgis.h:61
int numberReturned() const
Return WFS 2.0 "numberReturned" or WFS 1.1 "numberOfFeatures" attribute, or -1 if invalid/not found...
Definition: qgsgml.h:124
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:187
QMap< QgsFeatureId, QString > idsMap() const
Get feature ids map.
Definition: qgsgml.h:363
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:325
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
bool isTruncatedResponse() const
Return whether a "truncatedResponse" element is found.
Definition: qgsgml.h:133
QMap< QgsFeatureId, QgsFeature * > featuresMap() const
Get parsed features for given type name.
Definition: qgsgml.h:360
Class for storing a coordinate reference system (CRS)
bool isException() const
Return whether the document parser is a OGC exception.
Definition: qgsgml.h:127
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
const QString & srsName() const
Return the value of the srsName attribute.
Definition: qgsgml.h:112