1 |
# -*- coding:latin1 -*- |
2 |
# Copyright (C) 2004 by Intevation GmbH |
3 |
# Authors: |
4 |
# Jan Schüngel <[email protected]> |
5 |
# |
6 |
# This program is free software under the GPL (>=v2) |
7 |
# Read the file COPYING coming with Thuban for details. |
8 |
|
9 |
""" |
10 |
Classes to represent '.map'-file Objects. |
11 |
|
12 |
The following Classes, which are implemented in |
13 |
mapscript are not implemented yet in this extension: |
14 |
|
15 |
DBFInfo, errorObj, fontSetObj, graticuleObj, imageObj, itemObj, |
16 |
labelCacheMemberObj, labelCacheObj, lineObj, |
17 |
markerCacheMembet, msTiledSHPLayerInfo, OutputFormat, pointObj, queryMapObj, |
18 |
referenzMapObj, resultCacheMemberObj, resultCacheObj, scalebarObj, |
19 |
shapefileObj, shapeObj, VectorObj, WebObj |
20 |
""" |
21 |
|
22 |
__version__ = "$Revision$" |
23 |
# $Source$ |
24 |
# $Id$ |
25 |
|
26 |
|
27 |
# ################################################## |
28 |
# |
29 |
# import necessary modules from python and/or thuban |
30 |
# |
31 |
# ################################################## |
32 |
|
33 |
import os |
34 |
|
35 |
from Thuban.Model.color import Color, Transparent |
36 |
|
37 |
from mapscript import layerObj, classObj, colorObj, styleObj, rectObj |
38 |
|
39 |
|
40 |
# ################################### |
41 |
# |
42 |
# Definition of dictionaries |
43 |
# |
44 |
# ################################### |
45 |
|
46 |
shp_type = { 0:'point', |
47 |
1:'line', |
48 |
2:'polygon', |
49 |
3:'raster', |
50 |
4:'annotation', |
51 |
5:'circle', |
52 |
6:'query'} |
53 |
|
54 |
unit_type = { 0:"inches", |
55 |
1:"feet", |
56 |
2:"miles", |
57 |
3:"meters", |
58 |
4:"kilometers", |
59 |
5:"dd"} |
60 |
|
61 |
legend_status_type = { 0:"OFF", |
62 |
1:"ON", |
63 |
3:"embed" } |
64 |
# 2 = Default but will not be imported because |
65 |
# mapscript dont support it and its not allowed |
66 |
|
67 |
layer_status_type = { 0:"OFF", |
68 |
1:"ON", |
69 |
2:"default"} |
70 |
|
71 |
legend_position_type = { 0:"ul", |
72 |
1:"lr", |
73 |
2:"ur", |
74 |
3:"ll", |
75 |
6:"uc", |
76 |
7:"lc"} |
77 |
|
78 |
label_size_type = { 0:"tiny", |
79 |
1:"small", |
80 |
2:"medium", |
81 |
3:"large", |
82 |
4:"giant" } |
83 |
|
84 |
label_font_type = { 0:"truetype", |
85 |
1:"bitmap" } |
86 |
|
87 |
|
88 |
# ################################################## |
89 |
# |
90 |
# Class Definition |
91 |
# |
92 |
# ################################################## |
93 |
|
94 |
# ################################################## |
95 |
# General Classes that are not all explicitly defined through |
96 |
# a mapfile, but rather some helper-classes. |
97 |
|
98 |
class MF_Rectangle: |
99 |
""" |
100 |
Represents an rectanle with the bottom left |
101 |
and top right corner. |
102 |
""" |
103 |
def __init__(self,mf_rect): |
104 |
self._rect = mf_rect |
105 |
|
106 |
def get_minx(self): |
107 |
return self._rect.minx |
108 |
|
109 |
def get_miny(self): |
110 |
return self._rect.miny |
111 |
|
112 |
def get_maxx(self): |
113 |
return self._rect.maxx |
114 |
|
115 |
def get_maxy(self): |
116 |
return self._rect.maxy |
117 |
|
118 |
def get_rect(self): |
119 |
return (self._rect.minx,self._rect.miny,self._rect.maxx,self._rect.maxy) |
120 |
|
121 |
def set_rect(self, minx, miny, maxx, maxy): |
122 |
self._rect.minx = minx |
123 |
self._rect.miny = miny |
124 |
self._rect.maxx = maxx |
125 |
self._rect.maxy = maxy |
126 |
|
127 |
class MF_Color: |
128 |
""" |
129 |
The corresponding MapScript object contains also the |
130 |
attribute pen which defines the stroke of the feature. |
131 |
But this actually has nothing to do with the color and |
132 |
therefore is not support here. |
133 |
|
134 |
It needs to be discussed with the MapServer developers |
135 |
whether pen would better be moved to another class. |
136 |
|
137 |
The hex color definition which is also supported by |
138 |
mapscript and Thuban is not supported as it does |
139 |
not add any capability. |
140 |
|
141 |
color is definied as RGB 0..255 |
142 |
""" |
143 |
def __init__(self, mf_color): |
144 |
self._color = mf_color |
145 |
|
146 |
self._tbc_red = (float(self.get_red())/255) |
147 |
self._tbc_green = (float(self.get_green())/255) |
148 |
self._tbc_blue = (float(self.get_blue())/255) |
149 |
self._thubancolor = Color(self._tbc_red, self._tbc_green, self._tbc_blue) |
150 |
|
151 |
# TODO : Check if it is necessary to use rgb colors alone |
152 |
# or whether it is sufficient to only use the Thuban Color. |
153 |
# In some it is necessary as red == -1 indicates that no color |
154 |
# is set. |
155 |
def get_red(self): |
156 |
return self._color.red |
157 |
|
158 |
def get_green(self): |
159 |
return self._color.green |
160 |
|
161 |
def get_blue(self): |
162 |
return self._color.blue |
163 |
|
164 |
def set_rgbcolor(self, red, green, blue): |
165 |
self._color.red = red |
166 |
self._color.green = green |
167 |
self._color.blue = blue |
168 |
|
169 |
self._tbc_red = (float(self.get_red())/255) |
170 |
self._tbc_green = (float(self.get_green())/255) |
171 |
self._tbc_blue = (float(self.get_blue())/255) |
172 |
self._thubancolor = Color(self._tbc_red, self._tbc_green, self._tbc_blue) |
173 |
|
174 |
def get_mfcolor(self): |
175 |
return self._color |
176 |
|
177 |
def get_thubancolor(self): |
178 |
return self._thubancolor |
179 |
|
180 |
def set_thubancolor(self, thuban_color): |
181 |
if thuban_color != Transparent: |
182 |
self._color.red = int(thuban_color.red * 255) |
183 |
self._color.green = int(thuban_color.green * 255) |
184 |
self._color.blue = int(thuban_color.blue * 255) |
185 |
self._thubancolor = thuban_color |
186 |
|
187 |
|
188 |
class MF_Metadata: |
189 |
""" |
190 |
Metadata is not a Object in mapscript witch can be used |
191 |
by ease. Only the infos can get with the functions |
192 |
"getFirstMetaDataKey", "getNextMetaDataKey" and "getMetaData". |
193 |
To get some special Metadata you need a key. So the metadata will |
194 |
saved as an dictionary to geht the information with the key here. |
195 |
|
196 |
The metadata obj will be created and filled with infos like |
197 |
the following code sample: |
198 |
|
199 |
self.metadata = MF_Metadata() |
200 |
try: |
201 |
self.metafkey = varia.getFirstMetaDataKey() |
202 |
except: |
203 |
self.metadata = None |
204 |
else: |
205 |
while self.metafkey: |
206 |
self.metakeydata = varia.getMetaData(self.metafkey) |
207 |
self.metadata.add_metadata(self.metafkey,self.metakeydata) |
208 |
self.metafkey = varia.getNextMetaDataKey(self.metafkey) |
209 |
|
210 |
Metadata are not really needed at the moment. |
211 |
""" |
212 |
|
213 |
def __init__(self): |
214 |
self.data = {} |
215 |
|
216 |
def get_metadata(self): |
217 |
return self.data |
218 |
|
219 |
def get_metadatabykey(self, key): |
220 |
return self.data[key] |
221 |
|
222 |
def add_metadata(self, key, data): |
223 |
self.data[key] = data |
224 |
|
225 |
# ################################################## |
226 |
# Classes for MapServer Objects as they are |
227 |
# explicitly defined in a mapfile |
228 |
|
229 |
class MF_Symbol: |
230 |
""" |
231 |
defines a single symbol which is used in the Symbolset |
232 |
|
233 |
name, type, sizex, sizey, points, numpoints, filled, stylelength, |
234 |
style, imagepath, transparent, transparentcolor, character, antialias, |
235 |
font, gap, position, linecap, linejoin, linejoinmaxsize, setPoints(), |
236 |
getPoints(), setStyle() |
237 |
""" |
238 |
|
239 |
def __init__(self, mf_symbol): |
240 |
print "nothing" |
241 |
|
242 |
class MF_SymbolSet: |
243 |
""" |
244 |
defines a set of symbols, may be there can only be one |
245 |
|
246 |
filename, imagecachesize, numsymbols, symbol, getSymbol(), |
247 |
getSymbolByName(), index(), appendSymbol(), removeSymbol(), |
248 |
save() |
249 |
""" |
250 |
|
251 |
# TODO: include the symbolset, but found only the possibility to |
252 |
# create an extra symbol file and not to include it direct to the |
253 |
# mapfile itself |
254 |
def __init__(self, mf_symbolset): |
255 |
print "nothing" |
256 |
|
257 |
|
258 |
class MF_Class: |
259 |
""" |
260 |
The following parameters and functions, which the mapscript style obj |
261 |
contains, are used: |
262 |
styles, numstyles, name, |
263 |
getExpressionString(), setExpression(), getMetaData(), getFirstMetaDataKey(), |
264 |
getNextMetaDataKey(), getStyle() |
265 |
|
266 |
The following parameters and functions are not used: |
267 |
status, label, title, template, type, minscale, maxscale, layer, |
268 |
debig, keyimage, |
269 |
setExpression(), setText(), setMetaData(), drawLegendIcon(), |
270 |
createLegendIcon(), insertStyle(), removeStyle(), moveStyleUp(), |
271 |
moveStyleDown() |
272 |
""" |
273 |
def __init__(self, mf_class): |
274 |
""" |
275 |
Initialized a class from them given mapscript Class Object |
276 |
with a list of the included styles. |
277 |
Metadata Object will be created from the Metadata informations |
278 |
wich are holt as a List i think. |
279 |
""" |
280 |
self._clazz = mf_class |
281 |
self._styles = [] |
282 |
self._numstyles = mf_class.numstyles |
283 |
for i in range(0,self._numstyles,1): |
284 |
self._styles.append(MF_Style(mf_class.getStyle(i))) |
285 |
|
286 |
if self._clazz.getExpressionString() == '"(null)"': |
287 |
self._expression = None |
288 |
else: |
289 |
self._expression = self._clazz.getExpressionString() |
290 |
|
291 |
self.metadata = MF_Metadata() |
292 |
try: |
293 |
self.metafkey = mf_class.getFirstMetaDataKey() |
294 |
except: |
295 |
self.metadata = None |
296 |
else: |
297 |
while self.metafkey: |
298 |
self.metakeydata = mf_class.getMetaData(self.metafkey) |
299 |
self.metadata.add_metadata(self.metafkey,self.metakeydata) |
300 |
self.metafkey = mf_class.getNextMetaDataKey(self.metafkey) |
301 |
|
302 |
def get_styles(self): |
303 |
return self._styles |
304 |
|
305 |
def get_name(self): |
306 |
return self._clazz.name |
307 |
|
308 |
def get_keyimage(self): |
309 |
return self._clazz.keyimage |
310 |
|
311 |
def get_expressionstring(self): |
312 |
return self._expression |
313 |
|
314 |
def set_name(self, newname): |
315 |
self._clazz.name = newname |
316 |
|
317 |
def set_expressionstring(self, newstring): |
318 |
self._clazz.setExpression(newstring) |
319 |
self._expression = self._clazz.getExpressionString() |
320 |
|
321 |
def add_thubanstyle(self, tb_style, type="default"): |
322 |
""" |
323 |
added a thuban style object to the mapobject |
324 |
""" |
325 |
new_styleobj = MF_Style(styleObj(self._clazz)) |
326 |
if type == "line": |
327 |
new_styleobj.set_color(tb_style.GetLineColor()) |
328 |
elif type == "point": |
329 |
# set a default symbol to show circles not only a small dot |
330 |
# symbol "circle" must create before |
331 |
# TODO: create a Symbol (more see MF_SymbolSet) |
332 |
# first the default symbol circle will be created and the size 8 |
333 |
new_styleobj.set_symbolname('circle') |
334 |
new_styleobj.set_size(8) |
335 |
if tb_style.GetLineColor() != Transparent: |
336 |
new_styleobj.set_linecolor(tb_style.GetLineColor()) |
337 |
new_styleobj.set_color(tb_style.GetFill()) |
338 |
else: |
339 |
new_styleobj.set_size(tb_style.GetLineWidth()) |
340 |
new_styleobj.set_linecolor(tb_style.GetLineColor()) |
341 |
new_styleobj.set_color(tb_style.GetFill()) |
342 |
|
343 |
|
344 |
class MF_Layer: |
345 |
""" |
346 |
The following parameters and functions, which the mapscript style obj |
347 |
contains, are used: |
348 |
|
349 |
classitem, numclasses, name, data, type |
350 |
getClass(), getProjection(), getExtent(), getMetaData(), |
351 |
getFirstMetaDataKey(), getNextMetaDataKey(), status, |
352 |
|
353 |
|
354 |
The following paramters and functions are not used: |
355 |
index, map, header, footer, template, groupe, tolerance, |
356 |
toleranceunits, symbolscale, minscale, maxscale, labelminscale |
357 |
labelmaxscale, sizeunits, maxfeatures, offsite, transform, labelcache |
358 |
postlabelcache, labelitem, labelsizeitem, labelangleitem, labelitemindex |
359 |
labelsizeitemindex, labelangleitemindex, tileitem, tileindex, units |
360 |
connection, connectiontype, numitems, filteritem, styleitem, requires |
361 |
labelrequires, transparency, dump, debug, numprocessing, numjoins, |
362 |
removeClass(), open(), close(), getShape(), getNumResults(), getResult() |
363 |
getItem(), promote(), demote(), draw(), drawQuery(), queryByAttributes() |
364 |
queryByPoint(), queryByRect(), queryByFeatures(), queryByShape(), |
365 |
setFilter(), setFilterString(), setWKTProjection(), setProjection() |
366 |
addFeature(), getNumFeatures(), setMetaData(), removeMetaData(), |
367 |
getWMSFeatureInfoURL(), executeWFSGetFeature(), applySLD(), applySLDURL() |
368 |
enerateSLD(), moveClassUp(), moveClassDown(), setProcessing(), |
369 |
getProcessing(), clearProcessing() |
370 |
""" |
371 |
|
372 |
def __init__(self, mf_layer): |
373 |
""" |
374 |
Creates the Layer Object from the mapscript Layer Object. |
375 |
the class objects in the layer object will be stored in |
376 |
an array. The metadata are created as a new object. |
377 |
""" |
378 |
|
379 |
self._mf_layer = mf_layer |
380 |
|
381 |
# Create Classes |
382 |
# there could be more then 1 |
383 |
self._numclasses = mf_layer.numclasses |
384 |
i = -1 |
385 |
self._classes = [] |
386 |
while i < self._numclasses-1: |
387 |
i += 1 |
388 |
self._classes.append(MF_Class(self._mf_layer.getClass(i))) |
389 |
|
390 |
self._projection = MF_Projection(self._mf_layer.getProjection()) |
391 |
|
392 |
# Variable extent will not used for RasterLayer |
393 |
# this variable is not necessary, because it comes directly |
394 |
# from the shp file |
395 |
try: |
396 |
self._extent = MF_Rectangle(self._mf_layer.getExtent()) |
397 |
except: |
398 |
self._extent = None |
399 |
|
400 |
# Create Metadata |
401 |
self._metadata = MF_Metadata() |
402 |
try: |
403 |
self._metafkey = self._mf_layer.getFirstMetaDataKey() |
404 |
except: |
405 |
self._metadata = None |
406 |
else: |
407 |
while self._metafkey: |
408 |
self._metakeydata = self._mf_layer.getMetaData(self._metafkey) |
409 |
self._metadata.add_metadata(self._metafkey,self._metakeydata) |
410 |
self._metafkey = self._mf_layer.getNextMetaDataKey(self._metafkey) |
411 |
|
412 |
def get_name(self): |
413 |
return self._mf_layer.name |
414 |
|
415 |
def get_data(self): |
416 |
return self._mf_layer.data |
417 |
|
418 |
def get_classes(self): |
419 |
return self._classes |
420 |
|
421 |
def get_type(self): |
422 |
return shp_type[self._mf_layer.type] |
423 |
|
424 |
def get_classitem(self): |
425 |
return self._mf_layer.classitem |
426 |
|
427 |
def get_projection(self): |
428 |
return self._projection |
429 |
|
430 |
def get_status(self): |
431 |
# returns a integer value |
432 |
# 0 = off, 1 = on, 2 = default(always on) |
433 |
if self._mf_layer.status == 0: |
434 |
return False |
435 |
else: |
436 |
return True |
437 |
#return self._mf_layer.status |
438 |
|
439 |
def set_name(self, newname): |
440 |
self._mf_layer.name = newname |
441 |
|
442 |
def set_data(self, newdata, type="shape"): |
443 |
if type == "raster": |
444 |
self._mf_layer.data = newdata |
445 |
else: |
446 |
self._mf_layer.data = newdata[:-4] |
447 |
|
448 |
|
449 |
def set_status(self, newstatus): |
450 |
# status can set to true or false from thuban. |
451 |
# but mapserver supports the default value |
452 |
self._mf_layer.status = newstatus |
453 |
|
454 |
def set_classitem(self, tb_field): |
455 |
self._mf_layer.classitem = tb_field |
456 |
|
457 |
def set_type(self, tb_type): |
458 |
# if type = arc its a in shapetype line |
459 |
if tb_type == "arc": |
460 |
self._mf_layer.type = 1 |
461 |
if tb_type == "raster": |
462 |
self._mf_layer.type = 3 |
463 |
if shp_type.has_key(tb_type): |
464 |
self._mf_layer.type = tb_type |
465 |
else: |
466 |
for shp_paar_nr in shp_type: |
467 |
if shp_type[shp_paar_nr] == tb_type: |
468 |
self._mf_layer.type = shp_paar_nr |
469 |
return |
470 |
|
471 |
def set_projection(self, newprojection): |
472 |
self._mfnewprojstring = "" |
473 |
if newprojection: |
474 |
self._newparams = newprojection.GetAllParameters() |
475 |
for field in self._newparams: |
476 |
self._mfnewprojstring = self._mfnewprojstring+ "," + field |
477 |
self._mf_layer.setProjection(self._mfnewprojstring[1:]) |
478 |
self._projection.set_projection(newprojection) |
479 |
|
480 |
def add_thubanclass(self, tb_class, type=""): |
481 |
""" |
482 |
Add a thuban class object |
483 |
""" |
484 |
new_class = MF_Class(classObj(self._mf_layer)) |
485 |
new_class.set_name(tb_class.GetLabel()) |
486 |
if self.get_type() == "line": |
487 |
new_class.add_thubanstyle(tb_class.GetProperties(), type="line") |
488 |
elif self.get_type() == "point": |
489 |
new_class.add_thubanstyle(tb_class.GetProperties(), type="point") |
490 |
else: |
491 |
new_class.add_thubanstyle(tb_class.GetProperties()) |
492 |
if (type == "default"): |
493 |
return |
494 |
elif (tb_class.Matches("DEFAULT")): |
495 |
new_class.set_expressionstring('/./') |
496 |
else: |
497 |
new_class.set_expressionstring(str(tb_class.GetValue())) |
498 |
self._classes.append(new_class) |
499 |
|
500 |
|
501 |
class MF_Map: |
502 |
""" |
503 |
The following parameters and functions, which the mapscript style obj |
504 |
contains, are used: |
505 |
|
506 |
name, numlayers, extent, shapepath, imagecolor, imagetype, units, getLayer, |
507 |
status, getProjection, getMetaData, getFirstMetaDataKey, getNextMetaDataKey, |
508 |
save(), setExtent(), height, width, setProjection() |
509 |
|
510 |
|
511 |
The following parameters and functions are not used: |
512 |
maxsize, layers, symbolset, fontset, labelcache, |
513 |
transparent, interlace, imagequality, cellsize, debug, datapattern, |
514 |
templatepattern, configoptions |
515 |
zoomPoint(), zoomRectangle(), zoomScale(), getLayerOrder(), setLayerOrder(), |
516 |
clone(), removeLayer(), getLayerByName(), getSymbolByName(), |
517 |
prepareQuery(), prepareImage(), setImageType(), setOutputFormat(), draw(), |
518 |
drawQuery(), drawLegend(), drawScalebar(), embedLegend(), drawLabelCache(), |
519 |
nextLabel(), queryByPoint(), queryByRecht(), queryByFeatures(), queryByShape(), |
520 |
setWKTProjection(), saveQuery(), saveQueryASGML(), |
521 |
setMetaData(), removeMetaData(), setSymbolSet(), getNumSymbols(), setFontSet(), |
522 |
saveMapContext(), loadMapContext(), moveLayerUp(), moveLayerDown(), |
523 |
getLayersDrawingOrder(), setLayersDrawingOrder(), setConfigOption(), |
524 |
getConfigOption(), applyConfigOptions(), applySLD(), applySLDURL(), gernerateSLD(), |
525 |
procecssTemplate(), processLegemdTemplate(), processQueryTemplate(), |
526 |
getOutputFormatByName(), appendOutputFormat(), removeOutputFormat(), |
527 |
""" |
528 |
|
529 |
def __init__(self, mf_map): |
530 |
""" |
531 |
Create the map object from the mapfile mapobject which is given. |
532 |
|
533 |
All layers in the mapfile will be written to an array. |
534 |
""" |
535 |
|
536 |
self._mf_map = mf_map |
537 |
self._extent = MF_Rectangle(self._mf_map.extent) |
538 |
self._imagecolor = MF_Color(self._mf_map.imagecolor) |
539 |
self._web = MF_Web(self._mf_map.web) |
540 |
self._legend = MF_Legend(self._mf_map.legend) |
541 |
|
542 |
# if the map name is not set it will return a MS string. |
543 |
if self._mf_map.name != "MS": |
544 |
self._name = self._mf_map.name |
545 |
else: |
546 |
self._name = None |
547 |
|
548 |
self._projection = MF_Projection(self._mf_map.getProjection()) |
549 |
|
550 |
# Initial Layer List |
551 |
self._layers = [] |
552 |
self._numlayers = self._mf_map.numlayers |
553 |
self._i = 0 |
554 |
while self._i < self._numlayers: |
555 |
self._layers.append(MF_Layer(self._mf_map.getLayer(self._i))) |
556 |
self._i += 1 |
557 |
|
558 |
# Shapepath if not set, shapepath will be empty |
559 |
if self._mf_map.shapepath: |
560 |
self._shapepath = self._mf_map.shapepath |
561 |
else: |
562 |
self._shapepath = "" |
563 |
|
564 |
# Create Metadata |
565 |
self._metadata = MF_Metadata() |
566 |
try: |
567 |
self._metafkey = self._mf_map.getFirstMetaDataKey() |
568 |
except: |
569 |
self._metadata = None |
570 |
else: |
571 |
while self._metafkey: |
572 |
self._metakeydata = self._mf_map.getMetaData(self._metafkey) |
573 |
self._metadata.add_metadata(self._metafkey,self._metakeydata) |
574 |
self._metafkey = self._mf_map.getNextMetaDataKey(self._metafkey) |
575 |
|
576 |
def get_status(self): |
577 |
if layer_status_type[self._mf_map.status] == "OFF": |
578 |
return false |
579 |
else: |
580 |
return true |
581 |
|
582 |
def get_web(self): |
583 |
return self._web |
584 |
|
585 |
def get_legend(self): |
586 |
return self._legend |
587 |
|
588 |
def get_extent(self): |
589 |
return self._extent |
590 |
|
591 |
def get_layers(self): |
592 |
return self._layers |
593 |
|
594 |
def get_projection(self): |
595 |
return self._projection |
596 |
|
597 |
def get_name(self): |
598 |
return self._name |
599 |
|
600 |
def get_shapepath(self): |
601 |
return self._shapepath # where are the shape files located.. |
602 |
|
603 |
def get_imagetype(self): |
604 |
return self._imagetype |
605 |
|
606 |
def get_layerorder(self): |
607 |
# shows the order of layer as list |
608 |
return self._mf_map.getLayerOrder() |
609 |
|
610 |
def get_size(self): |
611 |
#returns the size |
612 |
return (self._mf_map.width, self._mf_map.height) |
613 |
|
614 |
def get_units(self): |
615 |
#returns the unittype |
616 |
return unit_type[self._mf_map.units] |
617 |
|
618 |
def get_imagecolor(self): |
619 |
return self._imagecolor |
620 |
|
621 |
def set_name(self, newname): |
622 |
# whitespace musst be replaced, either no |
623 |
# mapfile will be shown in the mapserver |
624 |
newname = newname.replace(" ","_") |
625 |
self._name = newname |
626 |
self._mf_map.name = newname |
627 |
|
628 |
def set_extent(self, newextent): |
629 |
# TODO: add the shown extend here instead of the total |
630 |
self._newrect = MF_Rectangle(rectObj()) |
631 |
try: |
632 |
self._newrect.set_rect(newextent[0],newextent[1],newextent[2],newextent[3]) |
633 |
self._mf_map.setExtent(newextent[0],newextent[1],newextent[2],newextent[3]) |
634 |
except: |
635 |
return |
636 |
|
637 |
def set_size(self, newwidth, newheight): |
638 |
self._mf_map.width = newwidth |
639 |
self._mf_map.height = newheight |
640 |
|
641 |
def set_projection(self, projection): |
642 |
self._mfnewprojstring = "" |
643 |
self._newparams = projection.GetAllParameters() |
644 |
for field in self._newparams: |
645 |
self._mfnewprojstring = self._mfnewprojstring+ "," + field |
646 |
self._mf_map.setProjection(self._mfnewprojstring[1:]) |
647 |
self._projection.set_projection(projection) |
648 |
|
649 |
def set_units(self, units): |
650 |
if unit_type.has_key(units): |
651 |
self._mf_map.units = units |
652 |
else: |
653 |
for unit_paar_nr in unit_type: |
654 |
if unit_type[unit_paar_nr] == units: |
655 |
self._mf_map.units = unit_paar_nr |
656 |
|
657 |
def add_thubanlayer(self, tb_layer): |
658 |
""" |
659 |
Add a thuban layer |
660 |
""" |
661 |
new_layer = MF_Layer(layerObj(self._mf_map)) |
662 |
new_layer.set_name(tb_layer.Title()) |
663 |
|
664 |
# TODO: implement relative pathnames |
665 |
# yet only absolute pathnames in the LayerObj are set |
666 |
try: |
667 |
new_layer.set_data(tb_layer.ShapeStore().FileName()) |
668 |
except: |
669 |
new_layer.set_data(tb_layer.GetImageFilename(), type="raster") |
670 |
new_layer.set_type("raster") |
671 |
else: |
672 |
new_layer.set_status(tb_layer.Visible()) |
673 |
new_layer.set_type(tb_layer.ShapeType()) |
674 |
|
675 |
if tb_layer.GetClassificationColumn(): |
676 |
new_layer.set_classitem(tb_layer.GetClassificationColumn()) |
677 |
if tb_layer.GetProjection(): |
678 |
new_layer.set_projection(tb_layer.GetProjection()) |
679 |
if tb_layer.GetClassification().GetNumGroups() > 0: |
680 |
for group in range(0, tb_layer.GetClassification().GetNumGroups(), 1): |
681 |
new_layer.add_thubanclass(tb_layer.GetClassification().GetGroup(group)) |
682 |
new_layer.add_thubanclass(tb_layer.GetClassification().GetDefaultGroup()) |
683 |
else: |
684 |
new_layer.add_thubanclass(tb_layer.GetClassification().GetDefaultGroup(), type="default") |
685 |
|
686 |
# set the projection to the layer. |
687 |
# if the layer has its own definition use is, else use the main projection |
688 |
if tb_layer.GetProjection(): |
689 |
new_layer.set_projection(tb_layer.GetProjection()) |
690 |
else: |
691 |
new_layer.set_projection(self._projection.get_projection()) |
692 |
self._layers.append(new_layer) |
693 |
|
694 |
def remove_layer(self, nr): |
695 |
self._mf_map.removeLayer(nr) |
696 |
self._numlayers -= 1 |
697 |
self._layers.remove(self._layers[nr]) |
698 |
|
699 |
def save_map(self, filepath): |
700 |
# save the Map |
701 |
# maybe an own saver can implement here |
702 |
self._mf_map.save(filepath) |
703 |
|
704 |
|
705 |
class MF_Web: |
706 |
""" |
707 |
Save the Web settings |
708 |
|
709 |
The following parametes are used: |
710 |
imagepath, imageurl |
711 |
|
712 |
The following parameters are not used: |
713 |
log, map, template, queryformat, header, footer, empty, error, extent, |
714 |
minscale, maxscale, mintemplate, maxtemplate |
715 |
""" |
716 |
def __init__(self, mf_web): |
717 |
self._mf_web = mf_web |
718 |
|
719 |
def get_imagepath(self): |
720 |
return self._mf_web.imagepath |
721 |
|
722 |
def set_imagepath(self, new_imagepath): |
723 |
self._mf_web.imagepath = new_imagepath |
724 |
|
725 |
def get_imageurl(self): |
726 |
return self._mf_web.imageurl |
727 |
|
728 |
def set_imageurl(self, new_imageurl): |
729 |
self._mf_web.imagepath = new_imageurl |
730 |
|
731 |
def get_queryformat(self): |
732 |
return self._mf_web.queryformat |
733 |
|
734 |
def set_queryformat(self, new_queryformat): |
735 |
self._mf_web.imagepath = new_queryformat |
736 |
|
737 |
|
738 |
class MF_Label: |
739 |
""" |
740 |
The following parameters from mapscript are used: |
741 |
type, color, size, offsetx, offsety, |
742 |
|
743 |
The following parameters are not used: |
744 |
font, outlinecolor, shadowcolor, shadowsizex, shadowsizey, |
745 |
backgroundcolor, backgroundshadowcolor, backgroundshadowsizex, |
746 |
backgroundshadowsizey, sizescaled, minsize, maxsize, position, angle, |
747 |
autoangle, buffer, antialias, wrap, minfeaturesize, |
748 |
autominfeaturesize, mindistance, partials, force |
749 |
""" |
750 |
def __init__(self, mf_label): |
751 |
""" |
752 |
Create a legend obj from the existing mapfile |
753 |
""" |
754 |
self._label = mf_label |
755 |
self._color = MF_Color(self._label.color) |
756 |
|
757 |
def get_size(self): |
758 |
return self._label.size |
759 |
|
760 |
def set_size(self, new_size): |
761 |
if label_size_type.has_key(new_size): |
762 |
self._label.size = new_size |
763 |
for label_size_type_nr in label_size_type: |
764 |
if label_size_type[label_size_type_nr] == new_size: |
765 |
self._label.size = label_size_type_nr |
766 |
else: |
767 |
self._label.size = new_size |
768 |
|
769 |
def get_color(self): |
770 |
return self._color |
771 |
|
772 |
def get_type(self): |
773 |
return label_font_type[self._label.type] |
774 |
|
775 |
def set_type(self, new_type): |
776 |
if label_font_type.has_key(new_type): |
777 |
self._label.type = new_type |
778 |
else: |
779 |
for label_font_type_nr in label_font_type: |
780 |
if label_font_type[label_font_type_nr] == new_type: |
781 |
self._label.type = label_font_type_nr |
782 |
|
783 |
def get_offset(self): |
784 |
return (self._label.offsetx, self._label.offsety) |
785 |
|
786 |
def set_offset(self, new_offsetx, new_offsety): |
787 |
self._label.offsetx = new_offsetx |
788 |
self._label.offsety = new_offsety |
789 |
|
790 |
|
791 |
class MF_Legend: |
792 |
""" |
793 |
The following parameters are (not) used: |
794 |
imagecolor, label, keysizex, keysizey, status, position, |
795 |
|
796 |
The following parameters are not used: |
797 |
keyspacingx, keyspacingy, |
798 |
outlinecolor, height, width, postlabelcache, template, map |
799 |
""" |
800 |
def __init__(self, mf_legend): |
801 |
""" |
802 |
Create a legend obj from the existing mapfile |
803 |
""" |
804 |
self._mf_legend = mf_legend |
805 |
self._imagecolor = MF_Color(self._mf_legend.imagecolor) |
806 |
self._label = MF_Label(self._mf_legend.label) |
807 |
|
808 |
def get_imagecolor(self): |
809 |
return self._imagecolor |
810 |
|
811 |
def get_label(self): |
812 |
return self._label |
813 |
|
814 |
def get_keysize(self): |
815 |
return (self._mf_legend.keysizex, self._mf_legend.keysizey) |
816 |
|
817 |
def set_keysize(self, new_keysizex, new_keysizey): |
818 |
self._mf_legend.keysizex = new_keysizex |
819 |
self._mf_legend.keysizey = new_keysizey |
820 |
|
821 |
def get_keyspacing(self): |
822 |
return (self._mf_legend.keyspacingx, self._mf_legend.keyspacingy) |
823 |
|
824 |
def set_keyspacing(self, new_keyspacingx, new_keyspacingy): |
825 |
self._mf_legend.keyspacingx = new_keyspacingx |
826 |
self._mf_legend.keyspacingy = new_keyspacingy |
827 |
|
828 |
def get_status(self, art="integer"): |
829 |
if art == "string": |
830 |
return legend_status_type[self._mf_legend.status] |
831 |
else: |
832 |
return self._mf_legend.status |
833 |
|
834 |
def set_status(self, new_status): |
835 |
if legend_status_type.has_key(new_status): |
836 |
self._mf_legend.status = new_status |
837 |
else: |
838 |
for legend_status_type_nr in legend_status_type: |
839 |
if legend_status_type[legend_status_type_nr] == new_status: |
840 |
self._mf_legend.status = legend_status_type_nr |
841 |
|
842 |
def get_position(self, art="integer"): |
843 |
if art == "string": |
844 |
return legend_position_type[self._mf_legend.position] |
845 |
else: |
846 |
return self._mf_legend.position |
847 |
|
848 |
def set_position(self, new_position): |
849 |
if legend_position_type.has_key(new_position): |
850 |
self._mf_legend.position = new_position |
851 |
else: |
852 |
for legend_position_type_nr in legend_position_type: |
853 |
if legend_position_type[legend_position_type_nr] == new_position: |
854 |
self._mf_legend.position = legend_position_type_nr |
855 |
|
856 |
|
857 |
class MF_Projection: |
858 |
""" |
859 |
The following parameter, which the mapscript style obj contains is used: |
860 |
|
861 |
numargs |
862 |
""" |
863 |
|
864 |
def __init__(self, mf_projection): |
865 |
""" |
866 |
Create a projection object from the given mapscript projection |
867 |
object. If it is a epsg code the extracted from the string |
868 |
(e.g."init=epsg:xxxx"), else the projection parameters will |
869 |
be splitted and an array with the parameters will be creaded. |
870 |
""" |
871 |
self._mfprojstring = mf_projection |
872 |
self._projstring = self._mfprojstring |
873 |
self._epsgcode = None |
874 |
self._params = None |
875 |
if self._mfprojstring: |
876 |
if self._mfprojstring.find("init=epsg:") != -1: |
877 |
self._initcode, self._epsgcode = self._mfprojstring.split(':') |
878 |
else: |
879 |
self._params = [] |
880 |
self._params = self._mfprojstring.split("+") |
881 |
if self._params[0] == "": |
882 |
self._params.remove("") |
883 |
|
884 |
def epsg_code_to_projection(self, epsg): |
885 |
""" |
886 |
Find the projection for the given epsg code. |
887 |
|
888 |
Copied from Extension/wms/layer.py |
889 |
|
890 |
epsg -- EPSG code as string |
891 |
""" |
892 |
#Needed only for this function |
893 |
from Thuban.Model.resource import get_system_proj_file, EPSG_PROJ_FILE, \ |
894 |
EPSG_DEPRECATED_PROJ_FILE |
895 |
|
896 |
proj_file, warnings = get_system_proj_file(EPSG_PROJ_FILE) |
897 |
|
898 |
for proj in proj_file.GetProjections(): |
899 |
if proj.EPSGCode() == epsg: |
900 |
return proj |
901 |
|
902 |
proj_file, warnings = get_system_proj_file(EPSG_DEPRECATED_PROJ_FILE) |
903 |
for proj in proj_file.GetProjections(): |
904 |
if proj.EPSGCode() == epsg: |
905 |
return proj |
906 |
return None |
907 |
|
908 |
def get_params(self): |
909 |
#Parameter from the mf_projectionstring as Array |
910 |
return self._params |
911 |
|
912 |
def get_epsgcode(self): |
913 |
# returnes the epsg number |
914 |
return self._epsgcode |
915 |
|
916 |
def get_epsgproj(self): |
917 |
# get an epsg projectionobject |
918 |
return self.epsg_code_to_projection(self._epsgcode) |
919 |
|
920 |
def get_projection(self): |
921 |
return self._projstring |
922 |
|
923 |
def set_projection(self, newprojection): |
924 |
self._projstring = newprojection |
925 |
self._params = newprojection.GetAllParameters() |
926 |
self._mfnewprojstring = "" |
927 |
for field in self._params: |
928 |
self._mfnewprojstring = self._mfnewprojstring+ "+" + field |
929 |
self._mfprojstring = self._mfnewprojstring |
930 |
|
931 |
|
932 |
class MF_Style: |
933 |
""" |
934 |
The following parameters, which the mapscript style obj |
935 |
contains, are used: |
936 |
color, backgroundcolor, outlinecolor, size, symbolname |
937 |
|
938 |
The following are not used: |
939 |
symbol, sizescaled, minsize, maxsize, offsetx, offsety, |
940 |
antialias |
941 |
""" |
942 |
|
943 |
def __init__(self, mf_style): |
944 |
""" |
945 |
Create a style object from the given mapscript style object. |
946 |
The color Object from the color and the outlinecolor parameter |
947 |
will be created. if the color (red, green or blue) is -1 there |
948 |
is no definition in the mapfile and so there is no color object, |
949 |
it will set to 'None'. |
950 |
""" |
951 |
self._style = mf_style |
952 |
if self._style.color.red == -1: |
953 |
self._color = None |
954 |
else: |
955 |
self._color = MF_Color(self._style.color) |
956 |
if self._style.outlinecolor.red == -1: |
957 |
self._outlinecolor = None |
958 |
else: |
959 |
self._outlinecolor = MF_Color(self._style.outlinecolor) |
960 |
|
961 |
def get_color(self): |
962 |
return self._color |
963 |
|
964 |
def get_outlinecolor(self): |
965 |
return self._outlinecolor |
966 |
|
967 |
def get_size(self): |
968 |
return self._style.size |
969 |
|
970 |
def set_linecolor(self, tb_color): |
971 |
self._color = tb_color |
972 |
new_linecolor = MF_Color(colorObj()) |
973 |
new_linecolor.set_thubancolor(tb_color) |
974 |
self._outlinecolor = new_linecolor |
975 |
self._style.outlinecolor = new_linecolor.get_mfcolor() |
976 |
|
977 |
def set_color(self, tb_color): |
978 |
self._color = tb_color |
979 |
new_color = MF_Color(colorObj()) |
980 |
new_color.set_thubancolor(tb_color) |
981 |
self._color = new_color |
982 |
self._style.color = new_color.get_mfcolor() |
983 |
|
984 |
def set_size(self, newsize): |
985 |
self._style.size = newsize |
986 |
|
987 |
def set_symbolname(self, newsymbol): |
988 |
# its possible to use stringnames instead of numbers |
989 |
self._style.symbolname = newsymbol |
990 |
|