-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathNewProject.py
773 lines (688 loc) · 31.5 KB
/
NewProject.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
# -*- coding: utf-8 -*-
'''
Video Uav Tracker v 2.0
Replay a video in sync with a gps track displayed on the map.
-------------------
copyright : (C) 2017 by Salvatore Agosta
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.
INSTRUCTION:
Syncing:
- Create new project
- Select video and .gpx track (1 trkpt per second)
- Identify first couple Frame/GpsTime and select it.
- Push Synchronize
- Push Start
Replay:
- Move on map
- Create associated DB shapefile
- Add POI with associated video frame saved
- Extract frames with associated coordinates for rapid photogrammetry use
'''
from qgis.core import *
from PyQt5 import QtWidgets
from PyQt5.QtCore import QUrl , Qt , QVariant , QRegExp
from PyQt5.QtGui import QRegExpValidator
from PyQt5.QtWidgets import QFileDialog , QStyle, QDialog, QMessageBox , QTableWidget, QTableWidgetItem
from PyQt5.QtMultimedia import QMediaPlayer, QMediaContent
from vut_newproject import Ui_NewProject
from tableManagerUi import Ui_Dialog
from tableManagerUiRename import Ui_Rename
from tableManagerUiClone import Ui_Clone
from tableManagerUiInsert import Ui_Insert
import sys
import os
PACKAGE_PARENT = '..'
SCRIPT_DIR = os.path.dirname(os.path.realpath(os.path.join(os.getcwd(), os.path.expanduser(__file__))))
sys.path.append(os.path.normpath(os.path.join(SCRIPT_DIR, PACKAGE_PARENT)))
import tempfile
import time
from geographiclib.geodesic import Geodesic
from xml.dom.minidom import parse
class NewProject(QtWidgets.QWidget, Ui_NewProject):
def __init__(self,projectfile,MainWidget):
QtWidgets.QWidget.__init__(self)
self.setupUi(self)
self.setWindowFlags(Qt.WindowStaysOnTopHint)
self.Main = MainWidget
self.iface = self.Main.iface
self.muteButton.setIcon(
self.style().standardIcon(QStyle.SP_MediaVolume))
self.replayPlay_pushButton.setIcon(
self.style().standardIcon(QStyle.SP_MediaPlay))
if projectfile.split('.')[-1] =="vgp":
self.projectfile = projectfile
else:
self.projectfile = projectfile +'.vgp'
self.videofile = None
self.GPXfile = None
self.GPXList = None
self.fps = None
self.RealFps = None
self.DB = None
self.player = QMediaPlayer()
self.player.setVideoOutput(self.video_frame_2)
self.player.durationChanged.connect(self.durationChanged)
self.player.positionChanged.connect(self.positionChanged)
self.player.stateChanged.connect(self.mediaStateChanged)
self.toolButton_3.clicked.connect(self.ManageDB)
self.pushButton_2.clicked.connect(self.Synchronize)
self.pushButton.clicked.connect(self.SelectVideoGPX)
self.replayPlay_pushButton.clicked.connect(self.PlayPause)
self.muteButton.clicked.connect(self.MuteUnmute)
self.horizontalSlider.sliderMoved.connect(self.setPosition)
self.toolButton.clicked.connect(self.SkipBackward)
self.toolButton_2.clicked.connect(self.SkipForward)
self.SkipBacktoolButton_7.clicked.connect(self.BackwardFrame)
self.SkipFortoolButton_8.clicked.connect(self.ForwardFrame)
def closeEvent(self, *args, **kwargs):
self.player.stop()
return QtWidgets.QWidget.closeEvent(self, *args, **kwargs)
def mediaStateChanged(self, state):
if self.player.state() == QMediaPlayer.PlayingState:
self.replayPlay_pushButton.setIcon(
self.style().standardIcon(QStyle.SP_MediaPause))
else:
self.replayPlay_pushButton.setIcon(
self.style().standardIcon(QStyle.SP_MediaPlay))
def Synchronize(self):
TimeItem = self.comboBox.currentIndex()
duration = self.player.duration()
position = self.player.position()
VideoPartLen = round((duration-position) / 1000)
GpxPartition = self.GPXList[TimeItem:VideoPartLen+TimeItem]
outputFile = open(self.projectfile ,'w')
if self.DB == None:
outputFile.write('VideoGis Project v0.1 DO NOT MODIFY'+
'\nVideo file location = ' +self.videofile+
'\nVideo start at msecond: '+
str(self.player.position())+
' #fps = '+str(self.RealFps)+
'\nDB = None'+
'\n'+'Latitude # Longitude # Ele # Speed (m/s) # Course # Time \n')
else:
outputFile.write('Video file location = ' +self.videofile+
'\nVideo start at msecond: '+
str(self.player.position())+
' #fps = '+str(self.RealFps)+
'\nDB = '+str(self.DB.dataProvider().dataSourceUri().split('|')[0])+
'\n'+'Latitude # Longitude # Ele # Speed (m/s) # Course # Time \n')
Counter = 0
for x in GpxPartition:
if Counter != 0:
ActualLatitude = x[1][0]
ActualLongitude = x[1][1]
PreviousLatitude = GpxPartition[Counter-1][1][0]
PreviousLongitude = GpxPartition[Counter-1][1][1]
GeodesicCalcolus = Geodesic.WGS84.Inverse(PreviousLatitude, PreviousLongitude, ActualLatitude, ActualLongitude)
Speed = GeodesicCalcolus['s12'] /1
Course = GeodesicCalcolus['azi2']
if Course < 0:
Course += 360
Ele = x[1][2]
Time = x[1][3]
Counter = Counter + 1
else:
ActualLatitude = x[1][0]
ActualLongitude = x[1][1]
PreviousLatitude = GpxPartition[Counter+1][1][0]
PreviousLongitude = GpxPartition[Counter+1][1][1]
GeodesicCalcolus = Geodesic.WGS84.Inverse(ActualLatitude, ActualLongitude, PreviousLatitude, PreviousLongitude)
Speed = GeodesicCalcolus['s12'] * 1
Course = GeodesicCalcolus['azi2']
if Course < 0:
Course += 360
Ele = x[1][2]
Time = x[1][3]
Counter = Counter + 1
outputFile.write(str(ActualLatitude)+' '+str(ActualLongitude)+' '+str(Ele)+' '+str(Speed)+' '+str(Course)+' '+str(Time)+'\n')
outputFile.close()
self.Main.LoadProjFromNew(self.projectfile)
if os.name == 'nt':
os.remove (self.tmp)
self.close()
def SelectVideoGPX(self):
if os.name == 'nt':
ffmpeg = os.path.dirname(__file__)+'/FFMPEG/ffmpeg.exe'
versione = 'ffmpeg.exe'
else:
ffmpeg = os.path.dirname(__file__)+'/FFMPEG/./ffmpeg'
versione = 'ffmpeg'
if os.path.exists(ffmpeg) == True:
self.comboBox.clear()
if self.player.state() == QMediaPlayer.PlayingState:
self.player.pause()
self.videofile = None
self.GPXfile = None
options = QFileDialog.Options()
options |= QFileDialog.DontUseNativeDialog
self.videofile, _ = QFileDialog.getOpenFileName(self,"Select Video File", "","All Files (*);;Video File (*.mp4 *.avi *.ogv)", options=options)
if self.videofile:
self.GPXfile, _ = QFileDialog.getOpenFileName(self,"Select GPX file", "","All Files (*);;Video File (*.gpx)", options=options)
if self.GPXfile:
self.ParseGpx(self.GPXfile)
self.LoadVideo(self.videofile)
self.replayPosition_label.setText( "-:- / -:-")
else:
ret = QMessageBox.warning(self, "Warning", 'missing ffmpeg binaries, please download it from https://github.com/sagost/VideoUavTracker/blob/master/FFMPEG/'+versione+' and paste it in /.qgis3/python/plugins/Video_UAV_Tracker/FFMPEG/ ', QMessageBox.Ok)
self.close()
def ParseGpx(self,GPXfile):
gpx = parse(GPXfile)
track = gpx.getElementsByTagName("trkpt")
GPXList = []
Error = 0
GpxProgressiveNumber = 0
Timestamp = 'Segnaposto'
for name in track:
dict = {'Lat': 0, 'Lon': 0, 'Ele': 0, 'Time':0}
a = (name.toprettyxml(indent = '') ).split()
for x in a:
if x.find('lat') == 0:
lat = float(x.split('"')[1])
dict['Lat'] = float(x.split('"')[1])
elif x.find('lon') == 0:
lon = float(x.split('"')[1])
dict['Lon'] = float(x.split('"')[1])
elif x.find('<ele>') == 0:
dict['Ele'] = float(x[5:-6])
elif x.find('<time>') == 0:
try:
gpxtime = time.strftime('%Y-%m-%dT%H:%M:%S.%fZ',time.strptime(x[6:-7], '%Y-%m-%dT%H:%M:%S.%fZ'))
dict['Time']= x[6:-7]
except ValueError:
try:
gpxtime = time.strftime('%Y-%m-%dT%H:%M:%SZ',time.strptime(x[6:-7],'%Y-%m-%dT%H:%M:%SZ'))
dict['Time']= x[6:-7]
except ValueError:
try:
gpxtime = time.strftime('%Y-%m-%dT%H:%M:%S',time.strptime(x[6:-7],'%Y-%m-%dT%H:%M:%S'))
dict['Time']= x[6:-7]
except ValueError:
try:
gpxtime = time.strftime('%Y-%m-%dT%H.%M.%S',time.strptime(x[6:-7],'%Y-%m-%dT%H.%M.%S'))
dict['Time']= x[6:-7]
except ValueError:
try:
gpxtime = time.strftime('%Y-%m-%dT%H.%M.%S',time.strptime(x[6:-13],'%Y-%m-%dT%H.%M.%S'))
dict['Time']= x[6:-13]
except ValueError:
try:
gpxtime = time.strftime('%Y-%m-%dT%H.%M.%S',time.strptime(x[6:-13],'%Y-%m-%dT%H:%M:%S'))
dict['Time']= x[6:-13]
except ValueError:
Error = 1
FormatoErrore = str(x)
if dict['Time'] != Timestamp:
Point = [dict['Lat'],dict['Lon'],dict['Ele'],dict['Time']]
self.comboBox.addItem(str(GpxProgressiveNumber) + '-'+gpxtime )
GPXList.append([GpxProgressiveNumber,Point])
GpxProgressiveNumber = GpxProgressiveNumber + 1
Timestamp = dict['Time']
else:
Timestamp = dict['Time']
if Error == 0:
self.GPXList = GPXList
else:
ret = QMessageBox.warning(self, "Warning", FormatoErrore +' UNKOWN GPX TIME FORMAT - ABORTED', QMessageBox.Ok)
self.close
def LoadVideo(self,videofile):
fps = self.getVideoDetails(str(videofile))
self.RealFps = float(fps)
self.fps = (1 / self.RealFps )*1000
url = QUrl.fromLocalFile(str(self.videofile))
mc = QMediaContent(url)
self.player.setMedia(mc)
self.player.play()
def setPosition(self, position):
self.player.setPosition(position*1000)
def durationChanged(self, duration):
duration /= 1000
self.horizontalSlider.setMaximum(duration)
def secTotime(self,seconds):
m, s = divmod(seconds, 60)
h, m = divmod(m, 60)
return "%d:%02d:%02d" % (h, m, s)
def positionChanged(self, progress):
duration = self.player.duration()
totalTime = self.secTotime(duration/1000)
actualTime = self.secTotime(progress/1000)
self.replayPosition_label.setText(actualTime + ' / '+totalTime)
progress /= 1000
if not self.horizontalSlider.isSliderDown():
self.horizontalSlider.setValue(progress)
def MuteUnmute(self):
if self.player.mediaStatus() == 6 :
if self.player.isMuted() == 1:
self.player.setMuted(0)
self.muteButton.setIcon(
self.style().standardIcon(QStyle.SP_MediaVolume))
elif self.player.isMuted() == 0:
self.player.setMuted(1)
self.muteButton.setIcon(
self.style().standardIcon(QStyle.SP_MediaVolumeMuted))
def PlayPause(self):
if self.player.state() == QMediaPlayer.PlayingState:
self.player.pause()
else:
self.player.play()
def getVideoDetails(self,filepath):
if os.name == 'nt':
tmp = os.path.dirname(__file__)[0:-18]+'/Video_UAV_Tracker/tmp'
tmp2 = '"'+tmp+'"'
filepath2 = '"'+filepath+'"'
a = open(tmp,'w')
a.close()
ffmpeg = '"'+os.path.dirname(__file__)[0:-18]+'/Video_UAV_Tracker/FFMPEG/ffmpeg.exe'+'"'
a = os.popen(str(ffmpeg + ' -i '+filepath2+' 2> '+tmp2))
while os.stat(tmp).st_size < 1500:
pass
a = open(tmp,'r')
lines = a.readlines()
a.close()
for l in lines:
l = l.strip()
if str(l).startswith("Stream #0:0"):
linea = str(l).split(',')[-4]
dopo = linea.find('fps')
fps = float(linea[0:dopo])
self.tmp = tmp
return fps
else:
tmpf = tempfile.NamedTemporaryFile()
ffmpeg = os.path.dirname(__file__)+'/FFMPEG/./ffmpeg'
os.system(str(ffmpeg)+" -i \"%s\" 2> %s" % (filepath, tmpf.name))
lines = tmpf.readlines()
tmpf.close()
for l in lines:
l = l.strip()
if str(l).startswith("b'Stream #0:0"):
linea = str(l).split(',')[-4]
dopo = linea.find('fps')
fps = float(linea[0:dopo])
return fps
def SkipForward(self):
position = self.player.position()
self.player.setPosition(position+1000)
def SkipBackward(self):
position = self.player.position()
self.player.setPosition(position-1000)
def ForwardFrame(self):
position = self.player.position()
self.player.setPosition(position+int(self.fps))
def BackwardFrame(self):
position = self.player.position()
self.player.setPosition(position-int(self.fps))
def ManageDB(self):
self.player.pause()
shapeFileFirst,_ = QFileDialog.getSaveFileName(caption = 'Save shape file', filter = "Esri shp (*.shp)")
if shapeFileFirst:
if shapeFileFirst.split('.')[-1] == 'shp':
shapeFile = shapeFileFirst
else:
shapeFile = shapeFileFirst + '.shp'
try:
os.remove(shapeFile)
os.remove(shapeFileFirst.split('.')[0]+'.qpg')
os.remove(shapeFileFirst.split('.')[0]+'.prj')
os.remove(shapeFileFirst.split('.')[0]+'.cpg')
os.remove(shapeFileFirst.split('.')[0]+'.shx')
os.remove(shapeFileFirst.split('.')[0]+'.dbf')
except OSError:
pass
crs = QgsCoordinateReferenceSystem(4326, QgsCoordinateReferenceSystem.EpsgCrsId)
fields = QgsFields()
QgsVectorFileWriter(shapeFile, "CP1250", fields, QgsWkbTypes.Point, crs, "ESRI Shapefile")
EmptyLayer = QgsVectorLayer(shapeFile, shapeFile.split('.')[0].split('/')[-1], 'ogr')
self.dialoga = TableManager(self.iface, EmptyLayer,self)
self.dialoga.exec_()
def AcceptNewDB(self,DB):
self.DB = DB
########## CLASS TableManager ##############################
class TableManager(QDialog, Ui_Dialog):
def __init__(self, iface, EmptyLayer, Main):
QDialog.__init__(self)
self.iface = iface
self.setupUi(self)
self.Main = Main
self.layer = EmptyLayer
self.provider = self.layer.dataProvider()
self.fields = self.readFields( self.provider.fields() )
self.isUnsaved = False # No unsaved changes yet
if self.provider.storageType() == 'ESRI Shapefile': # Is provider saveable?
self.isSaveable = True
else:
self.isSaveable = False
self.needsRedraw = True # Preview table is redrawed only on demand. This is for initial drawing.
self.lastFilter = None
self.selection = -1 # Don't highlight any field on startup
self.selection_list = [] #Update: Santiago Banchero 09-06-2009
self.butUp.clicked.connect(self.doMoveUp)
self.butDown.clicked.connect(self.doMoveDown)
self.butDel.clicked.connect(self.doDelete)
self.butIns.clicked.connect(self.doInsert)
self.butClone.clicked.connect(self.doClone)
self.butRename.clicked.connect(self.doRename)
self.butSaveAs.clicked.connect(self.doSaveAs)
self.fieldsTable.itemSelectionChanged.connect(self.selectionChanged)
self.tabWidget.currentChanged.connect(self.drawDataTable)
self.setWindowTitle(self.tr('Table Manager: {0}').format(self.layer.name()))
self.drawFieldsTable()
self.readData()
def readFields(self, providerFields): # Populates the self.fields dictionary with providerFields
fieldsDict = {}
i=0
for field in providerFields:
fieldsDict.update({i:field})
i+=1
return fieldsDict
def drawFieldsTable(self): # Draws the fields table on startup and redraws it when changed
fields = self.fields
self.fieldsTable.setRowCount(0)
for i in range(len(fields)):
self.fieldsTable.setRowCount(i+1)
item = QTableWidgetItem(fields[i].name())
item.setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled)
item.setData(Qt.UserRole, i) # set field index
self.fieldsTable.setItem(i,0,item)
item = QTableWidgetItem(fields[i].typeName())
item.setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled)
self.fieldsTable.setItem(i,1,item)
self.fieldsTable.setColumnWidth(0, 128)
self.fieldsTable.setColumnWidth(1, 64)
def readData(self): # Reads data from the 'provider' QgsDataProvider into the 'data' list [[column1] [column2] [column3]...]
fields = self.fields
self.data = []
for i in range(len(fields)):
self.data += [[]]
steps = self.provider.featureCount()
stepp = steps / 10
if stepp == 0:
stepp = 1
progress = self.tr('Reading data ') # As a progress bar is used the main window's status bar, because the own one is not initialized yet
n = 0
for feat in self.provider.getFeatures():
attrs = feat.attributes()
for i in range(len(attrs)):
self.data[i] += [attrs[i]]
n += 1
if n % stepp == 0:
progress += '|'
self.iface.mainWindow().statusBar().showMessage(progress)
self.iface.mainWindow().statusBar().showMessage('')
def drawDataTable(self,tab): # Called when user switches tabWidget to the Table Preview
if tab != 1 or self.needsRedraw == False: return
fields = self.fields
self.dataTable.clear()
self.repaint()
self.dataTable.setColumnCount(len(fields))
self.dataTable.setRowCount(self.provider.featureCount())
header = []
for i in fields.values():
header.append(i.name())
self.dataTable.setHorizontalHeaderLabels(header)
formatting = True
if formatting: # slower procedure, with formatting the table items
for i in range(len(self.data)):
for j in range(len(self.data[i])):
item = QTableWidgetItem(unicode(self.data[i][j] or 'NULL'))
item.setFlags(Qt.ItemIsSelectable)
if fields[i].type() == 6 or fields[i].type() == 2:
item.setTextAlignment(Qt.AlignRight | Qt.AlignVCenter)
self.dataTable.setItem(j,i,item)
else: # about 25% faster procedure, without formatting
for i in range(len(self.data)):
for j in range(len(self.data[i])):
self.dataTable.setItem(j,i,QTableWidgetItem(unicode(self.data[i][j] or 'NULL')))
self.dataTable.resizeColumnsToContents()
self.needsRedraw = False
def setChanged(self): # Called after making any changes
self.butSaveAs.setEnabled(True)
self.isUnsaved = True # data are unsaved
self.needsRedraw = True # preview table needs to redraw
def selectionChanged(self): # Called when user is changing field selection of field
self.selection_list = [i for i in range(self.fieldsTable.rowCount()) if self.fieldsTable.item(i,0).isSelected()]
if len(self.selection_list)==1:
self.selection = self.selection_list[0]
else:
self.selection = -1
self.butDel.setEnabled( len(self.selection_list)>0 )
item = self.selection
if item == -1:
self.butUp.setEnabled(False)
self.butDown.setEnabled(False)
self.butRename.setEnabled(False)
self.butClone.setEnabled(False)
else:
if item == 0:
self.butUp.setEnabled(False)
else:
self.butUp.setEnabled(True)
if item == self.fieldsTable.rowCount()-1:
self.butDown.setEnabled(False)
else:
self.butDown.setEnabled(True)
if self.fields[item].type() in [2,6,10]:
self.butRename.setEnabled(True)
self.butClone.setEnabled(True)
else:
self.butRename.setEnabled(False)
self.butClone.setEnabled(False)
def doMoveUp(self): # Called when appropriate button was pressed
item = self.selection
tmp = self.fields[item]
self.fields[item] = self.fields[item-1]
self.fields[item-1] = tmp
for i in range(0,2):
tmp = QTableWidgetItem(self.fieldsTable.item(item,i))
self.fieldsTable.setItem(item,i,QTableWidgetItem(self.fieldsTable.item(item-1,i)))
self.fieldsTable.setItem(item-1,i,tmp)
if item > 0:
self.fieldsTable.clearSelection()
self.fieldsTable.setCurrentCell(item-1,0)
tmp = self.data[item]
self.data[item]=self.data[item-1]
self.data[item-1]=tmp
self.setChanged()
def doMoveDown(self): # Called when appropriate button was pressed
item = self.selection
tmp = self.fields[item]
self.fields[self.selection] = self.fields[self.selection+1]
self.fields[self.selection+1] = tmp
for i in range(0,2):
tmp = QTableWidgetItem(self.fieldsTable.item(item,i))
self.fieldsTable.setItem(item,i,QTableWidgetItem(self.fieldsTable.item(item+1,i)))
self.fieldsTable.setItem(item+1,i,tmp)
if item < self.fieldsTable.rowCount()-1:
self.fieldsTable.clearSelection()
self.fieldsTable.setCurrentCell(item+1,0)
tmp = self.data[item]
self.data[item]=self.data[item+1]
self.data[item+1]=tmp
self.setChanged()
def doRename(self): # Called when appropriate button was pressed
dlg = DialogRename(self.iface,self.fields,self.selection)
if dlg.exec_() == QDialog.Accepted:
newName = dlg.newName()
self.fields[self.selection].setName(newName)
item = self.fieldsTable.item(self.selection,0)
item.setText(newName)
self.fieldsTable.setItem(self.selection,0,item)
self.fieldsTable.setColumnWidth(0, 128)
self.fieldsTable.setColumnWidth(1, 64)
self.setChanged()
def doDelete(self): # Called when appropriate button was pressed
#self.selection_list = sorted(self.selection_list,reverse=True)
all_fields_to_del = [self.fields[i].name() for i in self.selection_list if i != -1]
warning = self.tr('Are you sure you want to remove the following fields?\n{0}').format(", ".join(all_fields_to_del))
if QMessageBox.warning(self, self.tr('Delete field'), warning , QMessageBox.Yes, QMessageBox.No) == QMessageBox.No:
return
self.selection_list.sort(reverse=True) # remove them in reverse order to avoid index changes!!!
for r in self.selection_list:
if r != -1:
del(self.data[r])
del(self.fields[r])
self.fields = dict(zip(range(len(self.fields)), self.fields.values()))
self.drawFieldsTable()
self.setChanged()
self.selection_list = []
#</---- Update: Santiago Banchero 09-06-2009 ---->
def doInsert(self): # Called when appropriate button was pressed
dlg = DialogInsert(self.iface,self.fields,self.selection)
if dlg.exec_() == QDialog.Accepted:
(aName, aType, aPos) = dlg.result()
if aType == 0:
aLength = 10
aPrec = 0
aVariant = QVariant.Int
aTypeName = 'Integer'
elif aType == 1:
aLength = 32
aPrec = 3
aVariant = QVariant.Double
aTypeName = 'Real'
else:
aLength = 80
aPrec = 0
aVariant = QVariant.String
aTypeName = 'String'
self.data += [[]]
if aPos < len(self.fields):
fieldsToMove = range(aPos+1,len(self.fields)+1)
fieldsToMove = reversed(fieldsToMove)
for i in fieldsToMove:
self.fields[i] = self.fields[i-1]
self.data[i] = self.data[i-1]
self.fields[aPos] = QgsField(aName, aVariant, aTypeName, aLength, aPrec, "")
aData = []
if aType == 2:
aItem = None
else:
aItem = None
for i in range(len(self.data[0])):
aData += [aItem]
self.data[aPos] = aData
self.drawFieldsTable()
self.fieldsTable.setCurrentCell(aPos,0)
self.setChanged()
def doClone(self): # Called when appropriate button was pressed
dlg = DialogClone(self.iface,self.fields,self.selection)
if dlg.exec_() == QDialog.Accepted:
(dst, newName) = dlg.result()
self.data += [[]]
movedField = QgsField(self.fields[self.selection])
movedData = self.data[self.selection]
if dst < len(self.fields):
fieldsToMove = range(dst+1,len(self.fields)+1)
fieldsToMove = reversed(fieldsToMove)
for i in fieldsToMove:
self.fields[i] = self.fields[i-1]
self.data[i] = self.data[i-1]
self.fields[dst] = movedField
self.fields[dst].setName(newName)
self.data[dst] = movedData
self.drawFieldsTable()
self.fieldsTable.setCurrentCell(dst,0)
self.setChanged()
def doSaveAs(self): # write data to memory layer
# create destination layer
fields = QgsFields()
keys = list(self.fields.keys())
keys.sort()
for key in keys:
fields.append(self.fields[key])
qfields = []
for field in fields:
qfields.append(field)
self.provider.addAttributes([QgsField('id', QVariant.Int)])
self.provider.addAttributes(qfields)
self.provider.addAttributes([QgsField("Lon(WGS84)", QVariant.String),
QgsField("Lat(WGS84)", QVariant.String),
QgsField('Image link', QVariant.String)])
self.layer.updateExtents()
self.Main.AcceptNewDB(self.layer)
self.close()
class DialogRename(QDialog, Ui_Rename):
def __init__(self, iface, fields, selection):
QDialog.__init__(self)
self.iface = iface
self.setupUi(self)
self.fields = fields
self.selection = selection
self.setWindowTitle(self.tr('Rename field: {0}').format(fields[selection].name()))
self.lineEdit.setValidator(QRegExpValidator(QRegExp('[\w\ _]{,10}'),self))
self.lineEdit.setText(fields[selection].name())
def accept(self):
if self.newName() == self.fields[self.selection].name():
QDialog.reject(self)
return
for i in self.fields.values():
if self.newName().upper() == i.name().upper() and i != self.fields[self.selection]:
QMessageBox.warning(self,self.tr('Rename field'),self.tr('There is another field with the same name.\nPlease type different one.'))
return
if not self.newName():
QMessageBox.warning(self,self.tr('Rename field'),self.tr('The new name cannot be empty'))
self.lineEdit.setText(self.fields[self.selection].name())
return
QDialog.accept(self)
def newName(self):
return self.lineEdit.text()
########## CLASS DialogClone ##############################
class DialogClone(QDialog, Ui_Clone):
def __init__(self, iface, fields, selection):
QDialog.__init__(self)
self.iface = iface
self.setupUi(self)
self.fields = fields
self.selection = selection
self.setWindowTitle(self.tr('Clone field: ')+fields[selection].name())
self.comboDsn.addItem(self.tr('at the first position'))
for i in range(len(fields)):
self.comboDsn.addItem(self.tr('after the {0} field').format(fields[i].name()))
self.comboDsn.setCurrentIndex(selection+1)
self.lineDsn.setValidator(QRegExpValidator(QRegExp('[\w\ _]{,10}'),self))
self.lineDsn.setText(fields[selection].name()[:8] + '_2')
def accept(self):
if not self.result()[1]:
QMessageBox.warning(self,self.tr('Clone field'),self.tr('The new name cannot be empty'))
return
if self.result()[1] == self.fields[self.selection].name():
QMessageBox.warning(self,self.tr('Clone field'),self.tr('The new field\'s name must be different then source\'s one!'))
return
for i in self.fields.values():
if self.result()[1].upper() == i.name().upper():
QMessageBox.warning(self,self.tr('Clone field'),self.tr('There is another field with the same name.\nPlease type different one.'))
return
QDialog.accept(self)
def result(self):
return self.comboDsn.currentIndex(), self.lineDsn.text()
########## CLASS DialogInsert ##############################
class DialogInsert(QDialog, Ui_Insert):
def __init__(self, iface, fields, selection):
QDialog.__init__(self)
self.iface = iface
self.setupUi(self)
self.fields = fields
self.selection = selection
self.setWindowTitle(self.tr('Insert field'))
self.lineName.setValidator(QRegExpValidator(QRegExp('[\w\ _]{,10}'),self))
self.comboType.addItem(self.tr('Integer'))
self.comboType.addItem(self.tr('Real'))
self.comboType.addItem(self.tr('String'))
self.comboPos.addItem(self.tr('at the first position'))
for i in range(len(fields)):
self.comboPos.addItem(self.tr('after the {0} field').format(fields[i].name()))
self.comboPos.setCurrentIndex(selection+1)
def accept(self):
if not self.result()[0]:
QMessageBox.warning(self,self.tr('Insert new field'),self.tr('The new name cannot be empty'))
return
for i in self.fields.values():
if self.result()[0].upper() == i.name().upper():
QMessageBox.warning(self,self.tr('Insert new field'),self.tr('There is another field with the same name.\nPlease type different one.'))
return
QDialog.accept(self)
def result(self):
return self.lineName.text(), self.comboType.currentIndex(), self.comboPos.currentIndex()