QGIS API Documentation  2.15.0-Master (af20121)
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 
38 /* Description of feature class in GML */
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 
73 class CORE_EXPORT QgsGmlSchema : public QObject
74 {
75  Q_OBJECT
76  public:
77  QgsGmlSchema();
78 
79  ~QgsGmlSchema();
80 
82  bool parseXSD( const QByteArray &xml );
83 
89  bool guessSchema( const QByteArray &data );
90 
92  QStringList typeNames() const;
93 
95  QList<QgsField> fields( const QString & typeName );
96 
98  QStringList geometryAttributes( const QString & typeName );
99 
101  QgsError error() const { return mError; }
102 
103  private:
104 
105  enum ParseMode
106  {
107  none,
108  boundingBox,
109  featureMembers, // gml:featureMembers
110  featureMember, // gml:featureMember
111  feature, // feature element containint attrs and geo (inside gml:featureMember)
112  attribute,
113  geometry
114  };
115 
117  void startElement( const XML_Char* el, const XML_Char** attr );
118  void endElement( const XML_Char* el );
119  void characters( const XML_Char* chars, int len );
120  static void start( void* data, const XML_Char* el, const XML_Char** attr )
121  {
122  static_cast<QgsGmlSchema*>( data )->startElement( el, attr );
123  }
124  static void end( void* data, const XML_Char* el )
125  {
126  static_cast<QgsGmlSchema*>( data )->endElement( el );
127  }
128  static void chars( void* data, const XML_Char* chars, int len )
129  {
130  static_cast<QgsGmlSchema*>( data )->characters( chars, len );
131  }
132  // Add attribute or reset its type according to value of current feature
133  void addAttribute( const QString& name, const QString& value );
134 
135  //helper routines
136 
139  QString readAttribute( const QString& attributeName, const XML_Char** attr ) const;
140 
142  QWidget* findMainWindow() const;
143 
145  QList<QDomElement> domElements( const QDomElement &element, const QString & path );
146 
148  QDomElement domElement( const QDomElement &element, const QString & path );
149 
151  QList<QDomElement> domElements( QList<QDomElement> &elements, const QString & attr, const QString & attrVal );
152 
154  QDomElement domElement( const QDomElement &element, const QString & path, const QString & attr, const QString & attrVal );
155 
157  QString stripNS( const QString & name );
158 
164  QString xsdComplexTypeGmlBaseType( const QDomElement &element, const QString & name );
165 
167  bool xsdFeatureClass( const QDomElement &element, const QString & typeName, QgsGmlFeatureClass & featureClass );
168 
169 
171  ParseMode modeStackTop() { return mParseModeStack.isEmpty() ? none : mParseModeStack.top(); }
172 
174  ParseMode modeStackPop() { return mParseModeStack.isEmpty() ? none : mParseModeStack.pop(); }
175 
177  //std::stack<ParseMode> mParseModeStack;
178  QStack<ParseMode> mParseModeStack;
180  QString mStringCash;
181  QgsFeature* mCurrentFeature;
182  QString mCurrentFeatureId;
183  int mFeatureCount;
184  QString mAttributeName;
186  QString mCoordinateSeparator;
188  QString mTupleSeparator;
189 
190  /* Schema information guessed/parsed from GML in getSchema() */
191 
193  int mLevel;
194 
196  int mSkipLevel;
197 
199  QStringList mParsePathStack;
200 
201  QString mCurrentFeatureName;
202 
203  // List of know geometries (Point, Multipoint,...)
204  QStringList mGeometryTypes;
205 
206  /* Feature classes map with element paths as keys */
207  QMap<QString, QgsGmlFeatureClass> mFeatureClassMap;
208 
209  /* Error set if something failed */
210  QgsError mError;
211 };
212 
213 #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:45
static QString stripNS(const QString &string)
Definition: qgsgml.cpp:313
QString path() const
Definition: qgsgmlschema.h:49
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:101
QStringList & geometryAttributes()
Definition: qgsgmlschema.h:51