-
Notifications
You must be signed in to change notification settings - Fork 0
/
csv2vector_controller.py
191 lines (141 loc) · 7.89 KB
/
csv2vector_controller.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# -*- coding: utf-8 -*-
import csv, os, sys
from PyQt4 import QtCore, QtGui
for p in sys.path:
print p
from qgis.core import *
from qgis.gui import *
from csv2vector_dialog_view import Ui_Dialog
from csv2vector_model import Csv2vectorModel, recalculateCoordinates
class Csv2vectorDialog( QtGui.QDialog, Ui_Dialog ):
def __init__( self ):
QtGui.QDialog.__init__( self )
self.setupUi( self )
self.__model = Csv2vectorModel();
self.__model.subscribToModelChange(self.onUpdateModel)
QtCore.QObject.connect( self.selectCSVFileButton, QtCore.SIGNAL( "clicked()" ), self.selectCSVFile )
QtCore.QObject.connect( self.selectSHPFileButton, QtCore.SIGNAL( "clicked()" ), self.selectSHPFile )
QtCore.QObject.connect(self.delimiterTabRadioButton, QtCore.SIGNAL( "clicked()" ), self.checkTabDelimiter )
QtCore.QObject.connect(self.delimiterSpaceRadioButton, QtCore.SIGNAL( "clicked()" ), self.checkSpaceDelimiter )
QtCore.QObject.connect(self.delimiterCommaRadioButton, QtCore.SIGNAL( "clicked()" ), self.checkCommaDelimiter )
QtCore.QObject.connect(self.delimiterSemicolonRadioButton, QtCore.SIGNAL( "clicked()" ), self.checkSemicolonDelimiter )
QtCore.QObject.connect(self.delimiterColonRadioButton, QtCore.SIGNAL( "clicked()" ), self.checkColonDelimiter )
def checkTabDelimiter(self):
self.__model.csvDelimiter = "\t"
def checkSpaceDelimiter(self):
self.__model.csvDelimiter = " "
def checkCommaDelimiter(self):
self.__model.csvDelimiter = ","
def checkSemicolonDelimiter(self):
self.__model.csvDelimiter = ";"
def checkColonDelimiter(self):
self.__model.csvDelimiter = ":"
def accept( self ):
if self.__model.shpFileInfo[0] == None:
QtGui.QMessageBox.warning(self,
self.tr("File is not specified."),
self.tr("Please choose shape.")
)
return
outFile = QtCore.QFile( self.__model.shpFileInfo[0] )
if outFile.exists():
if not QgsVectorFileWriter.deleteShapeFile( self.__model.shpFileInfo[0] ):
QtGui.QMessageBox.warning( self, self.tr("Delete error"), self.tr("Can't delete file: " + self.__model.shpFileInfo[0] ) )
return
xAttrIndex = self.attrXCoordComboBox.currentIndex()
yAttrIndex = self.attrYCoordComboBox.currentIndex()
azAttrIndex = self.attrAzCoordComboBox.currentIndex()
distAttrIndex = self.attrDistCoordComboBox.currentIndex()
geoDataAttributes = self.__model.getGeoDataAttributes(xAttrIndex, yAttrIndex, azAttrIndex, distAttrIndex)
shapeFields = QgsFields()
for attrName in geoDataAttributes.values():
shapeFields.append(QgsField(attrName, QtCore.QVariant.String, u"", 255))
crs = QgsCoordinateReferenceSystem( 4326 )
shapeFileWriter = QgsVectorFileWriter( self.__model.shpFileInfo[0], self.__model.shpFileInfo[1], shapeFields, QGis.WKBPoint, crs )
skipAllBadPointFlag = False
for (coord, displacement, attributes) in self.__model.getGeoDataIterator(xAttrIndex, yAttrIndex, azAttrIndex, distAttrIndex):
try:
(x,y) = recalculateCoordinates(
map(lambda x: float(x), coord),
map(lambda x: float(x), displacement))
feature = QgsFeature()
feature.initAttributes(geoDataAttributes.__len__())
geometry = QgsGeometry()
point = QgsPoint( x, y )
feature.setGeometry( geometry.fromPoint( point ) )
for attrCounter in range(geoDataAttributes.__len__()):
feature.setAttribute(attrCounter, attributes[attrCounter])
shapeFileWriter.addFeature( feature )
except ValueError as ex:
if skipAllBadPointFlag == False:
msgBox = QtGui.QMessageBox(self)
msgBox.setWindowTitle(self.tr("Wrong attribute type"))
msgBox.setText("Point has incorrect attribute type. Poit's attributes: " + str(coord) + str(displacement))
ignorethisButton = msgBox.addButton(self.tr("Ignore this"), QtGui.QMessageBox.ActionRole)
ignoreallButton = msgBox.addButton(self.tr("Ignore all"), QtGui.QMessageBox.ActionRole)
msgBox.exec_()
if (msgBox.clickedButton() == ignorethisButton):
pass
elif (msgBox.clickedButton() == ignoreallButton):
skipAllBadPointFlag = True
else:
pass
del shapeFileWriter
newLayer = QgsVectorLayer(self.__model.shpFileInfo[0], QtCore.QFileInfo( self.__model.shpFileInfo[0] ).baseName(), "ogr" )
QgsMapLayerRegistry.instance().addMapLayer( newLayer )
self.close()
def selectCSVFile(self):
csvFilter = u"CSV files (*.csv *.CSV)";
fileDialog = QgsEncodingFileDialog( self, self.tr( "Select input csv file" ), u"", csvFilter, u"")
fileDialog.setDefaultSuffix( u"csv" )
fileDialog.setFileMode( QtGui.QFileDialog.AnyFile )
if not fileDialog.exec_() == QtGui.QDialog.Accepted:
return
self.__model.csvFilePath = unicode( QtCore.QFileInfo( fileDialog.selectedFiles()[0] ).absoluteFilePath() )
self.groupBox.setEnabled(True)
self.groupBox_2.setEnabled(True)
self.groupBox_3.setEnabled(True)
def selectSHPFile(self):
shpFilter = u"Shapefiles (*.shp *.SHP)";
fileDialog = QgsEncodingFileDialog( self, self.tr( "Select output shapefile" ), u"", shpFilter, u"" )
fileDialog.setDefaultSuffix( u"shp" )
fileDialog.setFileMode( QtGui.QFileDialog.AnyFile )
fileDialog.setAcceptMode( QtGui.QFileDialog.AcceptSave )
fileDialog.setConfirmOverwrite( True )
if not fileDialog.exec_() == QtGui.QDialog.Accepted:
return
self.__model.shpFileInfo = (fileDialog.selectedFiles()[0], fileDialog.encoding())
self.shpFileEdit.setText( self.__model.shpFileInfo[0] )
def onUpdateModel(self):
self.csvFileEdit.setText(self.__model.csvFilePath)
if (self.__model.csvDelimiter == "\t"):
self.delimiterTabRadioButton.setChecked(True)
elif (self.__model.csvDelimiter == " "):
self.delimiterSpaceRadioButton.setChecked(True)
elif (self.__model.csvDelimiter == ","):
self.delimiterCommaRadioButton.setChecked(True)
elif (self.__model.csvDelimiter == ";"):
self.delimiterSemicolonRadioButton.setChecked(True)
elif (self.__model.csvDelimiter == ":"):
self.delimiterColonRadioButton.setChecked(True)
self.tableWidget.clear()
self.attrXCoordComboBox.clear()
self.attrYCoordComboBox.clear()
self.attrAzCoordComboBox.clear()
self.attrDistCoordComboBox.clear()
csvAttrCount = self.__model.csvHeader.__len__()
self.tableWidget.setColumnCount(csvAttrCount)
listOfLables = [lable for lable in self.__model.csvHeader]
self.tableWidget.setHorizontalHeaderLabels ( listOfLables)
self.attrXCoordComboBox.insertItems ( 0, listOfLables )
self.attrYCoordComboBox.insertItems ( 0, listOfLables )
self.attrAzCoordComboBox.insertItems ( 0, listOfLables )
self.attrDistCoordComboBox.insertItems ( 0, listOfLables )
numOfDataRows = 2
self.tableWidget.setRowCount(numOfDataRows)
for rowCounter in range(numOfDataRows):
dataRow = self.__model.csvDataIterator.next()
for colCounter in range(self.__model.csvHeader.__len__()):
self.tableWidget.setItem(rowCounter, colCounter, QtGui.QTableWidgetItem(dataRow[colCounter]) )
if __name__ == "__main__":
print "Hello"