QGIS API Documentation  2.6.0-Brighton
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgsosmdatabase.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsosmdatabase.h
3  --------------------------------------
4  Date : January 2013
5  Copyright : (C) 2013 by Martin Dobias
6  Email : wonder dot sk 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 
16 #ifndef OSMDATABASE_H
17 #define OSMDATABASE_H
18 
19 #include <QString>
20 #include <QStringList>
21 
22 #include "qgsosmbase.h"
23 
24 #include "qgsgeometry.h"
25 
26 class QgsOSMNodeIterator;
28 
29 typedef QPair<QString, int> QgsOSMTagCountPair;
30 
46 class ANALYSIS_EXPORT QgsOSMDatabase
47 {
48  public:
49  explicit QgsOSMDatabase( const QString& dbFileName = QString() );
50  ~QgsOSMDatabase();
51 
52  void setFileName( const QString& dbFileName ) { mDbFileName = dbFileName; }
53  QString filename() const { return mDbFileName; }
54  bool isOpen() const;
55 
56  bool open();
57  bool close();
58 
59  QString errorString() const { return mError; }
60 
61  // data access
62 
63  int countNodes() const;
64  int countWays() const;
65 
66  QgsOSMNodeIterator listNodes() const;
67  QgsOSMWayIterator listWays() const;
68 
69  QgsOSMNode node( QgsOSMId id ) const;
70  QgsOSMWay way( QgsOSMId id ) const;
71  //OSMRelation relation( OSMId id ) const;
72 
73  QgsOSMTags tags( bool way, QgsOSMId id ) const;
74 
75  QList<QgsOSMTagCountPair> usedTags( bool ways ) const;
76 
77  QgsPolyline wayPoints( QgsOSMId id ) const;
78 
79  // export to spatialite
80 
81  enum ExportType { Point, Polyline, Polygon };
82  bool exportSpatiaLite( ExportType type, const QString& tableName, const QStringList& tagKeys = QStringList() );
83 
84  protected:
85  bool prepareStatements();
86  int runCountStatement( const char* sql ) const;
87  void deleteStatement( sqlite3_stmt*& stmt );
88 
89  void exportSpatiaLiteNodes( const QString& tableName, const QStringList& tagKeys );
90  void exportSpatiaLiteWays( bool closed, const QString& tableName, const QStringList& tagKeys );
91  bool createSpatialTable( const QString& tableName, const QString& geometryType, const QStringList& tagKeys );
92  bool createSpatialIndex( const QString& tableName );
93 
94  QString quotedIdentifier( QString id );
95  QString quotedValue( QString value );
96 
97  private:
99  QString mDbFileName;
100 
101  QString mError;
102 
104  sqlite3* mDatabase;
105 
106  sqlite3_stmt* mStmtNode;
107  sqlite3_stmt* mStmtNodeTags;
108  sqlite3_stmt* mStmtWay;
109  sqlite3_stmt* mStmtWayNode;
110  sqlite3_stmt* mStmtWayNodePoints;
111  sqlite3_stmt* mStmtWayTags;
112 };
113 
114 
116 class ANALYSIS_EXPORT QgsOSMNodeIterator
117 {
118  public:
120 
121  QgsOSMNode next();
122  void close();
123 
124  protected:
125  QgsOSMNodeIterator( sqlite3* handle );
126 
127  sqlite3_stmt* mStmt;
128 
129  friend class QgsOSMDatabase;
130 };
131 
132 
133 
135 class ANALYSIS_EXPORT QgsOSMWayIterator
136 {
137  public:
139 
140  QgsOSMWay next();
141  void close();
142 
143  protected:
144  QgsOSMWayIterator( sqlite3* handle );
145 
146  sqlite3_stmt* mStmt;
147 
148  friend class QgsOSMDatabase;
149 };
150 
151 
152 
153 #endif // OSMDATABASE_H