2
//////////////////////////////////////////////////////////////////////////////
4
// Copyright (C) 2008 by Mateusz Loskot <mateusz@loskot.net>
6
// This program is free software; you can redistribute it and/or modify
7
// it under the terms of the GNU General Public License as published by
8
// the Free Software Foundation; either version 2 of the License,
9
// or (at your option) any later version.
11
//////////////////////////////////////////////////////////////////////////////
13
// qgis::plugin::ogrconv
16
#include "translator.h"
18
#include <qgsapplication.h>
19
#include <qgslogger.h>
20
#include <qgscontexthelp.h>
21
// TODO: Add support of QGIS projection selector
22
//#include <qgsprojectionselector.h>
24
#include <QtAlgorithms>
26
#include <QFileDialog>
27
#include <QMessageBox>
30
#include <QStringList>
35
Dialog::Dialog( QWidget* parent, Qt::WFlags fl )
36
: QDialog( parent, fl )
48
void Dialog::resetSrcUi()
50
// Clear all settings and states
51
inputSrcDataset->clear();
52
// TODO: Transformation support
53
//inputSrcSrs->clear();
54
comboSrcLayer->clear();
55
radioSrcFile->setDisabled( true );
56
radioSrcFile->setChecked( false );
57
radioSrcDirectory->setDisabled( true );
58
radioSrcDirectory->setChecked( false );
59
radioSrcProtocol->setDisabled( true );
60
radioSrcProtocol->setChecked( false );
62
// Configure types of input sources
63
unsigned char const& type = mSrcFormat.type();
65
if ( isFormatType( type, Format::eFile ) )
67
radioSrcFile->setDisabled( false );
68
radioSrcFile->setChecked( true );
71
if ( isFormatType( type, Format::eDirectory ) )
73
radioSrcDirectory->setDisabled( false );
74
if ( !radioSrcFile->isEnabled() )
75
radioSrcDirectory->setChecked( true );
78
if ( isFormatType( type, Format::eProtocol ) )
80
radioSrcProtocol->setDisabled( false );
82
if ( !radioSrcFile->isEnabled() && !radioSrcDirectory->isEnabled() )
84
radioSrcProtocol->setChecked( true );
85
inputSrcDataset->setText( mSrcFormat.protocol() );
89
setButtonState( buttonSelectSrc, isFormatType( type, Format::eProtocol ) );
92
void Dialog::resetDstUi()
94
inputDstDataset->clear();
95
// TODO: Transformation support
96
//inputDstSrs->clear();
98
unsigned char const& type = mDstFormat.type();
99
bool isProtocol = isFormatType( type, Format::eProtocol );
103
inputDstDataset->setText( mDstFormat.protocol() );
106
setButtonState( buttonSelectDst, isProtocol );
109
void Dialog::setButtonState( QPushButton* btn, bool isProtocol )
115
btn->setText( tr( "Connect" ) );
119
btn->setText( tr( "Browse" ) );
123
void Dialog::populateFormats()
125
comboSrcFormats->clear();
126
comboDstFormats->clear();
128
QStringList drvSrcList;
129
QStringList drvDstList;
132
QgsApplication::registerOgrDrivers();
133
int const drvCount = OGRGetDriverCount();
135
for ( int i = 0; i < drvCount; ++i )
137
OGRSFDriverH drv = OGRGetDriver( i );
141
drvName = OGR_Dr_GetName( drv );
142
drvSrcList.append( drvName );
144
if ( 0 != OGR_Dr_TestCapability( drv, ODrCCreateDataSource ) )
146
drvDstList.append( drvName );
151
qSort( drvSrcList.begin(), drvSrcList.end() );
152
qSort( drvDstList.begin(), drvDstList.end() );
153
comboSrcFormats->addItems( drvSrcList );
154
comboDstFormats->addItems( drvDstList );
157
void Dialog::populateLayers( QString const& url )
159
comboSrcLayer->clear();
161
OGRDataSourceH ds = OGROpen( url.toAscii().constData(), 0, 0 );
167
int const size = OGR_DS_GetLayerCount( ds );
168
for ( int i = 0; i < size; ++i )
170
OGRLayerH lyr = OGR_DS_GetLayer( ds, i );
173
OGRFeatureDefnH lyrDef = OGR_L_GetLayerDefn( lyr );
174
Q_ASSERT( 0 != lyrDef );
176
lyrName = OGR_FD_GetName( lyrDef );
178
OGRwkbGeometryType const geomType = OGR_FD_GetGeomType( lyrDef );
179
lyrType = OGRGeometryTypeToName( geomType );
181
// FIXME: Appending type to layer name prevents from layer finding
182
//comboSrcLayer->addItem(lyrName + " (" + lyrType.toUpper() + ")");
183
comboSrcLayer->addItem( lyrName );
187
OGR_DS_Destroy( ds );
191
QMessageBox::warning( this,
192
tr( "OGR Converter" ),
193
tr( "Could not establish connection to: '%1'" ).arg( url ),
194
QMessageBox::Close );
198
bool Dialog::testConnection( QString const& url )
200
bool success = false;
202
OGRDataSourceH ds = OGROpen( url.toAscii().constData(), 0, 0 );
206
OGR_DS_Destroy( ds );
212
QString Dialog::openFile()
215
QString path = QFileDialog::getOpenFileName( this,
216
tr( "Open OGR file" ),
217
sets.value( "/Plugin-OGR/ogr-file", "./" ).toString(),
218
tr( "OGR File Data Source (*.*)" ) );
223
QString Dialog::openDirectory()
225
QString path = QFileDialog::getExistingDirectory( this,
226
tr( "Open Directory" ), "",
227
QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks );
232
void Dialog::on_buttonBox_accepted()
234
// Validate input settings
235
QString srcUrl( inputSrcDataset->text() );
236
QString srcLayer( comboSrcLayer->currentText() );
238
if ( srcUrl.isEmpty() )
240
QMessageBox::warning( this,
241
tr( "OGR Layer Converter" ),
242
tr( "Input OGR dataset is missing!" ) );
246
if ( srcLayer.isEmpty() )
248
QMessageBox::warning( this,
249
tr( "OGR Layer Converter" ),
250
tr( "Input OGR layer name is missing!" ) );
254
// Validate output settings
255
QString dstFormat( comboDstFormats->currentText() );
256
QString dstUrl( inputDstDataset->text() );
257
QString dstLayer( inputDstLayer->text() );
258
if ( dstLayer.isEmpty() )
261
if ( dstFormat.isEmpty() )
263
QMessageBox::warning( this,
264
tr( "OGR Layer Converter" ),
265
tr( "Target OGR format not selected!" ) );
269
if ( dstUrl.isEmpty() )
271
QMessageBox::warning( this,
272
tr( "OGR Layer Converter" ),
273
tr( "Output OGR dataset is missing!" ) );
277
if ( dstLayer.isEmpty() )
279
QMessageBox::warning( this,
280
tr( "OGR Layer Converter" ),
281
tr( "Output OGR layer name is missing!" ) );
285
// TODO: SRS transformation support
286
//QString srcSrs("EPSG:");
287
//QString dstSrs("EPSG:");
288
//srcSrs += inputSrcSrs->text();
289
//dstSrs += inputDstSrs->text();
291
// Execute layer translation
292
bool success = false;
294
// TODO: Use try-catch to display more meaningful error messages from Translator
295
Translator worker( srcUrl, dstUrl, dstFormat );
296
worker.setSourceLayer( srcLayer );
297
worker.setTargetLayer( dstLayer );
298
success = worker.translate();
302
QMessageBox::information( this,
303
tr( "OGR Layer Converter" ),
304
tr( "Successfully translated layer '%1'" ).arg( srcLayer ) );
308
QMessageBox::information( this,
309
tr( "OGR Layer Converter" ),
310
tr( "Failed to translate layer '%1'" ).arg( srcLayer ) );
317
void Dialog::on_buttonBox_rejected()
322
void Dialog::on_radioSrcFile_toggled( bool checked )
326
unsigned char const& type = mSrcFormat.type();
327
Q_ASSERT( isFormatType( type, Format::eFile ) );
329
inputSrcDataset->clear();
330
setButtonState( buttonSelectSrc, isFormatType( type, Format::eProtocol ) );
334
void Dialog::on_radioSrcDirectory_toggled( bool checked )
338
unsigned char const& type = mSrcFormat.type();
339
Q_ASSERT( isFormatType( type, Format::eDirectory ) );
341
inputSrcDataset->clear();
342
setButtonState( buttonSelectSrc, isFormatType( type, Format::eProtocol ) );
346
void Dialog::on_radioSrcProtocol_toggled( bool checked )
350
unsigned char const& type = mSrcFormat.type();
351
Q_ASSERT( isFormatType( type, Format::eProtocol ) );
353
inputSrcDataset->setText( mSrcFormat.protocol() );
354
setButtonState( buttonSelectSrc, isFormatType( type, Format::eProtocol ) );
358
void Dialog::on_comboSrcFormats_currentIndexChanged( int index )
360
// Select source data format
361
QString frmtCode = comboSrcFormats->currentText();
362
mSrcFormat = mFrmts.find( frmtCode );
367
void Dialog::on_comboDstFormats_currentIndexChanged( int index )
369
// Select destination data format
370
QString frmtCode = comboDstFormats->currentText();
371
mDstFormat = mFrmts.find( frmtCode );
376
void Dialog::on_buttonSelectSrc_clicked()
381
if ( radioSrcFile->isChecked() )
385
else if ( radioSrcDirectory->isChecked() )
387
src = openDirectory();
389
else if ( radioSrcProtocol->isChecked() )
391
src = inputSrcDataset->text();
395
Q_ASSERT( !"SHOULD NEVER GET HERE" );
398
inputSrcDataset->setText( src );
400
if ( !src.isEmpty() )
402
populateLayers( src );
406
void Dialog::on_buttonSelectDst_clicked()
412
unsigned char const& type = mDstFormat.type();
413
if ( isFormatType( type, Format::eProtocol ) )
415
dst = inputDstDataset->text();
417
if ( testConnection( dst ) )
419
msg = tr( "Successfully connected to: '%1'" ).arg( dst );
423
msg = tr( "Could not establish connection to: '%1'" ).arg( dst );
426
QMessageBox::information( this, tr( "OGR Converter" ), msg, QMessageBox::Close );
428
else if ( isFormatType( type, Format::eDirectory ) )
430
dst = openDirectory();
432
else if ( isFormatType( type, Format::eFile ) )
434
dst = QFileDialog::getSaveFileName( this,
435
tr( "Choose a file name to save to" ),
436
"output", tr( "OGR File Data Source (*.*)" ) );
440
Q_ASSERT( !"SHOULD NEVER GET HERE" );
443
inputDstDataset->setText( dst );