-
-
Notifications
You must be signed in to change notification settings - Fork 90
/
datamanager.py
392 lines (299 loc) · 12.6 KB
/
datamanager.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
# -*- coding: utf-8 -*-
# (C) 2014 Minoru Akagi
# SPDX-License-Identifier: GPL-2.0-or-later
# begin: 2014-01-16
import os
from PyQt5.QtCore import Qt, QSize, QUrl
from PyQt5.QtGui import QColor, QImage, QPainter
from qgis.core import QgsMapLayer
from . import utils
from .utils import logMessage
class DataManager:
""" manages a list of unique items """
def __init__(self):
self._list = []
def count(self):
return len(self._list)
def _index(self, data):
if data in self._list:
return self._list.index(data)
index = len(self._list)
self._list.append(data)
return index
class ImageManager(DataManager):
IMG_MAP = 1
IMG_LAYER = 2
IMG_FILE = 3
def __init__(self, exportSettings):
DataManager.__init__(self)
self.exportSettings = exportSettings
self._renderer = None
def mapImageIndex(self, width, height, extent, transp_background, format):
img = (self.IMG_MAP, (None, width, height, extent, transp_background), format)
return self._index(img)
def layerImageIndex(self, layerids, width, height, extent, transp_background, format):
img = (self.IMG_LAYER, (layerids, width, height, extent, transp_background), format)
return self._index(img)
def imageFileIndex(self, path):
img = (self.IMG_FILE, path, "")
return self._index(img)
def renderedImage(self, layerids, width, height, extent, transp_background=False):
# render layers with QgsMapRendererCustomPainterJob
from qgis.core import QgsMapRendererCustomPainterJob
antialias = True
settings = self.exportSettings.mapSettings
# store old map settings
old_outputSize = settings.outputSize()
old_extent = settings.extent()
old_rotation = settings.rotation()
old_layers = settings.layers()
old_backgroundColor = settings.backgroundColor()
# map settings
settings.setOutputSize(QSize(width, height))
settings.setExtent(extent.unrotatedRect())
settings.setRotation(extent.rotation())
if layerids:
settings.setLayers(utils.getLayersByLayerIds(layerids))
if transp_background:
settings.setBackgroundColor(QColor(Qt.transparent))
has_pluginlayer = False
for layer in settings.layers():
if layer and layer.type() == QgsMapLayer.PluginLayer:
has_pluginlayer = True
break
# create an image
image = QImage(width, height, QImage.Format_ARGB32_Premultiplied)
painter = QPainter()
painter.begin(image)
if antialias:
painter.setRenderHint(QPainter.Antialiasing)
# rendering
job = QgsMapRendererCustomPainterJob(settings, painter)
if has_pluginlayer:
job.renderSynchronously() # use this method so that TileLayerPlugin layer is rendered correctly
else:
job.start()
job.waitForFinished()
painter.end()
# restore map settings
settings.setOutputSize(old_outputSize)
settings.setExtent(old_extent)
settings.setRotation(old_rotation)
settings.setLayers(old_layers)
settings.setBackgroundColor(old_backgroundColor)
return image
def image(self, index):
imageType, args, fmt = self._list[index]
if imageType == self.IMG_FILE:
image_path = args
if os.path.isfile(image_path):
return QImage(image_path)
else:
logMessage("Image file not found: {0}".format(image_path), warning=True)
else: # IMG_MAP or IMG_LAYER
image = self.renderedImage(*args)
if fmt == "JPEG":
return utils.jpegCompressedImage(image)
return image
image = QImage(1, 1, QImage.Format_RGB32)
image.fill(Qt.lightGray)
return image
def dataUri(self, index):
imageType, args, fmt = self._list[index]
if imageType == self.IMG_FILE:
return utils.imageFile2dataUri(args)
image = self.image(index)
if image:
return utils.image2dataUri(image, fmt=fmt)
return ""
def write(self, index, path):
imageType, args, fmt = self._list[index]
if imageType == self.IMG_FILE:
image_path = args
if os.path.isfile(image_path):
utils.copyFile(image_path, path, overwrite=True)
return
self.image(index).save(path)
class MaterialManager(DataManager):
# following six material types are defined also in JS
# first three types are basic material types
MESH_LAMBERT = 0
MESH_PHONG = 1
MESH_TOON = 2
LINE = 3
LINE_MESH = 4
SPRITE_IMAGE = 5
POINT = 6
# other material types for internal use
MESH_MATERIAL = 10
MESH_FLAT = 11
MESH_BASIC = 13
WIREFRAME = 12
MAP_IMAGE = 21
LAYER_IMAGE = 22
IMAGE_FILE = 23
ERROR_COLOR = "0"
def __init__(self, imageManager, basicType=MESH_LAMBERT):
DataManager.__init__(self)
self.imageManager = imageManager
self.basicMaterialType = basicType
def _indexCol(self, type, color, opacity=1, doubleSide=False, opts=None):
if color[0:2] != "0x":
color = self.ERROR_COLOR
mtl = (type, color, opacity, doubleSide, opts)
return self._index(mtl)
def getMeshMaterialIndex(self, color, opacity=1, doubleSide=False):
return self._indexCol(self.MESH_MATERIAL, color, opacity, doubleSide)
def getMeshFlatMaterialIndex(self, color, opacity=1, doubleSide=False):
return self._indexCol(self.MESH_FLAT, color, opacity, doubleSide)
def getMeshBasicMaterialIndex(self, color, opacity=1, doubleSide=False):
return self._indexCol(self.MESH_BASIC, color, opacity, doubleSide)
def getPointMaterialIndex(self, color, opacity=1, size=1):
return self._indexCol(self.POINT, color, opacity, False, size)
def getLineIndex(self, color, opacity=1, dashed=False):
return self._indexCol(self.LINE, color, opacity, opts=dashed)
def getMeshLineIndex(self, color, opacity=1, thickness=1, dashed=False):
return self._indexCol(self.LINE_MESH, color, opacity, opts=(thickness, dashed))
def getWireframeIndex(self, color, opacity=1):
return self._indexCol(self.WIREFRAME, color, opacity)
def getMapImageIndex(self, width, height, extent, opacity=1, transp_background=False, shading=True, format="PNG"):
mtl = (self.MAP_IMAGE, None, opacity, True, ((width, height, extent, transp_background, format), shading))
return self._index(mtl)
def getLayerImageIndex(self, layerids, width, height, extent, opacity=1, transp_background=False, shading=True, format="PNG"):
mtl = (self.LAYER_IMAGE, None, opacity, True, ((layerids, width, height, extent, transp_background, format), shading))
return self._index(mtl)
def getImageFileIndex(self, path, opacity=1, transp_background=False, doubleSide=False, shading=True):
mtl = (self.IMAGE_FILE, None, opacity, doubleSide, (path, transp_background, shading))
return self._index(mtl)
def getSpriteImageIndex(self, path_url, opacity=1):
transp_background = True
mtl = (self.SPRITE_IMAGE, None, opacity, False, (path_url, transp_background))
return self._index(mtl)
def build(self, index, filepath=None, url=None, base64=False):
mt, color, opacity, doubleSide, opts = self._list[index]
transp_background = False
shading = True
m = {
"type": mt if mt in [self.POINT, self.LINE, self.LINE_MESH, self.SPRITE_IMAGE] else self.basicMaterialType
}
if color is None:
if mt == self.MAP_IMAGE:
args, shading = opts
imgIndex = self.imageManager.mapImageIndex(*args)
elif mt == self.LAYER_IMAGE:
args, shading = opts
imgIndex = self.imageManager.layerImageIndex(*args)
elif mt == self.IMAGE_FILE:
imagepath, transp_background, shading = opts
imgIndex = self.imageManager.imageFileIndex(imagepath)
elif mt == self.SPRITE_IMAGE:
path_url, transp_background = opts
if path_url.startswith("http:") or path_url.startswith("https:"):
url = path_url
filepath = None
else:
imgIndex = self.imageManager.imageFileIndex(path_url)
if url is None:
if base64:
m["image"] = {"base64": self.imageManager.dataUri(imgIndex)}
else:
m["image"] = {"object": self.imageManager.image(imgIndex)}
else:
m["image"] = {"url": url}
if filepath:
# write image to a file
self.imageManager.write(imgIndex, filepath)
else:
m["c"] = int(color, 16)
if mt == self.POINT:
m["s"] = opts # size
elif mt == self.LINE:
m["dashed"] = opts
elif mt == self.LINE_MESH:
thickness, dashed = opts
m["thickness"] = thickness
m["dashed"] = dashed
elif mt == self.MESH_BASIC:
shading = False
if transp_background:
m["t"] = 1
if mt == self.WIREFRAME:
m["w"] = 1
if mt == self.MESH_FLAT:
m["flat"] = 1
if opacity < 1:
m["o"] = opacity
if doubleSide:
m["ds"] = 1
if not shading:
m["bm"] = True
return m
def buildAll(self, pathRoot=None, urlRoot=None, base64=False):
mList = []
for i, item in enumerate(self._list):
mt, color, opacity, doubleSide, opts = item
filepath = url = None
if pathRoot and color is None:
if mt == self.SPRITE_IMAGE:
path_url = opts[0]
if not path_url.startswith("http:") and not path_url.startswith("https:"):
ext = os.path.splitext(path_url)[1].lower()
suffix = "{}{}".format(i, ext)
filepath = pathRoot + suffix
url = urlRoot + suffix
m = self.build(i, filepath, url, base64)
mList.append(m)
return mList
class ModelManager(DataManager):
def __init__(self, exportSettings):
DataManager.__init__(self)
self.exportSettings = exportSettings
def modelIndex(self, path):
return self._index(path)
def build(self, export=True, base64=False):
a = []
for path_url in self._list:
if path_url.startswith("http:") or path_url.startswith("https:"):
a.append({"url": path_url})
elif base64:
_, ext = os.path.splitext(path_url)
a.append({"base64": utils.base64file(path_url),
"ext": ext[1:],
"resourcePath": "./data/{}/models/".format(self.exportSettings.outputFileTitle())})
else:
if export:
url = "./data/{}/models/{}".format(self.exportSettings.outputFileTitle(),
os.path.basename(path_url))
else:
url = QUrl.fromLocalFile(path_url).toString()
a.append({"url": url})
return a
def hasColladaModel(self):
for f in self._list:
_, ext = os.path.splitext(f)
if ext == ".dae":
return True
return False
def hasGLTFModel(self):
for f in self._list:
_, ext = os.path.splitext(f)
if ext in [".gltf", ".glb"]:
return True
return False
def filesToCopy(self):
f = []
if self._list:
if self.hasColladaModel():
f.append({"files": ["js/threejs/loaders/ColladaLoader.js"], "dest": "threejs/loaders"})
if self.hasGLTFModel():
f.append({"files": ["js/threejs/loaders/GLTFLoader.js"], "dest": "threejs/loaders"})
f.append({"files": self._list, "dest": "./data/{}/models".format(self.exportSettings.outputFileTitle())})
return f
def scripts(self):
s = []
if self._list:
if self.hasColladaModel():
s.append("./threejs/loaders/ColladaLoader.js")
if self.hasGLTFModel():
s.append("./threejs/loaders/GLTFLoader.js")
return s