23 : mDbFileName( dbFileName )
24 , mDatabase( nullptr )
25 , mStmtNode( nullptr )
26 , mStmtNodeTags( nullptr )
28 , mStmtWayNode( nullptr )
29 , mStmtWayNodePoints( nullptr )
30 , mStmtWayTags( nullptr )
42 return nullptr != mDatabase;
50 if ( res != SQLITE_OK )
52 mError =
QString(
"Failed to open database [%1]: %2" ).
arg( res ).
arg( mDbFileName );
71 sqlite3_finalize( stmt );
86 Q_ASSERT( !mStmtNode );
102 int res = sqlite3_prepare_v2( mDatabase, sql, -1, &stmt,
nullptr );
103 if ( res != SQLITE_OK )
106 res = sqlite3_step( stmt );
107 if ( res != SQLITE_ROW )
110 int count = sqlite3_column_int( stmt, 0 );
111 sqlite3_finalize( stmt );
141 sqlite3_bind_int64( mStmtNode, 1,
id );
143 if ( sqlite3_step( mStmtNode ) != SQLITE_ROW )
146 sqlite3_reset( mStmtNode );
150 double lon = sqlite3_column_double( mStmtNode, 0 );
151 double lat = sqlite3_column_double( mStmtNode, 1 );
155 sqlite3_reset( mStmtNode );
163 sqlite3_stmt* stmtTags = way ? mStmtWayTags : mStmtNodeTags;
165 sqlite3_bind_int64( stmtTags, 1,
id );
167 while ( sqlite3_step( stmtTags ) == SQLITE_ROW )
174 sqlite3_reset( stmtTags );
183 QString sql =
QString(
"SELECT k, count(k) FROM %1_tags GROUP BY k" ).
arg( ways ?
"ways" :
"nodes" );
186 if ( sqlite3_prepare_v2( mDatabase, sql.
toUtf8().
data(), -1, &stmt, nullptr ) != SQLITE_OK )
189 while ( sqlite3_step( stmt ) == SQLITE_ROW )
192 int count = sqlite3_column_int( stmt, 1 );
193 pairs.
append( qMakePair( k, count ) );
196 sqlite3_finalize( stmt );
208 sqlite3_bind_int64( mStmtWayNode, 1,
id );
212 while ( sqlite3_step( mStmtWayNode ) == SQLITE_ROW )
214 QgsOSMId nodeId = sqlite3_column_int64( mStmtWayNode, 0 );
218 sqlite3_reset( mStmtWayNode );
239 sqlite3_bind_int64( mStmtWayNodePoints, 1,
id );
241 while ( sqlite3_step( mStmtWayNodePoints ) == SQLITE_ROW )
243 if ( sqlite3_column_type( mStmtWayNodePoints, 0 ) == SQLITE_NULL )
245 double lon = sqlite3_column_double( mStmtWayNodePoints, 0 );
246 double lat = sqlite3_column_double( mStmtWayNodePoints, 1 );
250 sqlite3_reset( mStmtWayNodePoints );
260 "SELECT lon,lat FROM nodes WHERE id=?",
261 "SELECT k,v FROM nodes_tags WHERE id=?",
262 "SELECT id FROM ways WHERE id=?",
263 "SELECT node_id FROM ways_nodes WHERE way_id=? ORDER BY way_pos",
264 "SELECT n.lon, n.lat FROM ways_nodes wn LEFT JOIN nodes n ON wn.node_id = n.id WHERE wn.way_id=? ORDER BY wn.way_pos",
265 "SELECT k,v FROM ways_tags WHERE id=?" 267 sqlite3_stmt** sqlite[] =
276 int count =
sizeof( sql ) /
sizeof(
const char* );
277 Q_ASSERT( count ==
sizeof( sqlite ) /
sizeof( sqlite3_stmt** ) );
279 for (
int i = 0; i < count; ++i )
281 if ( sqlite3_prepare_v2( mDatabase, sql[i], -1, sqlite[i],
nullptr ) != SQLITE_OK )
283 const char* errMsg = sqlite3_errmsg( mDatabase );
284 mError =
QString(
"Error preparing SQL command:\n%1\nSQL:\n%2" )
302 if ( type ==
Point ) geometryType =
"POINT";
303 else if ( type ==
Polyline ) geometryType =
"LINESTRING";
304 else if ( type ==
Polygon ) geometryType =
"POLYGON";
305 else Q_ASSERT(
false &&
"Unknown export type" );
312 int retX = sqlite3_exec( mDatabase,
"BEGIN",
nullptr,
nullptr,
nullptr );
313 Q_ASSERT( retX == SQLITE_OK );
318 else if ( type ==
Point )
321 Q_ASSERT(
false &&
"Unknown export type" );
323 int retY = sqlite3_exec( mDatabase,
"COMMIT",
nullptr,
nullptr,
nullptr );
324 Q_ASSERT( retY == SQLITE_OK );
337 for (
int i = 0; i < tagKeys.
count(); ++i )
339 sqlCreateTable +=
')';
341 char *errMsg =
nullptr;
342 int ret = sqlite3_exec( mDatabase, sqlCreateTable.
toUtf8().
constData(),
nullptr,
nullptr, &errMsg );
343 if ( ret != SQLITE_OK )
346 sqlite3_free( errMsg );
350 QString sqlAddGeomColumn =
QString(
"SELECT AddGeometryColumn(%1, 'geometry', 4326, %2, 'XY')" )
353 ret = sqlite3_exec( mDatabase, sqlAddGeomColumn.
toUtf8().
constData(),
nullptr,
nullptr, &errMsg );
354 if ( ret != SQLITE_OK )
357 sqlite3_free( errMsg );
368 char *errMsg =
nullptr;
369 int ret = sqlite3_exec( mDatabase, sqlSpatialIndex.
toUtf8().
constData(),
nullptr,
nullptr, &errMsg );
370 if ( ret != SQLITE_OK )
373 sqlite3_free( errMsg );
384 for (
int i = 0; i < tagKeys.
count(); ++i )
385 sqlInsertPoint +=
QString(
",?" );
386 sqlInsertPoint +=
", GeomFromWKB(?, 4326))";
387 sqlite3_stmt* stmtInsert;
388 if ( sqlite3_prepare_v2( mDatabase, sqlInsertPoint.
toUtf8().
constData(), -1, &stmtInsert, nullptr ) != SQLITE_OK )
390 mError =
"Prepare SELECT FROM nodes failed.";
396 while (( n = nodes.
next() ).isValid() )
401 if ( t.
count() == 0 )
405 bool skipNull =
false;
406 for (
int i = 0; i < notNullTagKeys.
count() && !skipNull; ++i )
407 if ( !t.
contains( notNullTagKeys[i] ) )
415 sqlite3_bind_int64( stmtInsert, ++col, n.
id() );
418 for (
int i = 0; i < tagKeys.
count(); ++i )
421 sqlite3_bind_text( stmtInsert, ++col, t.
value( tagKeys[i] ).
toUtf8().
constData(), -1, SQLITE_TRANSIENT );
423 sqlite3_bind_null( stmtInsert, ++col );
426 sqlite3_bind_blob( stmtInsert, ++col, geom->
asWkb(), ( int ) geom->
wkbSize(), SQLITE_STATIC );
428 int insertRes = sqlite3_step( stmtInsert );
429 if ( insertRes != SQLITE_DONE )
431 mError =
QString(
"Error inserting node %1 [%2]" ).
arg( n.
id() ).arg( insertRes );
436 sqlite3_reset( stmtInsert );
437 sqlite3_clear_bindings( stmtInsert );
441 sqlite3_finalize( stmtInsert );
452 for (
int i = 0; i < tagKeys.
count(); ++i )
453 sqlInsertLine +=
QString(
",?" );
454 sqlInsertLine +=
", GeomFromWKB(?, 4326))";
455 sqlite3_stmt* stmtInsert;
456 if ( sqlite3_prepare_v2( mDatabase, sqlInsertLine.
toUtf8().
constData(), -1, &stmtInsert, nullptr ) != SQLITE_OK )
458 mError =
"Prepare SELECT FROM ways failed.";
464 while (( w = ways.
next() ).isValid() )
470 if ( polyline.count() < 2 )
473 bool isArea = ( polyline.first() == polyline.last() );
483 if ( closed != isArea )
487 bool skipNull =
false;
488 for (
int i = 0; i < notNullTagKeys.
count() && !skipNull; ++i )
489 if ( !t.
contains( notNullTagKeys[i] ) )
497 sqlite3_bind_int64( stmtInsert, ++col, w.
id() );
500 for (
int i = 0; i < tagKeys.
count(); ++i )
503 sqlite3_bind_text( stmtInsert, ++col, t.
value( tagKeys[i] ).
toUtf8().
constData(), -1, SQLITE_TRANSIENT );
505 sqlite3_bind_null( stmtInsert, ++col );
509 sqlite3_bind_blob( stmtInsert, ++col, geom->
asWkb(), ( int ) geom->
wkbSize(), SQLITE_STATIC );
511 sqlite3_bind_null( stmtInsert, ++col );
513 int insertRes = sqlite3_step( stmtInsert );
514 if ( insertRes != SQLITE_DONE )
516 mError =
QString(
"Error inserting way %1 [%2]" ).
arg( w.
id() ).arg( insertRes );
521 sqlite3_reset( stmtInsert );
522 sqlite3_clear_bindings( stmtInsert );
526 sqlite3_finalize( stmtInsert );
552 const char* sql =
"SELECT id,lon,lat FROM nodes";
553 if ( sqlite3_prepare_v2( handle, sql, -1, &
mStmt,
nullptr ) != SQLITE_OK )
555 qDebug(
"OSMNodeIterator: error prepare" );
570 if ( sqlite3_step(
mStmt ) != SQLITE_ROW )
577 double lon = sqlite3_column_double(
mStmt, 1 );
578 double lat = sqlite3_column_double(
mStmt, 2 );
587 sqlite3_finalize(
mStmt );
598 const char* sql =
"SELECT id FROM ways";
599 if ( sqlite3_prepare_v2( handle, sql, -1, &
mStmt,
nullptr ) != SQLITE_OK )
601 qDebug(
"OSMWayIterator: error prepare" );
616 if ( sqlite3_step(
mStmt ) != SQLITE_ROW )
631 sqlite3_finalize(
mStmt );
QgsOSMWayIterator(sqlite3 *handle)
A way is an ordered list of nodes which normally also has at least one tag or is included within a Re...
QgsOSMTags tags(bool way, QgsOSMId id) const
QgsOSMNode node(QgsOSMId id) const
void append(const T &value)
Encapsulate iteration over table of ways.
QVector< QgsPoint > QgsPolyline
Polyline is represented as a vector of points.
A geometry is the spatial representation of a feature.
QgsOSMWayIterator listWays() const
int wkbSize() const
Returns the size of the WKB in asWkb().
static int sqlite3_close(sqlite3 *)
void exportSpatiaLiteNodes(const QString &tableName, const QStringList &tagKeys, const QStringList ¬NullTagKeys=QStringList())
int count(const T &value) const
void append(const T &value)
QString fromUtf8(const char *str, int size)
QgsOSMWay way(QgsOSMId id) const
QList< QgsOSMTagCountPair > usedTags(bool ways) const
static int sqlite3_open_v2(const char *filename, sqlite3 **ppDb, int flags, const char *zVfs)
QString quotedIdentifier(QString id)
const char * constData() const
A node is one of the core elements in the OpenStreetMap data model.
const unsigned char * asWkb() const
Returns the buffer containing this geometry in WKB format.
QVector< QgsPolyline > QgsPolygon
Polygon: first item of the list is outer ring, inner rings (if any) start from second item...
A class to represent a point.
static QgsGeometry * fromPoint(const QgsPoint &point)
Creates a new geometry from a QgsPoint object.
void deleteStatement(sqlite3_stmt *&stmt)
bool createSpatialTable(const QString &tableName, const QString &geometryType, const QStringList &tagKeys)
bool exportSpatiaLite(ExportType type, const QString &tableName, const QStringList &tagKeys=QStringList(), const QStringList &noNullTagKeys=QStringList())
QString & replace(int position, int n, QChar after)
QString quotedValue(QString value)
Encapsulate iteration over table of nodes/.
QgsOSMDatabase(const QString &dbFileName=QString())
QgsOSMNodeIterator listNodes() const
static QgsGeometry * fromPolyline(const QgsPolyline &polyline)
Creates a new geometry from a QgsPolyline object.
bool createSpatialIndex(const QString &tableName)
QgsPolyline wayPoints(QgsOSMId id) const
static QgsGeometry * fromPolygon(const QgsPolygon &polygon)
Creates a new geometry from a QgsPolygon.
int runCountStatement(const char *sql) const
QgsOSMNodeIterator(sqlite3 *handle)
void exportSpatiaLiteWays(bool closed, const QString &tableName, const QStringList &tagKeys, const QStringList ¬NullTagKeys=QStringList())
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
QByteArray toUtf8() const