From e333f7e3e87d8b9335113c3a338a43e22405d1ba Mon Sep 17 00:00:00 2001 From: Etienne Trimaille Date: Wed, 28 Oct 2015 12:32:10 +0700 Subject: [PATCH] #10 add CSV export --- gui/export/__init__.py | 1 + gui/export/csv.py | 96 ++++++++++++++ gui/main_window.py | 2 + ui/export/__init__.py | 1 + ui/export/export_csv.py | 150 ++++++++++++++++++++++ ui/export/export_csv.ui | 273 ++++++++++++++++++++++++++++++++++++++++ 6 files changed, 523 insertions(+) create mode 100644 gui/export/__init__.py create mode 100644 gui/export/csv.py create mode 100644 ui/export/__init__.py create mode 100644 ui/export/export_csv.py create mode 100644 ui/export/export_csv.ui diff --git a/gui/export/__init__.py b/gui/export/__init__.py new file mode 100644 index 0000000..2afaff1 --- /dev/null +++ b/gui/export/__init__.py @@ -0,0 +1 @@ +__author__ = 'etienne' diff --git a/gui/export/csv.py b/gui/export/csv.py new file mode 100644 index 0000000..0d061ab --- /dev/null +++ b/gui/export/csv.py @@ -0,0 +1,96 @@ +# -*- coding: utf-8 -*- +""" +/*************************************************************************** + + GeoHealth + A QGIS plugin + + ------------------- + begin : 2014-08-20 + copyright : (C) 2014 by Etienne Trimaille + email : etienne@trimaille.eu + ***************************************************************************/ + +/*************************************************************************** + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + ***************************************************************************/ +""" + +import codecs +from PyQt4.QtGui import QWidget, QDialogButtonBox, QFileDialog + +from qgis.utils import iface +from qgis.core import QgsMapLayer + +from GeoHealth.ui.export.export_csv import Ui_Form +from GeoHealth.core.tools import trans + + +class CsvExport(QWidget, Ui_Form): + + def __init__(self, parent=None): + self.parent = parent + super(CsvExport, self).__init__() + self.setupUi(self) + self.fill_combobox_layer() + + self.buttonBox.button(QDialogButtonBox.Save).clicked.connect( + self.save_csv) + # noinspection PyUnresolvedReferences + self.bt_browse.clicked.connect(self.open_file_browser) + + self.delimiters = { + 'tab': ' ', + 'pipe': '|', + 'comma': ',', + 'semicolon': ';' + } + + def fill_combobox_layer(self): + """Fill combobox about layers.""" + self.cbx_layer.clear() + + for layer in iface.legendInterface().layers(): + if layer.type() == QgsMapLayer.VectorLayer: + self.cbx_layer.addItem(layer.name(), layer) + + def open_file_browser(self): + # noinspection PyArgumentList + output_file = QFileDialog.getSaveFileNameAndFilter( + parent=self.parent, + caption=trans('Export as CSV'), + filter='CSV (*.csv)') + self.le_output.setText(output_file[0]) + + def save_csv(self): + path = self.le_output.text() + index = self.cbx_layer.currentIndex() + layer = self.cbx_layer.itemData(index) + + if self.tab_delimiter.isChecked(): + delimiter = self.delimiters['tab'] + elif self.pipe_delimiter.isChecked(): + delimiter = self.delimiters['pipe'] + elif self.semicolon_delimiter.isChecked(): + delimiter = self.delimiters['semicolon'] + else: + delimiter = self.delimiters['comma'] + + csv_file = codecs.open(path, 'w', 'utf-8') + # csv_file = open(path, 'wb') + + provider = layer.dataProvider() + fields = provider.fieldNameMap() + header = u'%s\n' % delimiter.join(fields) + csv_file.write(header) + + for feature in layer.getFeatures(): + line = u'%s\n' % delimiter.join(feature.attributes()) + csv_file.write(line) + + csv_file.close() diff --git a/gui/main_window.py b/gui/main_window.py index 182a8e4..d81ff9d 100644 --- a/gui/main_window.py +++ b/gui/main_window.py @@ -30,6 +30,7 @@ from GeoHealth.gui.analysis.incidence_dialog import IncidenceDialog from GeoHealth.gui.analysis.density_dialog import DensityDialog from GeoHealth.gui.analysis.histogram_dialog import HistogramDialog +from GeoHealth.gui.export.csv import CsvExport from GeoHealth.gui.about import AboutWidget from GeoHealth.gui.wip import WipWidget @@ -54,6 +55,7 @@ def __init__(self, parent=None): 22: IncidenceDialog(), 23: DensityDialog(), 24: HistogramDialog(), + 31: CsvExport(), 41: AboutWidget(), 100: WipWidget() } diff --git a/ui/export/__init__.py b/ui/export/__init__.py new file mode 100644 index 0000000..2afaff1 --- /dev/null +++ b/ui/export/__init__.py @@ -0,0 +1 @@ +__author__ = 'etienne' diff --git a/ui/export/export_csv.py b/ui/export/export_csv.py new file mode 100644 index 0000000..f319427 --- /dev/null +++ b/ui/export/export_csv.py @@ -0,0 +1,150 @@ +# -*- coding: utf-8 -*- + +# Form implementation generated from reading ui file 'export_csv.ui' +# +# Created: Wed Oct 28 11:11:25 2015 +# by: PyQt4 UI code generator 4.10.4 +# +# WARNING! All changes made in this file will be lost! + +from PyQt4 import QtCore, QtGui + +try: + _fromUtf8 = QtCore.QString.fromUtf8 +except AttributeError: + def _fromUtf8(s): + return s + +try: + _encoding = QtGui.QApplication.UnicodeUTF8 + def _translate(context, text, disambig): + return QtGui.QApplication.translate(context, text, disambig, _encoding) +except AttributeError: + def _translate(context, text, disambig): + return QtGui.QApplication.translate(context, text, disambig) + +class Ui_Form(object): + def setupUi(self, Form): + Form.setObjectName(_fromUtf8("Form")) + Form.resize(689, 538) + self.verticalLayout = QtGui.QVBoxLayout(Form) + self.verticalLayout.setObjectName(_fromUtf8("verticalLayout")) + self.formLayout = QtGui.QFormLayout() + self.formLayout.setFieldGrowthPolicy(QtGui.QFormLayout.ExpandingFieldsGrow) + self.formLayout.setObjectName(_fromUtf8("formLayout")) + self.label_3 = QtGui.QLabel(Form) + self.label_3.setObjectName(_fromUtf8("label_3")) + self.formLayout.setWidget(0, QtGui.QFormLayout.LabelRole, self.label_3) + self.cbx_layer = QtGui.QComboBox(Form) + sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.MinimumExpanding, QtGui.QSizePolicy.Fixed) + sizePolicy.setHorizontalStretch(0) + sizePolicy.setVerticalStretch(0) + sizePolicy.setHeightForWidth(self.cbx_layer.sizePolicy().hasHeightForWidth()) + self.cbx_layer.setSizePolicy(sizePolicy) + self.cbx_layer.setObjectName(_fromUtf8("cbx_layer")) + self.formLayout.setWidget(0, QtGui.QFormLayout.FieldRole, self.cbx_layer) + self.label = QtGui.QLabel(Form) + self.label.setObjectName(_fromUtf8("label")) + self.formLayout.setWidget(1, QtGui.QFormLayout.LabelRole, self.label) + self.horizontalLayout_2 = QtGui.QHBoxLayout() + self.horizontalLayout_2.setObjectName(_fromUtf8("horizontalLayout_2")) + self.tab_delimiter = QtGui.QRadioButton(Form) + self.tab_delimiter.setChecked(True) + self.tab_delimiter.setObjectName(_fromUtf8("tab_delimiter")) + self.horizontalLayout_2.addWidget(self.tab_delimiter) + spacerItem = QtGui.QSpacerItem(40, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum) + self.horizontalLayout_2.addItem(spacerItem) + self.comma_delimiter = QtGui.QRadioButton(Form) + self.comma_delimiter.setObjectName(_fromUtf8("comma_delimiter")) + self.horizontalLayout_2.addWidget(self.comma_delimiter) + spacerItem1 = QtGui.QSpacerItem(40, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum) + self.horizontalLayout_2.addItem(spacerItem1) + self.pipe_delimiter = QtGui.QRadioButton(Form) + self.pipe_delimiter.setObjectName(_fromUtf8("pipe_delimiter")) + self.horizontalLayout_2.addWidget(self.pipe_delimiter) + spacerItem2 = QtGui.QSpacerItem(40, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum) + self.horizontalLayout_2.addItem(spacerItem2) + self.semicolon_delimiter = QtGui.QRadioButton(Form) + self.semicolon_delimiter.setObjectName(_fromUtf8("semicolon_delimiter")) + self.horizontalLayout_2.addWidget(self.semicolon_delimiter) + self.formLayout.setLayout(1, QtGui.QFormLayout.FieldRole, self.horizontalLayout_2) + self.label_2 = QtGui.QLabel(Form) + self.label_2.setObjectName(_fromUtf8("label_2")) + self.formLayout.setWidget(2, QtGui.QFormLayout.LabelRole, self.label_2) + self.horizontalLayout_3 = QtGui.QHBoxLayout() + self.horizontalLayout_3.setObjectName(_fromUtf8("horizontalLayout_3")) + self.le_output = QtGui.QLineEdit(Form) + self.le_output.setInputMask(_fromUtf8("")) + self.le_output.setText(_fromUtf8("")) + self.le_output.setReadOnly(True) + self.le_output.setPlaceholderText(_fromUtf8("")) + self.le_output.setObjectName(_fromUtf8("le_output")) + self.horizontalLayout_3.addWidget(self.le_output) + self.bt_browse = QtGui.QPushButton(Form) + self.bt_browse.setObjectName(_fromUtf8("bt_browse")) + self.horizontalLayout_3.addWidget(self.bt_browse) + self.formLayout.setLayout(2, QtGui.QFormLayout.FieldRole, self.horizontalLayout_3) + self.mGroupBox = gui.QgsCollapsibleGroupBox(Form) + self.mGroupBox.setEnabled(False) + sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.MinimumExpanding, QtGui.QSizePolicy.Preferred) + sizePolicy.setHorizontalStretch(0) + sizePolicy.setVerticalStretch(0) + sizePolicy.setHeightForWidth(self.mGroupBox.sizePolicy().hasHeightForWidth()) + self.mGroupBox.setSizePolicy(sizePolicy) + self.mGroupBox.setCheckable(True) + self.mGroupBox.setChecked(False) + self.mGroupBox.setCollapsed(True) + self.mGroupBox.setSaveCollapsedState(False) + self.mGroupBox.setObjectName(_fromUtf8("mGroupBox")) + self.formLayoutWidget_2 = QtGui.QWidget(self.mGroupBox) + self.formLayoutWidget_2.setGeometry(QtCore.QRect(7, 36, 253, 50)) + self.formLayoutWidget_2.setObjectName(_fromUtf8("formLayoutWidget_2")) + self.formLayout_2 = QtGui.QFormLayout(self.formLayoutWidget_2) + self.formLayout_2.setMargin(0) + self.formLayout_2.setObjectName(_fromUtf8("formLayout_2")) + self.label_4 = QtGui.QLabel(self.formLayoutWidget_2) + self.label_4.setObjectName(_fromUtf8("label_4")) + self.formLayout_2.setWidget(0, QtGui.QFormLayout.LabelRole, self.label_4) + self.horizontalLayout_4 = QtGui.QHBoxLayout() + self.horizontalLayout_4.setObjectName(_fromUtf8("horizontalLayout_4")) + self.radioButton = QtGui.QRadioButton(self.formLayoutWidget_2) + self.radioButton.setObjectName(_fromUtf8("radioButton")) + self.horizontalLayout_4.addWidget(self.radioButton) + spacerItem3 = QtGui.QSpacerItem(40, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum) + self.horizontalLayout_4.addItem(spacerItem3) + self.radioButton_2 = QtGui.QRadioButton(self.formLayoutWidget_2) + self.radioButton_2.setObjectName(_fromUtf8("radioButton_2")) + self.horizontalLayout_4.addWidget(self.radioButton_2) + self.formLayout_2.setLayout(0, QtGui.QFormLayout.FieldRole, self.horizontalLayout_4) + self.label_5 = QtGui.QLabel(self.formLayoutWidget_2) + self.label_5.setObjectName(_fromUtf8("label_5")) + self.formLayout_2.setWidget(1, QtGui.QFormLayout.LabelRole, self.label_5) + self.formLayout.setWidget(3, QtGui.QFormLayout.FieldRole, self.mGroupBox) + self.verticalLayout.addLayout(self.formLayout) + self.buttonBox = QtGui.QDialogButtonBox(Form) + self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Save) + self.buttonBox.setObjectName(_fromUtf8("buttonBox")) + self.verticalLayout.addWidget(self.buttonBox) + spacerItem4 = QtGui.QSpacerItem(20, 40, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding) + self.verticalLayout.addItem(spacerItem4) + + self.retranslateUi(Form) + QtCore.QMetaObject.connectSlotsByName(Form) + + def retranslateUi(self, Form): + Form.setWindowTitle(_translate("Form", "Form", None)) + self.label_3.setText(_translate("Form", "Layer", None)) + self.label.setText(_translate("Form", "Delimiter", None)) + self.tab_delimiter.setText(_translate("Form", "tab", None)) + self.comma_delimiter.setText(_translate("Form", "comma", None)) + self.pipe_delimiter.setText(_translate("Form", "pipe", None)) + self.semicolon_delimiter.setText(_translate("Form", "semicolon", None)) + self.label_2.setText(_translate("Form", "Output", None)) + self.bt_browse.setText(_translate("Form", "Browse", None)) + self.mGroupBox.setTitle(_translate("Form", "Export geometry", None)) + self.label_4.setText(_translate("Form", "Format", None)) + self.radioButton.setText(_translate("Form", "As XY", None)) + self.radioButton_2.setText(_translate("Form", "As YX", None)) + self.label_5.setText(_translate("Form", "CRS", None)) + +from qgis import gui diff --git a/ui/export/export_csv.ui b/ui/export/export_csv.ui new file mode 100644 index 0000000..77c0d94 --- /dev/null +++ b/ui/export/export_csv.ui @@ -0,0 +1,273 @@ + + + Form + + + + 0 + 0 + 689 + 538 + + + + Form + + + + + + QFormLayout::ExpandingFieldsGrow + + + + + Layer + + + + + + + + 0 + 0 + + + + + + + + Delimiter + + + + + + + + + tab + + + true + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + comma + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + pipe + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + semicolon + + + + + + + + + Output + + + + + + + + + + + + + + + true + + + + + + + + + + Browse + + + + + + + + + false + + + + 0 + 0 + + + + Export geometry + + + true + + + false + + + true + + + false + + + + + 7 + 36 + 253 + 50 + + + + + + + Format + + + + + + + + + As XY + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + As YX + + + + + + + + + CRS + + + + + + + + + + + + + QDialogButtonBox::Cancel|QDialogButtonBox::Save + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + + QgsCollapsibleGroupBox + QGroupBox +
qgscollapsiblegroupbox.h
+ 1 +
+
+ + +