QGIS API Documentation  2.2.0-Valmiera
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgsgml.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsgml.h
3  ---------------------
4  begin : February 2013
5  copyright : (C) 2013 by Radim Blazek
6  email : radim dot blazek at gmail 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 QGSGML_H
16 #define QGSGML_H
17 
18 #include <expat.h>
19 #include "qgis.h"
20 #include "qgsapplication.h"
22 #include "qgsdataprovider.h"
23 #include "qgsfeature.h"
24 #include "qgsfield.h"
25 #include "qgslogger.h"
26 #include "qgspoint.h"
27 #include "qgsrectangle.h"
28 
29 #include <QPair>
30 #include <QByteArray>
31 #include <QDomElement>
32 #include <QStringList>
33 #include <QStack>
34 
35 class QgsRectangle;
36 
41 class CORE_EXPORT QgsGml : public QObject
42 {
43  Q_OBJECT
44  public:
45  QgsGml(
46  const QString& typeName,
47  const QString& geometryAttribute,
48  const QgsFields & fields );
49 
50  ~QgsGml();
51 
59  int getFeatures( const QString& uri, QGis::WkbType* wkbType, QgsRectangle* extent = 0 );
60 
64  int getFeatures( const QByteArray &data, QGis::WkbType* wkbType, QgsRectangle* extent = 0 );
65 
67  QMap<QgsFeatureId, QgsFeature* > featuresMap() const { return mFeatures; }
68 
70  QMap<QgsFeatureId, QString > idsMap() const { return mIdMap; }
71 
74  QgsCoordinateReferenceSystem crs() const;
75 
76  private slots:
77 
78  void setFinished();
79 
81  void handleProgressEvent( qint64 progress, qint64 totalSteps );
82 
83  signals:
84  void dataReadProgress( int progress );
85  void totalStepsUpdate( int totalSteps );
86  //also emit signal with progress and totalSteps together (this is better for the status message)
87  void dataProgressAndSteps( int progress, int totalSteps );
88 
89  private:
90 
91  enum ParseMode
92  {
95  feature, // feature element containing attrs and geo (inside gml:featureMember)
102  multiPolygon
103  };
104 
106  void startElement( const XML_Char* el, const XML_Char** attr );
107  void endElement( const XML_Char* el );
108  void characters( const XML_Char* chars, int len );
109  static void start( void* data, const XML_Char* el, const XML_Char** attr )
110  {
111  static_cast<QgsGml*>( data )->startElement( el, attr );
112  }
113  static void end( void* data, const XML_Char* el )
114  {
115  static_cast<QgsGml*>( data )->endElement( el );
116  }
117  static void chars( void* data, const XML_Char* chars, int len )
118  {
119  static_cast<QgsGml*>( data )->characters( chars, len );
120  }
121 
122  //helper routines
123 
129  int readEpsgFromAttribute( int& epsgNr, const XML_Char** attr ) const;
135  QString readAttribute( const QString& attributeName, const XML_Char** attr ) const;
138  int createBBoxFromCoordinateString( QgsRectangle &bb, const QString& coordString ) const;
144  int pointsFromCoordinateString( QList<QgsPoint>& points, const QString& coordString ) const;
145 
152  int pointsFromPosListString( QList<QgsPoint>& points, const QString& coordString, int dimension ) const;
153 
154  int pointsFromString( QList<QgsPoint>& points, const QString& coordString ) const;
155  int getPointWKB( unsigned char** wkb, int* size, const QgsPoint& ) const;
156  int getLineWKB( unsigned char** wkb, int* size, const QList<QgsPoint>& lineCoordinates ) const;
157  int getRingWKB( unsigned char** wkb, int* size, const QList<QgsPoint>& ringCoordinates ) const;
163  int createMultiLineFromFragments();
164  int createMultiPointFromFragments();
165  int createPolygonFromFragments();
166  int createMultiPolygonFromFragments();
168  int totalWKBFragmentSize() const;
169 
171  QWidget* findMainWindow() const;
177  void calculateExtentFromFeatures();
178 
180  ParseMode modeStackTop() { return mParseModeStack.isEmpty() ? none : mParseModeStack.top(); }
181 
183  ParseMode modeStackPop() { return mParseModeStack.isEmpty() ? none : mParseModeStack.pop(); }
184 
185  QString mTypeName;
186  QString mUri;
187  //results are members such that handler routines are able to manipulate them
191  //QMap<QgsFeatureId, QgsFeature* > &mFeatures;
192  QMap<QgsFeatureId, QgsFeature* > mFeatures;
193  //QMap<QString, QMap<QgsFeatureId, QgsFeature* > > mFeatures;
194 
196  //QMap<QgsFeatureId, QString > &mIdMap;
197  QMap<QgsFeatureId, QString > mIdMap;
198  //QMap<QString, QMap<QgsFeatureId, QString > > mIdMap;
201  //const QMap<QString, QPair<int, QgsField> > &mThematicAttributes;
202  QMap<QString, QPair<int, QgsField> > mThematicAttributes;
205  bool mFinished;
207  QStack<ParseMode> mParseModeStack;
209  QString mStringCash;
211  QVector<QVariant> mCurrentAttributes; //attributes of current feature
215  unsigned char* mCurrentWKB;
223  QList< QList<unsigned char*> > mCurrentWKBFragments;
225  QList< QList<int> > mCurrentWKBFragmentSizes;
226  QString mAttributeName;
237  int mEpsg;
238 };
239 
240 #endif