Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add not null option for OSM export
  • Loading branch information
kiselev-dv committed May 3, 2015
1 parent ce18a60 commit 6004c14
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 14 deletions.
33 changes: 25 additions & 8 deletions src/analysis/openstreetmap/qgsosmdatabase.cpp
Expand Up @@ -297,9 +297,9 @@ bool QgsOSMDatabase::prepareStatements()
return true;
}



bool QgsOSMDatabase::exportSpatiaLite( ExportType type, const QString& tableName, const QStringList& tagKeys )
bool QgsOSMDatabase::exportSpatiaLite( ExportType type, const QString& tableName,
const QStringList& tagKeys,
const QStringList& notNullTagKeys)
{
mError.clear();

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

if ( type == Polyline || type == Polygon )
exportSpatiaLiteWays( type == Polygon, tableName, tagKeys );
exportSpatiaLiteWays( type == Polygon, tableName, tagKeys, notNullTagKeys );
else if ( type == Point )
exportSpatiaLiteNodes( tableName, tagKeys );
exportSpatiaLiteNodes( tableName, tagKeys, notNullTagKeys );
else
Q_ASSERT( false && "Unknown export type" );

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


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


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

//check not null tags
for( int i = 0; i < notNullTagKeys.count(); ++i )
if ( !t.contains( notNullTagKeys[i] ) )
continue;

QgsGeometry* geom = QgsGeometry::fromPoint( n.point() );
int col = 0;
sqlite3_bind_int64( stmtInsert, ++col, n.id() );
Expand Down Expand Up @@ -440,7 +445,9 @@ void QgsOSMDatabase::exportSpatiaLiteNodes( const QString& tableName, const QStr
}


void QgsOSMDatabase::exportSpatiaLiteWays( bool closed, const QString& tableName, const QStringList& tagKeys )
void QgsOSMDatabase::exportSpatiaLiteWays( bool closed, const QString& tableName,
const QStringList& tagKeys,
const QStringList& notNullTagKeys)
{
Q_UNUSED( tagKeys );

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

bool skipNull = false;

//check not null tags
for( int i = 0; i < notNullTagKeys.count() && skipNull == false; ++i )
if ( !t.contains( notNullTagKeys[i] ) )
skipNull = true;

if(skipNull)
continue;

QgsGeometry* geom = closed ? QgsGeometry::fromPolygon( QgsPolygon() << polyline ) : QgsGeometry::fromPolyline( polyline );
int col = 0;
sqlite3_bind_int64( stmtInsert, ++col, w.id() );
Expand Down
8 changes: 5 additions & 3 deletions src/analysis/openstreetmap/qgsosmdatabase.h
Expand Up @@ -79,15 +79,17 @@ class ANALYSIS_EXPORT QgsOSMDatabase
// export to spatialite

enum ExportType { Point, Polyline, Polygon };
bool exportSpatiaLite( ExportType type, const QString& tableName, const QStringList& tagKeys = QStringList() );
bool exportSpatiaLite( ExportType type, const QString& tableName,
const QStringList& tagKeys = QStringList(),
const QStringList& noNullTagKeys = QStringList() );

protected:
bool prepareStatements();
int runCountStatement( const char* sql ) const;
void deleteStatement( sqlite3_stmt*& stmt );

void exportSpatiaLiteNodes( const QString& tableName, const QStringList& tagKeys );
void exportSpatiaLiteWays( bool closed, const QString& tableName, const QStringList& tagKeys );
void exportSpatiaLiteNodes( const QString& tableName, const QStringList& tagKeys, const QStringList& notNullTagKeys );
void exportSpatiaLiteWays( bool closed, const QString& tableName, const QStringList& tagKeys, const QStringList& notNullTagKeys );
bool createSpatialTable( const QString& tableName, const QString& geometryType, const QStringList& tagKeys );
bool createSpatialIndex( const QString& tableName );

Expand Down
17 changes: 14 additions & 3 deletions src/app/openstreetmap/qgsosmexportdialog.cpp
Expand Up @@ -44,7 +44,7 @@ QgsOSMExportDialog::QgsOSMExportDialog( QWidget *parent ) :
connect( btnUnselectAll, SIGNAL( clicked() ), this, SLOT( onUnselectAll() ) );

mTagsModel = new QStandardItemModel( this );
mTagsModel->setHorizontalHeaderLabels( QStringList() << tr( "Tag" ) << tr( "Count" ) );
mTagsModel->setHorizontalHeaderLabels( QStringList() << tr( "Tag" ) << tr( "Count" ) << tr( "Not null" ) );
viewTags->setModel( mTagsModel );
}

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

mTagsModel->setColumnCount( 2 );
mTagsModel->setColumnCount( 3 );
mTagsModel->setRowCount( pairs.count() );

for ( int i = 0; i < pairs.count(); ++i )
Expand All @@ -115,9 +115,15 @@ void QgsOSMExportDialog::onLoadTags()
QStandardItem* item = new QStandardItem( p.first );
item->setCheckable( true );
mTagsModel->setItem( i, 0, item );

QStandardItem* item2 = new QStandardItem();
item2->setData( p.second, Qt::DisplayRole );
mTagsModel->setItem( i, 1, item2 );

QStandardItem* item3 = new QStandardItem();
item3->setData( "Not null", Qt::DisplayRole );
item3->setCheckable( true );
mTagsModel->setItem( i, 2, item3 );
}

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

QStringList tagKeys;
QStringList notNullTagKeys;

for ( int i = 0; i < mTagsModel->rowCount(); ++i )
{
QStandardItem* item = mTagsModel->item( i, 0 );
if ( item->checkState() == Qt::Checked )
tagKeys << item->text();

QStandardItem* item2 = mTagsModel->item( i, 2 );
if ( item2->checkState() == Qt::Checked )
notNullTagKeys << item->text();
}

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

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

0 comments on commit 6004c14

Please sign in to comment.