QGIS API Documentation  2.17.0-Master (06698cd)
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 <expat.h>
19 #include "qgis.h"
20 #include "qgsapplication.h"
21 #include "qgsdataprovider.h"
22 #include "qgserror.h"
23 #include "qgsfeature.h"
24 #include "qgsfield.h"
25 #include "qgslogger.h"
26 #include "qgspoint.h"
27 #include <list>
28 #include <set>
29 #include <stack>
30 #include <QPair>
31 #include <QByteArray>
32 #include <QDomElement>
33 #include <QStringList>
34 #include <QStack>
35 class QgsRectangle;
37 
41 class CORE_EXPORT QgsGmlFeatureClass
42 {
43  public:
45  QgsGmlFeatureClass( const QString& name, const QString& path );
46 
47  QList<QgsField> & fields() { return mFields; }
48 
49  int fieldIndex( const QString & name );
50 
51  QString path() const { return mPath; }
52 
53  QStringList & geometryAttributes() { return mGeometryAttributes; }
54 
55  private:
56  /* Feature class name:
57  * - element name without NS or known prefix/suffix (_feature)
58  * - typeName attribute name */
59  QString mName;
60 
61  //QString mElementName;
62 
63  /* Dot separated path to element including the name */
64  QString mPath;
65 
66  /* Fields */
67  // Do not use QMap to keep original fields order. If it gets to performance,
68  // add a field index map
69  QList<QgsField> mFields;
70 
71  /* Geometry attribute */
72  QStringList mGeometryAttributes;
73 };
74 
78 class CORE_EXPORT QgsGmlSchema : public QObject
79 {
80  Q_OBJECT
81  public:
82  QgsGmlSchema();
83 
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;
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:35
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:187
QList< QgsField > & fields()
Definition: qgsgmlschema.h:47
static QString stripNS(const QString &string)
Definition: qgsgml.cpp:317
QString path() const
Definition: qgsgmlschema.h:51
Description of feature class in GML.
Definition: qgsgmlschema.h:41
QgsError is container for error messages (report).
Definition: qgserror.h:80
Class for storing a coordinate reference system (CRS)
QgsError error() const
Get error if parseXSD() or guessSchema() failed.
Definition: qgsgmlschema.h:106
QStringList & geometryAttributes()
Definition: qgsgmlschema.h:53