QGIS API Documentation  2.15.0-Master (af20121)
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 
44 class CORE_EXPORT QgsGmlStreamingParser
45 {
46  public:
47 
49 
52  {
53  public:
56 
61  };
62 
64  typedef enum
65  {
73 
75  QgsGmlStreamingParser( const QString& typeName,
76  const QString& geometryAttribute,
77  const QgsFields & fields,
78  AxisOrientationLogic axisOrientationLogic = Honour_EPSG_if_urn,
79  bool invertAxisOrientation = false );
80 
82  QgsGmlStreamingParser( const QList<LayerProperties>& layerProperties,
83  const QgsFields & fields,
84  const QMap< QString, QPair<QString, QString> >& mapFieldNameToSrcLayerNameFieldName,
85  AxisOrientationLogic axisOrientationLogic = Honour_EPSG_if_urn,
86  bool invertAxisOrientation = false );
88 
91  bool processData( const QByteArray& data, bool atEnd, QString& errorMsg );
92 
95  bool processData( const QByteArray& data, bool atEnd );
96 
101  QVector<QgsGmlFeaturePtrGmlIdPair> getAndStealReadyFeatures();
102 
104  int getEPSGCode() const { return mEpsg; }
105 
107  QGis::WkbType wkbType() const { return mWkbType; }
108 
110  int numberMatched() const { return mNumberMatched; }
111 
113  int numberReturned() const { return mNumberReturned; }
114 
116  bool isException() const { return mIsException; }
117 
119  const QString& exceptionText() const { return mExceptionText; }
120 
122  bool isTruncatedResponse() const { return mTruncatedResponse; }
123 
124  private:
125 
126  enum ParseMode
127  {
128  none,
129  boundingBox,
130  null,
131  envelope,
132  lowerCorner,
133  upperCorner,
134  feature, // feature element containing attrs and geo (inside gml:featureMember)
135  attribute,
136  tuple, // wfs:Tuple of a join layer
137  featureTuple,
138  attributeTuple,
139  geometry,
140  coordinate,
141  posList,
142  multiPoint,
143  multiLine,
144  multiPolygon,
145  ExceptionReport,
146  ExceptionText
147  };
148 
150  void startElement( const XML_Char* el, const XML_Char** attr );
151  void endElement( const XML_Char* el );
152  void characters( const XML_Char* chars, int len );
153  static void start( void* data, const XML_Char* el, const XML_Char** attr )
154  {
155  static_cast<QgsGmlStreamingParser*>( data )->startElement( el, attr );
156  }
157  static void end( void* data, const XML_Char* el )
158  {
159  static_cast<QgsGmlStreamingParser*>( data )->endElement( el );
160  }
161  static void chars( void* data, const XML_Char* chars, int len )
162  {
163  static_cast<QgsGmlStreamingParser*>( data )->characters( chars, len );
164  }
165 
166  // Set current feature attribute
167  void setAttribute( const QString& name, const QString& value );
168 
169  //helper routines
170 
176  int readEpsgFromAttribute( int& epsgNr, const XML_Char** attr );
182  QString readAttribute( const QString& attributeName, const XML_Char** attr ) const;
185  int createBBoxFromCoordinateString( QgsRectangle &bb, const QString& coordString ) const;
191  int pointsFromCoordinateString( QList<QgsPoint>& points, const QString& coordString ) const;
192 
199  int pointsFromPosListString( QList<QgsPoint>& points, const QString& coordString, int dimension ) const;
200 
201  int pointsFromString( QList<QgsPoint>& points, const QString& coordString ) const;
202  int getPointWKB( QgsWkbPtr &wkbPtr, const QgsPoint& ) const;
203  int getLineWKB( QgsWkbPtr &wkbPtr, const QList<QgsPoint>& lineCoordinates ) const;
204  int getRingWKB( QgsWkbPtr &wkbPtr, const QList<QgsPoint>& ringCoordinates ) const;
210  int createMultiLineFromFragments();
211  int createMultiPointFromFragments();
212  int createPolygonFromFragments();
213  int createMultiPolygonFromFragments();
215  int totalWKBFragmentSize() const;
216 
218  ParseMode modeStackTop() { return mParseModeStack.isEmpty() ? none : mParseModeStack.top(); }
219 
221  ParseMode modeStackPop() { return mParseModeStack.isEmpty() ? none : mParseModeStack.pop(); }
222 
224  XML_Parser mParser;
225 
228 
230  QList<LayerProperties> mLayerProperties;
231  QMap< QString, LayerProperties > mMapTypeNameToProperties;
232 
234  QString mTypeName;
235  QByteArray mTypeNameBA;
236  const char* mTypeNamePtr;
237 
238  QGis::WkbType mWkbType;
239 
240  //results are members such that handler routines are able to manipulate them
241 
243  QString mGeometryAttribute;
244  QByteArray mGeometryAttributeBA;
245  const char* mGeometryAttributePtr;
246 
247  QgsFields mFields;
248  QMap<QString, QPair<int, QgsField> > mThematicAttributes;
249 
250  bool mIsException;
251  QString mExceptionText;
252  bool mTruncatedResponse;
254  int mParseDepth;
255  int mFeatureTupleDepth;
256  QString mCurrentTypename;
258  QStack<ParseMode> mParseModeStack;
260  QString mStringCash;
261  QgsFeature* mCurrentFeature;
262  QVector<QVariant> mCurrentAttributes; //attributes of current feature
263  QString mCurrentFeatureId;
264  int mFeatureCount;
266  QgsWkbPtr mCurrentWKB;
267  QgsRectangle mCurrentExtent;
268  bool mBoundedByNullFound;
273  QList< QList<QgsWkbPtr> > mCurrentWKBFragments;
274  QString mAttributeName;
275  char mEndian;
277  QString mCoordinateSeparator;
279  QString mTupleSeparator;
281  int mDimension;
283  ParseMode mCoorMode;
285  int mEpsg;
287  QString mGMLNameSpaceURI;
288  const char* mGMLNameSpaceURIPtr;
290  AxisOrientationLogic mAxisOrientationLogic;
292  bool mInvertAxisOrientationRequest;
294  bool mInvertAxisOrientation;
296  int mNumberReturned;
298  int mNumberMatched;
299 };
300 
301 
306 class CORE_EXPORT QgsGml : public QObject
307 {
308  Q_OBJECT
309  public:
310  QgsGml(
311  const QString& typeName,
312  const QString& geometryAttribute,
313  const QgsFields & fields );
314 
315  ~QgsGml();
316 
328  int getFeatures( const QString& uri,
329  QGis::WkbType* wkbType,
330  QgsRectangle* extent = nullptr,
331  const QString& userName = QString(),
332  const QString& password = QString(),
333  const QString& authcfg = QString() );
334 
338  int getFeatures( const QByteArray &data, QGis::WkbType* wkbType, QgsRectangle* extent = nullptr );
339 
341  QMap<QgsFeatureId, QgsFeature* > featuresMap() const { return mFeatures; }
342 
344  QMap<QgsFeatureId, QString > idsMap() const { return mIdMap; }
345 
348  QgsCoordinateReferenceSystem crs() const;
349 
350  private slots:
351 
352  void setFinished();
353 
355  void handleProgressEvent( qint64 progress, qint64 totalSteps );
356 
357  signals:
358  void dataReadProgress( int progress );
359  void totalStepsUpdate( int totalSteps );
360  //also emit signal with progress and totalSteps together (this is better for the status message)
361  void dataProgressAndSteps( int progress, int totalSteps );
362 
363  private:
364 
370  void calculateExtentFromFeatures();
371 
372  void fillMapsFromParser();
373 
374  QgsGmlStreamingParser mParser;
375 
377  QString mTypeName;
378 
380  bool mFinished;
381 
383  //QMap<QgsFeatureId, QgsFeature* > &mFeatures;
385  //QMap<QString, QMap<QgsFeatureId, QgsFeature* > > mFeatures;
386 
388  //QMap<QgsFeatureId, QString > &mIdMap;
390  //QMap<QString, QMap<QgsFeatureId, QString > > mIdMap;
391 
393  QgsRectangle mExtent;
394 };
395 
396 #endif
int getEPSGCode() const
Return the EPSG code, or 0 if unknown.
Definition: qgsgml.h:104
A rectangle specified with double values.
Definition: qgsrectangle.h:35
const QString & exceptionText() const
Return the exception text.
Definition: qgsgml.h:119
AxisOrientationLogic
Axis orientation logic.
Definition: qgsgml.h:64
int numberMatched() const
Return WFS 2.0 "numberMatched" attribute, or -1 if invalid/not found.
Definition: qgsgml.h:110
Container of fields for a vector layer.
Definition: qgsfield.h:193
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:113
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:344
Ignore EPSG axis order.
Definition: qgsgml.h:71
Honour EPSG axis order.
Definition: qgsgml.h:69
This class reads data from a WFS server or alternatively from a GML file.
Definition: qgsgml.h:306
Honour EPSG axis order only if srsName is of the form urn:ogc:def:crs:EPSG:
Definition: qgsgml.h:67
QPair< QgsFeature *, QString > QgsGmlFeaturePtrGmlIdPair
Definition: qgsgml.h:48
A class to represent a point.
Definition: qgspoint.h:117
QString mGeometryAttribute
Geometry attribute name.
Definition: qgsgml.h:60
bool isTruncatedResponse() const
Return whether a "truncatedResponse" element is found.
Definition: qgsgml.h:122
QMap< QgsFeatureId, QgsFeature * > featuresMap() const
Get parsed features for given type name.
Definition: qgsgml.h:341
Class for storing a coordinate reference system (CRS)
bool isException() const
Return whether the document parser is a OGC exception.
Definition: qgsgml.h:116
QGis::WkbType wkbType() const
Return the geometry type.
Definition: qgsgml.h:107
This class builds features from GML data in a streaming way.
Definition: qgsgml.h:44