23 : mDbFileName( dbFileName )
29 , mStmtWayNodePoints( 0 )
42 return mDatabase != 0;
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 == 0 );
102 int res = sqlite3_prepare_v2( mDatabase, sql, -1, &stmt, 0 );
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 )
169 QString k = QString::fromUtf8((
const char* ) sqlite3_column_text( stmtTags, 0 ) );
170 QString v = QString::fromUtf8((
const char* ) sqlite3_column_text( stmtTags, 1 ) );
174 sqlite3_reset( stmtTags );
181 QList<QgsOSMTagCountPair> pairs;
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, 0 ) != SQLITE_OK )
189 while ( sqlite3_step( stmt ) == SQLITE_ROW )
191 QString k = QString::fromUtf8((
const char* ) sqlite3_column_text( stmt, 0 ) );
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 );
210 QList<QgsOSMId> nodes;
212 while ( sqlite3_step( mStmtWayNode ) == SQLITE_ROW )
214 QgsOSMId nodeId = sqlite3_column_int64( mStmtWayNode, 0 );
215 nodes.append( nodeId );
218 sqlite3_reset( mStmtWayNode );
220 if ( nodes.isEmpty() )
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 );
247 points.append(
QgsPoint( lon, lat ) );
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], 0 ) != SQLITE_OK )
283 const char* errMsg = sqlite3_errmsg( mDatabase );
284 mError = QString(
"Error preparing SQL command:\n%1\nSQL:\n%2" )
285 .arg( QString::fromUtf8( errMsg ) ).arg( QString::fromUtf8( sql[i] ) );
301 QString geometryType;
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", NULL, NULL, 0 );
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", NULL, NULL, 0 );
324 Q_ASSERT( retY == SQLITE_OK );
330 return mError.isEmpty();
336 QString sqlCreateTable = QString(
"CREATE TABLE %1 (id INTEGER PRIMARY KEY" ).arg(
quotedIdentifier( tableName ) );
337 for (
int i = 0; i < tagKeys.count(); ++i )
338 sqlCreateTable += QString(
", %1 TEXT" ).arg(
quotedIdentifier( tagKeys[i] ) );
339 sqlCreateTable +=
")";
342 int ret = sqlite3_exec( mDatabase, sqlCreateTable.toUtf8().constData(), NULL, NULL, &errMsg );
343 if ( ret != SQLITE_OK )
345 mError =
"Unable to create table:\n" + QString::fromUtf8( errMsg );
346 sqlite3_free( errMsg );
350 QString sqlAddGeomColumn = QString(
"SELECT AddGeometryColumn(%1, 'geometry', 4326, %2, 'XY')" )
353 ret = sqlite3_exec( mDatabase, sqlAddGeomColumn.toUtf8().constData(), NULL, NULL, &errMsg );
354 if ( ret != SQLITE_OK )
356 mError =
"Unable to add geometry column:\n" + QString::fromUtf8( errMsg );
357 sqlite3_free( errMsg );
367 QString sqlSpatialIndex = QString(
"SELECT CreateSpatialIndex(%1, 'geometry')" ).arg(
quotedValue( tableName ) );
369 int ret = sqlite3_exec( mDatabase, sqlSpatialIndex.toUtf8().constData(), NULL, NULL, &errMsg );
370 if ( ret != SQLITE_OK )
372 mError =
"Unable to create spatial index:\n" + QString::fromUtf8( errMsg );
373 sqlite3_free( errMsg );
383 QString sqlInsertPoint = QString(
"INSERT INTO %1 VALUES (?" ).arg(
quotedIdentifier( tableName ) );
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, 0 ) != SQLITE_OK )
390 mError =
"Prepare SELECT FROM nodes failed.";
396 while (( n = nodes.
next() ).isValid() )
401 if ( t.
count() == 0 )
406 sqlite3_bind_int64( stmtInsert, ++col, n.
id() );
409 for (
int i = 0; i < tagKeys.count(); ++i )
412 sqlite3_bind_text( stmtInsert, ++col, t.
value( tagKeys[i] ).toUtf8().constData(), -1, SQLITE_TRANSIENT );
414 sqlite3_bind_null( stmtInsert, ++col );
417 sqlite3_bind_blob( stmtInsert, ++col, geom->
asWkb(), ( int ) geom->
wkbSize(), SQLITE_STATIC );
419 int insertRes = sqlite3_step( stmtInsert );
420 if ( insertRes != SQLITE_DONE )
422 mError = QString(
"Error inserting node %1 [%2]" ).arg( n.
id() ).arg( insertRes );
427 sqlite3_reset( stmtInsert );
428 sqlite3_clear_bindings( stmtInsert );
432 sqlite3_finalize( stmtInsert );
440 QString sqlInsertLine = QString(
"INSERT INTO %1 VALUES (?" ).arg(
quotedIdentifier( tableName ) );
441 for (
int i = 0; i < tagKeys.count(); ++i )
442 sqlInsertLine += QString(
",?" );
443 sqlInsertLine +=
", GeomFromWKB(?, 4326))";
444 sqlite3_stmt* stmtInsert;
445 if ( sqlite3_prepare_v2( mDatabase, sqlInsertLine.toUtf8().constData(), -1, &stmtInsert, 0 ) != SQLITE_OK )
447 mError =
"Prepare SELECT FROM ways failed.";
453 while (( w = ways.
next() ).isValid() )
459 if ( polyline.count() < 2 )
462 bool isArea = ( polyline.first() == polyline.last() );
466 if ( t.
value(
"area" ) !=
"yes" )
470 if ( closed != isArea )
475 sqlite3_bind_int64( stmtInsert, ++col, w.
id() );
478 for (
int i = 0; i < tagKeys.count(); ++i )
481 sqlite3_bind_text( stmtInsert, ++col, t.
value( tagKeys[i] ).toUtf8().constData(), -1, SQLITE_TRANSIENT );
483 sqlite3_bind_null( stmtInsert, ++col );
487 sqlite3_bind_blob( stmtInsert, ++col, geom->
asWkb(), ( int ) geom->
wkbSize(), SQLITE_STATIC );
489 sqlite3_bind_null( stmtInsert, ++col );
491 int insertRes = sqlite3_step( stmtInsert );
492 if ( insertRes != SQLITE_DONE )
494 mError = QString(
"Error inserting way %1 [%2]" ).arg( w.
id() ).arg( insertRes );
499 sqlite3_reset( stmtInsert );
500 sqlite3_clear_bindings( stmtInsert );
504 sqlite3_finalize( stmtInsert );
511 id.replace(
"\"",
"\"\"" );
512 return QString(
"\"%1\"" ).arg(
id );
517 if ( value.isNull() )
520 value.replace(
"'",
"''" );
521 return QString(
"'%1'" ).arg( value );
530 const char* sql =
"SELECT id,lon,lat FROM nodes";
531 if ( sqlite3_prepare_v2( handle, sql, -1, &
mStmt, 0 ) != SQLITE_OK )
533 qDebug(
"OSMNodeIterator: error prepare" );
548 if ( sqlite3_step(
mStmt ) != SQLITE_ROW )
555 double lon = sqlite3_column_double(
mStmt, 1 );
556 double lat = sqlite3_column_double(
mStmt, 2 );
565 sqlite3_finalize(
mStmt );
576 const char* sql =
"SELECT id FROM ways";
577 if ( sqlite3_prepare_v2( handle, sql, -1, &
mStmt, 0 ) != SQLITE_OK )
579 qDebug(
"OSMWayIterator: error prepare" );
594 if ( sqlite3_step(
mStmt ) != SQLITE_ROW )
602 return QgsOSMWay(
id, QList<QgsOSMId>() );
609 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...
bool exportSpatiaLite(ExportType type, const QString &tableName, const QStringList &tagKeys=QStringList())
int runCountStatement(const char *sql) const
size_t wkbSize() const
Returns the size of the WKB in asWkb().
Encapsulate iteration over table of ways.
QVector< QgsPoint > QgsPolyline
polyline is represented as a vector of points
static int sqlite3_close(sqlite3 *)
QList< QgsOSMTagCountPair > usedTags(bool ways) const
static int sqlite3_open_v2(const char *filename, sqlite3 **ppDb, int flags, const char *zVfs)
QgsOSMNodeIterator listNodes() const
QString quotedIdentifier(QString id)
A node is one of the core elements in the OpenStreetMap data model.
QVector< QgsPolyline > QgsPolygon
polygon: first item of the list is outer ring, inner rings (if any) start from second item ...
QgsOSMTags tags(bool way, QgsOSMId id) const
A class to represent a point.
static QgsGeometry * fromPoint(const QgsPoint &point)
construct geometry from a point
void deleteStatement(sqlite3_stmt *&stmt)
bool createSpatialTable(const QString &tableName, const QString &geometryType, const QStringList &tagKeys)
QgsOSMNode node(QgsOSMId id) const
QgsPolyline wayPoints(QgsOSMId id) const
QgsOSMWay way(QgsOSMId id) const
QString quotedValue(QString value)
Encapsulate iteration over table of nodes.
QgsOSMWayIterator listWays() const
QgsOSMDatabase(const QString &dbFileName=QString())
static QgsGeometry * fromPolyline(const QgsPolyline &polyline)
construct geometry from a polyline
bool createSpatialIndex(const QString &tableName)
static QgsGeometry * fromPolygon(const QgsPolygon &polygon)
construct geometry from a polygon
void exportSpatiaLiteNodes(const QString &tableName, const QStringList &tagKeys)
void exportSpatiaLiteWays(bool closed, const QString &tableName, const QStringList &tagKeys)
QgsOSMNodeIterator(sqlite3 *handle)
const unsigned char * asWkb() const
Returns the buffer containing this geometry in WKB format.