Quantum GIS API Documentation  master-693a1fe
src/core/qgsgmlschema.h
Go to the documentation of this file.
00001 /***************************************************************************
00002      qgsgmlschema.h
00003      --------------------------------------
00004     Date                 : Sun Sep 16 12:19:55 AKDT 2007
00005     Copyright            : (C) 2007 by Gary E. Sherman
00006     Email                : sherman at mrcc dot com
00007  ***************************************************************************
00008  *                                                                         *
00009  *   This program is free software; you can redistribute it and/or modify  *
00010  *   it under the terms of the GNU General Public License as published by  *
00011  *   the Free Software Foundation; either version 2 of the License, or     *
00012  *   (at your option) any later version.                                   *
00013  *                                                                         *
00014  ***************************************************************************/
00015 #ifndef QGSGMLSCHEMA_H
00016 #define QGSGMLSCHEMA_H
00017 
00018 #include <expat.h>
00019 #include "qgis.h"
00020 #include "qgsapplication.h"
00021 #include "qgsdataprovider.h"
00022 #include "qgsfeature.h"
00023 #include "qgsfield.h"
00024 #include "qgslogger.h"
00025 #include "qgspoint.h"
00026 #include <list>
00027 #include <set>
00028 #include <stack>
00029 #include <QPair>
00030 #include <QByteArray>
00031 #include <QDomElement>
00032 #include <QStringList>
00033 #include <QStack>
00034 class QgsRectangle;
00035 class QgsCoordinateReferenceSystem;
00036 
00037 /* Description of feature class in GML */
00038 class CORE_EXPORT QgsGmlFeatureClass
00039 {
00040   public:
00041     QgsGmlFeatureClass( );
00042     QgsGmlFeatureClass( QString name, QString path );
00043 
00044     ~QgsGmlFeatureClass();
00045 
00046     QList<QgsField> & fields() { return  mFields; }
00047 
00048     int fieldIndex( const QString & name );
00049 
00050     QString path() const { return mPath; }
00051 
00052     QStringList & geometryAttributes() { return mGeometryAttributes; }
00053 
00054   private:
00055     /* Feature class name:
00056      *  - element name without NS or known prefix/suffix (_feature)
00057      *  - typeName attribute name */
00058     QString mName;
00059 
00060     //QString mElementName;
00061 
00062     /* Dot separated path to element including the name */
00063     QString mPath;
00064 
00065     /* Fields */
00066     // Do not use QMap to keep original fields order. If it gets to performance,
00067     // add a field index map
00068     QList<QgsField> mFields;
00069 
00070     /* Geometry attribute */
00071     QStringList mGeometryAttributes;
00072 };
00073 
00074 class CORE_EXPORT QgsGmlSchema: public QObject
00075 {
00076     Q_OBJECT
00077   public:
00078     QgsGmlSchema();
00079 
00080     ~QgsGmlSchema();
00081 
00083     bool parseXSD( const QByteArray &xml );
00084 
00089     bool guessSchema( const QByteArray &data );
00090 
00092     QStringList typeNames() const;
00093 
00095     //QMap<int, QgsField> fields();
00096 
00098     QList<QgsField> fields( const QString & typeName );
00099 
00101     QStringList geometryAttributes( const QString & typeName );
00102 
00103   private:
00104 
00105     enum ParseMode
00106     {
00107       none,
00108       boundingBox,
00109       featureMember, // gml:featureMember
00110       feature,  // feature element containint attrs and geo (inside gml:featureMember)
00111       attribute,
00112       geometry
00113     };
00114 
00116     void startElement( const XML_Char* el, const XML_Char** attr );
00117     void endElement( const XML_Char* el );
00118     void characters( const XML_Char* chars, int len );
00119     static void start( void* data, const XML_Char* el, const XML_Char** attr )
00120     {
00121       static_cast<QgsGmlSchema*>( data )->startElement( el, attr );
00122     }
00123     static void end( void* data, const XML_Char* el )
00124     {
00125       static_cast<QgsGmlSchema*>( data )->endElement( el );
00126     }
00127     static void chars( void* data, const XML_Char* chars, int len )
00128     {
00129       static_cast<QgsGmlSchema*>( data )->characters( chars, len );
00130     }
00131 
00132     //helper routines
00133 
00136     QString readAttribute( const QString& attributeName, const XML_Char** attr ) const;
00137 
00139     QWidget* findMainWindow() const;
00140 
00142     QList<QDomElement> domElements( const QDomElement &element, const QString & path );
00143 
00145     QDomElement domElement( const QDomElement &element, const QString & path );
00146 
00148     QList<QDomElement> domElements( QList<QDomElement> &elements, const QString & attr, const QString & attrVal );
00149 
00151     QDomElement domElement( const QDomElement &element, const QString & path, const QString & attr, const QString & attrVal );
00152 
00154     QString stripNS( const QString & name );
00155 
00161     QString xsdComplexTypeGmlBaseType( const QDomElement &element, const QString & name );
00162 
00164     bool xsdFeatureClass( const QDomElement &element, const QString & typeName, QgsGmlFeatureClass & featureClass );
00165 
00166 
00168     ParseMode modeStackTop() { return mParseModeStack.isEmpty() ? none : mParseModeStack.top(); }
00169 
00171     ParseMode modeStackPop() { return mParseModeStack.isEmpty() ? none : mParseModeStack.pop(); }
00172 
00174     //std::stack<ParseMode> mParseModeStack;
00175     QStack<ParseMode> mParseModeStack;
00177     QString mStringCash;
00178     QgsFeature* mCurrentFeature;
00179     QString mCurrentFeatureId;
00180     int mFeatureCount;
00181     QString mAttributeName;
00183     QString mCoordinateSeparator;
00185     QString mTupleSeparator;
00186 
00187     /* Schema information guessed/parsed from GML in getSchema() */
00188 
00190     int mLevel;
00191 
00193     int mSkipLevel;
00194 
00196     QStringList mParsePathStack;
00197 
00198     QString mCurrentFeatureName;
00199 
00200     // List of know geometries (Point, Multipoint,...)
00201     QStringList mGeometryTypes;
00202 
00203     /* Feature classes map with element paths as keys */
00204     QMap<QString, QgsGmlFeatureClass> mFeatureClassMap;
00205 };
00206 
00207 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines