QGIS API Documentation  2.99.0-Master (9caa722)
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:
42 
46  QgsGmlFeatureClass() = default;
47  QgsGmlFeatureClass( const QString &name, const QString &path );
48 
49  QList<QgsField> &fields() { return mFields; }
50 
51  int fieldIndex( const QString &name );
52 
53  QString path() const { return mPath; }
54 
55  QStringList &geometryAttributes() { return mGeometryAttributes; }
56 
57  private:
58  /* Feature class name:
59  * - element name without NS or known prefix/suffix (_feature)
60  * - typeName attribute name */
61  QString mName;
62 
63  //QString mElementName;
64 
65  /* Dot separated path to element including the name */
66  QString mPath;
67 
68  /* Fields */
69  // Do not use QMap to keep original fields order. If it gets to performance,
70  // add a field index map
71  QList<QgsField> mFields;
72 
73  /* Geometry attribute */
74  QStringList mGeometryAttributes;
75 };
76 
80 class CORE_EXPORT QgsGmlSchema : public QObject
81 {
82  Q_OBJECT
83  public:
84  QgsGmlSchema();
85 
87  bool parseXSD( const QByteArray &xml );
88 
94  bool guessSchema( const QByteArray &data );
95 
97  QStringList typeNames() const;
98 
100  QList<QgsField> fields( const QString &typeName );
101 
103  QStringList geometryAttributes( const QString &typeName );
104 
106  QgsError error() const { return mError; }
107 
108  private:
109 
110  enum ParseMode
111  {
112  None,
113  BoundingBox,
114  FeatureMembers, // gml:featureMembers
115  FeatureMember, // gml:featureMember
116  Feature, // feature element containint attrs and geo (inside gml:featureMember)
117  Attribute,
118  Geometry
119  };
120 
122  void startElement( const XML_Char *el, const XML_Char **attr );
123  void endElement( const XML_Char *el );
124  void characters( const XML_Char *chars, int len );
125  static void start( void *data, const XML_Char *el, const XML_Char **attr )
126  {
127  static_cast<QgsGmlSchema *>( data )->startElement( el, attr );
128  }
129  static void end( void *data, const XML_Char *el )
130  {
131  static_cast<QgsGmlSchema *>( data )->endElement( el );
132  }
133  static void chars( void *data, const XML_Char *chars, int len )
134  {
135  static_cast<QgsGmlSchema *>( data )->characters( chars, len );
136  }
137  // Add attribute or reset its type according to value of current feature
138  void addAttribute( const QString &name, const QString &value );
139 
140  //helper routines
141 
144  QString readAttribute( const QString &attributeName, const XML_Char **attr ) const;
145 
147  QWidget *findMainWindow() const;
148 
150  QList<QDomElement> domElements( const QDomElement &element, const QString &path );
151 
153  QDomElement domElement( const QDomElement &element, const QString &path );
154 
156  QList<QDomElement> domElements( QList<QDomElement> &elements, const QString &attr, const QString &attrVal );
157 
159  QDomElement domElement( const QDomElement &element, const QString &path, const QString &attr, const QString &attrVal );
160 
162  QString stripNS( const QString &name );
163 
169  QString xsdComplexTypeGmlBaseType( const QDomElement &element, const QString &name );
170 
172  bool xsdFeatureClass( const QDomElement &element, const QString &typeName, QgsGmlFeatureClass &featureClass );
173 
174 
176  ParseMode modeStackTop() { return mParseModeStack.isEmpty() ? None : mParseModeStack.top(); }
177 
179  ParseMode modeStackPop() { return mParseModeStack.isEmpty() ? None : mParseModeStack.pop(); }
180 
182  //std::stack<ParseMode> mParseModeStack;
183  QStack<ParseMode> mParseModeStack;
185  QString mStringCash;
186  QgsFeature *mCurrentFeature = nullptr;
187  QString mCurrentFeatureId;
188  int mFeatureCount;
189  QString mAttributeName;
191  QString mCoordinateSeparator;
193  QString mTupleSeparator;
194 
195  /* Schema information guessed/parsed from GML in getSchema() */
196 
198  int mLevel;
199 
201  int mSkipLevel;
202 
204  QStringList mParsePathStack;
205 
206  QString mCurrentFeatureName;
207 
208  // List of know geometries (Point, Multipoint,...)
209  QStringList mGeometryTypes;
210 
211  /* Feature classes map with element paths as keys */
212  QMap<QString, QgsGmlFeatureClass> mFeatureClassMap;
213 
214  /* Error set if something failed */
215  QgsError mError;
216 };
217 
218 #endif
A rectangle specified with double values.
Definition: qgsrectangle.h:38
QString path() const
Definition: qgsgmlschema.h:53
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:49
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:106
QStringList & geometryAttributes()
Definition: qgsgmlschema.h:55