QGIS API Documentation  3.6.0-Noosa (5873452)
qgsgmlschema.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsgmlschema.h
3  --------------------------------------
4  Date : Sun Sep 16 12:19:55 AKDT 2007
5  Copyright : (C) 2007 by Gary E. Sherman
6  Email : sherman at mrcc 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 QGSGMLSCHEMA_H
16 #define QGSGMLSCHEMA_H
17 
18 #include "qgis_core.h"
19 #include <expat.h>
20 #include "qgis_sip.h"
21 #include "qgserror.h"
22 #include "qgsfields.h"
23 #include <list>
24 #include <set>
25 #include <stack>
26 #include <QPair>
27 #include <QByteArray>
28 #include <QDomElement>
29 #include <QStringList>
30 #include <QStack>
31 
32 class QgsRectangle;
34 class QgsFeature;
35 
40 class CORE_EXPORT QgsGmlFeatureClass
41 {
42  public:
43 
47  QgsGmlFeatureClass() = default;
48  QgsGmlFeatureClass( const QString &name, const QString &path );
49 
50  QList<QgsField> &fields() { return mFields; }
51 
52  int fieldIndex( const QString &name );
53 
54  QString path() const { return mPath; }
55 
56  QStringList &geometryAttributes() { return mGeometryAttributes; }
57 
58  private:
59  /* Feature class name:
60  * - element name without NS or known prefix/suffix (_feature)
61  * - typeName attribute name */
62  QString mName;
63 
64  //QString mElementName;
65 
66  /* Dot separated path to element including the name */
67  QString mPath;
68 
69  /* Fields */
70  // Do not use QMap to keep original fields order. If it gets to performance,
71  // add a field index map
72  QList<QgsField> mFields;
73 
74  /* Geometry attribute */
75  QStringList mGeometryAttributes;
76 };
77 
82 class CORE_EXPORT QgsGmlSchema : public QObject
83 {
84  Q_OBJECT
85  public:
86  QgsGmlSchema();
87 
89  bool parseXSD( const QByteArray &xml );
90 
97  bool guessSchema( const QByteArray &data );
98 
100  QStringList typeNames() const;
101 
103  QList<QgsField> fields( const QString &typeName );
104 
106  QStringList geometryAttributes( const QString &typeName );
107 
109  QgsError error() const { return mError; }
110 
111  private:
112 
113  enum ParseMode
114  {
115  None,
116  BoundingBox,
117  FeatureMembers, // gml:featureMembers
118  FeatureMember, // gml:featureMember
119  Feature, // feature element containing attrs and geo (inside gml:featureMember)
120  Attribute,
121  Geometry
122  };
123 
125  void startElement( const XML_Char *el, const XML_Char **attr );
126  void endElement( const XML_Char *el );
127  void characters( const XML_Char *chars, int len );
128  static void start( void *data, const XML_Char *el, const XML_Char **attr )
129  {
130  static_cast<QgsGmlSchema *>( data )->startElement( el, attr );
131  }
132  static void end( void *data, const XML_Char *el )
133  {
134  static_cast<QgsGmlSchema *>( data )->endElement( el );
135  }
136  static void chars( void *data, const XML_Char *chars, int len )
137  {
138  static_cast<QgsGmlSchema *>( data )->characters( chars, len );
139  }
140  // Add attribute or reset its type according to value of current feature
141  void addAttribute( const QString &name, const QString &value );
142 
143  //helper routines
144 
148  QString readAttribute( const QString &attributeName, const XML_Char **attr ) const;
149 
151  QWidget *findMainWindow() const;
152 
154  QList<QDomElement> domElements( const QDomElement &element, const QString &path );
155 
157  QDomElement domElement( const QDomElement &element, const QString &path );
158 
160  QList<QDomElement> domElements( QList<QDomElement> &elements, const QString &attr, const QString &attrVal );
161 
163  QDomElement domElement( const QDomElement &element, const QString &path, const QString &attr, const QString &attrVal );
164 
166  QString stripNS( const QString &name );
167 
174  QString xsdComplexTypeGmlBaseType( const QDomElement &element, const QString &name );
175 
177  bool xsdFeatureClass( const QDomElement &element, const QString &typeName, QgsGmlFeatureClass &featureClass );
178 
179 
181  ParseMode modeStackTop() { return mParseModeStack.isEmpty() ? None : mParseModeStack.top(); }
182 
184  ParseMode modeStackPop() { return mParseModeStack.isEmpty() ? None : mParseModeStack.pop(); }
185 
187  //std::stack<ParseMode> mParseModeStack;
188  QStack<ParseMode> mParseModeStack;
190  QString mStringCash;
191  QgsFeature *mCurrentFeature = nullptr;
192  QString mCurrentFeatureId;
193  int mFeatureCount = 0;
194  QString mAttributeName;
196  QString mCoordinateSeparator;
198  QString mTupleSeparator;
199 
200  /* Schema information guessed/parsed from GML in getSchema() */
201 
203  int mLevel = 0;
204 
206  int mSkipLevel;
207 
209  QStringList mParsePathStack;
210 
211  QString mCurrentFeatureName;
212 
213  // List of know geometries (Point, Multipoint,...)
214  QStringList mGeometryTypes;
215 
216  /* Feature classes map with element paths as keys */
217  QMap<QString, QgsGmlFeatureClass> mFeatureClassMap;
218 
219  /* Error set if something failed */
220  QgsError mError;
221 };
222 
223 #endif
A rectangle specified with double values.
Definition: qgsrectangle.h:41
QString path() const
Definition: qgsgmlschema.h:54
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:55
QList< QgsField > & fields()
Definition: qgsgmlschema.h:50
const QString & typeName
Description of feature class in GML.
Definition: qgsgmlschema.h:40
QgsError is container for error messages (report).
Definition: qgserror.h:80
This class represents a coordinate reference system (CRS).
QgsError error() const
Gets error if parseXSD() or guessSchema() failed.
Definition: qgsgmlschema.h:109
QStringList & geometryAttributes()
Definition: qgsgmlschema.h:56