QGIS API Documentation  2.99.0-Master (8ec3eaf)
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 "qgsfields.h"
21 #include "qgsrectangle.h"
22 #include "qgswkbptr.h"
23 #include "qgsfeature.h"
24 
25 #include <QPair>
26 #include <QByteArray>
27 #include <QDomElement>
28 #include <QStringList>
29 #include <QStack>
30 #include <QVector>
31 
32 #include <string>
33 
35 
44 class CORE_EXPORT QgsGmlStreamingParser
45 {
46  public:
47 
48  typedef QPair<QgsFeature*, QString> QgsGmlFeaturePtrGmlIdPair;
49 
54  {
55  public:
58 
60  QString mName;
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  QgsWkbTypes::Type 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 
135  QgsGmlStreamingParser& operator=( const QgsGmlStreamingParser& other );
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 );
188 
194  QString readAttribute( const QString& attributeName, const XML_Char** attr ) const;
196  bool createBBoxFromCoordinateString( QgsRectangle &bb, const QString& coordString ) const;
197 
203  int pointsFromCoordinateString( QList<QgsPoint>& points, const QString& coordString ) const;
204 
211  int pointsFromPosListString( QList<QgsPoint>& points, const QString& coordString, int dimension ) const;
212 
213  int pointsFromString( QList<QgsPoint>& points, const QString& coordString ) const;
214  int getPointWKB( QgsWkbPtr &wkbPtr, const QgsPoint& ) const;
215  int getLineWKB( QgsWkbPtr &wkbPtr, const QList<QgsPoint>& lineCoordinates ) const;
216  int getRingWKB( QgsWkbPtr &wkbPtr, const QList<QgsPoint>& ringCoordinates ) const;
217 
223  int createMultiLineFromFragments();
224  int createMultiPointFromFragments();
225  int createPolygonFromFragments();
226  int createMultiPolygonFromFragments();
228  int totalWKBFragmentSize() const;
229 
231  ParseMode modeStackTop() { return mParseModeStack.isEmpty() ? none : mParseModeStack.top(); }
232 
234  ParseMode modeStackPop() { return mParseModeStack.isEmpty() ? none : mParseModeStack.pop(); }
235 
237  XML_Parser mParser;
238 
240  QVector<QgsGmlFeaturePtrGmlIdPair> mFeatureList;
241 
243  QList<LayerProperties> mLayerProperties;
244  QMap< QString, LayerProperties > mMapTypeNameToProperties;
245 
247  QString mTypeName;
248  QByteArray mTypeNameBA;
249  const char* mTypeNamePtr;
250 
251  QgsWkbTypes::Type mWkbType;
252 
253  //results are members such that handler routines are able to manipulate them
254 
256  QString mGeometryAttribute;
257  QByteArray mGeometryAttributeBA;
258  const char* mGeometryAttributePtr;
259 
260  QgsFields mFields;
261  QMap<QString, QPair<int, QgsField> > mThematicAttributes;
262 
263  bool mIsException;
264  QString mExceptionText;
265  bool mTruncatedResponse;
267  int mParseDepth;
268  int mFeatureTupleDepth;
269  QString mCurrentTypename;
270  QStack<ParseMode> mParseModeStack;
273  QString mStringCash;
274  QgsFeature* mCurrentFeature;
275  QVector<QVariant> mCurrentAttributes; //attributes of current feature
276  QString mCurrentFeatureId;
277  int mFeatureCount;
279  QgsWkbPtr mCurrentWKB;
280  QgsRectangle mCurrentExtent;
281  bool mBoundedByNullFound;
282 
287  QList< QList<QgsWkbPtr> > mCurrentWKBFragments;
288  QString mAttributeName;
289  char mEndian;
291  QString mCoordinateSeparator;
293  QString mTupleSeparator;
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  QgsWkbTypes::Type* 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, QgsWkbTypes::Type* 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;
407  QMap<QgsFeatureId, QgsFeature* > mFeatures;
408  //QMap<QString, QMap<QgsFeatureId, QgsFeature* > > mFeatures;
409 
411  //QMap<QgsFeatureId, QString > &mIdMap;
412  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:109
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
AxisOrientationLogic
Axis orientation logic.
Definition: qgsgml.h:66
int numberReturned() const
Return WFS 2.0 "numberReturned" or WFS 1.1 "numberOfFeatures" attribute, or -1 if invalid/not found...
Definition: qgsgml.h:121
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: qgsfields.h:36
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:135
bool isException() const
Return whether the document parser is a OGC exception.
Definition: qgsgml.h:124
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: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:69
QPair< QgsFeature *, QString > QgsGmlFeaturePtrGmlIdPair
Definition: qgsgml.h:48
A class to represent a point.
Definition: qgspoint.h:142
QString mGeometryAttribute
Geometry attribute name.
Definition: qgsgml.h:62
const QgsRectangle & layerExtent() const
Return layer bounding box.
Definition: qgsgml.h:112
bool isTruncatedResponse() const
Return whether a "truncatedResponse" element is found.
Definition: qgsgml.h:130
This class represents a coordinate reference system (CRS).
const QString & exceptionText() const
Return the exception text.
Definition: qgsgml.h:127
QgsWkbTypes::Type wkbType() const
Return the geometry type.
Definition: qgsgml.h:115
This class builds features from GML data in a streaming way.
Definition: qgsgml.h:44