QGIS API Documentation  2.99.0-Master (8ec3eaf)
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 "qgserror.h"
21 #include "qgsfields.h"
22 #include <list>
23 #include <set>
24 #include <stack>
25 #include <QPair>
26 #include <QByteArray>
27 #include <QDomElement>
28 #include <QStringList>
29 #include <QStack>
30 
31 class QgsRectangle;
33 class QgsFeature;
34 
38 class CORE_EXPORT QgsGmlFeatureClass
39 {
40  public:
42  QgsGmlFeatureClass( const QString& name, const QString& path );
43 
44  QList<QgsField> & fields() { return mFields; }
45 
46  int fieldIndex( const QString & name );
47 
48  QString path() const { return mPath; }
49 
50  QStringList & geometryAttributes() { return mGeometryAttributes; }
51 
52  private:
53  /* Feature class name:
54  * - element name without NS or known prefix/suffix (_feature)
55  * - typeName attribute name */
56  QString mName;
57 
58  //QString mElementName;
59 
60  /* Dot separated path to element including the name */
61  QString mPath;
62 
63  /* Fields */
64  // Do not use QMap to keep original fields order. If it gets to performance,
65  // add a field index map
66  QList<QgsField> mFields;
67 
68  /* Geometry attribute */
69  QStringList mGeometryAttributes;
70 };
71 
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
A rectangle specified with double values.
Definition: qgsrectangle.h:35
QString path() const
Definition: qgsgmlschema.h:48
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:135
QList< QgsField > & fields()
Definition: qgsgmlschema.h:44
static QString stripNS(const QString &string)
Definition: qgsgml.cpp:330
Description of feature class in GML.
Definition: qgsgmlschema.h:38
QgsError is container for error messages (report).
Definition: qgserror.h:80
This class represents a coordinate reference system (CRS).
QgsError error() const
Get error if parseXSD() or guessSchema() failed.
Definition: qgsgmlschema.h:103
QStringList & geometryAttributes()
Definition: qgsgmlschema.h:50