Skip to content

Commit 6004c14

Browse files
committed
Add not null option for OSM export
1 parent ce18a60 commit 6004c14

File tree

3 files changed

+44
-14
lines changed

3 files changed

+44
-14
lines changed

src/analysis/openstreetmap/qgsosmdatabase.cpp

+25-8
Original file line numberDiff line numberDiff line change
@@ -297,9 +297,9 @@ bool QgsOSMDatabase::prepareStatements()
297297
return true;
298298
}
299299

300-
301-
302-
bool QgsOSMDatabase::exportSpatiaLite( ExportType type, const QString& tableName, const QStringList& tagKeys )
300+
bool QgsOSMDatabase::exportSpatiaLite( ExportType type, const QString& tableName,
301+
const QStringList& tagKeys,
302+
const QStringList& notNullTagKeys)
303303
{
304304
mError.clear();
305305

@@ -321,9 +321,9 @@ bool QgsOSMDatabase::exportSpatiaLite( ExportType type, const QString& tableName
321321
Q_UNUSED( retX );
322322

323323
if ( type == Polyline || type == Polygon )
324-
exportSpatiaLiteWays( type == Polygon, tableName, tagKeys );
324+
exportSpatiaLiteWays( type == Polygon, tableName, tagKeys, notNullTagKeys );
325325
else if ( type == Point )
326-
exportSpatiaLiteNodes( tableName, tagKeys );
326+
exportSpatiaLiteNodes( tableName, tagKeys, notNullTagKeys );
327327
else
328328
Q_ASSERT( false && "Unknown export type" );
329329

@@ -338,7 +338,7 @@ bool QgsOSMDatabase::exportSpatiaLite( ExportType type, const QString& tableName
338338
}
339339

340340

341-
bool QgsOSMDatabase::createSpatialTable( const QString& tableName, const QString& geometryType, const QStringList& tagKeys )
341+
bool QgsOSMDatabase::createSpatialTable( const QString& tableName, const QString& geometryType, const QStringList& tagKeys)
342342
{
343343
QString sqlCreateTable = QString( "CREATE TABLE %1 (id INTEGER PRIMARY KEY" ).arg( quotedIdentifier( tableName ) );
344344
for ( int i = 0; i < tagKeys.count(); ++i )
@@ -385,7 +385,7 @@ bool QgsOSMDatabase::createSpatialIndex( const QString& tableName )
385385
}
386386

387387

388-
void QgsOSMDatabase::exportSpatiaLiteNodes( const QString& tableName, const QStringList& tagKeys )
388+
void QgsOSMDatabase::exportSpatiaLiteNodes( const QString& tableName, const QStringList& tagKeys, const QStringList& notNullTagKeys )
389389
{
390390
QString sqlInsertPoint = QString( "INSERT INTO %1 VALUES (?" ).arg( quotedIdentifier( tableName ) );
391391
for ( int i = 0; i < tagKeys.count(); ++i )
@@ -408,6 +408,11 @@ void QgsOSMDatabase::exportSpatiaLiteNodes( const QString& tableName, const QStr
408408
if ( t.count() == 0 )
409409
continue;
410410

411+
//check not null tags
412+
for( int i = 0; i < notNullTagKeys.count(); ++i )
413+
if ( !t.contains( notNullTagKeys[i] ) )
414+
continue;
415+
411416
QgsGeometry* geom = QgsGeometry::fromPoint( n.point() );
412417
int col = 0;
413418
sqlite3_bind_int64( stmtInsert, ++col, n.id() );
@@ -440,7 +445,9 @@ void QgsOSMDatabase::exportSpatiaLiteNodes( const QString& tableName, const QStr
440445
}
441446

442447

443-
void QgsOSMDatabase::exportSpatiaLiteWays( bool closed, const QString& tableName, const QStringList& tagKeys )
448+
void QgsOSMDatabase::exportSpatiaLiteWays( bool closed, const QString& tableName,
449+
const QStringList& tagKeys,
450+
const QStringList& notNullTagKeys)
444451
{
445452
Q_UNUSED( tagKeys );
446453

@@ -477,6 +484,16 @@ void QgsOSMDatabase::exportSpatiaLiteWays( bool closed, const QString& tableName
477484
if ( closed != isArea )
478485
continue; // skip if it's not what we're looking for
479486

487+
bool skipNull = false;
488+
489+
//check not null tags
490+
for( int i = 0; i < notNullTagKeys.count() && skipNull == false; ++i )
491+
if ( !t.contains( notNullTagKeys[i] ) )
492+
skipNull = true;
493+
494+
if(skipNull)
495+
continue;
496+
480497
QgsGeometry* geom = closed ? QgsGeometry::fromPolygon( QgsPolygon() << polyline ) : QgsGeometry::fromPolyline( polyline );
481498
int col = 0;
482499
sqlite3_bind_int64( stmtInsert, ++col, w.id() );

src/analysis/openstreetmap/qgsosmdatabase.h

+5-3
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,17 @@ class ANALYSIS_EXPORT QgsOSMDatabase
7979
// export to spatialite
8080

8181
enum ExportType { Point, Polyline, Polygon };
82-
bool exportSpatiaLite( ExportType type, const QString& tableName, const QStringList& tagKeys = QStringList() );
82+
bool exportSpatiaLite( ExportType type, const QString& tableName,
83+
const QStringList& tagKeys = QStringList(),
84+
const QStringList& noNullTagKeys = QStringList() );
8385

8486
protected:
8587
bool prepareStatements();
8688
int runCountStatement( const char* sql ) const;
8789
void deleteStatement( sqlite3_stmt*& stmt );
8890

89-
void exportSpatiaLiteNodes( const QString& tableName, const QStringList& tagKeys );
90-
void exportSpatiaLiteWays( bool closed, const QString& tableName, const QStringList& tagKeys );
91+
void exportSpatiaLiteNodes( const QString& tableName, const QStringList& tagKeys, const QStringList& notNullTagKeys );
92+
void exportSpatiaLiteWays( bool closed, const QString& tableName, const QStringList& tagKeys, const QStringList& notNullTagKeys );
9193
bool createSpatialTable( const QString& tableName, const QString& geometryType, const QStringList& tagKeys );
9294
bool createSpatialIndex( const QString& tableName );
9395

src/app/openstreetmap/qgsosmexportdialog.cpp

+14-3
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ QgsOSMExportDialog::QgsOSMExportDialog( QWidget *parent ) :
4444
connect( btnUnselectAll, SIGNAL( clicked() ), this, SLOT( onUnselectAll() ) );
4545

4646
mTagsModel = new QStandardItemModel( this );
47-
mTagsModel->setHorizontalHeaderLabels( QStringList() << tr( "Tag" ) << tr( "Count" ) );
47+
mTagsModel->setHorizontalHeaderLabels( QStringList() << tr( "Tag" ) << tr( "Count" ) << tr( "Not null" ) );
4848
viewTags->setModel( mTagsModel );
4949
}
5050

@@ -106,7 +106,7 @@ void QgsOSMExportDialog::onLoadTags()
106106
QList<QgsOSMTagCountPair> pairs = mDatabase->usedTags( !radPoints->isChecked() );
107107
mDatabase->close();
108108

109-
mTagsModel->setColumnCount( 2 );
109+
mTagsModel->setColumnCount( 3 );
110110
mTagsModel->setRowCount( pairs.count() );
111111

112112
for ( int i = 0; i < pairs.count(); ++i )
@@ -115,9 +115,15 @@ void QgsOSMExportDialog::onLoadTags()
115115
QStandardItem* item = new QStandardItem( p.first );
116116
item->setCheckable( true );
117117
mTagsModel->setItem( i, 0, item );
118+
118119
QStandardItem* item2 = new QStandardItem();
119120
item2->setData( p.second, Qt::DisplayRole );
120121
mTagsModel->setItem( i, 1, item2 );
122+
123+
QStandardItem* item3 = new QStandardItem();
124+
item3->setData( "Not null", Qt::DisplayRole );
125+
item3->setCheckable( true );
126+
mTagsModel->setItem( i, 2, item3 );
121127
}
122128

123129
viewTags->resizeColumnToContents( 0 );
@@ -144,15 +150,20 @@ void QgsOSMExportDialog::onOK()
144150
QApplication::setOverrideCursor( Qt::WaitCursor );
145151

146152
QStringList tagKeys;
153+
QStringList notNullTagKeys;
147154

148155
for ( int i = 0; i < mTagsModel->rowCount(); ++i )
149156
{
150157
QStandardItem* item = mTagsModel->item( i, 0 );
151158
if ( item->checkState() == Qt::Checked )
152159
tagKeys << item->text();
160+
161+
QStandardItem* item2 = mTagsModel->item( i, 2 );
162+
if ( item2->checkState() == Qt::Checked )
163+
notNullTagKeys << item->text();
153164
}
154165

155-
bool res = mDatabase->exportSpatiaLite( type, editLayerName->text(), tagKeys );
166+
bool res = mDatabase->exportSpatiaLite( type, editLayerName->text(), tagKeys, notNullTagKeys );
156167

157168
// load the layer into canvas if that was requested
158169
if ( chkLoadWhenFinished->isChecked() )

0 commit comments

Comments
 (0)