QGIS API Documentation  2.3.0-Master
 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 
108  {
111  featureMembers, // gml:featureMembers
112  featureMember, // gml:featureMember
113  feature, // feature element containint attrs and geo (inside gml:featureMember)
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 
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;
184  QString mAttributeName;
189 
190  /* Schema information guessed/parsed from GML in getSchema() */
191 
193  int mLevel;
194 
197 
199  QStringList mParsePathStack;
200 
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 */
211 };
212 
213 #endif
A rectangle specified with double values.
Definition: qgsrectangle.h:35
int mSkipLevel
Skip all levels under this.
Definition: qgsgmlschema.h:196
QStringList mGeometryTypes
Definition: qgsgmlschema.h:204
QString mAttributeName
Definition: qgsgmlschema.h:184
QMap< QString, QgsGmlFeatureClass > mFeatureClassMap
Definition: qgsgmlschema.h:207
static void start(void *data, const XML_Char *el, const XML_Char **attr)
Definition: qgsgmlschema.h:122
QStringList mParsePathStack
Path to current level.
Definition: qgsgmlschema.h:199
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:114
QList< QgsField > mFields
Definition: qgsgmlschema.h:69
QList< QgsField > & fields()
Definition: qgsgmlschema.h:47
QStack< ParseMode > mParseModeStack
Keep track about the most important nested elements.
Definition: qgsgmlschema.h:178
ParseMode modeStackTop()
Get safely (if empty) top from mode stack.
Definition: qgsgmlschema.h:171
static void chars(void *data, const XML_Char *chars, int len)
Definition: qgsgmlschema.h:130
QString path() const
Definition: qgsgmlschema.h:51
QString mCurrentFeatureName
Definition: qgsgmlschema.h:201
QString mCoordinateSeparator
Coordinate separator for coordinate strings.
Definition: qgsgmlschema.h:186
static void end(void *data, const XML_Char *el)
Definition: qgsgmlschema.h:126
QString mTupleSeparator
Tuple separator for coordinate strings.
Definition: qgsgmlschema.h:188
int mLevel
Depth level, root element is 0.
Definition: qgsgmlschema.h:193
QgsError is container for error messages (report).
Definition: qgserror.h:77
Class for storing a coordinate reference system (CRS)
QgsError error() const
Get error if parseXSD() or guessSchema() failed.
Definition: qgsgmlschema.h:103
QgsError mError
Definition: qgsgmlschema.h:210
ParseMode modeStackPop()
Safely (if empty) pop from mode stack.
Definition: qgsgmlschema.h:174
QStringList & geometryAttributes()
Definition: qgsgmlschema.h:53
QStringList mGeometryAttributes
Definition: qgsgmlschema.h:72
QString mStringCash
This contains the character data if an important element has been encountered.
Definition: qgsgmlschema.h:180
QgsFeature * mCurrentFeature
Definition: qgsgmlschema.h:181
QString mCurrentFeatureId
Definition: qgsgmlschema.h:182