Skip to content

Commit 9921126

Browse files
committed
[DBManager] allow to export table data to supported ogr file formats other than shapefile (fix #12111)
1 parent 2a1cacd commit 9921126

File tree

2 files changed

+75
-41
lines changed

2 files changed

+75
-41
lines changed

python/plugins/db_manager/dlg_export_vector.py

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,13 @@ def __init__(self, inLayer, inDb, parent=None):
3838
self.db = inDb
3939
self.setupUi(self)
4040

41+
vectorFilterName = "lastVectorFileFilter" # "lastRasterFileFilter"
42+
self.lastUsedVectorFilterSettingsKey = u"/UI/{0}".format(vectorFilterName)
43+
self.lastUsedVectorDirSettingsKey = u"/UI/{0}Dir".format(vectorFilterName)
44+
4145
# update UI
4246
self.setupWorkingMode()
47+
self.populateFileFilters()
4348
self.populateEncodings()
4449

4550
def setupWorkingMode(self):
@@ -61,18 +66,21 @@ def checkSupports(self):
6166
# self.chkSpatialIndex.setEnabled(allowSpatial and hasGeomType)
6267

6368
def chooseOutputFile(self):
64-
# get last used dir and format
69+
# get last used dir
6570
settings = QSettings()
66-
lastDir = settings.value("/db_manager/lastUsedDir", "")
71+
lastUsedDir = settings.value( self.lastUsedVectorDirSettingsKey, "." );
72+
73+
# get selected filter
74+
selectedFilter = self.cboFileFormat.itemData(self.cboFileFormat.currentIndex())
75+
6776
# ask for a filename
68-
filename = QFileDialog.getSaveFileName(self, self.tr("Choose where to save the file"), lastDir,
69-
self.tr("Shapefiles") + " (*.shp)")
77+
filename = QFileDialog.getSaveFileName(self, self.tr("Choose where to save the file"), lastUsedDir,
78+
selectedFilter)
7079
if filename == "":
7180
return
72-
if filename[-4:] != ".shp":
73-
filename += ".shp"
74-
# store the last used dir and format
75-
settings.setValue("/db_manager/lastUsedDir", QFileInfo(filename).filePath())
81+
82+
# store the last used dir
83+
settings.setValue( self.lastUsedVectorDirSettingsKey, QFileInfo(filename).filePath() )
7684

7785
self.editOutputFile.setText(filename)
7886

@@ -88,10 +96,25 @@ def populateEncodings(self):
8896
idx = 0
8997
self.cboEncoding.setCurrentIndex(idx)
9098

99+
def populateFileFilters(self):
100+
# populate the combo with supported vector file formats
101+
for name, filt in qgis.core.QgsVectorFileWriter.ogrDriverList().iteritems():
102+
self.cboFileFormat.addItem(name, filt)
103+
104+
# set the last used filter
105+
settings = QSettings()
106+
filt = settings.value( self.lastUsedVectorFilterSettingsKey, "ESRI Shapefile" );
107+
108+
idx = self.cboFileFormat.findText(filt)
109+
if idx < 0:
110+
idx = 0
111+
self.cboFileFormat.setCurrentIndex(idx)
112+
113+
91114
def accept(self):
92115
# sanity checks
93116
if self.editOutputFile.text() == "":
94-
QMessageBox.information(self, self.tr("Export to file"), self.tr("Output table name is required"))
117+
QMessageBox.information(self, self.tr("Export to file"), self.tr("Output file name is required"))
95118
return
96119

97120
if self.chkSourceSrid.isEnabled() and self.chkSourceSrid.isChecked():
@@ -117,12 +140,13 @@ def accept(self):
117140
try:
118141
uri = self.editOutputFile.text()
119142
providerName = "ogr"
120-
driverName = "ESRI Shapefile"
121143

122144
options = {}
123145

124146
# set the OGR driver will be used
147+
driverName = self.cboFileFormat.itemData(self.cboFileFormat.currentIndex())
125148
options['driverName'] = driverName
149+
126150
# set the output file encoding
127151
if self.chkEncoding.isEnabled() and self.chkEncoding.isChecked():
128152
enc = self.cboEncoding.currentText()

python/plugins/db_manager/ui/DlgExportVector.ui

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,46 +7,59 @@
77
<x>0</x>
88
<y>0</y>
99
<width>514</width>
10-
<height>185</height>
10+
<height>253</height>
1111
</rect>
1212
</property>
1313
<property name="windowTitle">
1414
<string>Export to vector file</string>
1515
</property>
1616
<layout class="QGridLayout" name="gridLayout_2">
17-
<item row="0" column="0">
18-
<widget class="QLabel" name="label_5">
19-
<property name="sizePolicy">
20-
<sizepolicy hsizetype="Fixed" vsizetype="Preferred">
21-
<horstretch>0</horstretch>
22-
<verstretch>0</verstretch>
23-
</sizepolicy>
24-
</property>
17+
<item row="1" column="1">
18+
<widget class="QLineEdit" name="editOutputFile">
2519
<property name="text">
26-
<string>Output file</string>
20+
<string notr="true"/>
2721
</property>
2822
</widget>
2923
</item>
30-
<item row="0" column="1">
31-
<widget class="QLineEdit" name="editOutputFile">
32-
<property name="text">
33-
<string notr="true"/>
24+
<item row="3" column="0" colspan="3">
25+
<widget class="QDialogButtonBox" name="buttonBox">
26+
<property name="orientation">
27+
<enum>Qt::Horizontal</enum>
28+
</property>
29+
<property name="standardButtons">
30+
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
3431
</property>
3532
</widget>
3633
</item>
37-
<item row="0" column="2">
38-
<widget class="QToolButton" name="btnChooseOutputFile">
34+
<item row="0" column="1" colspan="2">
35+
<widget class="QComboBox" name="cboFileFormat"/>
36+
</item>
37+
<item row="1" column="0">
38+
<widget class="QLabel" name="label_5">
39+
<property name="sizePolicy">
40+
<sizepolicy hsizetype="Fixed" vsizetype="Preferred">
41+
<horstretch>0</horstretch>
42+
<verstretch>0</verstretch>
43+
</sizepolicy>
44+
</property>
3945
<property name="text">
40-
<string>...</string>
46+
<string>Save as</string>
4147
</property>
4248
</widget>
4349
</item>
44-
<item row="1" column="0" colspan="3">
50+
<item row="2" column="0" colspan="3">
4551
<widget class="QGroupBox" name="groupBox_2">
4652
<property name="title">
4753
<string>Options</string>
4854
</property>
4955
<layout class="QGridLayout" name="gridLayout">
56+
<item row="2" column="0" colspan="2">
57+
<widget class="QCheckBox" name="chkDropTable">
58+
<property name="text">
59+
<string>Drop existing one</string>
60+
</property>
61+
</widget>
62+
</item>
5063
<item row="0" column="0" colspan="2">
5164
<layout class="QHBoxLayout" name="horizontalLayout">
5265
<item>
@@ -127,23 +140,20 @@
127140
</property>
128141
</widget>
129142
</item>
130-
<item row="2" column="0" colspan="2">
131-
<widget class="QCheckBox" name="chkDropTable">
132-
<property name="text">
133-
<string>Drop existing one</string>
134-
</property>
135-
</widget>
136-
</item>
137143
</layout>
138144
</widget>
139145
</item>
140-
<item row="2" column="0" colspan="3">
141-
<widget class="QDialogButtonBox" name="buttonBox">
142-
<property name="orientation">
143-
<enum>Qt::Horizontal</enum>
146+
<item row="1" column="2">
147+
<widget class="QToolButton" name="btnChooseOutputFile">
148+
<property name="text">
149+
<string>...</string>
144150
</property>
145-
<property name="standardButtons">
146-
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
151+
</widget>
152+
</item>
153+
<item row="0" column="0">
154+
<widget class="QLabel" name="label">
155+
<property name="text">
156+
<string>Format</string>
147157
</property>
148158
</widget>
149159
</item>

0 commit comments

Comments
 (0)