forked from lao-tseu-is-alive/qconsolidate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsolidatethread.py
294 lines (245 loc) · 9.73 KB
/
consolidatethread.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
# -*- coding: utf-8 -*-
#******************************************************************************
#
# QConsolidate
# ---------------------------------------------------------
# Consolidates all layers from current QGIS project into one directory and
# creates copy of current project using this consolidated layers.
#
# Copyright (C) 2012-2013 Alexander Bruy ([email protected])
#
# 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 2 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.
#
#******************************************************************************
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtXml import *
from qgis.core import *
from qgis.gui import *
from osgeo import gdal
import glob
ogrDatabase = ["PGeo",
"SDE",
"IDB",
"INGRES",
"MySQL",
"MSSQLSpatial",
"OCI",
"ODBC",
"OGDI",
"PostgreSQL",
"SQLite" # file or db?
]
ogrDirectory = ["AVCBin",
"GRASS",
"UK. NTF",
"TIGER"
]
ogrProtocol = ["DODS",
"GeoJSON" # file or protocol?
]
vectorProviders = ["gpx",
"osm",
"grass",
"memory",
"postgres",
"spatialite",
"sqlanywhere",
"delimitedtext"
]
class ConsolidateThread(QThread):
processError = pyqtSignal(str)
rangeChanged = pyqtSignal(int)
updateProgress = pyqtSignal()
processFinished = pyqtSignal()
processInterrupted = pyqtSignal()
def __init__(self, iface, outputDir, projectFile):
QThread.__init__(self, QThread.currentThread())
self.mutex = QMutex()
self.stopMe = 0
self.iface = iface
self.outputDir = outputDir
self.layersDir = outputDir + "/layers"
self.projectFile = projectFile
def run(self):
self.mutex.lock()
self.stopMe = 0
self.mutex.unlock()
interrupted = False
gdal.AllRegister()
# read project
doc = self.loadProject()
root = doc.documentElement()
# ensure that relative path used
e = root.firstChildElement("properties")
e.firstChildElement("Paths").firstChild().firstChild().setNodeValue("false")
# get layers section in project
e = root.firstChildElement("projectlayers")
# process layers
layers = self.iface.legendInterface().layers()
self.rangeChanged.emit(len(layers))
ogrSupported = ogrDatabase + ogrDirectory
for layer in layers:
layerType = layer.type()
if layerType == QgsMapLayer.VectorLayer:
layerName = layer.name()
layerSource = layer.source()
pt = layer.providerType()
if pt == "ogr":
storage = str(layer.storageType())
if storage in ogrSupported:
self.copyGenericVectorLayer(e, layer, layerName)
elif storage in ogrProtocol:
print "Storage type '%s' currently not supported" % storage
else:
self.copyFileLayer(e, layerSource, layerName)
elif pt in vectorProviders:
self.copyGenericVectorLayer(e, layer, layerName)
else:
print "Vector provider '%s' currently not supported" % pt
elif layerType == QgsMapLayer.RasterLayer:
pt = layer.providerType()
if pt == "gdal":
self.copyRasterLayer(e, layer.source(), layer.name())
else:
print "Raster provider '%s' currently not supported" % pt
else:
print "Layers with type '%s' currently not supported" % layerType
self.updateProgress.emit()
self.mutex.lock()
s = self.stopMe
self.mutex.unlock()
if s == 1:
interrupted = True
break
# save updated project
self.saveProject(doc)
if not interrupted:
self.processFinished.emit()
else:
self.processInterrupted.emit()
def stop(self):
self.mutex.lock()
self.stopMe = 1
self.mutex.unlock()
QThread.wait(self)
def loadProject(self):
f = QFile(self.projectFile)
if not f.open(QIODevice.ReadOnly | QIODevice.Text):
msg = self.tr("Cannot read file %s:\n%s.") % (self.projectFile, f.errorString())
self.processError.emit(msg)
return
doc = QDomDocument()
setOk, errorString, errorLine, errorColumn = doc.setContent(f, True)
if not setOk:
msg = self.tr("Parse error at line %d, column %d:\n%s") % (errorLine, errorColumn, errorString)
self.processError.emit(msg)
return
f.close()
return doc
def saveProject(self, doc):
f = QFile(self.projectFile)
if not f.open(QIODevice.WriteOnly | QIODevice.Text):
msg = self.tr("Cannot write file %s:\n%s.") % (self.projectFile, f.errorString())
self.processError.emit(msg)
return
out = QTextStream(f)
doc.save(out, 4)
f.close()
def copyFileLayer(self, layerElement, layerSource, layerName):
# copy all files
fi = QFileInfo(layerSource)
mask = fi.path() + "/" + fi.baseName() + ".*"
files = glob.glob(unicode(mask))
fl = QFile()
for f in files:
fi.setFile(f)
fl.setFileName(f)
fl.copy(self.layersDir + "/" + fi.fileName())
# update project
layerNode = self.findLayerInProject(layerElement, layerName)
sourceNode = layerNode.firstChildElement("datasource")
p = "./layers/" + QFileInfo(sourceNode.text()).fileName()
sourceNode.firstChild().setNodeValue(p)
def copyGenericVectorLayer(self, layerElement, vLayer, layerName):
crs = vLayer.crs()
enc = vLayer.dataProvider().encoding()
outFile = "%s/%s.shp" % (self.layersDir, layerName)
error = QgsVectorFileWriter.writeAsVectorFormat(vLayer, outFile, enc, crs)
if error != QgsVectorFileWriter.NoError:
msg = self.tr("Cannot copy layer %s") % layerName
self.processError.emit(msg)
return
# update project
layerNode = self.findLayerInProject(layerElement, layerName)
tmpNode = layerNode.firstChildElement("datasource")
p = "./layers/%s.shp" % layerName
tmpNode.firstChild().setNodeValue(p)
tmpNode = layerNode.firstChildElement("provider")
tmpNode.setAttribute("encoding", enc)
tmpNode.firstChild().setNodeValue("ogr")
def copyRasterLayer(self, layerElement, layerPath, layerName):
outputFormat = "GTiff"
creationOptions = ["COMPRESS=PACKBITS", "TILED=YES", "TFW=YES", "BIGTIFF=IF_NEEDED"]
driver = gdal.GetDriverByName(outputFormat)
if driver is None:
print "Format driver %s not found." % outputFormat
return
metadata = driver.GetMetadata()
if "DCAP_CREATE" not in metadata:
print "Format driver %s does not support creation and piecewise writing" % outputFormat
return
# open source raster
src = gdal.Open(unicode(layerPath))
if src is None:
print "Unable to open file", layerPath
return
# extract some metadata from source raster
width = src.RasterXSize
height = src.RasterYSize
bands = src.RasterCount
dataType = src.GetRasterBand(1).DataType
crs = src.GetProjection()
geoTransform = src.GetGeoTransform()
# copy raster
dstFilename = unicode("%s/%s.tif" % (self.layersDir, layerName))
dst = driver.Create(dstFilename, width, height, bands, dataType, creationOptions)
if dst is None:
print "Creation failed"
return
dst.SetProjection(crs)
dst.SetGeoTransform(geoTransform)
# copy data from source file into output file
for i in xrange(1, bands + 1):
sBand = src.GetRasterBand(i)
dBand = dst.GetRasterBand(i)
data = sBand.ReadRaster(0, 0, width, height, width, height, dataType)
dBand.WriteRaster(0, 0, width, height, data, width, height, dataType)
src = None
dst = None
# update project
layerNode = self.findLayerInProject(layerElement, layerName)
tmpNode = layerNode.firstChildElement("datasource")
p = "./layers/%s.tif" % layerName
tmpNode.firstChild().setNodeValue(p)
def findLayerInProject(self, layerElement, layerName):
child = layerElement.firstChildElement()
while not child.isNull():
nm = child.firstChildElement("layername")
if nm.text() == layerName:
return child
child = child.nextSiblingElement()
return None