1 |
jan |
1725 |
# Copyright (C) 2003 by Intevation GmbH |
2 |
|
|
# Authors: |
3 |
|
|
# Jan-Oliver Wagner <[email protected]> |
4 |
|
|
# |
5 |
|
|
# This program is free software under the GPL (>=v2) |
6 |
|
|
# Read the file COPYING coming with Thuban for details. |
7 |
|
|
|
8 |
|
|
""" |
9 |
|
|
Classes for ArcView Objects as in '.apr'-files. |
10 |
|
|
|
11 |
|
|
The classes are only added to this module if they |
12 |
|
|
are considered to be complete and whenever possible |
13 |
|
|
accompanied by unit tests (see tests/). |
14 |
|
|
Experimental classes should remain in importAPR.py. |
15 |
|
|
""" |
16 |
|
|
|
17 |
|
|
__version__ = "$Revision$" |
18 |
|
|
|
19 |
jan |
1883 |
from math import ceil |
20 |
|
|
|
21 |
jan |
1725 |
from Thuban.Model.color import Color, Transparent, Black |
22 |
|
|
from Thuban.Model.range import Range |
23 |
jan |
1883 |
from Thuban.Model.classification import ClassGroupProperties |
24 |
jan |
1725 |
|
25 |
|
|
from odb import ODBBaseObject |
26 |
|
|
|
27 |
jan |
1883 |
class APR_BLnSym(ODBBaseObject): |
28 |
|
|
"""Line symbol object. |
29 |
|
|
Always references a color object TClr via 'Color'. |
30 |
|
|
|
31 |
|
|
The stroke width 'Width' is always given, but the scale is |
32 |
|
|
unclear so far (e.g. which width is actually meant with value '0.1'?). |
33 |
|
|
Meanwhile, the ceiling of 'Width' is applied to be stroke width |
34 |
|
|
in pixels. |
35 |
|
|
|
36 |
|
|
Finally, there seems always to be a 'Pattern'-List consisting of |
37 |
|
|
float values. No idea so far, how to interpret this (Thuban does |
38 |
|
|
not support pattern yet anyway). |
39 |
|
|
""" |
40 |
|
|
_obj_refs = [ 'Color' ] |
41 |
|
|
_values = [ 'Width', 'Pattern' ] |
42 |
|
|
|
43 |
|
|
def GetThubanProp(self): |
44 |
|
|
"""Create a Thuban ClassGroupProperty from this object and |
45 |
|
|
return it. |
46 |
|
|
""" |
47 |
|
|
prop = ClassGroupProperties() |
48 |
|
|
prop.SetLineColor(self.Get('Color').GetThubanColor()) |
49 |
|
|
prop.SetLineWidth(int(ceil(float(self.Get('Width'))))) |
50 |
|
|
return prop |
51 |
|
|
|
52 |
|
|
class APR_BMkSym(ODBBaseObject): |
53 |
|
|
"""Point symbol object. |
54 |
|
|
Always references a Color and a Background Color via 'Color', 'BgColor'. |
55 |
|
|
For Thuban, Color is interpreted as line color and BGColor is |
56 |
|
|
interpreted as fill color. |
57 |
|
|
|
58 |
|
|
Next, there is always a 'Font' reference. Probably this defines |
59 |
|
|
the font for the label. This is not interpreted for Thuban. |
60 |
|
|
|
61 |
|
|
There is always a Size element. SinceThuban can currently not |
62 |
|
|
handle different sizes of point symbols, Size is used to set |
63 |
|
|
the line width. |
64 |
|
|
|
65 |
|
|
There is always a Angle element, but I don't know how this is |
66 |
|
|
defined. I only sighted the value of 360 so far. |
67 |
|
|
|
68 |
|
|
|
69 |
|
|
Finally, there seems always to be a 'Pattern'-List consisting of |
70 |
|
|
float values. No idea so far, how to interpret this (Thuban does |
71 |
|
|
not support pattern yet anyway). |
72 |
|
|
""" |
73 |
|
|
|
74 |
|
|
_obj_refs = [ 'Color', 'BgColor', 'Font' ] |
75 |
|
|
_values = [ 'Angle', 'Size', 'Pattern' ] |
76 |
|
|
|
77 |
|
|
def GetThubanProp(self): |
78 |
|
|
"""Create a Thuban ClassGroupProperty from this object and |
79 |
|
|
return it. |
80 |
|
|
|
81 |
|
|
In Thuban, the points have all the same size, |
82 |
|
|
but we can vary the width of the line. |
83 |
|
|
""" |
84 |
|
|
prop = ClassGroupProperties() |
85 |
|
|
prop.SetLineWidth(int(ceil(float(self.Get('Size'))))) |
86 |
|
|
prop.SetLineColor(self.Get('Color').GetThubanColor()) |
87 |
|
|
prop.SetFill(self.Get('BgColor').GetThubanColor()) |
88 |
|
|
return prop |
89 |
|
|
|
90 |
|
|
class APR_BShSym(ODBBaseObject): |
91 |
|
|
"""Polygon symbol object. |
92 |
|
|
Always references TClr objects via 'Color', 'OutlineColor' and 'BgColor'. |
93 |
|
|
Always has attributes 'OutlineWidth' and 'Outline'. |
94 |
|
|
|
95 |
|
|
OutlineColor is interpreted to be the Thuban line color, OutlineWidth |
96 |
|
|
as the Thuban line width. |
97 |
|
|
'Color' is interpreted to be the Thuban fill color. |
98 |
|
|
'BgColor' is not interpreted and it is not clear what this color |
99 |
|
|
defines. |
100 |
|
|
|
101 |
|
|
It is unclear what 'Outline' defines and thus is not used for Tuban. |
102 |
|
|
""" |
103 |
|
|
_obj_refs = [ 'Color', 'OutlineColor', 'BgColor' ] |
104 |
|
|
_values = [ 'OutlineWidth', 'Outline' ] |
105 |
|
|
|
106 |
|
|
def GetThubanProp(self): |
107 |
|
|
"""Create a Thuban ClassGroupProperty from this object and |
108 |
|
|
return it. |
109 |
|
|
""" |
110 |
|
|
prop = ClassGroupProperties() |
111 |
|
|
prop.SetLineWidth(int(ceil(float(self.Get('OutlineWidth'))))) |
112 |
|
|
prop.SetLineColor(self.Get('OutlineColor').GetThubanColor()) |
113 |
|
|
prop.SetFill(self.Get('Color').GetThubanColor()) |
114 |
|
|
return prop |
115 |
|
|
|
116 |
jan |
1725 |
class APR_LClass(ODBBaseObject): |
117 |
|
|
"""This object describes the range and label of a class |
118 |
|
|
within a legend. |
119 |
|
|
|
120 |
|
|
'IsText' determines whether 'MinStr'/'MaxStr' are given, else |
121 |
|
|
'MinNum'/'MaxNum' should be there. |
122 |
|
|
So far, only String-Ranges with identical 'MinStr'/'MaxStr' have |
123 |
|
|
been sighted. |
124 |
|
|
|
125 |
|
|
'MinNum' may not be there. In this case assume it to be -oo. |
126 |
|
|
|
127 |
|
|
There are objects with 'IsNoData' set to 1. Not yet sure how to |
128 |
|
|
treat them. |
129 |
|
|
|
130 |
|
|
However, objects have been sighted that only have 'IsText' and |
131 |
|
|
'Precision': We assume an empty label and a full range. |
132 |
|
|
|
133 |
|
|
No referenced objects. |
134 |
|
|
""" |
135 |
|
|
_obj_refs = [ ] |
136 |
|
|
_values = [ 'IsText', 'MinStr', 'MaxStr', 'MinNum', 'MaxNum', 'Label', |
137 |
|
|
'Precision', 'IsNoData' ] |
138 |
|
|
|
139 |
|
|
def GetThubanRange(self): |
140 |
jan |
1733 |
"""Return a Thuban range that corresponds to this object. |
141 |
|
|
|
142 |
|
|
The returned object is a |
143 |
|
|
- Range-Object in case of a numerical range. |
144 |
|
|
- String-Object in case of a text range (assuming that |
145 |
|
|
text objects occur only with 'MinStr' == 'MaxStr'. |
146 |
|
|
""" |
147 |
|
|
if hasattr(self, 'IsText'): |
148 |
|
|
if hasattr(self, 'MinStr'): |
149 |
|
|
return self.MinStr |
150 |
|
|
else: |
151 |
|
|
return '' |
152 |
|
|
|
153 |
jan |
1725 |
# build range |
154 |
|
|
if hasattr(self, 'MinNum'): |
155 |
|
|
range_str = ']' + self.MinNum + ';' |
156 |
|
|
else: |
157 |
|
|
range_str = ']-oo;' |
158 |
|
|
if hasattr(self, 'MaxNum'): |
159 |
|
|
range_str = range_str + self.MaxNum + ']' |
160 |
|
|
else: |
161 |
|
|
range_str = None |
162 |
|
|
|
163 |
|
|
if hasattr(self, 'MinNum') and hasattr(self, 'MaxNum'): |
164 |
|
|
if self.MinNum == self.MaxNum: |
165 |
|
|
range_str = '[' + self.MinNum + ';' + self.MaxNum + ']' |
166 |
|
|
return Range(range_str) |
167 |
|
|
|
168 |
|
|
def GetLabel(self): |
169 |
|
|
"""Return the label string. |
170 |
|
|
Return an empty string if there is no 'Label'. |
171 |
|
|
""" |
172 |
|
|
if hasattr(self, 'Label'): |
173 |
|
|
return self.Label |
174 |
|
|
else: |
175 |
|
|
return '' |
176 |
|
|
|
177 |
|
|
|
178 |
|
|
class APR_TClr(ODBBaseObject): |
179 |
|
|
"""Color object. Appears in 3 styles: |
180 |
|
|
1. no attributes: (so far assumed as black) |
181 |
|
|
2. only 'Name': a string that describes the color. |
182 |
|
|
Seen only "Transparent". |
183 |
|
|
3. 'Red', 'Green', 'Blue': RGB code. Each value in '0xffff' style. |
184 |
|
|
3.1 Only one or two of the colors are defined. It is assumed |
185 |
|
|
that in this case the rest is equal to 0x0000 |
186 |
|
|
3.2 Some hex-codes are incomplete (eg. 0xff). It is assumed |
187 |
|
|
that the missing digits are "0". |
188 |
|
|
|
189 |
|
|
No referenced objects. |
190 |
|
|
""" |
191 |
|
|
_obj_refs = [ ] |
192 |
|
|
_values = [ 'Name', 'Red', 'Green', 'Blue' ] |
193 |
|
|
|
194 |
|
|
def GetThubanColor(self): |
195 |
|
|
"""Return a Thuban Color object; returns None if a problem |
196 |
|
|
occured. |
197 |
|
|
""" |
198 |
|
|
if hasattr(self, 'Red') or hasattr(self, 'Green') or \ |
199 |
|
|
hasattr(self, 'Blue'): |
200 |
|
|
rgb = { 'Red': 0, 'Green': 0, 'Blue': 0 } # default for missing |
201 |
|
|
# parts: 0x0000 |
202 |
|
|
|
203 |
|
|
for color in [ 'Red', 'Green', 'Blue' ]: |
204 |
|
|
if hasattr(self, color): |
205 |
|
|
s = getattr(self, color) |
206 |
|
|
while len(s) < 6: |
207 |
|
|
s = s + '0' |
208 |
|
|
rgb[color] = int(s, 16)/float(int('0xffff', 16)) |
209 |
|
|
return Color(rgb['Red'], rgb['Green'], rgb['Blue']) |
210 |
|
|
elif hasattr(self, 'Name'): |
211 |
|
|
if self.Name == 'Transparent': |
212 |
|
|
return Transparent |
213 |
|
|
else: |
214 |
|
|
return None |
215 |
|
|
else: |
216 |
|
|
return Black |