18 #include <spatialite.h>
26 : mDbFileName( dbFileName )
32 , mStmtWayNodePoints( 0 )
46 return mDatabase != 0;
56 int res = sqlite3_open_v2( mDbFileName.toUtf8().data(), &mDatabase, SQLITE_OPEN_READWRITE, 0 );
57 if ( res != SQLITE_OK )
59 mError = QString(
"Failed to open database [%1]: %2" ).arg( res ).arg( mDbFileName );
78 sqlite3_finalize( stmt );
93 Q_ASSERT( mStmtNode == 0 );
96 if ( sqlite3_close( mDatabase ) != SQLITE_OK )
109 int res = sqlite3_prepare_v2( mDatabase, sql, -1, &stmt, 0 );
110 if ( res != SQLITE_OK )
113 res = sqlite3_step( stmt );
114 if ( res != SQLITE_ROW )
117 int count = sqlite3_column_int( stmt, 0 );
118 sqlite3_finalize( stmt );
148 sqlite3_bind_int64( mStmtNode, 1,
id );
150 if ( sqlite3_step( mStmtNode ) != SQLITE_ROW )
153 sqlite3_reset( mStmtNode );
157 double lon = sqlite3_column_double( mStmtNode, 0 );
158 double lat = sqlite3_column_double( mStmtNode, 1 );
162 sqlite3_reset( mStmtNode );
170 sqlite3_stmt* stmtTags = way ? mStmtWayTags : mStmtNodeTags;
172 sqlite3_bind_int64( stmtTags, 1,
id );
174 while ( sqlite3_step( stmtTags ) == SQLITE_ROW )
176 QString k = QString::fromUtf8((
const char* ) sqlite3_column_text( stmtTags, 0 ) );
177 QString v = QString::fromUtf8((
const char* ) sqlite3_column_text( stmtTags, 1 ) );
181 sqlite3_reset( stmtTags );
188 QList<QgsOSMTagCountPair> pairs;
190 QString sql = QString(
"SELECT k, count(k) FROM %1_tags GROUP BY k" ).arg( ways ?
"ways" :
"nodes" );
193 if ( sqlite3_prepare_v2( mDatabase, sql.toUtf8().data(), -1, &stmt, 0 ) != SQLITE_OK )
196 while ( sqlite3_step( stmt ) == SQLITE_ROW )
198 QString k = QString::fromUtf8((
const char* ) sqlite3_column_text( stmt, 0 ) );
199 int count = sqlite3_column_int( stmt, 1 );
200 pairs.append( qMakePair( k, count ) );
203 sqlite3_finalize( stmt );
215 sqlite3_bind_int64( mStmtWayNode, 1,
id );
217 QList<QgsOSMId> nodes;
219 while ( sqlite3_step( mStmtWayNode ) == SQLITE_ROW )
221 QgsOSMId nodeId = sqlite3_column_int64( mStmtWayNode, 0 );
222 nodes.append( nodeId );
225 sqlite3_reset( mStmtWayNode );
227 if ( nodes.isEmpty() )
246 sqlite3_bind_int64( mStmtWayNodePoints, 1,
id );
248 while ( sqlite3_step( mStmtWayNodePoints ) == SQLITE_ROW )
250 if ( sqlite3_column_type( mStmtWayNodePoints, 0 ) == SQLITE_NULL )
252 double lon = sqlite3_column_double( mStmtWayNodePoints, 0 );
253 double lat = sqlite3_column_double( mStmtWayNodePoints, 1 );
254 points.append(
QgsPoint( lon, lat ) );
257 sqlite3_reset( mStmtWayNodePoints );
267 "SELECT lon,lat FROM nodes WHERE id=?",
268 "SELECT k,v FROM nodes_tags WHERE id=?",
269 "SELECT id FROM ways WHERE id=?",
270 "SELECT node_id FROM ways_nodes WHERE way_id=? ORDER BY way_pos",
271 "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",
272 "SELECT k,v FROM ways_tags WHERE id=?"
274 sqlite3_stmt** sqlite[] =
283 int count =
sizeof( sql ) /
sizeof(
const char* );
284 Q_ASSERT( count ==
sizeof( sqlite ) /
sizeof( sqlite3_stmt** ) );
286 for (
int i = 0; i < count; ++i )
288 if ( sqlite3_prepare_v2( mDatabase, sql[i], -1, sqlite[i], 0 ) != SQLITE_OK )
290 const char* errMsg = sqlite3_errmsg( mDatabase );
291 mError = QString(
"Error preparing SQL command:\n%1\nSQL:\n%2" )
292 .arg( QString::fromUtf8( errMsg ) ).arg( QString::fromUtf8( sql[i] ) );
308 QString geometryType;
309 if ( type ==
Point ) geometryType =
"POINT";
310 else if ( type ==
Polyline ) geometryType =
"LINESTRING";
311 else if ( type ==
Polygon ) geometryType =
"POLYGON";
312 else Q_ASSERT(
false &&
"Unknown export type" );
319 int retX = sqlite3_exec( mDatabase,
"BEGIN", NULL, NULL, 0 );
320 Q_ASSERT( retX == SQLITE_OK );
325 else if ( type ==
Point )
328 Q_ASSERT(
false &&
"Unknown export type" );
330 int retY = sqlite3_exec( mDatabase,
"COMMIT", NULL, NULL, 0 );
331 Q_ASSERT( retY == SQLITE_OK );
337 return mError.isEmpty();
343 QString sqlCreateTable = QString(
"CREATE TABLE %1 (id INTEGER PRIMARY KEY" ).arg(
quotedIdentifier( tableName ) );
344 for (
int i = 0; i < tagKeys.count(); ++i )
345 sqlCreateTable += QString(
", %1 TEXT" ).arg(
quotedIdentifier( tagKeys[i] ) );
346 sqlCreateTable +=
")";
349 int ret = sqlite3_exec( mDatabase, sqlCreateTable.toUtf8().constData(), NULL, NULL, &errMsg );
350 if ( ret != SQLITE_OK )
352 mError =
"Unable to create table:\n" + QString::fromUtf8( errMsg );
353 sqlite3_free( errMsg );
357 QString sqlAddGeomColumn = QString(
"SELECT AddGeometryColumn(%1, 'geometry', 4326, %2, 'XY')" )
360 ret = sqlite3_exec( mDatabase, sqlAddGeomColumn.toUtf8().constData(), NULL, NULL, &errMsg );
361 if ( ret != SQLITE_OK )
363 mError =
"Unable to add geometry column:\n" + QString::fromUtf8( errMsg );
364 sqlite3_free( errMsg );
374 QString sqlSpatialIndex = QString(
"SELECT CreateSpatialIndex(%1, 'geometry')" ).arg(
quotedValue( tableName ) );
376 int ret = sqlite3_exec( mDatabase, sqlSpatialIndex.toUtf8().constData(), NULL, NULL, &errMsg );
377 if ( ret != SQLITE_OK )
379 mError =
"Unable to create spatial index:\n" + QString::fromUtf8( errMsg );
380 sqlite3_free( errMsg );
390 QString sqlInsertPoint = QString(
"INSERT INTO %1 VALUES (?" ).arg(
quotedIdentifier( tableName ) );
391 for (
int i = 0; i < tagKeys.count(); ++i )
392 sqlInsertPoint += QString(
",?" );
393 sqlInsertPoint +=
", GeomFromWKB(?, 4326))";
394 sqlite3_stmt* stmtInsert;
395 if ( sqlite3_prepare_v2( mDatabase, sqlInsertPoint.toUtf8().constData(), -1, &stmtInsert, 0 ) != SQLITE_OK )
397 mError =
"Prepare SELECT FROM nodes failed.";
403 while (( n = nodes.
next() ).isValid() )
408 if ( t.
count() == 0 )
413 sqlite3_bind_int64( stmtInsert, ++col, n.
id() );
416 for (
int i = 0; i < tagKeys.count(); ++i )
419 sqlite3_bind_text( stmtInsert, ++col, t.
value( tagKeys[i] ).toUtf8().constData(), -1, SQLITE_TRANSIENT );
421 sqlite3_bind_null( stmtInsert, ++col );
424 sqlite3_bind_blob( stmtInsert, ++col, geom->
asWkb(), ( int ) geom->
wkbSize(), SQLITE_STATIC );
426 int insertRes = sqlite3_step( stmtInsert );
427 if ( insertRes != SQLITE_DONE )
429 mError = QString(
"Error inserting node %1 [%2]" ).arg( n.
id() ).arg( insertRes );
434 sqlite3_reset( stmtInsert );
435 sqlite3_clear_bindings( stmtInsert );
439 sqlite3_finalize( stmtInsert );
447 QString sqlInsertLine = QString(
"INSERT INTO %1 VALUES (?" ).arg(
quotedIdentifier( tableName ) );
448 for (
int i = 0; i < tagKeys.count(); ++i )
449 sqlInsertLine += QString(
",?" );
450 sqlInsertLine +=
", GeomFromWKB(?, 4326))";
451 sqlite3_stmt* stmtInsert;
452 if ( sqlite3_prepare_v2( mDatabase, sqlInsertLine.toUtf8().constData(), -1, &stmtInsert, 0 ) != SQLITE_OK )
454 mError =
"Prepare SELECT FROM ways failed.";
460 while (( w = ways.
next() ).isValid() )
466 if ( polyline.count() < 2 )
469 bool isArea = ( polyline.first() == polyline.last() );
473 if ( t.
value(
"area" ) !=
"yes" )
477 if ( closed != isArea )
482 sqlite3_bind_int64( stmtInsert, ++col, w.
id() );
485 for (
int i = 0; i < tagKeys.count(); ++i )
488 sqlite3_bind_text( stmtInsert, ++col, t.
value( tagKeys[i] ).toUtf8().constData(), -1, SQLITE_TRANSIENT );
490 sqlite3_bind_null( stmtInsert, ++col );
494 sqlite3_bind_blob( stmtInsert, ++col, geom->
asWkb(), ( int ) geom->
wkbSize(), SQLITE_STATIC );
496 sqlite3_bind_null( stmtInsert, ++col );
498 int insertRes = sqlite3_step( stmtInsert );
499 if ( insertRes != SQLITE_DONE )
501 mError = QString(
"Error inserting way %1 [%2]" ).arg( w.
id() ).arg( insertRes );
506 sqlite3_reset( stmtInsert );
507 sqlite3_clear_bindings( stmtInsert );
511 sqlite3_finalize( stmtInsert );
518 id.replace(
"\"",
"\"\"" );
519 return QString(
"\"%1\"" ).arg(
id );
524 if ( value.isNull() )
527 value.replace(
"'",
"''" );
528 return QString(
"'%1'" ).arg( value );
537 const char* sql =
"SELECT id,lon,lat FROM nodes";
538 if ( sqlite3_prepare_v2( handle, sql, -1, &
mStmt, 0 ) != SQLITE_OK )
540 qDebug(
"OSMNodeIterator: error prepare" );
555 if ( sqlite3_step(
mStmt ) != SQLITE_ROW )
562 double lon = sqlite3_column_double(
mStmt, 1 );
563 double lat = sqlite3_column_double(
mStmt, 2 );
572 sqlite3_finalize(
mStmt );
583 const char* sql =
"SELECT id FROM ways";
584 if ( sqlite3_prepare_v2( handle, sql, -1, &
mStmt, 0 ) != SQLITE_OK )
586 qDebug(
"OSMWayIterator: error prepare" );
601 if ( sqlite3_step(
mStmt ) != SQLITE_ROW )
609 return QgsOSMWay(
id, QList<QgsOSMId>() );
616 sqlite3_finalize(
mStmt );