QGIS API Documentation
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 
43 class CORE_EXPORT QgsGmlStreamingParser
44 {
45  public:
46 
48 
50  typedef enum
51  {
59 
61  QgsGmlStreamingParser( const QString& typeName,
62  const QString& geometryAttribute,
63  const QgsFields & fields,
64  AxisOrientationLogic axisOrientationLogic = Honour_EPSG_if_urn,
65  bool invertAxisOrientation = false );
67 
70  bool processData( const QByteArray& data, bool atEnd );
71 
76  QVector<QgsGmlFeaturePtrGmlIdPair> getAndStealReadyFeatures();
77 
79  int getEPSGCode() const { return mEpsg; }
80 
82  QGis::WkbType wkbType() const { return mWkbType; }
83 
85  int numberMatched() const { return mNumberMatched; }
86 
88  int numberReturned() const { return mNumberReturned; }
89 
91  bool isException() const { return mIsException; }
92 
94  const QString& exceptionText() const { return mExceptionText; }
95 
96  private:
97 
98  enum ParseMode
99  {
100  none,
101  boundingBox,
102  null,
103  envelope,
104  lowerCorner,
105  upperCorner,
106  feature, // feature element containing attrs and geo (inside gml:featureMember)
107  attribute,
108  geometry,
109  coordinate,
110  posList,
111  multiPoint,
112  multiLine,
113  multiPolygon,
114  ExceptionReport,
115  ExceptionText
116  };
117 
119  void startElement( const XML_Char* el, const XML_Char** attr );
120  void endElement( const XML_Char* el );
121  void characters( const XML_Char* chars, int len );
122  static void start( void* data, const XML_Char* el, const XML_Char** attr )
123  {
124  static_cast<QgsGmlStreamingParser*>( data )->startElement( el, attr );
125  }
126  static void end( void* data, const XML_Char* el )
127  {
128  static_cast<QgsGmlStreamingParser*>( data )->endElement( el );
129  }
130  static void chars( void* data, const XML_Char* chars, int len )
131  {
132  static_cast<QgsGmlStreamingParser*>( data )->characters( chars, len );
133  }
134 
135  // Set current feature attribute
136  void setAttribute( const QString& name, const QString& value );
137 
138  //helper routines
139 
145  int readEpsgFromAttribute( int& epsgNr, const XML_Char** attr );
151  QString readAttribute( const QString& attributeName, const XML_Char** attr ) const;
154  int createBBoxFromCoordinateString( QgsRectangle &bb, const QString& coordString ) const;
160  int pointsFromCoordinateString( QList<QgsPoint>& points, const QString& coordString ) const;
161 
168  int pointsFromPosListString( QList<QgsPoint>& points, const QString& coordString, int dimension ) const;
169 
170  int pointsFromString( QList<QgsPoint>& points, const QString& coordString ) const;
171  int getPointWKB( QgsWkbPtr &wkbPtr, const QgsPoint& ) const;
172  int getLineWKB( QgsWkbPtr &wkbPtr, const QList<QgsPoint>& lineCoordinates ) const;
173  int getRingWKB( QgsWkbPtr &wkbPtr, const QList<QgsPoint>& ringCoordinates ) const;
179  int createMultiLineFromFragments();
180  int createMultiPointFromFragments();
181  int createPolygonFromFragments();
182  int createMultiPolygonFromFragments();
184  int totalWKBFragmentSize() const;
185 
187  ParseMode modeStackTop() { return mParseModeStack.isEmpty() ? none : mParseModeStack.top(); }
188 
190  ParseMode modeStackPop() { return mParseModeStack.isEmpty() ? none : mParseModeStack.pop(); }
191 
193  XML_Parser mParser;
194 
197 
199  QString mTypeName;
200  QByteArray mTypeNameBA;
201  const char* mTypeNamePtr;
202 
203  QGis::WkbType mWkbType;
204 
205  //results are members such that handler routines are able to manipulate them
206 
208  QString mGeometryAttribute;
209  QByteArray mGeometryAttributeBA;
210  const char* mGeometryAttributePtr;
211 
212  QgsFields mFields;
213  QMap<QString, QPair<int, QgsField> > mThematicAttributes;
214 
215  bool mIsException;
216  QString mExceptionText;
218  int mParseDepth;
220  QStack<ParseMode> mParseModeStack;
222  QString mStringCash;
223  QgsFeature* mCurrentFeature;
224  QVector<QVariant> mCurrentAttributes; //attributes of current feature
225  QString mCurrentFeatureId;
226  int mFeatureCount;
228  QgsWkbPtr mCurrentWKB;
229  QgsRectangle mCurrentExtent;
230  bool mBoundedByNullFound;
235  QList< QList<QgsWkbPtr> > mCurrentWKBFragments;
236  QString mAttributeName;
237  char mEndian;
239  QString mCoordinateSeparator;
241  QString mTupleSeparator;
243  int mDimension;
245  ParseMode mCoorMode;
247  int mEpsg;
249  QString mGMLNameSpaceURI;
250  const char* mGMLNameSpaceURIPtr;
252  AxisOrientationLogic mAxisOrientationLogic;
254  bool mInvertAxisOrientationRequest;
256  bool mInvertAxisOrientation;
258  int mNumberReturned;
260  int mNumberMatched;
261 };
262 
263 
268 class CORE_EXPORT QgsGml : public QObject
269 {
270  Q_OBJECT
271  public:
272  QgsGml(
273  const QString& typeName,
274  const QString& geometryAttribute,
275  const QgsFields & fields );
276 
277  ~QgsGml();
278 
290  int getFeatures( const QString& uri,
291  QGis::WkbType* wkbType,
292  QgsRectangle* extent = nullptr,
293  const QString& userName = QString(),
294  const QString& password = QString(),
295  const QString& authcfg = QString() );
296 
300  int getFeatures( const QByteArray &data, QGis::WkbType* wkbType, QgsRectangle* extent = nullptr );
301 
303  QMap<QgsFeatureId, QgsFeature* > featuresMap() const { return mFeatures; }
304 
306  QMap<QgsFeatureId, QString > idsMap() const { return mIdMap; }
307 
310  QgsCoordinateReferenceSystem crs() const;
311 
312  private slots:
313 
314  void setFinished();
315 
317  void handleProgressEvent( qint64 progress, qint64 totalSteps );
318 
319  signals:
320  void dataReadProgress( int progress );
321  void totalStepsUpdate( int totalSteps );
322  //also emit signal with progress and totalSteps together (this is better for the status message)
323  void dataProgressAndSteps( int progress, int totalSteps );
324 
325  private:
326 
332  void calculateExtentFromFeatures();
333 
334  void fillMapsFromParser();
335 
336  QgsGmlStreamingParser mParser;
337 
339  QString mTypeName;
340 
342  bool mFinished;
343 
345  //QMap<QgsFeatureId, QgsFeature* > &mFeatures;
347  //QMap<QString, QMap<QgsFeatureId, QgsFeature* > > mFeatures;
348 
350  //QMap<QgsFeatureId, QString > &mIdMap;
352  //QMap<QString, QMap<QgsFeatureId, QString > > mIdMap;
353 
355  QgsRectangle mExtent;
356 };
357 
358 #endif
int getEPSGCode() const
Return the EPSG code, or 0 if unknown.
Definition: qgsgml.h:79
A rectangle specified with double values.
Definition: qgsrectangle.h:35
const QString & exceptionText() const
Return the exception text.
Definition: qgsgml.h:94
AxisOrientationLogic
Axis orientation logic.
Definition: qgsgml.h:50
int numberMatched() const
Return WFS 2.0 "numberMatched" attribute, or -1 if invalid/not found.
Definition: qgsgml.h:85
Container of fields for a vector layer.
Definition: qgsfield.h:187
WkbType
Used for symbology operations.
Definition: qgis.h:57
int numberReturned() const
Return WFS 2.0 "numberReturned" or WFS 1.1 "numberOfFeatures" attribute, or -1 if invalid/not found...
Definition: qgsgml.h:88
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:306
Ignore EPSG axis order.
Definition: qgsgml.h:57
Honour EPSG axis order.
Definition: qgsgml.h:55
This class reads data from a WFS server or alternatively from a GML file.
Definition: qgsgml.h:268
Honour EPSG axis order only if srsName is of the form urn:ogc:def:crs:EPSG:
Definition: qgsgml.h:53
QPair< QgsFeature *, QString > QgsGmlFeaturePtrGmlIdPair
Definition: qgsgml.h:47
A class to represent a point.
Definition: qgspoint.h:117
QMap< QgsFeatureId, QgsFeature * > featuresMap() const
Get parsed features for given type name.
Definition: qgsgml.h:303
Class for storing a coordinate reference system (CRS)
bool isException() const
Return whether the document parser is a OGC exception.
Definition: qgsgml.h:91
QGis::WkbType wkbType() const
Return the geometry type.
Definition: qgsgml.h:82
This class builds features from GML data in a streaming way.
Definition: qgsgml.h:43