QGIS API Documentation  2.8.2-Wien
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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( QString name, QString path );
44 
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 
75 class CORE_EXPORT QgsGmlSchema : public QObject
76 {
77  Q_OBJECT
78  public:
79  QgsGmlSchema();
80 
81  ~QgsGmlSchema();
82 
84  bool parseXSD( const QByteArray &xml );
85 
91  bool guessSchema( const QByteArray &data );
92 
94  QStringList typeNames() const;
95 
97  QList<QgsField> fields( const QString & typeName );
98 
100  QStringList geometryAttributes( const QString & typeName );
101 
103  QgsError error() const { return mError; }
104 
105  private:
106 
107  enum ParseMode
108  {
109  none,
110  boundingBox,
111  featureMembers, // gml:featureMembers
112  featureMember, // gml:featureMember
113  feature, // feature element containint attrs and geo (inside gml:featureMember)
114  attribute,
115  geometry
116  };
117 
119  void startElement( const XML_Char* el, const XML_Char** attr );
120  void endElement( const XML_Char* el );
121  void characters( const XML_Char* chars, int len );
122  static void start( void* data, const XML_Char* el, const XML_Char** attr )
123  {
124  static_cast<QgsGmlSchema*>( data )->startElement( el, attr );
125  }
126  static void end( void* data, const XML_Char* el )
127  {
128  static_cast<QgsGmlSchema*>( data )->endElement( el );
129  }
130  static void chars( void* data, const XML_Char* chars, int len )
131  {
132  static_cast<QgsGmlSchema*>( data )->characters( chars, len );
133  }
134  // Add attribute or reset its type according to value of current feature
135  void addAttribute( const QString& name, const QString& value );
136 
137  //helper routines
138 
141  QString readAttribute( const QString& attributeName, const XML_Char** attr ) const;
142 
144  QWidget* findMainWindow() const;
145 
147  QList<QDomElement> domElements( const QDomElement &element, const QString & path );
148 
150  QDomElement domElement( const QDomElement &element, const QString & path );
151 
153  QList<QDomElement> domElements( QList<QDomElement> &elements, const QString & attr, const QString & attrVal );
154 
156  QDomElement domElement( const QDomElement &element, const QString & path, const QString & attr, const QString & attrVal );
157 
159  QString stripNS( const QString & name );
160 
166  QString xsdComplexTypeGmlBaseType( const QDomElement &element, const QString & name );
167 
169  bool xsdFeatureClass( const QDomElement &element, const QString & typeName, QgsGmlFeatureClass & featureClass );
170 
171 
173  ParseMode modeStackTop() { return mParseModeStack.isEmpty() ? none : mParseModeStack.top(); }
174 
176  ParseMode modeStackPop() { return mParseModeStack.isEmpty() ? none : mParseModeStack.pop(); }
177 
179  //std::stack<ParseMode> mParseModeStack;
180  QStack<ParseMode> mParseModeStack;
182  QString mStringCash;
183  QgsFeature* mCurrentFeature;
184  QString mCurrentFeatureId;
185  int mFeatureCount;
186  QString mAttributeName;
188  QString mCoordinateSeparator;
190  QString mTupleSeparator;
191 
192  /* Schema information guessed/parsed from GML in getSchema() */
193 
195  int mLevel;
196 
198  int mSkipLevel;
199 
201  QStringList mParsePathStack;
202 
203  QString mCurrentFeatureName;
204 
205  // List of know geometries (Point, Multipoint,...)
206  QStringList mGeometryTypes;
207 
208  /* Feature classes map with element paths as keys */
209  QMap<QString, QgsGmlFeatureClass> mFeatureClassMap;
210 
211  /* Error set if something failed */
212  QgsError mError;
213 };
214 
215 #endif