Skip to content

Commit

Permalink
#10 add CSV export
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustry committed Oct 28, 2015
1 parent 2df9e77 commit e333f7e
Show file tree
Hide file tree
Showing 6 changed files with 523 additions and 0 deletions.
1 change: 1 addition & 0 deletions gui/export/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__author__ = 'etienne'
96 changes: 96 additions & 0 deletions gui/export/csv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# -*- coding: utf-8 -*-
"""
/***************************************************************************
GeoHealth
A QGIS plugin
-------------------
begin : 2014-08-20
copyright : (C) 2014 by Etienne Trimaille
email : [email protected]
***************************************************************************/
/***************************************************************************
* *
* 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()
2 changes: 2 additions & 0 deletions gui/main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -54,6 +55,7 @@ def __init__(self, parent=None):
22: IncidenceDialog(),
23: DensityDialog(),
24: HistogramDialog(),
31: CsvExport(),
41: AboutWidget(),
100: WipWidget()
}
Expand Down
1 change: 1 addition & 0 deletions ui/export/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__author__ = 'etienne'
150 changes: 150 additions & 0 deletions ui/export/export_csv.py
Original file line number Diff line number Diff line change
@@ -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
Loading

0 comments on commit e333f7e

Please sign in to comment.