QGIS API Documentation 3.99.0-Master (e9821da5c6b)
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
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 QString srsName() const { return mSrsName; }
136
138 const QgsRectangle &layerExtent() const { return mLayerExtent; }
139
141 Qgis::WkbType wkbType() const { return mWkbType; }
142
144 int numberMatched() const { return mNumberMatched; }
145
147 int numberReturned() const { return mNumberReturned; }
148
150 bool isException() const { return mIsException; }
151
153 QString exceptionText() const { return mExceptionText; }
154
156 bool isTruncatedResponse() const { return mTruncatedResponse; }
157
158 private:
159
160 enum ParseMode
161 {
162 None,
163 BoundingBox,
164 Null,
165 Envelope,
166 LowerCorner,
167 UpperCorner,
168 Feature, // feature element containing attrs and geo (inside gml:featureMember)
169 Attribute,
170 Tuple, // wfs:Tuple of a join layer
171 FeatureTuple,
172 AttributeTuple,
173 Geometry,
174 Coordinate,
175 PosList,
176 MultiPoint,
177 MultiLine,
178 MultiPolygon,
179 ExceptionReport,
180 ExceptionText
181 };
182
184 void startElement( const XML_Char *el, const XML_Char **attr );
185 void endElement( const XML_Char *el );
186 void characters( const XML_Char *chars, int len );
187 static void start( void *data, const XML_Char *el, const XML_Char **attr )
188 {
189 static_cast<QgsGmlStreamingParser *>( data )->startElement( el, attr );
190 }
191 static void end( void *data, const XML_Char *el )
192 {
193 static_cast<QgsGmlStreamingParser *>( data )->endElement( el );
194 }
195 static void chars( void *data, const XML_Char *chars, int len )
196 {
197 static_cast<QgsGmlStreamingParser *>( data )->characters( chars, len );
198 }
199
200 // Add mStringCash to the current json object
201 void addStringContentToJson();
202
203 // Set current feature attribute
204 void setAttribute( const QString &name, const QString &value );
205
206 //helper routines
207
213 int readSrsNameAndDimensionAttributes( const XML_Char **attr );
214
221 QString readAttribute( const QString &attributeName, const XML_Char **attr ) const;
223 bool createBBoxFromCoordinateString( QgsRectangle &bb, const QString &coordString ) const;
224
232 int pointsFromCoordinateString( QList<QgsPoint> &points, const QString &coordString, int *dimension = nullptr ) const;
233
241 int pointsFromPosListString( QList<QgsPoint> &points, const QString &coordString, int dimension ) const;
242
243 int pointsFromString( QList<QgsPoint> &points, const QString &coordString, int *dimension = nullptr ) const;
244 int getPointWKB( QByteArray &wkbPtr, const QgsPoint &, int dimension ) const;
245 int getLineWKB( QByteArray &wkbPtr, const QList<QgsPoint> &lineCoordinates, int dimension ) const;
246 int getRingWKB( QByteArray &wkbPtr, const QList<QgsPoint> &ringCoordinates, int dimension ) const;
247
254 int createMultiLineFromFragments();
255 int createMultiPointFromFragments();
256 int createPolygonFromFragments();
257 int createMultiPolygonFromFragments();
259 int totalWKBFragmentSize() const;
260
262 ParseMode modeStackTop() { return mParseModeStack.isEmpty() ? None : mParseModeStack.top(); }
263
265 ParseMode modeStackPop() { return mParseModeStack.isEmpty() ? None : mParseModeStack.pop(); }
266
268 void createParser( const QByteArray &encoding = QByteArray() );
269
271 XML_Parser mParser = nullptr;
272
274 QVector<QgsGmlFeaturePtrGmlIdPair> mFeatureList;
275
277 QList<LayerProperties> mLayerProperties;
278 QMap< QString, LayerProperties > mMapTypeNameToProperties;
279
281 QString mTypeName;
282 QByteArray mTypeNameBA;
283 const char *mTypeNamePtr = nullptr;
284 size_t mTypeNameUTF8Len;
285
286 Qgis::WkbType mWkbType;
287
288 //results are members such that handler routines are able to manipulate them
289
291 QString mGeometryAttribute;
292 QByteArray mGeometryAttributeBA;
293 const char *mGeometryAttributePtr = nullptr;
294 size_t mGeometryAttributeUTF8Len;
295
296 QgsFields mFields;
297 QMap<QString, QPair<int, QgsField> > mThematicAttributes;
298 QMap<QString, QPair<QString, bool>> mMapXPathToFieldNameAndIsNestedContent;
299 QMap<QString, QString> mMapNamespaceURIToNamespacePrefix;
300
301 bool mIsException;
302 QString mExceptionText;
303 bool mTruncatedResponse;
305 int mParseDepth;
306 int mFeatureTupleDepth;
307 QString mCurrentTypename;
309 QStack<ParseMode> mParseModeStack;
310 QString mCurrentXPathWithinFeature;
312 QString mStringCash;
313 QgsFeature *mCurrentFeature = nullptr;
314 QVector<QVariant> mCurrentAttributes; //attributes of current feature
315 QString mCurrentFeatureId;
316 int mFeatureCount;
318 QByteArray mCurrentWKB;
319 QgsRectangle mCurrentExtent;
320 bool mBoundedByNullFound;
321
328 QList< QList< QByteArray > > mCurrentWKBFragments;
329 QString mAttributeName;
330 int mAttributeDepth = -1;
331 bool mAttributeValIsNested = false;
333 QMap< QString, QString > mMapFieldNameToJSONContent;
334 nlohmann::json mAttributeJson;
335 QStack<nlohmann::json *> mAttributeJsonCurrentStack;
336 char mEndian;
338 QString mCoordinateSeparator;
340 QString mTupleSeparator;
342 QStack<int> mDimensionStack;
344 int mDimension;
346 ParseMode mCoorMode;
348 QString mSrsName;
350 int mDimensionForCurSrsName = 0;
352 QgsRectangle mLayerExtent;
354 QString mGMLNameSpaceURI;
355 const char *mGMLNameSpaceURIPtr = nullptr;
357 AxisOrientationLogic mAxisOrientationLogic;
359 bool mInvertAxisOrientationRequest;
361 bool mInvertAxisOrientation;
363 int mNumberReturned;
365 int mNumberMatched;
367 std::string mGeometryString;
369 bool mFoundUnhandledGeometryElement;
371 QTextCodec *mCodec = nullptr;
372};
373
374#endif
375
384class CORE_EXPORT QgsGml : public QObject
385{
386 Q_OBJECT
387 public:
388 QgsGml(
389 const QString &typeName,
390 const QString &geometryAttribute,
391 const QgsFields &fields );
392
405 int getFeatures( const QString &uri,
406 Qgis::WkbType *wkbType,
407 QgsRectangle *extent = nullptr,
408 const QString &userName = QString(),
409 const QString &password = QString(),
410 const QString &authcfg = QString() ) SIP_PYNAME( getFeaturesUri );
411
417 int getFeatures( const QByteArray &data, Qgis::WkbType *wkbType, QgsRectangle *extent = nullptr );
418
420 QMap<QgsFeatureId, QgsFeature * > featuresMap() const { return mFeatures; }
421
423 QMap<QgsFeatureId, QString > idsMap() const { return mIdMap; }
424
429
430 signals:
431
437 void dataReadProgress( int progress );
438
444 void totalStepsUpdate( int totalSteps );
445
452 void dataProgressAndSteps( int progress, int totalSteps );
453
454 private slots:
455
456 void setFinished();
457
459 void handleProgressEvent( qint64 progress, qint64 totalSteps );
460
461 private:
462
469 void calculateExtentFromFeatures();
470
471 void fillMapsFromParser();
472
473 QgsGmlStreamingParser mParser;
474
476 QString mTypeName;
477
479 bool mFinished = false;
480
482 //QMap<QgsFeatureId, QgsFeature* > &mFeatures;
483 QMap<QgsFeatureId, QgsFeature * > mFeatures;
484 //QMap<QString, QMap<QgsFeatureId, QgsFeature* > > mFeatures;
485
487 //QMap<QgsFeatureId, QString > &mIdMap;
488 QMap<QgsFeatureId, QString > mIdMap;
489 //QMap<QString, QMap<QgsFeatureId, QString > > mIdMap;
490
492 QgsRectangle mExtent;
493};
494
495#endif
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:280
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:73
Builds features from GML data in a streaming way.
Definition qgsgml.h:55
QPair< QgsFeature *, QString > QgsGmlFeaturePtrGmlIdPair
Definition qgsgml.h:58
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:429
bool isException() const
Returns whether the document parser is a OGC exception.
Definition qgsgml.h:150
int numberReturned() const
Returns WFS 2.0 "numberReturned" or WFS 1.1 "numberOfFeatures" attribute, or -1 if invalid/not found.
Definition qgsgml.h:147
Qgis::WkbType wkbType() const
Returns the geometry type.
Definition qgsgml.h:141
@ 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:144
const QgsRectangle & layerExtent() const
Returns layer bounding box.
Definition qgsgml.h:138
QgsGmlStreamingParser(const QString &typeName, const QString &geometryAttribute, const QgsFields &fields, AxisOrientationLogic axisOrientationLogic=Honour_EPSG_if_urn, bool invertAxisOrientation=false)
Constructor.
Definition qgsgml.cpp:290
QgsGmlStreamingParser(const QgsGmlStreamingParser &other)=delete
bool isTruncatedResponse() const
Returns whether a "truncatedResponse" element is found.
Definition qgsgml.h:156
bool processData(const QByteArray &data, bool atEnd, QString &errorMsg)
Process a new chunk of data.
Definition qgsgml.cpp:467
QString exceptionText() const
Returns the exception text.
Definition qgsgml.h:153
QgsGmlStreamingParser & operator=(const QgsGmlStreamingParser &other)=delete
QVector< QgsGmlFeaturePtrGmlIdPair > getAndStealReadyFeatures()
Returns the list of features that have been completely parsed.
Definition qgsgml.cpp:512
QString srsName() const
Returns the value of the srsName attribute.
Definition qgsgml.h:135
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:423
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:73
QMap< QgsFeatureId, QgsFeature * > featuresMap() const
Gets parsed features for given type name.
Definition qgsgml.h:420
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:89