QGIS API Documentation 3.39.0-Master (d85f3c2a281)
Loading...
Searching...
No Matches
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
235 int pointsFromCoordinateString( QList<QgsPointXY> &points, const QString &coordString ) const;
236
244 int pointsFromPosListString( QList<QgsPointXY> &points, const QString &coordString, int dimension ) const;
245
246 int pointsFromString( QList<QgsPointXY> &points, const QString &coordString ) const;
247 int getPointWKB( QgsWkbPtr &wkbPtr, const QgsPointXY & ) const;
248 int getLineWKB( QgsWkbPtr &wkbPtr, const QList<QgsPointXY> &lineCoordinates ) const;
249 int getRingWKB( QgsWkbPtr &wkbPtr, const QList<QgsPointXY> &ringCoordinates ) const;
250
257 int createMultiLineFromFragments();
258 int createMultiPointFromFragments();
259 int createPolygonFromFragments();
260 int createMultiPolygonFromFragments();
262 int totalWKBFragmentSize() const;
263
265 ParseMode modeStackTop() { return mParseModeStack.isEmpty() ? None : mParseModeStack.top(); }
266
268 ParseMode modeStackPop() { return mParseModeStack.isEmpty() ? None : mParseModeStack.pop(); }
269
271 void createParser( const QByteArray &encoding = QByteArray() );
272
274 XML_Parser mParser = nullptr;
275
277 QVector<QgsGmlFeaturePtrGmlIdPair> mFeatureList;
278
280 QList<LayerProperties> mLayerProperties;
281 QMap< QString, LayerProperties > mMapTypeNameToProperties;
282
284 QString mTypeName;
285 QByteArray mTypeNameBA;
286 const char *mTypeNamePtr = nullptr;
287 size_t mTypeNameUTF8Len;
288
289 Qgis::WkbType mWkbType;
290
291 //results are members such that handler routines are able to manipulate them
292
294 QString mGeometryAttribute;
295 QByteArray mGeometryAttributeBA;
296 const char *mGeometryAttributePtr = nullptr;
297 size_t mGeometryAttributeUTF8Len;
298
299 QgsFields mFields;
300 QMap<QString, QPair<int, QgsField> > mThematicAttributes;
301 QMap<QString, QPair<QString, bool>> mMapXPathToFieldNameAndIsNestedContent;
302 QMap<QString, QString> mMapNamespaceURIToNamespacePrefix;
303
304 bool mIsException;
305 QString mExceptionText;
306 bool mTruncatedResponse;
308 int mParseDepth;
309 int mFeatureTupleDepth;
310 QString mCurrentTypename;
312 QStack<ParseMode> mParseModeStack;
313 QString mCurrentXPathWithinFeature;
315 QString mStringCash;
316 QgsFeature *mCurrentFeature = nullptr;
317 QVector<QVariant> mCurrentAttributes; //attributes of current feature
318 QString mCurrentFeatureId;
319 int mFeatureCount;
321 QgsWkbPtr mCurrentWKB;
322 QgsRectangle mCurrentExtent;
323 bool mBoundedByNullFound;
324
331 QList< QList<QgsWkbPtr> > mCurrentWKBFragments;
332 QString mAttributeName;
333 int mAttributeDepth = -1;
334 bool mAttributeValIsNested = false;
336 QMap< QString, QString > mMapFieldNameToJSONContent;
337 nlohmann::json mAttributeJson;
338 QStack<nlohmann::json *> mAttributeJsonCurrentStack;
339 char mEndian;
341 QString mCoordinateSeparator;
343 QString mTupleSeparator;
345 QStack<int> mDimensionStack;
347 int mDimension;
349 ParseMode mCoorMode;
351 int mEpsg;
353 QString mSrsName;
355 QgsRectangle mLayerExtent;
357 QString mGMLNameSpaceURI;
358 const char *mGMLNameSpaceURIPtr = nullptr;
360 AxisOrientationLogic mAxisOrientationLogic;
362 bool mInvertAxisOrientationRequest;
364 bool mInvertAxisOrientation;
366 int mNumberReturned;
368 int mNumberMatched;
370 std::string mGeometryString;
372 bool mFoundUnhandledGeometryElement;
374 QTextCodec *mCodec = nullptr;
375};
376
377#endif
378
387class CORE_EXPORT QgsGml : public QObject
388{
389 Q_OBJECT
390 public:
391 QgsGml(
392 const QString &typeName,
393 const QString &geometryAttribute,
394 const QgsFields &fields );
395
408 int getFeatures( const QString &uri,
409 Qgis::WkbType *wkbType,
410 QgsRectangle *extent = nullptr,
411 const QString &userName = QString(),
412 const QString &password = QString(),
413 const QString &authcfg = QString() ) SIP_PYNAME( getFeaturesUri );
414
420 int getFeatures( const QByteArray &data, Qgis::WkbType *wkbType, QgsRectangle *extent = nullptr );
421
423 QMap<QgsFeatureId, QgsFeature * > featuresMap() const { return mFeatures; }
424
426 QMap<QgsFeatureId, QString > idsMap() const { return mIdMap; }
427
432
433 signals:
434
440 void dataReadProgress( int progress );
441
447 void totalStepsUpdate( int totalSteps );
448
455 void dataProgressAndSteps( int progress, int totalSteps );
456
457 private slots:
458
459 void setFinished();
460
462 void handleProgressEvent( qint64 progress, qint64 totalSteps );
463
464 private:
465
472 void calculateExtentFromFeatures();
473
474 void fillMapsFromParser();
475
476 QgsGmlStreamingParser mParser;
477
479 QString mTypeName;
480
482 bool mFinished;
483
485 //QMap<QgsFeatureId, QgsFeature* > &mFeatures;
486 QMap<QgsFeatureId, QgsFeature * > mFeatures;
487 //QMap<QString, QMap<QgsFeatureId, QgsFeature* > > mFeatures;
488
490 //QMap<QgsFeatureId, QString > &mIdMap;
491 QMap<QgsFeatureId, QString > mIdMap;
492 //QMap<QString, QMap<QgsFeatureId, QString > > mIdMap;
493
495 QgsRectangle mExtent;
496};
497
498#endif
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:256
This class 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
This class 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
This class reads data from a WFS server or alternatively from a GML file.
Definition qgsgml.h:388
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:426
QMap< QgsFeatureId, QgsFeature * > featuresMap() const
Gets parsed features for given type name.
Definition qgsgml.h:423
void dataProgressAndSteps(int progress, int totalSteps)
Emitted when data reading progresses or the total number of bytes to read changes.
A class to represent a 2D point.
Definition qgspointxy.h:60
A rectangle specified with double values.
WKB pointer handler.
Definition qgswkbptr.h:44
#define SIP_PYNAME(name)
Definition qgis_sip.h:81
const QgsCoordinateReferenceSystem & crs
const QString & typeName