QGIS API Documentation 4.1.0-Master (659fe69c07c)
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 <expat.h>
19#include <string>
20
21#include "qgis_core.h"
22#include "qgis_sip.h"
23#include "qgsfeature.h"
24#include "qgsfields.h"
25#include "qgsrectangle.h"
26#include "qgswkbptr.h"
27
28#include <QByteArray>
29#include <QDomElement>
30#include <QPair>
31#include <QStack>
32#include <QStringList>
33#include <QVector>
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 typedef QPair<QgsFeature *, QString> QgsGmlFeaturePtrGmlIdPair;
58
64 {
65 public:
66 LayerProperties() = default;
67
69 QString mName;
72 };
73
75 typedef enum
76 {
83 } AxisOrientationLogic;
84
87 const QString &typeName, const QString &geometryAttribute, const QgsFields &fields, AxisOrientationLogic axisOrientationLogic = Honour_EPSG_if_urn, bool invertAxisOrientation = false
88 );
89
92 const QList<LayerProperties> &layerProperties,
93 const QgsFields &fields,
94 const QMap< QString, QPair<QString, QString> > &fieldNameToSrcLayerNameFieldNameMap,
95 AxisOrientationLogic axisOrientationLogic = Honour_EPSG_if_urn,
96 bool invertAxisOrientation = false
97 );
99
102
108 void setFieldsXPath( const QMap<QString, QPair<QString, bool>> &fieldNameToSrcLayerNameFieldNameMap, const QMap<QString, QString> &namespacePrefixToURIMap );
109
114 bool processData( const QByteArray &data, bool atEnd, QString &errorMsg );
115
120 bool processData( const QByteArray &data, bool atEnd );
121
128 QVector<QgsGmlFeaturePtrGmlIdPair> getAndStealReadyFeatures();
129
131 QString srsName() const { return mSrsName; }
132
134 const QgsRectangle &layerExtent() const { return mLayerExtent; }
135
137 Qgis::WkbType wkbType() const { return mWkbType; }
138
140 int numberMatched() const { return mNumberMatched; }
141
143 int numberReturned() const { return mNumberReturned; }
144
146 bool isException() const { return mIsException; }
147
149 QString exceptionText() const { return mExceptionText; }
150
152 bool isTruncatedResponse() const { return mTruncatedResponse; }
153
154 private:
155 enum ParseMode
156 {
157 None,
158 BoundingBox,
159 Null,
160 Envelope,
161 LowerCorner,
162 UpperCorner,
163 Feature,
164 Attribute,
165 Tuple,
166 FeatureTuple,
167 AttributeTuple,
168 Geometry,
169 Coordinate,
170 PosList,
171 MultiPoint,
172 MultiLine,
173 MultiPolygon,
174 ExceptionReport,
175 ExceptionText
176 };
177
179 void startElement( const XML_Char *el, const XML_Char **attr );
180 void endElement( const XML_Char *el );
181 void characters( const XML_Char *chars, int len );
182 static void start( void *data, const XML_Char *el, const XML_Char **attr ) { static_cast<QgsGmlStreamingParser *>( data )->startElement( el, attr ); }
183 static void end( void *data, const XML_Char *el ) { static_cast<QgsGmlStreamingParser *>( data )->endElement( el ); }
184 static void chars( void *data, const XML_Char *chars, int len ) { static_cast<QgsGmlStreamingParser *>( data )->characters( chars, len ); }
185
186 // Add mStringCash to the current json object
187 void addStringContentToJson();
188
189 // Set current feature attribute
190 void setAttribute( const QString &name, const QString &value );
191
192 //helper routines
193
199 int readSrsNameAndDimensionAttributes( const XML_Char **attr );
200
207 QString readAttribute( const QString &attributeName, const XML_Char **attr ) const;
209 bool createBBoxFromCoordinateString( QgsRectangle &bb, const QString &coordString ) const;
210
218 int pointsFromCoordinateString( QList<QgsPoint> &points, const QString &coordString, int *dimension = nullptr ) const;
219
227 int pointsFromPosListString( QList<QgsPoint> &points, const QString &coordString, int dimension ) const;
228
229 int pointsFromString( QList<QgsPoint> &points, const QString &coordString, int *dimension = nullptr ) const;
230 int getPointWKB( QByteArray &wkbPtr, const QgsPoint &, int dimension ) const;
231 int getLineWKB( QByteArray &wkbPtr, const QList<QgsPoint> &lineCoordinates, int dimension ) const;
232 int getRingWKB( QByteArray &wkbPtr, const QList<QgsPoint> &ringCoordinates, int dimension ) const;
233
240 int createMultiLineFromFragments();
241 int createMultiPointFromFragments();
242 int createPolygonFromFragments();
243 int createMultiPolygonFromFragments();
245 int totalWKBFragmentSize() const;
246
248 ParseMode modeStackTop() { return mParseModeStack.isEmpty() ? None : mParseModeStack.top(); }
249
251 ParseMode modeStackPop() { return mParseModeStack.isEmpty() ? None : mParseModeStack.pop(); }
252
254 void createParser( const QByteArray &encoding = QByteArray() );
255
257 XML_Parser mParser = nullptr;
258
260 QVector<QgsGmlFeaturePtrGmlIdPair> mFeatureList;
261
263 QList<LayerProperties> mLayerProperties;
264 QMap< QString, LayerProperties > mMapTypeNameToProperties;
265
267 QString mTypeName;
268 QByteArray mTypeNameBA;
269 const char *mTypeNamePtr = nullptr;
270 size_t mTypeNameUTF8Len;
271
272 Qgis::WkbType mWkbType;
273
274 //results are members such that handler routines are able to manipulate them
275
277 QString mGeometryAttribute;
278 QByteArray mGeometryAttributeBA;
279 const char *mGeometryAttributePtr = nullptr;
280 size_t mGeometryAttributeUTF8Len;
281
282 QgsFields mFields;
283 QMap<QString, QPair<int, QgsField> > mThematicAttributes;
284 QMap<QString, QPair<QString, bool>> mMapXPathToFieldNameAndIsNestedContent;
285 QMap<QString, QString> mMapNamespaceURIToNamespacePrefix;
286
287 bool mIsException;
288 QString mExceptionText;
289 bool mTruncatedResponse;
291 int mParseDepth;
292 int mFeatureTupleDepth;
293 QString mCurrentTypename;
295 QStack<ParseMode> mParseModeStack;
296 QString mCurrentXPathWithinFeature;
298 QString mStringCash;
299 QgsFeature *mCurrentFeature = nullptr;
300 QVector<QVariant> mCurrentAttributes; //attributes of current feature
301 QString mCurrentFeatureId;
302 int mFeatureCount;
304 QByteArray mCurrentWKB;
305 QgsRectangle mCurrentExtent;
306 bool mBoundedByNullFound;
307
314 QList< QList< QByteArray > > mCurrentWKBFragments;
315 QString mAttributeName;
316 int mAttributeDepth = -1;
317 bool mAttributeValIsNested = false;
319 QMap< QString, QString > mMapFieldNameToJSONContent;
320 nlohmann::json mAttributeJson;
321 QStack<nlohmann::json *> mAttributeJsonCurrentStack;
322 char mEndian;
324 QString mCoordinateSeparator;
326 QString mTupleSeparator;
328 QStack<int> mDimensionStack;
330 int mDimension;
332 ParseMode mCoorMode;
334 QString mSrsName;
336 int mDimensionForCurSrsName = 0;
338 QgsRectangle mLayerExtent;
340 QString mGMLNameSpaceURI;
341 const char *mGMLNameSpaceURIPtr = nullptr;
343 AxisOrientationLogic mAxisOrientationLogic;
345 bool mInvertAxisOrientationRequest;
347 bool mInvertAxisOrientation;
349 int mNumberReturned;
351 int mNumberMatched;
353 std::string mGeometryString;
355 bool mFoundUnhandledGeometryElement;
357 QTextCodec *mCodec = nullptr;
358};
359
360#endif
361
370class CORE_EXPORT QgsGml : public QObject
371{
372 Q_OBJECT
373 public:
374 QgsGml( const QString &typeName, const QString &geometryAttribute, const QgsFields &fields );
375
388 int getFeatures(
389 const QString &uri, Qgis::WkbType *wkbType, QgsRectangle *extent = nullptr, const QString &userName = QString(), const QString &password = QString(), const QString &authcfg = QString()
390 ) SIP_PYNAME( getFeaturesUri );
391
397 int getFeatures( const QByteArray &data, Qgis::WkbType *wkbType, QgsRectangle *extent = nullptr );
398
400 QMap<QgsFeatureId, QgsFeature * > featuresMap() const { return mFeatures; }
401
403 QMap<QgsFeatureId, QString > idsMap() const { return mIdMap; }
404
409
410 signals:
411
417 void dataReadProgress( int progress );
418
424 void totalStepsUpdate( int totalSteps );
425
432 void dataProgressAndSteps( int progress, int totalSteps );
433
434 private slots:
435
436 void setFinished();
437
439 void handleProgressEvent( qint64 progress, qint64 totalSteps );
440
441 private:
448 void calculateExtentFromFeatures();
449
450 void fillMapsFromParser();
451
452 QgsGmlStreamingParser mParser;
453
455 QString mTypeName;
456
458 bool mFinished = false;
459
461 //QMap<QgsFeatureId, QgsFeature* > &mFeatures;
462 QMap<QgsFeatureId, QgsFeature * > mFeatures;
463 //QMap<QString, QMap<QgsFeatureId, QgsFeature* > > mFeatures;
464
466 //QMap<QgsFeatureId, QString > &mIdMap;
467 QMap<QgsFeatureId, QString > mIdMap;
468 //QMap<QString, QMap<QgsFeatureId, QString > > mIdMap;
469
471 QgsRectangle mExtent;
472};
473
474#endif
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:294
Represents a coordinate reference system (CRS).
Container of fields for a vector layer.
Definition qgsfields.h:46
QString mGeometryAttribute
Geometry attribute name.
Definition qgsgml.h:71
Builds features from GML data in a streaming way.
Definition qgsgml.h:55
QPair< QgsFeature *, QString > QgsGmlFeaturePtrGmlIdPair
Definition qgsgml.h:57
void setFieldsXPath(const QMap< QString, QPair< QString, bool > > &fieldNameToSrcLayerNameFieldNameMap, const QMap< QString, QString > &namespacePrefixToURIMap)
Define the XPath of the attributes and whether they are made of nested content.
Definition qgsgml.cpp:407
bool isException() const
Returns whether the document parser is a OGC exception.
Definition qgsgml.h:146
int numberReturned() const
Returns WFS 2.0 "numberReturned" or WFS 1.1 "numberOfFeatures" attribute, or -1 if invalid/not found.
Definition qgsgml.h:143
Qgis::WkbType wkbType() const
Returns the geometry type.
Definition qgsgml.h:137
@ Ignore_EPSG
Ignore EPSG axis order.
Definition qgsgml.h:82
@ Honour_EPSG
Honour EPSG axis order.
Definition qgsgml.h:80
@ Honour_EPSG_if_urn
Honour EPSG axis order only if srsName is of the form urn:ogc:def:crs:EPSG:
Definition qgsgml.h:78
int numberMatched() const
Returns WFS 2.0 "numberMatched" attribute, or -1 if invalid/not found.
Definition qgsgml.h:140
const QgsRectangle & layerExtent() const
Returns layer bounding box.
Definition qgsgml.h:134
QgsGmlStreamingParser(const QString &typeName, const QString &geometryAttribute, const QgsFields &fields, AxisOrientationLogic axisOrientationLogic=Honour_EPSG_if_urn, bool invertAxisOrientation=false)
Constructor.
Definition qgsgml.cpp:271
QgsGmlStreamingParser(const QgsGmlStreamingParser &other)=delete
bool isTruncatedResponse() const
Returns whether a "truncatedResponse" element is found.
Definition qgsgml.h:152
bool processData(const QByteArray &data, bool atEnd, QString &errorMsg)
Process a new chunk of data.
Definition qgsgml.cpp:443
QString exceptionText() const
Returns the exception text.
Definition qgsgml.h:149
QgsGmlStreamingParser & operator=(const QgsGmlStreamingParser &other)=delete
QVector< QgsGmlFeaturePtrGmlIdPair > getAndStealReadyFeatures()
Returns the list of features that have been completely parsed.
Definition qgsgml.cpp:484
QString srsName() const
Returns the value of the srsName attribute.
Definition qgsgml.h:131
void totalStepsUpdate(int totalSteps)
Emitted when the total number of bytes to read changes.
void dataReadProgress(int progress)
Emitted when data reading progresses.
QgsGml(const QString &typeName, const QString &geometryAttribute, const QgsFields &fields)
Definition qgsgml.cpp:59
QMap< QgsFeatureId, QString > idsMap() const
Gets feature ids map.
Definition qgsgml.h:403
int getFeatures(const QString &uri, Qgis::WkbType *wkbType, QgsRectangle *extent=nullptr, const QString &userName=QString(), const QString &password=QString(), const QString &authcfg=QString())
Does the HTTP GET request to the WFS server.
Definition qgsgml.cpp:70
QMap< QgsFeatureId, QgsFeature * > featuresMap() const
Gets parsed features for given type name.
Definition qgsgml.h:400
void dataProgressAndSteps(int progress, int totalSteps)
Emitted when data reading progresses or the total number of bytes to read changes.
A rectangle specified with double values.
#define SIP_PYNAME(name)
Definition qgis_sip.h:88