QGIS API Documentation 3.99.0-Master (51df526a401)
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
35#ifndef SIP_RUN
36#include <nlohmann/json.hpp>
37#endif
38
40class QTextCodec;
41
42#ifndef SIP_RUN
43
54class CORE_EXPORT QgsGmlStreamingParser
55{
56 public:
57
58 typedef QPair<QgsFeature *, QString> QgsGmlFeaturePtrGmlIdPair;
59
65 {
66 public:
67
68 LayerProperties() = default;
69
71 QString mName;
74 };
75
77 typedef enum
78 {
85 } AxisOrientationLogic;
86
88 QgsGmlStreamingParser( const QString &typeName,
89 const QString &geometryAttribute,
90 const QgsFields &fields,
91 AxisOrientationLogic axisOrientationLogic = Honour_EPSG_if_urn,
92 bool invertAxisOrientation = false );
93
95 QgsGmlStreamingParser( const QList<LayerProperties> &layerProperties,
96 const QgsFields &fields,
97 const QMap< QString, QPair<QString, QString> > &fieldNameToSrcLayerNameFieldNameMap,
98 AxisOrientationLogic axisOrientationLogic = Honour_EPSG_if_urn,
99 bool invertAxisOrientation = false );
101
104
110 void setFieldsXPath(
111 const QMap<QString, QPair<QString, bool>> &fieldNameToSrcLayerNameFieldNameMap,
112 const QMap<QString, QString> &namespacePrefixToURIMap );
113
118 bool processData( const QByteArray &data, bool atEnd, QString &errorMsg );
119
124 bool processData( const QByteArray &data, bool atEnd );
125
132 QVector<QgsGmlFeaturePtrGmlIdPair> getAndStealReadyFeatures();
133
135 int getEPSGCode() const { return mEpsg; }
136
138 QString srsName() const { return mSrsName; }
139
141 const QgsRectangle &layerExtent() const { return mLayerExtent; }
142
144 Qgis::WkbType wkbType() const { return mWkbType; }
145
147 int numberMatched() const { return mNumberMatched; }
148
150 int numberReturned() const { return mNumberReturned; }
151
153 bool isException() const { return mIsException; }
154
156 QString exceptionText() const { return mExceptionText; }
157
159 bool isTruncatedResponse() const { return mTruncatedResponse; }
160
161 private:
162
163 enum ParseMode
164 {
165 None,
166 BoundingBox,
167 Null,
168 Envelope,
169 LowerCorner,
170 UpperCorner,
171 Feature, // feature element containing attrs and geo (inside gml:featureMember)
172 Attribute,
173 Tuple, // wfs:Tuple of a join layer
174 FeatureTuple,
175 AttributeTuple,
176 Geometry,
177 Coordinate,
178 PosList,
179 MultiPoint,
180 MultiLine,
181 MultiPolygon,
182 ExceptionReport,
183 ExceptionText
184 };
185
187 void startElement( const XML_Char *el, const XML_Char **attr );
188 void endElement( const XML_Char *el );
189 void characters( const XML_Char *chars, int len );
190 static void start( void *data, const XML_Char *el, const XML_Char **attr )
191 {
192 static_cast<QgsGmlStreamingParser *>( data )->startElement( el, attr );
193 }
194 static void end( void *data, const XML_Char *el )
195 {
196 static_cast<QgsGmlStreamingParser *>( data )->endElement( el );
197 }
198 static void chars( void *data, const XML_Char *chars, int len )
199 {
200 static_cast<QgsGmlStreamingParser *>( data )->characters( chars, len );
201 }
202
203 // Add mStringCash to the current json object
204 void addStringContentToJson();
205
206 // Set current feature attribute
207 void setAttribute( const QString &name, const QString &value );
208
209 //helper routines
210
217 int readEpsgFromAttribute( int &epsgNr, const XML_Char **attr );
218
225 QString readAttribute( const QString &attributeName, const XML_Char **attr ) const;
227 bool createBBoxFromCoordinateString( QgsRectangle &bb, const QString &coordString ) const;
228
236 int pointsFromCoordinateString( QList<QgsPoint> &points, const QString &coordString, int *dimension = nullptr ) const;
237
245 int pointsFromPosListString( QList<QgsPoint> &points, const QString &coordString, int dimension ) const;
246
247 int pointsFromString( QList<QgsPoint> &points, const QString &coordString, int *dimension = nullptr ) const;
248 int getPointWKB( QByteArray &wkbPtr, const QgsPoint &, int dimension ) const;
249 int getLineWKB( QByteArray &wkbPtr, const QList<QgsPoint> &lineCoordinates, int dimension ) const;
250 int getRingWKB( QByteArray &wkbPtr, const QList<QgsPoint> &ringCoordinates, int dimension ) const;
251
258 int createMultiLineFromFragments();
259 int createMultiPointFromFragments();
260 int createPolygonFromFragments();
261 int createMultiPolygonFromFragments();
263 int totalWKBFragmentSize() const;
264
266 ParseMode modeStackTop() { return mParseModeStack.isEmpty() ? None : mParseModeStack.top(); }
267
269 ParseMode modeStackPop() { return mParseModeStack.isEmpty() ? None : mParseModeStack.pop(); }
270
272 void createParser( const QByteArray &encoding = QByteArray() );
273
275 XML_Parser mParser = nullptr;
276
278 QVector<QgsGmlFeaturePtrGmlIdPair> mFeatureList;
279
281 QList<LayerProperties> mLayerProperties;
282 QMap< QString, LayerProperties > mMapTypeNameToProperties;
283
285 QString mTypeName;
286 QByteArray mTypeNameBA;
287 const char *mTypeNamePtr = nullptr;
288 size_t mTypeNameUTF8Len;
289
290 Qgis::WkbType mWkbType;
291
292 //results are members such that handler routines are able to manipulate them
293
295 QString mGeometryAttribute;
296 QByteArray mGeometryAttributeBA;
297 const char *mGeometryAttributePtr = nullptr;
298 size_t mGeometryAttributeUTF8Len;
299
300 QgsFields mFields;
301 QMap<QString, QPair<int, QgsField> > mThematicAttributes;
302 QMap<QString, QPair<QString, bool>> mMapXPathToFieldNameAndIsNestedContent;
303 QMap<QString, QString> mMapNamespaceURIToNamespacePrefix;
304
305 bool mIsException;
306 QString mExceptionText;
307 bool mTruncatedResponse;
309 int mParseDepth;
310 int mFeatureTupleDepth;
311 QString mCurrentTypename;
313 QStack<ParseMode> mParseModeStack;
314 QString mCurrentXPathWithinFeature;
316 QString mStringCash;
317 QgsFeature *mCurrentFeature = nullptr;
318 QVector<QVariant> mCurrentAttributes; //attributes of current feature
319 QString mCurrentFeatureId;
320 int mFeatureCount;
322 QByteArray mCurrentWKB;
323 QgsRectangle mCurrentExtent;
324 bool mBoundedByNullFound;
325
332 QList< QList< QByteArray > > mCurrentWKBFragments;
333 QString mAttributeName;
334 int mAttributeDepth = -1;
335 bool mAttributeValIsNested = false;
337 QMap< QString, QString > mMapFieldNameToJSONContent;
338 nlohmann::json mAttributeJson;
339 QStack<nlohmann::json *> mAttributeJsonCurrentStack;
340 char mEndian;
342 QString mCoordinateSeparator;
344 QString mTupleSeparator;
346 QStack<int> mDimensionStack;
348 int mDimension;
350 ParseMode mCoorMode;
352 int mEpsg;
354 QString mSrsName;
356 QgsRectangle mLayerExtent;
358 QString mGMLNameSpaceURI;
359 const char *mGMLNameSpaceURIPtr = nullptr;
361 AxisOrientationLogic mAxisOrientationLogic;
363 bool mInvertAxisOrientationRequest;
365 bool mInvertAxisOrientation;
367 int mNumberReturned;
369 int mNumberMatched;
371 std::string mGeometryString;
373 bool mFoundUnhandledGeometryElement;
375 QTextCodec *mCodec = nullptr;
376};
377
378#endif
379
388class CORE_EXPORT QgsGml : public QObject
389{
390 Q_OBJECT
391 public:
392 QgsGml(
393 const QString &typeName,
394 const QString &geometryAttribute,
395 const QgsFields &fields );
396
409 int getFeatures( const QString &uri,
410 Qgis::WkbType *wkbType,
411 QgsRectangle *extent = nullptr,
412 const QString &userName = QString(),
413 const QString &password = QString(),
414 const QString &authcfg = QString() ) SIP_PYNAME( getFeaturesUri );
415
421 int getFeatures( const QByteArray &data, Qgis::WkbType *wkbType, QgsRectangle *extent = nullptr );
422
424 QMap<QgsFeatureId, QgsFeature * > featuresMap() const { return mFeatures; }
425
427 QMap<QgsFeatureId, QString > idsMap() const { return mIdMap; }
428
433
434 signals:
435
441 void dataReadProgress( int progress );
442
448 void totalStepsUpdate( int totalSteps );
449
456 void dataProgressAndSteps( int progress, int totalSteps );
457
458 private slots:
459
460 void setFinished();
461
463 void handleProgressEvent( qint64 progress, qint64 totalSteps );
464
465 private:
466
473 void calculateExtentFromFeatures();
474
475 void fillMapsFromParser();
476
477 QgsGmlStreamingParser mParser;
478
480 QString mTypeName;
481
483 bool mFinished;
484
486 //QMap<QgsFeatureId, QgsFeature* > &mFeatures;
487 QMap<QgsFeatureId, QgsFeature * > mFeatures;
488 //QMap<QString, QMap<QgsFeatureId, QgsFeature* > > mFeatures;
489
491 //QMap<QgsFeatureId, QString > &mIdMap;
492 QMap<QgsFeatureId, QString > mIdMap;
493 //QMap<QString, QMap<QgsFeatureId, QString > > mIdMap;
494
496 QgsRectangle mExtent;
497};
498
499#endif
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:256
Represents a coordinate reference system (CRS).
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:58
Container of fields for a vector layer.
Definition qgsfields.h:46
QString mGeometryAttribute
Geometry attribute name.
Definition qgsgml.h:73
Builds features from GML data in a streaming way.
Definition qgsgml.h:55
QPair< QgsFeature *, QString > QgsGmlFeaturePtrGmlIdPair
Definition qgsgml.h:58
bool isException() const
Returns whether the document parser is a OGC exception.
Definition qgsgml.h:153
int numberReturned() const
Returns WFS 2.0 "numberReturned" or WFS 1.1 "numberOfFeatures" attribute, or -1 if invalid/not found.
Definition qgsgml.h:150
Qgis::WkbType wkbType() const
Returns the geometry type.
Definition qgsgml.h:144
@ Ignore_EPSG
Ignore EPSG axis order.
Definition qgsgml.h:84
@ Honour_EPSG
Honour EPSG axis order.
Definition qgsgml.h:82
@ Honour_EPSG_if_urn
Honour EPSG axis order only if srsName is of the form urn:ogc:def:crs:EPSG:
Definition qgsgml.h:80
int numberMatched() const
Returns WFS 2.0 "numberMatched" attribute, or -1 if invalid/not found.
Definition qgsgml.h:147
const QgsRectangle & layerExtent() const
Returns layer bounding box.
Definition qgsgml.h:141
QgsGmlStreamingParser(const QgsGmlStreamingParser &other)=delete
bool isTruncatedResponse() const
Returns whether a "truncatedResponse" element is found.
Definition qgsgml.h:159
QString exceptionText() const
Returns the exception text.
Definition qgsgml.h:156
QgsGmlStreamingParser & operator=(const QgsGmlStreamingParser &other)=delete
int getEPSGCode() const
Returns the EPSG code, or 0 if unknown.
Definition qgsgml.h:135
QString srsName() const
Returns the value of the srsName attribute.
Definition qgsgml.h:138
Reads data from a WFS server or alternatively from a GML file.
Definition qgsgml.h:389
void totalStepsUpdate(int totalSteps)
Emitted when the total number of bytes to read changes.
void dataReadProgress(int progress)
Emitted when data reading progresses.
QMap< QgsFeatureId, QString > idsMap() const
Gets feature ids map.
Definition qgsgml.h:427
QMap< QgsFeatureId, QgsFeature * > featuresMap() const
Gets parsed features for given type name.
Definition qgsgml.h:424
void dataProgressAndSteps(int progress, int totalSteps)
Emitted when data reading progresses or the total number of bytes to read changes.
Point geometry type, with support for z-dimension and m-values.
Definition qgspoint.h:49
A rectangle specified with double values.
#define SIP_PYNAME(name)
Definition qgis_sip.h:81
const QgsCoordinateReferenceSystem & crs
const QString & typeName