QGIS API Documentation  2.15.0-Master (972fc9f)
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 
46 class CORE_EXPORT QgsGmlStreamingParser
47 {
48  public:
49 
51 
54  {
55  public:
58 
63  };
64 
66  typedef enum
67  {
75 
77  QgsGmlStreamingParser( const QString& typeName,
78  const QString& geometryAttribute,
79  const QgsFields & fields,
80  AxisOrientationLogic axisOrientationLogic = Honour_EPSG_if_urn,
81  bool invertAxisOrientation = false );
82 
84  QgsGmlStreamingParser( const QList<LayerProperties>& layerProperties,
85  const QgsFields & fields,
86  const QMap< QString, QPair<QString, QString> >& mapFieldNameToSrcLayerNameFieldName,
87  AxisOrientationLogic axisOrientationLogic = Honour_EPSG_if_urn,
88  bool invertAxisOrientation = false );
90 
93  bool processData( const QByteArray& data, bool atEnd, QString& errorMsg );
94 
97  bool processData( const QByteArray& data, bool atEnd );
98 
103  QVector<QgsGmlFeaturePtrGmlIdPair> getAndStealReadyFeatures();
104 
106  int getEPSGCode() const { return mEpsg; }
107 
109  const QString& srsName() const { return mSrsName; }
110 
112  const QgsRectangle& layerExtent() const { return mLayerExtent; }
113 
115  QGis::WkbType wkbType() const { return mWkbType; }
116 
118  int numberMatched() const { return mNumberMatched; }
119 
121  int numberReturned() const { return mNumberReturned; }
122 
124  bool isException() const { return mIsException; }
125 
127  const QString& exceptionText() const { return mExceptionText; }
128 
130  bool isTruncatedResponse() const { return mTruncatedResponse; }
131 
132  private:
133 
134  enum ParseMode
135  {
136  none,
137  boundingBox,
138  null,
139  envelope,
140  lowerCorner,
141  upperCorner,
142  feature, // feature element containing attrs and geo (inside gml:featureMember)
143  attribute,
144  tuple, // wfs:Tuple of a join layer
145  featureTuple,
146  attributeTuple,
147  geometry,
148  coordinate,
149  posList,
150  multiPoint,
151  multiLine,
152  multiPolygon,
153  ExceptionReport,
154  ExceptionText
155  };
156 
158  void startElement( const XML_Char* el, const XML_Char** attr );
159  void endElement( const XML_Char* el );
160  void characters( const XML_Char* chars, int len );
161  static void start( void* data, const XML_Char* el, const XML_Char** attr )
162  {
163  static_cast<QgsGmlStreamingParser*>( data )->startElement( el, attr );
164  }
165  static void end( void* data, const XML_Char* el )
166  {
167  static_cast<QgsGmlStreamingParser*>( data )->endElement( el );
168  }
169  static void chars( void* data, const XML_Char* chars, int len )
170  {
171  static_cast<QgsGmlStreamingParser*>( data )->characters( chars, len );
172  }
173 
174  // Set current feature attribute
175  void setAttribute( const QString& name, const QString& value );
176 
177  //helper routines
178 
184  int readEpsgFromAttribute( int& epsgNr, const XML_Char** attr );
190  QString readAttribute( const QString& attributeName, const XML_Char** attr ) const;
192  bool createBBoxFromCoordinateString( QgsRectangle &bb, const QString& coordString ) const;
198  int pointsFromCoordinateString( QList<QgsPoint>& points, const QString& coordString ) const;
199 
206  int pointsFromPosListString( QList<QgsPoint>& points, const QString& coordString, int dimension ) const;
207 
208  int pointsFromString( QList<QgsPoint>& points, const QString& coordString ) const;
209  int getPointWKB( QgsWkbPtr &wkbPtr, const QgsPoint& ) const;
210  int getLineWKB( QgsWkbPtr &wkbPtr, const QList<QgsPoint>& lineCoordinates ) const;
211  int getRingWKB( QgsWkbPtr &wkbPtr, const QList<QgsPoint>& ringCoordinates ) const;
217  int createMultiLineFromFragments();
218  int createMultiPointFromFragments();
219  int createPolygonFromFragments();
220  int createMultiPolygonFromFragments();
222  int totalWKBFragmentSize() const;
223 
225  ParseMode modeStackTop() { return mParseModeStack.isEmpty() ? none : mParseModeStack.top(); }
226 
228  ParseMode modeStackPop() { return mParseModeStack.isEmpty() ? none : mParseModeStack.pop(); }
229 
231  XML_Parser mParser;
232 
235 
237  QList<LayerProperties> mLayerProperties;
238  QMap< QString, LayerProperties > mMapTypeNameToProperties;
239 
241  QString mTypeName;
242  QByteArray mTypeNameBA;
243  const char* mTypeNamePtr;
244 
245  QGis::WkbType mWkbType;
246 
247  //results are members such that handler routines are able to manipulate them
248 
250  QString mGeometryAttribute;
251  QByteArray mGeometryAttributeBA;
252  const char* mGeometryAttributePtr;
253 
254  QgsFields mFields;
255  QMap<QString, QPair<int, QgsField> > mThematicAttributes;
256 
257  bool mIsException;
258  QString mExceptionText;
259  bool mTruncatedResponse;
261  int mParseDepth;
262  int mFeatureTupleDepth;
263  QString mCurrentTypename;
265  QStack<ParseMode> mParseModeStack;
267  QString mStringCash;
268  QgsFeature* mCurrentFeature;
269  QVector<QVariant> mCurrentAttributes; //attributes of current feature
270  QString mCurrentFeatureId;
271  int mFeatureCount;
273  QgsWkbPtr mCurrentWKB;
274  QgsRectangle mCurrentExtent;
275  bool mBoundedByNullFound;
280  QList< QList<QgsWkbPtr> > mCurrentWKBFragments;
281  QString mAttributeName;
282  char mEndian;
284  QString mCoordinateSeparator;
286  QString mTupleSeparator;
288  int mDimension;
290  ParseMode mCoorMode;
292  int mEpsg;
294  QString mSrsName;
296  QgsRectangle mLayerExtent;
298  QString mGMLNameSpaceURI;
299  const char* mGMLNameSpaceURIPtr;
301  AxisOrientationLogic mAxisOrientationLogic;
303  bool mInvertAxisOrientationRequest;
305  bool mInvertAxisOrientation;
307  int mNumberReturned;
309  int mNumberMatched;
311  std::string mGeometryString;
313  bool mFoundUnhandledGeometryElement;
314 };
315 
316 
321 class CORE_EXPORT QgsGml : public QObject
322 {
323  Q_OBJECT
324  public:
325  QgsGml(
326  const QString& typeName,
327  const QString& geometryAttribute,
328  const QgsFields & fields );
329 
330  ~QgsGml();
331 
343  int getFeatures( const QString& uri,
344  QGis::WkbType* wkbType,
345  QgsRectangle* extent = nullptr,
346  const QString& userName = QString(),
347  const QString& password = QString(),
348  const QString& authcfg = QString() );
349 
353  int getFeatures( const QByteArray &data, QGis::WkbType* wkbType, QgsRectangle* extent = nullptr );
354 
356  QMap<QgsFeatureId, QgsFeature* > featuresMap() const { return mFeatures; }
357 
359  QMap<QgsFeatureId, QString > idsMap() const { return mIdMap; }
360 
363  QgsCoordinateReferenceSystem crs() const;
364 
365  private slots:
366 
367  void setFinished();
368 
370  void handleProgressEvent( qint64 progress, qint64 totalSteps );
371 
372  signals:
373  void dataReadProgress( int progress );
374  void totalStepsUpdate( int totalSteps );
375  //also emit signal with progress and totalSteps together (this is better for the status message)
376  void dataProgressAndSteps( int progress, int totalSteps );
377 
378  private:
379 
385  void calculateExtentFromFeatures();
386 
387  void fillMapsFromParser();
388 
389  QgsGmlStreamingParser mParser;
390 
392  QString mTypeName;
393 
395  bool mFinished;
396 
398  //QMap<QgsFeatureId, QgsFeature* > &mFeatures;
400  //QMap<QString, QMap<QgsFeatureId, QgsFeature* > > mFeatures;
401 
403  //QMap<QgsFeatureId, QString > &mIdMap;
405  //QMap<QString, QMap<QgsFeatureId, QString > > mIdMap;
406 
408  QgsRectangle mExtent;
409 };
410 
411 #endif
int getEPSGCode() const
Return the EPSG code, or 0 if unknown.
Definition: qgsgml.h:106
A rectangle specified with double values.
Definition: qgsrectangle.h:35
const QString & exceptionText() const
Return the exception text.
Definition: qgsgml.h:127
AxisOrientationLogic
Axis orientation logic.
Definition: qgsgml.h:66
const QgsRectangle & layerExtent() const
Return layer bounding box.
Definition: qgsgml.h:112
int numberMatched() const
Return WFS 2.0 "numberMatched" attribute, or -1 if invalid/not found.
Definition: qgsgml.h:118
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:121
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:359
Ignore EPSG axis order.
Definition: qgsgml.h:73
Honour EPSG axis order.
Definition: qgsgml.h:71
This class reads data from a WFS server or alternatively from a GML file.
Definition: qgsgml.h:321
Honour EPSG axis order only if srsName is of the form urn:ogc:def:crs:EPSG:
Definition: qgsgml.h:69
QPair< QgsFeature *, QString > QgsGmlFeaturePtrGmlIdPair
Definition: qgsgml.h:50
A class to represent a point.
Definition: qgspoint.h:117
QString mGeometryAttribute
Geometry attribute name.
Definition: qgsgml.h:62
bool isTruncatedResponse() const
Return whether a "truncatedResponse" element is found.
Definition: qgsgml.h:130
QMap< QgsFeatureId, QgsFeature * > featuresMap() const
Get parsed features for given type name.
Definition: qgsgml.h:356
Class for storing a coordinate reference system (CRS)
bool isException() const
Return whether the document parser is a OGC exception.
Definition: qgsgml.h:124
QGis::WkbType wkbType() const
Return the geometry type.
Definition: qgsgml.h:115
This class builds features from GML data in a streaming way.
Definition: qgsgml.h:46
const QString & srsName() const
Return the value of the srsName attribute.
Definition: qgsgml.h:109