QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 
98  bool guessSchema( const QByteArray &data );
99 
101  QStringList typeNames() const;
102 
104  QList<QgsField> fields( const QString &typeName );
105 
107  QStringList geometryAttributes( const QString &typeName );
108 
110  QgsError error() const { return mError; }
111 
112  private:
113 
114  enum ParseMode
115  {
116  None,
117  BoundingBox,
118  FeatureMembers, // gml:featureMembers
119  FeatureMember, // gml:featureMember
120  Feature, // feature element containing attrs and geo (inside gml:featureMember)
121  Attribute,
122  Geometry
123  };
124 
126  void startElement( const XML_Char *el, const XML_Char **attr );
127  void endElement( const XML_Char *el );
128  void characters( const XML_Char *chars, int len );
129  static void start( void *data, const XML_Char *el, const XML_Char **attr )
130  {
131  static_cast<QgsGmlSchema *>( data )->startElement( el, attr );
132  }
133  static void end( void *data, const XML_Char *el )
134  {
135  static_cast<QgsGmlSchema *>( data )->endElement( el );
136  }
137  static void chars( void *data, const XML_Char *chars, int len )
138  {
139  static_cast<QgsGmlSchema *>( data )->characters( chars, len );
140  }
141  // Add attribute or reset its type according to value of current feature
142  void addAttribute( const QString &name, const QString &value );
143 
144  //helper routines
145 
149  QString readAttribute( const QString &attributeName, const XML_Char **attr ) const;
150 
152  QWidget *findMainWindow() const;
153 
155  QList<QDomElement> domElements( const QDomElement &element, const QString &path );
156 
158  QDomElement domElement( const QDomElement &element, const QString &path );
159 
161  QList<QDomElement> domElements( QList<QDomElement> &elements, const QString &attr, const QString &attrVal );
162 
164  QDomElement domElement( const QDomElement &element, const QString &path, const QString &attr, const QString &attrVal );
165 
167  QString stripNS( const QString &name );
168 
175  QString xsdComplexTypeGmlBaseType( const QDomElement &element, const QString &name );
176 
178  bool xsdFeatureClass( const QDomElement &element, const QString &typeName, QgsGmlFeatureClass &featureClass );
179 
180 
182  ParseMode modeStackTop() { return mParseModeStack.isEmpty() ? None : mParseModeStack.top(); }
183 
185  ParseMode modeStackPop() { return mParseModeStack.isEmpty() ? None : mParseModeStack.pop(); }
186 
188  //std::stack<ParseMode> mParseModeStack;
189  QStack<ParseMode> mParseModeStack;
191  QString mStringCash;
192  QgsFeature *mCurrentFeature = nullptr;
193  QString mCurrentFeatureId;
194  int mFeatureCount = 0;
195  QString mAttributeName;
197  QString mCoordinateSeparator;
199  QString mTupleSeparator;
200 
201  /* Schema information guessed/parsed from GML in getSchema() */
202 
204  int mLevel = 0;
205 
207  int mSkipLevel;
208 
210  QStringList mParsePathStack;
211 
212  QString mCurrentFeatureName;
213 
214  // List of know geometries (Point, Multipoint,...)
215  QStringList mGeometryTypes;
216 
217  /* Feature classes map with element paths as keys */
218  QMap<QString, QgsGmlFeatureClass> mFeatureClassMap;
219 
220  /* Error set if something failed */
221  QgsError mError;
222 };
223 
224 #endif
qgsfields.h
QgsGmlFeatureClass::geometryAttributes
QStringList & geometryAttributes()
Definition: qgsgmlschema.h:56
QgsGmlSchema::error
QgsError error() const
Gets error if parseXSD() or guessSchema() failed.
Definition: qgsgmlschema.h:110
QgsError
Definition: qgserror.h:80
QgsGmlSchema
Definition: qgsgmlschema.h:82
QgsRectangle
Definition: qgsrectangle.h:41
qgserror.h
qgis_sip.h
QgsGmlFeatureClass::fields
QList< QgsField > & fields()
Definition: qgsgmlschema.h:50
QgsGmlFeatureClass::path
QString path() const
Definition: qgsgmlschema.h:54
typeName
const QString & typeName
Definition: qgswfsgetfeature.cpp:109
QgsCoordinateReferenceSystem
Definition: qgscoordinatereferencesystem.h:206
QgsGmlFeatureClass
Definition: qgsgmlschema.h:40
QgsFeature
Definition: qgsfeature.h:55