forked from minorua/Qgis2threejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vectorobject.py
157 lines (137 loc) · 5.69 KB
/
vectorobject.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
# -*- coding: utf-8 -*-
"""
/***************************************************************************
Qgis2threejs
A QGIS plugin
export terrain and map image into web browser
-------------------
begin : 2014-01-11
copyright : (C) 2014 by Minoru Akagi
email : akaginch@gmail.com
***************************************************************************/
/***************************************************************************
* *
* 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. *
* *
***************************************************************************/
"""
from PyQt4.QtCore import qDebug
from PyQt4.QtGui import QColor
from qgis.core import QGis, QgsMessageLog
import sys
import random
from vectorstylewidgets import *
debug_mode = 1
colorNames = []
def list_modules():
for nam, mod in sys.modules.items():
qDebug(nam + ": " + str(mod))
class ObjectTypeModule:
def __init__(self, module):
self.module = module
self.geometryType = getattr(module, 'geometryType')()
self.objectTypeNames = getattr(module, 'objectTypeNames')()
self.setupForm = getattr(module, 'setupForm') # setupForm(dialog, mapTo3d, layer, type_index=0)
self.write = getattr(module, 'write') # write(mapTo3d, pt(s), properties, layer=None, f=None)
@classmethod
def load(self, modname):
if modname in sys.modules:
module = reload(sys.modules[modname])
else:
module = __import__(modname)
for comp in modname.split(".")[1:]:
module = getattr(module, comp)
return ObjectTypeModule(module)
class ObjectTypeItem:
def __init__(self, name, mod_index, type_index):
self.name = name
self.mod_index = mod_index
self.type_index = type_index
class ObjectTypeManager:
def __init__(self):
# load basic object types
self.modules = []
self.objTypes = {QGis.Point: [], QGis.Line: [], QGis.Polygon:[]} # each list item is ObjectTypeItem object
module_names = ["point_basic", "line_basic", "polygon_basic"]
module_fullnames = map(lambda x: "Qgis2threejs.objects." + x, module_names)
for modname in module_fullnames:
mod = ObjectTypeModule.load(modname)
mod_index = len(self.modules)
self.modules.append(mod)
for type_index, name in enumerate(mod.objectTypeNames):
self.objTypes[mod.geometryType].append(ObjectTypeItem(name, mod_index, type_index))
if debug_mode:
qDebug("ObjectTypeManager: " + str(self.objTypes))
def objectTypeNames(self, geom_type):
if geom_type in self.objTypes:
return map(lambda x: x.name, self.objTypes[geom_type])
return []
def objectTypeItem(self, geom_type, item_index):
if geom_type in self.objTypes:
return self.objTypes[geom_type][item_index]
return None
def module(self, mod_index):
if mod_index < len(self.modules):
return self.modules[mod_index]
return None
def setupForm(self, dialog, mapTo3d, layer, geom_type, item_index):
if geom_type in self.objTypes:
typeitem = self.objTypes[geom_type][item_index]
return self.modules[typeitem.mod_index].setupForm(dialog, mapTo3d, layer, typeitem.type_index)
return False
class VectorObjectProperties:
def __init__(self, prop_dict=None):
if prop_dict is None:
self.prop_dict = []
self.visible = False
else:
self.prop_dict = prop_dict
self.item_index = prop_dict["itemindex"]
typeitem = prop_dict["typeitem"]
self.type_name = typeitem.name
self.mod_index = typeitem.mod_index
self.type_index = typeitem.type_index
self.visible = prop_dict["visible"]
self.layer = None
def color(self, layer=None, f=None):
global colorNames
vals = self.prop_dict["color"]
if vals[0] == ColorWidgetFunc.RGB:
return vals[2]
elif vals[0] == ColorWidgetFunc.RANDOM or layer is None or f is None:
if len(colorNames) == 0:
colorNames = QColor.colorNames()
colorName = random.choice(colorNames)
colorNames.remove(colorName)
return QColor(colorName).name().replace("#", "0x")
symbol = layer.rendererV2().symbolForFeature(f)
if symbol is None:
QgsMessageLog.logMessage(u'Symbol for feature is not found. Once try to show layer: {0}'.format(layer.name()), "Qgis2threejs")
symbol = layer.rendererV2().symbols()[0]
return symbol.color().name().replace("#", "0x")
def useZ(self):
return self.prop_dict["height"][0] == HeightWidgetFunc.RELATIVE_TO_Z
def isHeightRelativeToSurface(self):
return self.prop_dict["height"][0] == HeightWidgetFunc.RELATIVE
def relativeHeight(self, f=None):
lst = self.prop_dict["height"]
if lst[0] in [HeightWidgetFunc.RELATIVE, HeightWidgetFunc.ABSOLUTE, HeightWidgetFunc.RELATIVE_TO_Z] or f is None:
return float(lst[2])
# attribute value + addend
return float(f.attribute(lst[1])) + float(lst[2])
def values(self, f=None):
vals = []
for i in range(32): # big number for style count
if i in self.prop_dict:
lst = self.prop_dict[i]
if lst[0] == FieldValueWidgetFunc.ABSOLUTE or f is None:
vals.append(lst[2])
else:
# attribute value * multiplier
vals.append(str(float(f.attribute(lst[1])) * float(lst[2])))
else:
break
return vals