forked from NaturalGIS/naturalgis_geotag_and_import_photos
/
importphotosdialog.py
288 lines (223 loc) · 10.3 KB
/
importphotosdialog.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
# -*- coding: utf-8 -*-
#******************************************************************************
#
# Geotag and import photos
# ---------------------------------------------------------
# Process photos from phototraps.
#
# Copyright (C) 2012-2013 Alexander Bruy (alexander.bruy@gmail.com)
#
# This source 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 3 of the License, or (at your option)
# any later version.
#
# This code is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# A copy of the GNU General Public License is available on the World Wide Web
# at <http://www.gnu.org/licenses/>. You can also obtain it by writing
# to the Free Software Foundation, 51 Franklin Street, Suite 500 Boston,
# MA 02110-1335 USA.
#
#******************************************************************************
import os
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *
from qgis.gui import *
import importthread
import geotagphotos_utils as utils
import exiftool
from ui.ui_importphotosdialogbase import Ui_Dialog
class ImportPhotosDialog(QDialog, Ui_Dialog):
def __init__(self, iface):
QDialog.__init__(self)
self.iface = iface
self.setupUi(self)
self.workThread = None
self.outFileName = None
self.outEncoding = None
self.btnOk = self.buttonBox.button(QDialogButtonBox.Ok)
self.btnClose = self.buttonBox.button(QDialogButtonBox.Close)
self.btnSelectPhotos.clicked.connect(self.selectPhotosDir)
self.btnSelectShape.clicked.connect(self.selectOutputShape)
self.btnSelectAll.clicked.connect(self.selectAllTags)
self.btnClearAll.clicked.connect(self.clearAllTags)
self.btnSelectConfig.clicked.connect(self.selectConfigFile)
self.manageGui()
def manageGui(self):
# read settings and restore controls state
settings = QSettings("Faunalia", "Geotagphotos")
self.chkRecurse.setChecked(bool(settings.value("ui/recurseDirs", False)))
self.chkAppend.setChecked(bool(settings.value("ui/appendFile", False)))
self.chkAddToCanvas.setChecked(bool(settings.value("ui/addToCanvas", False)))
def selectPhotosDir(self):
settings = QSettings("Faunalia", "Geotagphotos")
lastDir = settings.value("ui/lastPhotosDir", "")
dirName = QFileDialog.getExistingDirectory(None,
self.tr("Select directory"),
lastDir,
QFileDialog.ShowDirsOnly)
if dirName == "":
return
self.lePhotosPath.setText(dirName)
settings.setValue("ui/lastPhotosDir", os.path.dirname(unicode(dirName)))
self.loadTags(dirName)
def selectOutputShape(self):
(self.outFileName, self.outEncoding) = utils.saveDialog(self)
if self.outFileName is None or self.outEncoding is None:
return
self.leShapePath.setText(self.outFileName)
def loadTags(self, dirName):
if dirName == "":
return
self.lstTags.clear()
etPath = utils.getExifToolPath()
if not etPath == "":
etPath = os.path.join(os.path.normpath(unicode(etPath)), "exiftool")
else:
etPath = "exiftool"
# config file
cfgFile = self.leConfigFile.text()
if cfgFile == "":
cfgFile = utils.getConfigPath()
if cfgFile != "":
etPath += " -config " + unicode(cfgFile)
et = exiftool.ExifTool(etPath)
md = None
found = False
QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
# fetch metadata from first photo
with et:
for root, dirs, files in os.walk(unicode(dirName)):
if len(files) == 0:
continue
for f in files:
if f.lower().endswith(".jpg") or f.lower().endswith(".jpeg"):
md = et.get_metadata(os.path.join(root, f))
if len(md) > 0:
found = True
break
if found:
break
# process metadata and populate tag list
for k, v in md.iteritems():
if not k.startswith("EXIF:"):
continue
ti = QTreeWidgetItem(self.lstTags)
ti.setText(0, k)
ti.setCheckState(0, Qt.Unchecked)
self.lstTags.sortItems(0, Qt.AscendingOrder)
QApplication.restoreOverrideCursor()
def selectAllTags(self):
for i in xrange(self.lstTags.topLevelItemCount()):
self.lstTags.topLevelItem(i).setCheckState(0, Qt.Checked)
def clearAllTags(self):
for i in xrange(self.lstTags.topLevelItemCount()):
self.lstTags.topLevelItem(i).setCheckState(0, Qt.Unchecked)
def selectConfigFile(self):
settings = QSettings("Faunalia", "Geotagphotos")
lastDir = settings.value("ui/lastConfigDir", "")
fileName = QFileDialog.getOpenFileName(None,
self.tr("Select config file"),
lastDir,
self.tr("All files (*)"))
if fileName == "":
return
self.leConfigFile.setText(fileName)
settings.setValue("ui/lastConfigDir", os.path.dirname(unicode(fileName)))
# reread tags
self.loadTags(self.lePhotosPath.text())
def reject(self):
QDialog.reject(self)
def accept(self):
# check if exiftool installed
if not utils.exiftoolInstalled():
QMessageBox.warning(self,
self.tr("Missing exiftool"),
self.tr("Can't locate exiftool executable! Make sure that it is installed and available in PATH or, alternatively, specify path to binary in plugin settings"))
return
# save ui state
settings = QSettings("Faunalia", "Geotagphotos")
settings.setValue("ui/recurseDirs", self.chkRecurse.isChecked())
settings.setValue("ui/appendFile", self.chkAppend.isChecked())
settings.setValue("ui/addToCanvas", self.chkAddToCanvas.isChecked())
if self.lePhotosPath.text() == "":
QMessageBox.warning(self,
self.tr("Path not set"),
self.tr("Path to photos is not set. Please specify directory with photos."))
return
if self.outFileName is None or self.outFileName == "":
QMessageBox.warning(self,
self.tr("Output file is not set"),
self.tr("Output file name is missing. Please specify correct output file."))
return
# TODO: check for fields compatibility?
# config file
cfgFile = self.leConfigFile.text()
if cfgFile == "":
cfgFile = utils.getConfigPath()
QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
self.btnOk.setEnabled(False)
tagList = []
for i in xrange(self.lstTags.topLevelItemCount()):
item = self.lstTags.topLevelItem(i)
if item.checkState(0) == Qt.Checked:
tagList.append(unicode(item.text(0)))
self.workThread = importthread.ImportThread(self.lePhotosPath.text(),
self.chkRecurse.isChecked(),
tagList,
self.outFileName,
self.outEncoding,
self.chkAppend.isChecked(),
cfgFile)
self.workThread.rangeChanged.connect(self.setProgressRange)
self.workThread.updateProgress.connect(self.updateProgress)
self.workThread.processFinished.connect(self.processFinished)
self.workThread.processInterrupted.connect(self.processInterrupted)
self.btnClose.setText(self.tr("Cancel"))
self.buttonBox.rejected.disconnect(self.reject)
self.btnClose.clicked.connect(self.stopProcessing)
self.workThread.start()
def setProgressRange(self, data):
if data[0] != "":
self.progressBar.setFormat(data[0])
self.progressBar.setRange(0, data[1])
def updateProgress(self):
self.progressBar.setValue(self.progressBar.value() + 1)
def processFinished(self, hasOutput):
self.stopProcessing()
self.restoreGui()
if not hasOutput:
QMessageBox.warning(self,
self.tr("No output file"),
self.tr("There are no geotagged photos in selected directory.\nShapefile was not created/updated."))
if self.chkAddToCanvas.isChecked():
if self.chkAppend.isChecked() or hasOutput:
self.addLayerToCanvas()
def processInterrupted(self):
self.restoreGui()
def stopProcessing(self):
if self.workThread is not None:
self.workThread.stop()
self.workThread = None
def restoreGui(self):
self.progressBar.setFormat("%p%")
self.progressBar.setRange(0, 1)
self.progressBar.setValue(0)
self.buttonBox.rejected.connect(self.reject)
self.btnClose.clicked.disconnect(self.stopProcessing)
self.btnClose.setText(self.tr("Close"))
self.btnOk.setEnabled(True)
QApplication.restoreOverrideCursor()
def addLayerToCanvas(self):
newLayer = QgsVectorLayer(self.outFileName, QFileInfo(self.outFileName).baseName(), "ogr")
if newLayer.isValid():
QgsMapLayerRegistry.instance().addMapLayer(newLayer)
else:
QMessageBox.warning(self,
self.tr("Can't open file"),
self.tr("Error loading output shapefile:\n%s") % unicode(self.outFileName))