QGIS API Documentation  2.99.0-Master (716ff6c)
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.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 
39 class CORE_EXPORT QgsGmlFeatureClass
40 {
41  public:
43  QgsGmlFeatureClass( const QString &name, const QString &path );
44 
45  QList<QgsField> &fields() { return mFields; }
46 
47  int fieldIndex( const QString &name );
48 
49  QString path() const { return mPath; }
50 
51  QStringList &geometryAttributes() { return mGeometryAttributes; }
52 
53  private:
54  /* Feature class name:
55  * - element name without NS or known prefix/suffix (_feature)
56  * - typeName attribute name */
57  QString mName;
58 
59  //QString mElementName;
60 
61  /* Dot separated path to element including the name */
62  QString mPath;
63 
64  /* Fields */
65  // Do not use QMap to keep original fields order. If it gets to performance,
66  // add a field index map
67  QList<QgsField> mFields;
68 
69  /* Geometry attribute */
70  QStringList mGeometryAttributes;
71 };
72 
76 class CORE_EXPORT QgsGmlSchema : public QObject
77 {
78  Q_OBJECT
79  public:
80  QgsGmlSchema();
81 
83  bool parseXSD( const QByteArray &xml );
84 
90  bool guessSchema( const QByteArray &data );
91 
93  QStringList typeNames() const;
94 
96  QList<QgsField> fields( const QString &typeName );
97 
99  QStringList geometryAttributes( const QString &typeName );
100 
102  QgsError error() const { return mError; }
103 
104  private:
105 
106  enum ParseMode
107  {
108  None,
109  BoundingBox,
110  FeatureMembers, // gml:featureMembers
111  FeatureMember, // gml:featureMember
112  Feature, // feature element containint attrs and geo (inside gml:featureMember)
113  Attribute,
114  Geometry
115  };
116 
118  void startElement( const XML_Char *el, const XML_Char **attr );
119  void endElement( const XML_Char *el );
120  void characters( const XML_Char *chars, int len );
121  static void start( void *data, const XML_Char *el, const XML_Char **attr )
122  {
123  static_cast<QgsGmlSchema *>( data )->startElement( el, attr );
124  }
125  static void end( void *data, const XML_Char *el )
126  {
127  static_cast<QgsGmlSchema *>( data )->endElement( el );
128  }
129  static void chars( void *data, const XML_Char *chars, int len )
130  {
131  static_cast<QgsGmlSchema *>( data )->characters( chars, len );
132  }
133  // Add attribute or reset its type according to value of current feature
134  void addAttribute( const QString &name, const QString &value );
135 
136  //helper routines
137 
140  QString readAttribute( const QString &attributeName, const XML_Char **attr ) const;
141 
143  QWidget *findMainWindow() const;
144 
146  QList<QDomElement> domElements( const QDomElement &element, const QString &path );
147 
149  QDomElement domElement( const QDomElement &element, const QString &path );
150 
152  QList<QDomElement> domElements( QList<QDomElement> &elements, const QString &attr, const QString &attrVal );
153 
155  QDomElement domElement( const QDomElement &element, const QString &path, const QString &attr, const QString &attrVal );
156 
158  QString stripNS( const QString &name );
159 
165  QString xsdComplexTypeGmlBaseType( const QDomElement &element, const QString &name );
166 
168  bool xsdFeatureClass( const QDomElement &element, const QString &typeName, QgsGmlFeatureClass &featureClass );
169 
170 
172  ParseMode modeStackTop() { return mParseModeStack.isEmpty() ? None : mParseModeStack.top(); }
173 
175  ParseMode modeStackPop() { return mParseModeStack.isEmpty() ? None : mParseModeStack.pop(); }
176 
178  //std::stack<ParseMode> mParseModeStack;
179  QStack<ParseMode> mParseModeStack;
181  QString mStringCash;
182  QgsFeature *mCurrentFeature = nullptr;
183  QString mCurrentFeatureId;
184  int mFeatureCount;
185  QString mAttributeName;
187  QString mCoordinateSeparator;
189  QString mTupleSeparator;
190 
191  /* Schema information guessed/parsed from GML in getSchema() */
192 
194  int mLevel;
195 
197  int mSkipLevel;
198 
200  QStringList mParsePathStack;
201 
202  QString mCurrentFeatureName;
203 
204  // List of know geometries (Point, Multipoint,...)
205  QStringList mGeometryTypes;
206 
207  /* Feature classes map with element paths as keys */
208  QMap<QString, QgsGmlFeatureClass> mFeatureClassMap;
209 
210  /* Error set if something failed */
211  QgsError mError;
212 };
213 
214 #endif
A rectangle specified with double values.
Definition: qgsrectangle.h:38
QString path() const
Definition: qgsgmlschema.h:49
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:61
QList< QgsField > & fields()
Definition: qgsgmlschema.h:45
static QString stripNS(const QString &string)
Definition: qgsgml.cpp:329
Description of feature class in GML.
Definition: qgsgmlschema.h:39
QgsError is container for error messages (report).
Definition: qgserror.h:82
This class represents a coordinate reference system (CRS).
QgsError error() const
Get error if parseXSD() or guessSchema() failed.
Definition: qgsgmlschema.h:102
QStringList & geometryAttributes()
Definition: qgsgmlschema.h:51