QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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 "qgis_core.h"
19 #include <expat.h>
20 #include "qgis_sip.h"
21 #include "qgsfields.h"
22 #include "qgsrectangle.h"
23 #include "qgswkbptr.h"
24 #include "qgsfeature.h"
25 
26 #include <QPair>
27 #include <QByteArray>
28 #include <QDomElement>
29 #include <QStringList>
30 #include <QStack>
31 #include <QVector>
32 
33 #include <string>
34 
36 
37 #ifndef SIP_RUN
38 
48 class CORE_EXPORT QgsGmlStreamingParser
49 {
50  public:
51 
52  typedef QPair<QgsFeature *, QString> QgsGmlFeaturePtrGmlIdPair;
53 
59  {
60  public:
62  LayerProperties() = default;
63 
65  QString mName;
68  };
69 
71  typedef enum
72  {
79  } AxisOrientationLogic;
80 
82  QgsGmlStreamingParser( const QString &typeName,
83  const QString &geometryAttribute,
84  const QgsFields &fields,
85  AxisOrientationLogic axisOrientationLogic = Honour_EPSG_if_urn,
86  bool invertAxisOrientation = false );
87 
89  QgsGmlStreamingParser( const QList<LayerProperties> &layerProperties,
90  const QgsFields &fields,
91  const QMap< QString, QPair<QString, QString> > &mapFieldNameToSrcLayerNameFieldName,
92  AxisOrientationLogic axisOrientationLogic = Honour_EPSG_if_urn,
93  bool invertAxisOrientation = false );
95 
97  QgsGmlStreamingParser( const QgsGmlStreamingParser &other ) = delete;
100 
105  bool processData( const QByteArray &data, bool atEnd, QString &errorMsg );
106 
111  bool processData( const QByteArray &data, bool atEnd );
112 
119  QVector<QgsGmlFeaturePtrGmlIdPair> getAndStealReadyFeatures();
120 
122  int getEPSGCode() const { return mEpsg; }
123 
125  QString srsName() const { return mSrsName; }
126 
128  const QgsRectangle &layerExtent() const { return mLayerExtent; }
129 
131  QgsWkbTypes::Type wkbType() const { return mWkbType; }
132 
134  int numberMatched() const { return mNumberMatched; }
135 
137  int numberReturned() const { return mNumberReturned; }
138 
140  bool isException() const { return mIsException; }
141 
143  QString exceptionText() const { return mExceptionText; }
144 
146  bool isTruncatedResponse() const { return mTruncatedResponse; }
147 
148  private:
149 
150  enum ParseMode
151  {
152  None,
153  BoundingBox,
154  Null,
155  Envelope,
156  LowerCorner,
157  UpperCorner,
158  Feature, // feature element containing attrs and geo (inside gml:featureMember)
159  Attribute,
160  Tuple, // wfs:Tuple of a join layer
161  FeatureTuple,
162  AttributeTuple,
163  Geometry,
164  Coordinate,
165  PosList,
166  MultiPoint,
167  MultiLine,
168  MultiPolygon,
169  ExceptionReport,
170  ExceptionText
171  };
172 
174  void startElement( const XML_Char *el, const XML_Char **attr );
175  void endElement( const XML_Char *el );
176  void characters( const XML_Char *chars, int len );
177  static void start( void *data, const XML_Char *el, const XML_Char **attr )
178  {
179  static_cast<QgsGmlStreamingParser *>( data )->startElement( el, attr );
180  }
181  static void end( void *data, const XML_Char *el )
182  {
183  static_cast<QgsGmlStreamingParser *>( data )->endElement( el );
184  }
185  static void chars( void *data, const XML_Char *chars, int len )
186  {
187  static_cast<QgsGmlStreamingParser *>( data )->characters( chars, len );
188  }
189 
190  // Set current feature attribute
191  void setAttribute( const QString &name, const QString &value );
192 
193  //helper routines
194 
201  int readEpsgFromAttribute( int &epsgNr, const XML_Char **attr );
202 
209  QString readAttribute( const QString &attributeName, const XML_Char **attr ) const;
211  bool createBBoxFromCoordinateString( QgsRectangle &bb, const QString &coordString ) const;
212 
219  int pointsFromCoordinateString( QList<QgsPointXY> &points, const QString &coordString ) const;
220 
228  int pointsFromPosListString( QList<QgsPointXY> &points, const QString &coordString, int dimension ) const;
229 
230  int pointsFromString( QList<QgsPointXY> &points, const QString &coordString ) const;
231  int getPointWKB( QgsWkbPtr &wkbPtr, const QgsPointXY & ) const;
232  int getLineWKB( QgsWkbPtr &wkbPtr, const QList<QgsPointXY> &lineCoordinates ) const;
233  int getRingWKB( QgsWkbPtr &wkbPtr, const QList<QgsPointXY> &ringCoordinates ) const;
234 
241  int createMultiLineFromFragments();
242  int createMultiPointFromFragments();
243  int createPolygonFromFragments();
244  int createMultiPolygonFromFragments();
246  int totalWKBFragmentSize() const;
247 
249  ParseMode modeStackTop() { return mParseModeStack.isEmpty() ? None : mParseModeStack.top(); }
250 
252  ParseMode modeStackPop() { return mParseModeStack.isEmpty() ? None : mParseModeStack.pop(); }
253 
255  XML_Parser mParser;
256 
258  QVector<QgsGmlFeaturePtrGmlIdPair> mFeatureList;
259 
261  QList<LayerProperties> mLayerProperties;
262  QMap< QString, LayerProperties > mMapTypeNameToProperties;
263 
265  QString mTypeName;
266  QByteArray mTypeNameBA;
267  const char *mTypeNamePtr = nullptr;
268  size_t mTypeNameUTF8Len;
269 
270  QgsWkbTypes::Type mWkbType;
271 
272  //results are members such that handler routines are able to manipulate them
273 
275  QString mGeometryAttribute;
276  QByteArray mGeometryAttributeBA;
277  const char *mGeometryAttributePtr = nullptr;
278  size_t mGeometryAttributeUTF8Len;
279 
280  QgsFields mFields;
281  QMap<QString, QPair<int, QgsField> > mThematicAttributes;
282 
283  bool mIsException;
284  QString mExceptionText;
285  bool mTruncatedResponse;
287  int mParseDepth;
288  int mFeatureTupleDepth;
289  QString mCurrentTypename;
290  QStack<ParseMode> mParseModeStack;
293  QString mStringCash;
294  QgsFeature *mCurrentFeature = nullptr;
295  QVector<QVariant> mCurrentAttributes; //attributes of current feature
296  QString mCurrentFeatureId;
297  int mFeatureCount;
299  QgsWkbPtr mCurrentWKB;
300  QgsRectangle mCurrentExtent;
301  bool mBoundedByNullFound;
302 
309  QList< QList<QgsWkbPtr> > mCurrentWKBFragments;
310  QString mAttributeName;
311  char mEndian;
313  QString mCoordinateSeparator;
315  QString mTupleSeparator;
317  QStack<int> mDimensionStack;
319  int mDimension;
321  ParseMode mCoorMode;
323  int mEpsg;
325  QString mSrsName;
327  QgsRectangle mLayerExtent;
329  QString mGMLNameSpaceURI;
330  const char *mGMLNameSpaceURIPtr = nullptr;
332  AxisOrientationLogic mAxisOrientationLogic;
334  bool mInvertAxisOrientationRequest;
336  bool mInvertAxisOrientation;
338  int mNumberReturned;
340  int mNumberMatched;
342  std::string mGeometryString;
344  bool mFoundUnhandledGeometryElement;
345 };
346 
347 #endif
348 
356 class CORE_EXPORT QgsGml : public QObject
357 {
358  Q_OBJECT
359  public:
360  QgsGml(
361  const QString &typeName,
362  const QString &geometryAttribute,
363  const QgsFields &fields );
364 
377  int getFeatures( const QString &uri,
378  QgsWkbTypes::Type *wkbType,
379  QgsRectangle *extent = nullptr,
380  const QString &userName = QString(),
381  const QString &password = QString(),
382  const QString &authcfg = QString() ) SIP_PYNAME( getFeaturesUri );
383 
388  int getFeatures( const QByteArray &data, QgsWkbTypes::Type *wkbType, QgsRectangle *extent = nullptr );
389 
391  QMap<QgsFeatureId, QgsFeature * > featuresMap() const { return mFeatures; }
392 
394  QMap<QgsFeatureId, QString > idsMap() const { return mIdMap; }
395 
401 
402  signals:
403  void dataReadProgress( int progress );
404  void totalStepsUpdate( int totalSteps );
406  void dataProgressAndSteps( int progress, int totalSteps );
407 
408  private slots:
409 
410  void setFinished();
411 
413  void handleProgressEvent( qint64 progress, qint64 totalSteps );
414 
415  private:
416 
423  void calculateExtentFromFeatures();
424 
425  void fillMapsFromParser();
426 
427  QgsGmlStreamingParser mParser;
428 
430  QString mTypeName;
431 
433  bool mFinished;
434 
436  //QMap<QgsFeatureId, QgsFeature* > &mFeatures;
437  QMap<QgsFeatureId, QgsFeature * > mFeatures;
438  //QMap<QString, QMap<QgsFeatureId, QgsFeature* > > mFeatures;
439 
441  //QMap<QgsFeatureId, QString > &mIdMap;
442  QMap<QgsFeatureId, QString > mIdMap;
443  //QMap<QString, QMap<QgsFeatureId, QString > > mIdMap;
444 
446  QgsRectangle mExtent;
447 };
448 
449 #endif
QgsGmlStreamingParser::operator=
QgsGmlStreamingParser & operator=(const QgsGmlStreamingParser &other)=delete
QgsGmlStreamingParser cannot be copied.
QgsGml::totalStepsUpdate
void totalStepsUpdate(int totalSteps)
SIP_PYNAME
#define SIP_PYNAME(name)
Definition: qgis_sip.h:81
QgsGmlStreamingParser::LayerProperties::mName
QString mName
Layer name.
Definition: qgsgml.h:65
qgsfields.h
QgsGmlStreamingParser::srsName
QString srsName() const
Returns the value of the srsName attribute.
Definition: qgsgml.h:125
QgsGmlStreamingParser::exceptionText
QString exceptionText() const
Returns the exception text.
Definition: qgsgml.h:143
QgsGmlStreamingParser::Honour_EPSG
@ Honour_EPSG
Honour EPSG axis order.
Definition: qgsgml.h:76
qgsrectangle.h
QgsGml::dataReadProgress
void dataReadProgress(int progress)
qgswkbptr.h
QgsGmlStreamingParser
This class builds features from GML data in a streaming way.
Definition: qgsgml.h:49
crs
const QgsCoordinateReferenceSystem & crs
Definition: qgswfsgetfeature.cpp:51
QgsGmlStreamingParser::isException
bool isException() const
Returns whether the document parser is a OGC exception.
Definition: qgsgml.h:140
QgsFields
Container of fields for a vector layer.
Definition: qgsfields.h:45
qgsfeature.h
QgsGmlStreamingParser::Honour_EPSG_if_urn
@ Honour_EPSG_if_urn
Honour EPSG axis order only if srsName is of the form urn:ogc:def:crs:EPSG:
Definition: qgsgml.h:74
QgsWkbTypes::Type
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:70
QgsGmlStreamingParser::LayerProperties::LayerProperties
LayerProperties()=default
Constructor.
QgsRectangle
A rectangle specified with double values.
Definition: qgsrectangle.h:42
QgsGml::dataProgressAndSteps
void dataProgressAndSteps(int progress, int totalSteps)
Also emit signal with progress and totalSteps together (this is better for the status message)
QgsGmlStreamingParser::QgsGmlStreamingParser
QgsGmlStreamingParser(const QgsGmlStreamingParser &other)=delete
QgsGmlStreamingParser cannot be copied.
QgsGmlStreamingParser::getEPSGCode
int getEPSGCode() const
Returns the EPSG code, or 0 if unknown.
Definition: qgsgml.h:122
qgis_sip.h
QgsGml::featuresMap
QMap< QgsFeatureId, QgsFeature * > featuresMap() const
Gets parsed features for given type name.
Definition: qgsgml.h:391
typeName
const QString & typeName
Definition: qgswfsgetfeature.cpp:55
QgsGmlStreamingParser::isTruncatedResponse
bool isTruncatedResponse() const
Returns whether a "truncatedResponse" element is found.
Definition: qgsgml.h:146
QgsWkbPtr
Definition: qgswkbptr.h:43
QgsCoordinateReferenceSystem
This class represents a coordinate reference system (CRS).
Definition: qgscoordinatereferencesystem.h:206
QgsPointXY
A class to represent a 2D point.
Definition: qgspointxy.h:44
QgsGmlStreamingParser::Ignore_EPSG
@ Ignore_EPSG
Ignore EPSG axis order.
Definition: qgsgml.h:78
QgsGml
This class reads data from a WFS server or alternatively from a GML file.
Definition: qgsgml.h:357
QgsGmlStreamingParser::QgsGmlFeaturePtrGmlIdPair
QPair< QgsFeature *, QString > QgsGmlFeaturePtrGmlIdPair
Definition: qgsgml.h:52
QgsGmlStreamingParser::numberMatched
int numberMatched() const
Returns WFS 2.0 "numberMatched" attribute, or -1 if invalid/not found.
Definition: qgsgml.h:134
QgsGmlStreamingParser::numberReturned
int numberReturned() const
Returns WFS 2.0 "numberReturned" or WFS 1.1 "numberOfFeatures" attribute, or -1 if invalid/not found.
Definition: qgsgml.h:137
QgsGmlStreamingParser::LayerProperties
Layer properties.
Definition: qgsgml.h:59
QgsGmlStreamingParser::layerExtent
const QgsRectangle & layerExtent() const
Returns layer bounding box.
Definition: qgsgml.h:128
QgsFeature
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:56
QgsGmlStreamingParser::wkbType
QgsWkbTypes::Type wkbType() const
Returns the geometry type.
Definition: qgsgml.h:131
QgsGml::idsMap
QMap< QgsFeatureId, QString > idsMap() const
Gets feature ids map.
Definition: qgsgml.h:394
QgsGmlStreamingParser::LayerProperties::mGeometryAttribute
QString mGeometryAttribute
Geometry attribute name.
Definition: qgsgml.h:67