/[thuban]/trunk/thuban/Extensions/umn_mapserver/mapfile.py
ViewVC logotype

Contents of /trunk/thuban/Extensions/umn_mapserver/mapfile.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2316 - (show annotations)
Tue Aug 3 11:46:00 2004 UTC (20 years, 7 months ago) by jschuengel
File MIME type: text/x-python
File size: 48015 byte(s)
(MF_Metadata): Added a function to remove all metadata.
(MF_Layer): Added two functions to handle the group setting.
Removed the extra numlayers variable, used the mapscript parameter instead.
(MF_Map): Added a function to get the numlayers parameter.
Added a funtion to remove all layers.
(MF_Map.add_thubanlayer): Replaced the exception handling by a check if the object is an instance. Also added the annotation layer here to export, but only the layer is created in the mapfile.

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,
17 markerCacheMembet, msTiledSHPLayerInfo, queryMapObj,
18 referenzMapObj, resultCacheMemberObj, resultCacheObj,
19 shapefileObj, shapeObj, VectorObj
20
21 the following are only used to create a necessary object. They are not
22 realy created as a MF_Object.
23
24 lineObj, pointObj
25 """
26
27 __version__ = "$Revision$"
28 # $Source$
29 # $Id$
30
31
32 # ##################################################
33 #
34 # import necessary modules from python and/or thuban
35 #
36 # ##################################################
37
38 import os
39
40 from Thuban.Model.color import Color, Transparent
41
42 from Thuban.Model.classification import ClassGroupDefault, \
43 ClassGroupSingleton, ClassGroupRange
44
45 from Thuban.Model.layer import RasterLayer
46 from Extensions.umn_mapserver.mf_import import AnnotationLayer
47
48 from mapscript import layerObj, classObj, colorObj, styleObj, rectObj, symbolObj, \
49 pointObj, lineObj
50
51 # ###################################
52 #
53 # Definition of dictionaries
54 #
55 # the dictonaries are like in mapscript and are used to make it
56 # easear to unterstand the key from mapscript for the settings
57 #
58 # ###################################
59
60 shp_type = { 0:'point',
61 1:'line',
62 2:'polygon',
63 3:'raster',
64 4:'annotation',
65 5:'circle',
66 6:'query'}
67
68 unit_type = { 0:"inches",
69 1:"feet",
70 2:"miles",
71 3:"meters",
72 4:"kilometers",
73 5:"dd"}
74
75 legend_status_type = { 0:"OFF",
76 1:"ON",
77 3:"embed" }
78 # 2 = Default but is not allowed here
79
80 scalebar_status_type = { 0:"OFF",
81 1:"ON",
82 3:"embed" }
83 # 2 = Default but is not allowed here
84
85 scalebar_style_type = { 0:"0",
86 1:"1" }
87
88 scalebar_position_type = { 0:"ul",
89 1:"lr",
90 2:"ur",
91 3:"ll",
92 6:"uc",
93 7:"lc"}
94
95 layer_status_type = { 0:"OFF",
96 1:"ON",
97 2:"default"}
98
99 legend_position_type = { 0:"ul",
100 1:"lr",
101 2:"ur",
102 3:"ll",
103 6:"uc",
104 7:"lc"}
105
106 label_size_type = { 0:"tiny",
107 1:"small",
108 2:"medium",
109 3:"large",
110 4:"giant" }
111
112 #TODO: build in truetype (0:"truetype") support
113 label_font_type = { 1:"bitmap" }
114
115 label_position_type = { 0:"ul",
116 1:"lr",
117 2:"ur",
118 3:"ll",
119 4:"cr",
120 5:"cl",
121 6:"uc",
122 7:"lc",
123 8:"cc",
124 10:"auto"}
125
126
127 # ##################################################
128 #
129 # Class Definition
130 #
131 # ##################################################
132
133 # ##################################################
134 # General Classes that are not all explicitly defined through
135 # a mapfile, but rather some helper-classes.
136
137 class MF_Rectangle:
138 """
139 Represents an rectanle with the bottom left
140 and top right corner.
141 """
142 def __init__(self,mf_rect):
143 self._rect = mf_rect
144
145 def get_minx(self):
146 return self._rect.minx
147
148 def get_miny(self):
149 return self._rect.miny
150
151 def get_maxx(self):
152 return self._rect.maxx
153
154 def get_maxy(self):
155 return self._rect.maxy
156
157 def get_rect(self):
158 return (self._rect.minx,self._rect.miny,self._rect.maxx,self._rect.maxy)
159
160 def set_rect(self, minx, miny, maxx, maxy):
161 self._rect.minx = minx
162 self._rect.miny = miny
163 self._rect.maxx = maxx
164 self._rect.maxy = maxy
165
166 class MF_Color:
167 """
168 The corresponding MapScript object contains also the
169 attribute pen which defines the stroke of the feature.
170 But this actually has nothing to do with the color and
171 therefore is not support here.
172
173 It needs to be discussed with the MapServer developers
174 whether pen would better be moved to another class.
175
176 The hex color definition which is also supported by
177 mapscript and Thuban is not supported as it does
178 not add any capability.
179
180 color is definied as RGB 0..255
181 """
182 def __init__(self, mf_color):
183 self._color = mf_color
184 self._tbc_red = (float(self.get_red())/255)
185 self._tbc_green = (float(self.get_green())/255)
186 self._tbc_blue = (float(self.get_blue())/255)
187 self._thubancolor = Color(self._tbc_red,
188 self._tbc_green,
189 self._tbc_blue)
190
191 # TODO : Check if it is necessary to use rgb colors alone
192 # or whether it is sufficient to only use the Thuban Color.
193 # In some it is necessary as red == -1 indicates that no color
194 # is set.
195 def get_red(self):
196 return self._color.red
197
198 def get_green(self):
199 return self._color.green
200
201 def get_blue(self):
202 return self._color.blue
203
204 def set_rgbcolor(self, red, green, blue):
205 self._color.red = red
206 self._color.green = green
207 self._color.blue = blue
208
209 self._tbc_red = (float(self.get_red())/255)
210 self._tbc_green = (float(self.get_green())/255)
211 self._tbc_blue = (float(self.get_blue())/255)
212 self._thubancolor = Color(self._tbc_red,
213 self._tbc_green,
214 self._tbc_blue)
215
216 def get_mfcolor(self):
217 return self._color
218
219 def get_thubancolor(self):
220 return self._thubancolor
221
222 def set_thubancolor(self, thuban_color):
223 if thuban_color != Transparent:
224 self._color.red = int(thuban_color.red * 255)
225 self._color.green = int(thuban_color.green * 255)
226 self._color.blue = int(thuban_color.blue * 255)
227 self._thubancolor = thuban_color
228
229
230 class MF_Metadata:
231 """
232 Metadata is not a Object in mapscript witch can be used
233 by ease. Only the infos can get with the functions
234 "getFirstMetaDataKey", "getNextMetaDataKey" and "getMetaData".
235 To get some special Metadata you need a key. So there is a special
236 function which create a list of the metadatakeys.
237 """
238 def __init__(self, mapobj):
239 self.mapobj = mapobj
240
241 def remove_allmetadata(self):
242 keylist = self.get_metadatakeys()
243 if keylist:
244 for key in keylist:
245 self.mapobj.removeMetaData(key)
246
247 def get_metadatakeys(self):
248 keylist = []
249 try:
250 metafkey =self.mapobj.getFirstMetaDataKey()
251 keylist.append(metafkey)
252 except:
253 return None
254 else:
255 if metafkey:
256 while metafkey:
257 metafkey = self.mapobj.getNextMetaDataKey(metafkey)
258 if metafkey:
259 keylist.append(metafkey)
260 return keylist
261
262 def get_metadata(self):
263 keylist = self.get_metadatakeys()
264 metadatalist = []
265 if keylist:
266 for key in keylist:
267 metadatalist.append([key,self.mapobj.getMetaData(key)])
268 return metadatalist
269 else:
270 return None
271
272 def get_metadatabykey(self, key):
273 return self.mapobj.getMetaData(key)
274
275 def remove_metadatabykey(self, key):
276 self.mapobj.removeMetaData(key)
277
278 def add_metadata(self, key, data):
279 self.mapobj.setMetaData(key,data)
280
281 # ################################################
282 # Classes for MapServer Objects as they are
283 # explicitly defined in a mapfile
284
285 class MF_Outputformat:
286 """
287 The Outputformat defines which and how the image is
288 created by the mapserver.
289
290 The following settings are used:
291 name
292
293 The following settings are not used:
294 mimetye, driver, extension, renderer, imagemode, transparent,
295 bands, numfrotmatoptions, formatoptions, refcount, inmapfile
296 setExtension(), setMimetype(), setOption(), getOption()
297 """
298 def __init__(self, mf_outputformat):
299 self._outputformat = mf_outputformat
300
301 def get_name(self):
302 return self._outputformat.name
303
304
305 class MF_Symbol:
306 """
307 defines a single symbol which is used in the Symbolset
308
309 the following settings are used:
310 name, type,
311
312 the following settings are not used:
313 sizex, sizey, points, numpoints, filled, stylelength,
314 style, imagepath, transparent, transparentcolor, character, antialias,
315 font, gap, position, linecap, linejoin, linejoinmaxsize, setPoints(),
316 getPoints(), setStyle()
317 """
318 def __init__(self, mf_symbol = "newone"):
319 # create a circle Object like shown in Thuban
320 # because Thuban don't support other symbols
321
322 # TODO: include the options to create a symbol, but
323 # first implement a methode to edit Symbols in Thuban
324 if mf_symbol == "newone":
325 mf_symbol = symbolObj("")
326 newpoint = pointObj()
327 newpoint.x = 1
328 newpoint.y = 1
329 newline = lineObj()
330 newline.add(newpoint)
331 mf_symbol.setPoints(newline)
332
333 self._symbol = mf_symbol
334
335 def get_symbolObj(self):
336 return self._symbol
337
338 def get_name(self):
339 return self._symbol.name
340
341 def set_name(self, new_name):
342 self._symbol.name = new_name
343
344 def get_type(self):
345 return self._symbol.type
346
347 def set_type(self, new_type):
348 # TODO include a function to set the type by a string
349 self._symbol.type = new_type
350
351 def get_filled(self):
352 return self._symbol.filled
353
354 def set_filled(self, new_filled):
355 if new_filled:
356 self._symbol.filled = 1
357 else:
358 self._symbol.filled = 0
359
360
361 class MF_SymbolSet:
362 """
363 defines a set of symbols, may be there can only be one
364
365 the following settings are used:
366 numsymbols,
367 appendSymbol()
368
369 filename, imagecachesize, symbol, getSymbol(),
370 getSymbolByName(), index(), removeSymbol(),
371 save()
372 """
373 def __init__(self, mf_symbolset):
374 self._symbolset = mf_symbolset
375
376 # Initial Symbol List
377 self._symbols = []
378 self._i = 1
379 while self._i < self._symbolset.numsymbols:
380 self._symbols.append(MF_Symbol(self._symbolset.getSymbol(self._i)))
381 self._i += 1
382
383 def add_symbol(self, new_symbol):
384 self._symbolset.appendSymbol(new_symbol.get_symbolObj())
385 self._symbols.append(new_symbol)
386 # the save function must be run to set the symbols to the
387 # mapfile. I don't know why this ist so but it must be.
388 # the file is empty then an we can delete it
389 self._symbolset.save("tempsymbol")
390 os.remove("tempsymbol")
391
392 def get_symbol(self, symbolnr):
393 if symbolnr < self._symbolset.numsymbols:
394 return self._symbols[symbolnr-1]
395 else:
396 return None
397
398
399 class MF_Class:
400 """
401 The following parameters and functions, which the mapscript style obj
402 contains, are used:
403 styles, numstyles, name, status, keyimage, layer,
404 getExpressionString(), setExpression(), getMetaData(), getFirstMetaDataKey(),
405 getNextMetaDataKey(), getStyle()
406
407 The following parameters and functions are not used:
408 label, title, template, type, minscale, maxscale, debug,
409 setExpression(), setText(), setMetaData(), drawLegendIcon(),
410 createLegendIcon(), insertStyle(), removeStyle(), moveStyleUp(),
411 moveStyleDown()
412 """
413 def __init__(self, mf_class):
414 """
415 Initialized a class from them given mapscript Class Object
416 with a list of the included styles.
417 Metadata Object will be created from the Metadata informations
418 wich are holt as a List i think.
419 """
420 self._clazz = mf_class
421 self._styles = []
422 self._numstyles = mf_class.numstyles
423 for i in range(0,self._numstyles,1):
424 self._styles.append(MF_Style(mf_class.getStyle(i)))
425
426 if self._clazz.getExpressionString() == '"(null)"':
427 self._expression = None
428 else:
429 self._expression = self._clazz.getExpressionString()
430
431 self.metadata = MF_Metadata(self._clazz)
432
433 def get_styles(self):
434 return self._styles
435
436 def get_name(self):
437 return self._clazz.name
438
439 def get_keyimage(self):
440 return self._clazz.keyimage
441
442 def get_expressionstring(self):
443 return self._expression
444
445 def set_name(self, newname):
446 self._clazz.name = newname
447
448 def set_expressionstring(self, newstring):
449 self._clazz.setExpression(newstring)
450 self._expression = self._clazz.getExpressionString()
451
452 def get_status(self):
453 if self._clazz.status == 1:
454 return True
455 else:
456 return False
457
458 def set_status(self, new_status):
459 if new_status:
460 self._clazz.status = 1
461 else:
462 self._clazz.status = 0
463
464 def add_thubanstyle(self, tb_style, type="default"):
465 """
466 added a thuban style object to the mapobject
467 """
468 new_styleobj = MF_Style(styleObj(self._clazz))
469 if type == "line":
470 new_styleobj.set_color(tb_style.GetLineColor())
471 elif type == "point":
472 # set a default symbol to show circles not only a small dot
473 # symbol "circle" must create before
474 # TODO: create a Symbol (more see MF_SymbolSet)
475 # first the default symbol circle will be created and the size 8
476 new_styleobj.set_symbolname('circle')
477 new_styleobj.set_size(8)
478 if tb_style.GetLineColor() != Transparent:
479 new_styleobj.set_linecolor(tb_style.GetLineColor())
480 new_styleobj.set_color(tb_style.GetFill())
481 else:
482 new_styleobj.set_size(tb_style.GetLineWidth())
483 new_styleobj.set_linecolor(tb_style.GetLineColor())
484 new_styleobj.set_color(tb_style.GetFill())
485
486
487
488 class MF_Layer:
489 """
490 The following parameters and functions, which the mapscript style obj
491 contains, are used:
492
493 classitem, numclasses, name, data, type
494 getClass(), getProjection(), getExtent(), getMetaData(),
495 getFirstMetaDataKey(), getNextMetaDataKey(), status,
496
497
498 The following paramters and functions are not used:
499 index, map, header, footer, template, groupe, tolerance,
500 toleranceunits, symbolscale, minscale, maxscale, labelminscale
501 labelmaxscale, sizeunits, maxfeatures, offsite, transform, labelcache
502 postlabelcache, labelitem, labelsizeitem, labelangleitem, labelitemindex
503 labelsizeitemindex, labelangleitemindex, tileitem, tileindex, units
504 connection, connectiontype, numitems, filteritem, styleitem, requires
505 labelrequires, transparency, dump, debug, numprocessing, numjoins,
506 removeClass(), open(), close(), getShape(), getNumResults(), getResult()
507 getItem(), promote(), demote(), draw(), drawQuery(), queryByAttributes()
508 queryByPoint(), queryByRect(), queryByFeatures(), queryByShape(),
509 setFilter(), setFilterString(), setWKTProjection(), setProjection()
510 addFeature(), getNumFeatures(), setMetaData(), removeMetaData(),
511 getWMSFeatureInfoURL(), executeWFSGetFeature(), applySLD(), applySLDURL()
512 enerateSLD(), moveClassUp(), moveClassDown(), setProcessing(),
513 getProcessing(), clearProcessing()
514 """
515
516 def __init__(self, mf_layer):
517 """
518 Creates the Layer Object from the mapscript Layer Object.
519 the class objects in the layer object will be stored in
520 an array. The metadata are created as a new object.
521 """
522
523 self._mf_layer = mf_layer
524
525 # Create Classes
526 # there could be more then 1
527 self._numclasses = mf_layer.numclasses
528 i = -1
529 self._classes = []
530 while i < self._numclasses-1:
531 i += 1
532 self._classes.append(MF_Class(self._mf_layer.getClass(i)))
533
534 self._projection = MF_Projection(self._mf_layer.getProjection())
535
536 # Create Metadata
537 self._metadata = MF_Metadata(self._mf_layer)
538
539 def get_name(self):
540 return self._mf_layer.name
541
542 def get_data(self):
543 return self._mf_layer.data
544
545 def get_classes(self):
546 return self._classes
547
548 def get_metadata(self):
549 return self._metadata
550
551 def get_type(self):
552 return shp_type[self._mf_layer.type]
553
554 def get_classitem(self):
555 return self._mf_layer.classitem
556
557 def get_projection(self):
558 return self._projection
559
560 def get_status(self):
561 # returns a integer value
562 # 0 = off, 1 = on, 2 = default(always on)
563 if self._mf_layer.status == 0:
564 return False
565 else:
566 return True
567 #return self._mf_layer.status
568
569 def get_group(self):
570 return self._mf_layer.group
571
572 def set_group(self, new_group):
573 self._mf_layer.group = new_group
574
575
576 def set_name(self, newname):
577 self._mf_layer.name = newname
578
579 def set_data(self, newdata, type="shape"):
580 if type == "raster":
581 self._mf_layer.data = newdata
582 else:
583 self._mf_layer.data = newdata[:-4]
584
585
586 def set_status(self, newstatus):
587 # status can set to true or false from thuban.
588 # but mapserver supports the default value
589 self._mf_layer.status = newstatus
590
591 def set_classitem(self, tb_field):
592 self._mf_layer.classitem = tb_field
593
594 def set_type(self, tb_type):
595 # if type = arc its a in shapetype line
596 if tb_type == "arc":
597 self._mf_layer.type = 1
598 if tb_type == "raster":
599 self._mf_layer.type = 3
600 if shp_type.has_key(tb_type):
601 self._mf_layer.type = tb_type
602 else:
603 for shp_paar_nr in shp_type:
604 if shp_type[shp_paar_nr] == tb_type:
605 self._mf_layer.type = shp_paar_nr
606 return
607
608 def set_projection(self, newprojection):
609 self._mfnewprojstring = ""
610 if newprojection:
611 self._newparams = newprojection.GetAllParameters()
612 for field in self._newparams:
613 self._mfnewprojstring = self._mfnewprojstring+ "," + field
614 self._mf_layer.setProjection(self._mfnewprojstring[1:])
615 self._projection.set_projection(newprojection)
616
617 def add_thubanclass(self, tb_class, type=""):
618 """
619 Add a thuban class object
620 """
621 new_class = MF_Class(classObj(self._mf_layer))
622 # set the class name to the Label form thuban if given,
623 # else set it to the value
624 if tb_class.GetLabel() != "":
625 new_class.set_name(tb_class.GetLabel())
626 else:
627 if isinstance(tb_class, ClassGroupDefault):
628 new_class.set_name("default")
629 elif isinstance(tb_class, ClassGroupSingleton):
630 new_class.set_name(str(tb_class.GetValue()))
631 else:
632 new_class.set_name(None)
633 if self.get_type() == "line":
634 new_class.add_thubanstyle(tb_class.GetProperties(), type="line")
635 elif self.get_type() == "point":
636 new_class.add_thubanstyle(tb_class.GetProperties(), type="point")
637 else:
638 new_class.add_thubanstyle(tb_class.GetProperties())
639 if (type == "default"):
640 return
641 # removed the following two lines to check if the expressionstring
642 # is needed for points, because if expressionstring is a range type,
643 # no expressionstring in the default group is allowed
644 elif (tb_class.Matches("DEFAULT")):
645 return
646 # new_class.set_expressionstring('/./')
647 else:
648 #check which type of expression
649 if isinstance(tb_class, ClassGroupRange):
650 # get the needed infos from the Range-String
651 self._range_begin = tb_class.GetRange()[0]
652 self._range_min = str(tb_class.GetMin())
653 self._range_max = str(tb_class.GetMax())
654 self._range_end = tb_class.GetRange()[len(tb_class.GetRange())-1]
655 self._range_umn = ""
656 self._range_classitem = self.get_classitem()
657 # generate the operator
658 if self._range_begin == "[":
659 self._range_op1 = ">="
660 elif self._range_begin == "]":
661 self._range_op1 = ">"
662 else:
663 print "error in Thuban class properties"
664 #build op1 string for the lower limit
665 self._range_op1 = "[" + self._range_classitem + "] " + \
666 self._range_op1 + " " +\
667 self._range_min
668 # build op2 string for the upper limit
669 if self._range_end == "[":
670 self._range_op2 = "<"
671 elif self._range_end == "]":
672 self._range_op2 = "<="
673 else:
674 print "error in Thuban class properties"
675
676 self._range_op2 = "[" + self._range_classitem + "] " + \
677 self._range_op2 + " " +\
678 self._range_max
679 # we only need AND here at the moment, becaus of the limits
680 # in thuban
681 self._range_combine = "AND"
682 # check if the one limit is set to inf and then
683 # remove the second expression becaus is not needed.
684 if self._range_min == "-inf":
685 self._range_combine = ""
686 self._range_op1 = ""
687 elif self._range_max == "inf":
688 self._range_combine = ""
689 self._range_op2 = ""
690 # build the expression together
691 self._range_umn = "(" + self._range_umn + \
692 self._range_op1 + " " +\
693 self._range_combine + \
694 self._range_op2 + " )"
695
696 #set the expression to the mapscript
697 new_class.set_expressionstring(self._range_umn)
698 else:
699 new_class.set_expressionstring(str(tb_class.GetValue()))
700 new_class.set_status(tb_class.IsVisible())
701 self._classes.append(new_class)
702
703
704 class MF_Scalebar:
705 """
706 Represent the scalebar for a map
707
708 The following settings are used:
709 label, color, imagecolor, style, intervals, units,
710 status, position, height, width
711
712 The following settings are (not) used:
713 backgroundcolor,outlinecolor, postlabelcache
714 """
715 def __init__(self, mf_scalebar):
716 self._scalebar = mf_scalebar
717 self._color = MF_Color(self._scalebar.color)
718 self._imagecolor = MF_Color(self._scalebar.imagecolor)
719 self._label = MF_Label(self._scalebar.label)
720
721 def get_label(self):
722 return self._label
723
724 def get_color(self):
725 return self._color
726
727 def get_imagecolor(self):
728 return self._imagecolor
729
730 def get_style(self):
731 return self._scalebar.style
732
733 def set_style(self, new_style):
734 self._scalebar.style = new_style
735
736 def get_size(self):
737 #returns the size
738 return (self._scalebar.width, self._scalebar.height)
739
740 def set_size(self, new_width, new_height):
741 self._scalebar.width = new_width
742 self._scalebar.height = new_height
743
744 def get_intervals(self):
745 return self._scalebar.intervals
746
747 def set_intervals(self, new_intervals):
748 self._scalebar.intervals = new_intervals
749
750 def get_units(self):
751 #returns the unittype
752 return unit_type[self._scalebar.units]
753
754 def set_units(self, units):
755 if unit_type.has_key(units):
756 self._scalebar.units = units
757 else:
758 for unit_paar_nr in unit_type:
759 if unit_type[unit_paar_nr] == units:
760 self._scalebar.units = unit_paar_nr
761
762 def get_status(self, mode="integer"):
763 if mode == "string":
764 return scalebar_status_type[self._scalebar.status]
765 else:
766 return self._scalebar.status
767
768 def set_status(self, new_status):
769 if scalebar_status_type.has_key(new_status):
770 self._scalebar.status = new_status
771 else:
772 for scalebar_status_type_nr in scalebar_status_type:
773 if scalebar_status_type[scalebar_status_type_nr] == new_status:
774 self._scalebar.status = scalebar_status_type_nr
775
776 def get_position(self, mode="integer"):
777 if mode == "string":
778 return scalebar_position_type[self._scalebar.position]
779 else:
780 return self._scalebar.position
781
782 def set_position(self, new_position):
783 if scalebar_position_type.has_key(new_position):
784 self._scalebar.position = new_position
785 else:
786 for scalebar_position_type_nr in legend_position_type:
787 if scalebar_position_type[scalebar_position_type_nr] \
788 == new_position:
789 self._scalebar.position = scalebar_position_type_nr
790
791
792 class MF_Map:
793 """
794 The following parameters and functions, which the mapscript style obj
795 contains, are used:
796
797 name, numlayers, extent, shapepath, imagecolor, imagetype, units, getLayer,
798 status, getProjection, getMetaData, getFirstMetaDataKey, getNextMetaDataKey,
799 save(), setExtent(), height, width, setProjection(), setImageType(),
800
801 The following parameters and functions are not used:
802 maxsize, layers, symbolset, fontset, labelcache,
803 transparent, interlace, imagequality, cellsize, debug, datapattern,
804 templatepattern, configoptions
805 zoomPoint(), zoomRectangle(), zoomScale(), getLayerOrder(), setLayerOrder(),
806 clone(), removeLayer(), getLayerByName(), getSymbolByName(),
807 prepareQuery(), prepareImage(), setOutputFormat(), draw(),
808 drawQuery(), drawLegend(), drawScalebar(), embedLegend(), drawLabelCache(),
809 nextLabel(), queryByPoint(), queryByRecht(), queryByFeatures(),
810 queryByShape(), setWKTProjection(), saveQuery(), saveQueryASGML(),
811 setMetaData(), removeMetaData(), setSymbolSet(), getNumSymbols(),
812 setFontSet(), saveMapContext(), loadMapContext(), moveLayerUp(),
813 moveLayerDown(), getLayersDrawingOrder(), setLayersDrawingOrder(),
814 setConfigOption(), getConfigOption(), applyConfigOptions(), applySLD(),
815 applySLDURL(), gernerateSLD(), procecssTemplate(), processLegemdTemplate(), processQueryTemplate(),
816 getOutputFormatByName(), appendOutputFormat(), removeOutputFormat(),
817 """
818 def __init__(self, mf_map):
819 """
820 Create the map object from the mapfile mapobject which is given.
821
822 All layers in the mapfile will be written to an array.
823 """
824 self._mf_map = mf_map
825 self._extent = MF_Rectangle(self._mf_map.extent)
826 self._imagecolor = MF_Color(self._mf_map.imagecolor)
827 self._web = MF_Web(self._mf_map.web)
828 self._legend = MF_Legend(self._mf_map.legend)
829 self._scalebar = MF_Scalebar(self._mf_map.scalebar)
830
831 # TODO: generate the list dynamical by alle supported formats.
832 # At the moment outputformat only get by name, but in a next
833 # version there may be a function to get the outputformat by id
834 # then there is no need to define the formattypes here
835 image_types = ['gif', 'png', 'png24', 'jpeg', 'wbmp', \
836 'swf', 'pdf', 'imagemap']
837 self._alloutputformats = []
838 self._imagetype = self._mf_map.imagetype
839 # create a temp imagtype, because the function getOutputFormatByName()
840 # set the imagetype to the received OutputFormat
841 for fmtname in image_types:
842 theformat = self._mf_map.getOutputFormatByName(fmtname)
843 if theformat:
844 self._alloutputformats.append(MF_Outputformat(theformat))
845 self._mf_map.setImageType(self._imagetype)
846
847 self._outputformat = MF_Outputformat(self._mf_map.outputformat)
848
849 # symbols
850 self._symbolset = MF_SymbolSet(self._mf_map.symbolset)
851
852 # if the map name is not set it will return a MS string.
853 if self._mf_map.name != "MS":
854 self._name = self._mf_map.name
855 else:
856 self._name = None
857
858 self._projection = MF_Projection(self._mf_map.getProjection())
859
860 # Initial Layer List
861 self._layers = []
862 self._i = 0
863 while self._i < self._mf_map.numlayers:
864 self._layers.append(MF_Layer(self._mf_map.getLayer(self._i)))
865 self._i += 1
866
867 # Shapepath if not set, shapepath will be empty
868 if self._mf_map.shapepath:
869 self._shapepath = self._mf_map.shapepath
870 else:
871 self._shapepath = ""
872
873 # Create Metadata
874 self._metadata = MF_Metadata(self._mf_map)
875
876 def get_mappath(self):
877 return self._mf_map.mappath
878
879 def set_mappath(self, new_mappath):
880 self._mf_map.mappath = new_mappath
881
882 def get_outputformat(self):
883 return self._outputformat
884
885 def get_alloutputformats(self):
886 return self._alloutputformats
887
888 def get_imagetype(self):
889 return self._mf_map.imagetype
890
891 def set_imagetype(self, new_imagetype):
892 self._mf_map.setImageType(new_imagetype)
893
894 def get_symbolset(self):
895 return self._symbolset
896
897 def get_status(self):
898 if self._mf_map.status == 1:
899 return True
900 else:
901 return False
902
903 def set_status(self, new_status):
904 if new_status:
905 self._mf_map.status = 1
906 else:
907 self._mf_map.status = 0
908
909 def get_scalebar(self):
910 return self._scalebar
911
912 def get_web(self):
913 return self._web
914
915 def get_legend(self):
916 return self._legend
917
918 def get_extent(self):
919 return self._extent
920
921 def get_layers(self):
922 return self._layers
923
924 def get_numlayers(self):
925 return self._mf_map.numlayers
926
927 def get_projection(self):
928 return self._projection
929
930 def get_name(self):
931 return self._name
932
933 def get_shapepath(self):
934 # where are the shape files located.
935 return self._shapepath
936
937 def set_shapepath(self, new_shapepath):
938 # where are the shape files located..
939 self._shapepath = new_shapepath
940
941 def get_imagetype(self):
942 return self._mf_map.imagetype
943
944 def get_layerorder(self):
945 # shows the order of layer as list
946 return self._mf_map.getLayerOrder()
947
948 def get_size(self):
949 #returns the size
950 return (self._mf_map.width, self._mf_map.height)
951
952 def get_units(self):
953 #returns the unittype
954 return unit_type[self._mf_map.units]
955
956 def get_imagecolor(self):
957 return self._imagecolor
958
959 def set_name(self, newname):
960 # whitespace musst be replaced, either no
961 # mapfile will be shown in the mapserver
962 if newname:
963 newname = newname.replace(" ","_")
964 self._name = newname
965 self._mf_map.name = newname
966
967 def set_extent(self, newextent):
968 # TODO: add the shown extend here instead of the total
969 # if no size is set or if it is zero, the size will set to 1.
970 if self.get_size()[0] == - 1:
971 print "define the size first to set extent"
972 print "size is now set to (1,1)"
973 self.set_size(1,1)
974 # if an empty map is export newextent will be none
975 if newextent:
976 self._newrect = MF_Rectangle(rectObj(newextent[0],newextent[1], \
977 newextent[2],newextent[3]))
978 self._mf_map.setExtent(newextent[0],newextent[1], \
979 newextent[2],newextent[3])
980
981
982 def set_size(self, newwidth, newheight):
983 self._mf_map.width = newwidth
984 self._mf_map.height = newheight
985
986 def set_projection(self, projection):
987 self._mfnewprojstring = ""
988 self._newparams = projection.GetAllParameters()
989 for field in self._newparams:
990 self._mfnewprojstring = self._mfnewprojstring+ "," + field
991 self._mf_map.setProjection(self._mfnewprojstring[1:])
992 self._projection.set_projection(projection)
993
994 def set_units(self, units):
995 if unit_type.has_key(units):
996 self._mf_map.units = units
997 else:
998 for unit_paar_nr in unit_type:
999 if unit_type[unit_paar_nr] == units:
1000 self._mf_map.units = unit_paar_nr
1001
1002 def get_metadata(self):
1003 return self._metadata
1004
1005 def add_thubanlayer(self, tb_layer):
1006 """
1007 Add a thuban layer
1008 """
1009 new_layer = MF_Layer(layerObj(self._mf_map))
1010 new_layer.set_name(tb_layer.Title())
1011
1012 # TODO: implement relative pathnames
1013 # yet only absolute pathnames in the LayerObj are set
1014 if isinstance(tb_layer, RasterLayer ):
1015 new_layer.set_data(tb_layer.GetImageFilename(), type="raster")
1016 new_layer.set_type("raster")
1017 new_layer.set_status(tb_layer.Visible())
1018 elif isinstance(tb_layer, AnnotationLayer):
1019 new_layer.set_type("annotation")
1020 new_layer.set_status(tb_layer.Visible())
1021 else:
1022 new_layer.set_data(tb_layer.ShapeStore().FileName())
1023 new_layer.set_status(tb_layer.Visible())
1024 new_layer.set_type(tb_layer.ShapeType())
1025
1026 if tb_layer.GetClassificationColumn():
1027 new_layer.set_classitem(tb_layer.GetClassificationColumn())
1028 if tb_layer.GetProjection():
1029 new_layer.set_projection(tb_layer.GetProjection())
1030 if tb_layer.GetClassification().GetNumGroups() > 0:
1031 singletonexists = False
1032 for group in range(0, \
1033 tb_layer.GetClassification().GetNumGroups(), 1):
1034 if isinstance(tb_layer.GetClassification().GetGroup(group), \
1035 ClassGroupSingleton):
1036 singletonexists = True
1037 new_layer.add_thubanclass( \
1038 tb_layer.GetClassification().GetGroup(group))
1039 new_layer.add_thubanclass( \
1040 tb_layer.GetClassification().GetDefaultGroup())
1041 # remove the classitem if one singleton exists
1042 if singletonexists == False:
1043 new_layer.set_classitem(None)
1044 else:
1045 new_layer.add_thubanclass( \
1046 tb_layer.GetClassification().GetDefaultGroup(), \
1047 type="default")
1048
1049 # set the projection to the layer.
1050 # if the layer has its own definition use is,
1051 # else use the main projection
1052 if tb_layer.GetProjection():
1053 new_layer.set_projection(tb_layer.GetProjection())
1054 else:
1055 new_layer.set_projection(self._projection.get_projection())
1056 self._layers.append(new_layer)
1057
1058 def remove_layer(self, nr):
1059 #remove the last layer from the layer list and not the one which
1060 # is removed in mapscript. This must be, becaus mapscript
1061 # set the object links anew. Don't understand really :)
1062 self._layers.pop()
1063 self._mf_map.removeLayer(nr)
1064
1065 def remove_all_layers(self):
1066 for i in range (self._mf_map.numlayers,0,-1):
1067 self.remove_layer(i-1)
1068
1069 def save_map(self, filepath):
1070 # save the Map
1071 # maybe an own saver can implement here
1072 self._mf_map.save(filepath)
1073
1074
1075 class MF_Web:
1076 """
1077 Save the Web settings
1078
1079 The following parametes are used:
1080 imagepath, imageurl, queryformat,
1081
1082 The following parameters are not used:
1083 log, map, template, header, footer, empty, error, extent,
1084 minscale, maxscale, mintemplate, maxtemplate
1085 """
1086 def __init__(self, mf_web):
1087 self._mf_web = mf_web
1088
1089 def get_imagepath(self):
1090 return self._mf_web.imagepath
1091
1092 def set_imagepath(self, new_imagepath):
1093 self._mf_web.imagepath = new_imagepath
1094
1095 def get_imageurl(self):
1096 return self._mf_web.imageurl
1097
1098 def get_template(self):
1099 return self._mf_web.template
1100
1101 def set_template(self, new_template):
1102 self._mf_web.template = new_template
1103
1104 def set_imageurl(self, new_imageurl):
1105 self._mf_web.imageurl = new_imageurl
1106
1107 def get_queryformat(self):
1108 return self._mf_web.queryformat
1109
1110 def set_queryformat(self, new_queryformat):
1111 self._mf_web.imagepath = new_queryformat
1112
1113
1114 class MF_Label:
1115 """
1116 The following parameters from mapscript are used:
1117 type, color, size, offsetx, offsety, partials, force, buffer,
1118 minfeaturesize, mindistance,
1119
1120 The following parameters are not used:
1121 font, outlinecolor, shadowcolor, shadowsizex, shadowsizey,
1122 backgroundcolor, backgroundshadowcolor, backgroundshadowsizex,
1123 backgroundshadowsizey, sizescaled, minsize, maxsize, position, angle,
1124 autoangle, antialias, wrap, autominfeaturesize,
1125 """
1126 def __init__(self, mf_label):
1127 """
1128 Create a legend obj from the existing mapfile
1129 """
1130 self._label = mf_label
1131 self._color = MF_Color(self._label.color)
1132
1133 def get_size(self):
1134 return self._label.size
1135
1136 def set_size(self, new_size):
1137 if label_size_type.has_key(new_size):
1138 self._label.size = new_size
1139 for label_size_type_nr in label_size_type:
1140 if label_size_type[label_size_type_nr] == new_size:
1141 self._label.size = label_size_type_nr
1142 else:
1143 self._label.size = new_size
1144
1145 def get_color(self):
1146 return self._color
1147
1148 def get_partials(self):
1149 if self._label.partials == 1:
1150 return True
1151 else:
1152 return False
1153
1154 def set_partials(self, new_partials):
1155 # if partials = True
1156 if new_partials:
1157 self._label.partials = 1
1158 elif new_partials == False:
1159 self._label.partials = 0
1160 else:
1161 print "must be boolean"
1162
1163 def get_buffer(self):
1164 return self._label.buffer
1165
1166 def set_buffer(self, new_buffer):
1167 self._label.buffer = new_buffer
1168
1169 def get_mindistance(self):
1170 return self._label.mindistance
1171
1172 def set_mindistance(self, new_mindistance):
1173 self._label.mindistance = new_mindistance
1174
1175 def get_minfeaturesize(self):
1176 return self._label.minfeaturesize
1177
1178 def set_minfeaturesize(self, new_minfeaturesize):
1179 self._label.minfeaturesize = new_minfeaturesize
1180
1181 def get_position(self, mode="integer"):
1182 if mode == "string":
1183 return label_position_type[self._label.position]
1184 else:
1185 return self._label.position
1186
1187 def set_position(self, new_position):
1188 if label_position_type.has_key(new_position):
1189 self._label.position = new_position
1190 else:
1191 for label_position_type_nr in label_position_type:
1192 if label_position_type[label_position_type_nr] == new_position:
1193 self._label.position = label_position_type_nr
1194
1195 def get_force(self):
1196 if self._label.force == 1:
1197 return True
1198 else:
1199 return False
1200
1201 def set_force(self, new_force):
1202 if new_force:
1203 self._label.force = 1
1204 else:
1205 self._label.force = 0
1206
1207 def get_type(self):
1208 return label_font_type[self._label.type]
1209
1210 def set_type(self, new_type):
1211 if label_font_type.has_key(new_type):
1212 self._label.type = new_type
1213 else:
1214 for label_font_type_nr in label_font_type:
1215 if label_font_type[label_font_type_nr] == new_type:
1216 self._label.type = label_font_type_nr
1217
1218 def get_offset(self):
1219 return (self._label.offsetx, self._label.offsety)
1220
1221 def set_offset(self, new_offsetx, new_offsety):
1222 self._label.offsetx = new_offsetx
1223 self._label.offsety = new_offsety
1224
1225
1226 class MF_Legend:
1227 """
1228 The following parameters are (not) used:
1229 imagecolor, label, keysizex, keysizey, status, position,
1230
1231 The following parameters are not used:
1232 keyspacingx, keyspacingy,
1233 outlinecolor, height, width, postlabelcache, template, map
1234 """
1235 def __init__(self, mf_legend):
1236 """
1237 Create a legend obj from the existing mapfile
1238 """
1239 self._mf_legend = mf_legend
1240 self._imagecolor = MF_Color(self._mf_legend.imagecolor)
1241 self._label = MF_Label(self._mf_legend.label)
1242
1243 def get_imagecolor(self):
1244 return self._imagecolor
1245
1246 def get_label(self):
1247 return self._label
1248
1249 def get_keysize(self):
1250 return (self._mf_legend.keysizex, self._mf_legend.keysizey)
1251
1252 def set_keysize(self, new_keysizex, new_keysizey):
1253 self._mf_legend.keysizex = new_keysizex
1254 self._mf_legend.keysizey = new_keysizey
1255
1256 def get_keyspacing(self):
1257 return (self._mf_legend.keyspacingx, self._mf_legend.keyspacingy)
1258
1259 def set_keyspacing(self, new_keyspacingx, new_keyspacingy):
1260 self._mf_legend.keyspacingx = new_keyspacingx
1261 self._mf_legend.keyspacingy = new_keyspacingy
1262
1263 def get_status(self, mode="integer"):
1264 if mode == "string":
1265 return legend_status_type[self._mf_legend.status]
1266 else:
1267 return self._mf_legend.status
1268
1269 def set_status(self, new_status):
1270 if legend_status_type.has_key(new_status):
1271 self._mf_legend.status = new_status
1272 else:
1273 for legend_status_type_nr in legend_status_type:
1274 if legend_status_type[legend_status_type_nr] == new_status:
1275 self._mf_legend.status = legend_status_type_nr
1276
1277 def get_position(self, mode="integer"):
1278 if mode == "string":
1279 return legend_position_type[self._mf_legend.position]
1280 else:
1281 return self._mf_legend.position
1282
1283 def set_position(self, new_position):
1284 if legend_position_type.has_key(new_position):
1285 self._mf_legend.position = new_position
1286 else:
1287 for legend_position_type_nr in legend_position_type:
1288 if legend_position_type[legend_position_type_nr]== new_position:
1289 self._mf_legend.position = legend_position_type_nr
1290
1291 class MF_Projection:
1292 """
1293 The following parameter, which the mapscript style obj contains is used:
1294
1295 numargs
1296 """
1297
1298 def __init__(self, mf_projection):
1299 """
1300 Create a projection object from the given mapscript projection
1301 object. If it is a epsg code the extracted from the string
1302 (e.g."init=epsg:xxxx"), else the projection parameters will
1303 be splitted and an array with the parameters will be creaded.
1304 """
1305 self._mfprojstring = mf_projection
1306 self._projstring = self._mfprojstring
1307 self._epsgcode = None
1308 self._params = None
1309 if self._mfprojstring:
1310 if self._mfprojstring.find("init=epsg:") != -1:
1311 self._initcode, self._epsgcode = self._mfprojstring.split(':')
1312 else:
1313 self._params = []
1314 self._params = self._mfprojstring.split("+")
1315 if self._params[0] == "":
1316 self._params.remove("")
1317
1318 def epsg_code_to_projection(self, epsg):
1319 """
1320 Find the projection for the given epsg code.
1321
1322 Copied from Extension/wms/layer.py
1323
1324 epsg -- EPSG code as string
1325 """
1326 #Needed only for this function
1327 from Thuban.Model.resource import get_system_proj_file, EPSG_PROJ_FILE,\
1328 EPSG_DEPRECATED_PROJ_FILE
1329
1330 proj_file, warnings = get_system_proj_file(EPSG_PROJ_FILE)
1331
1332 for proj in proj_file.GetProjections():
1333 if proj.EPSGCode() == epsg:
1334 return proj
1335
1336 proj_file, warnings = get_system_proj_file(EPSG_DEPRECATED_PROJ_FILE)
1337 for proj in proj_file.GetProjections():
1338 if proj.EPSGCode() == epsg:
1339 return proj
1340 return None
1341
1342 def get_params(self):
1343 #Parameter from the mf_projectionstring as Array
1344 return self._params
1345
1346 def get_epsgcode(self):
1347 # returnes the epsg number
1348 return self._epsgcode
1349
1350 def get_epsgproj(self):
1351 # get an epsg projectionobject
1352 return self.epsg_code_to_projection(self._epsgcode)
1353
1354 def get_projection(self):
1355 return self._projstring
1356
1357 def set_projection(self, newprojection):
1358 self._projstring = newprojection
1359 self._params = newprojection.GetAllParameters()
1360 self._mfnewprojstring = ""
1361 for field in self._params:
1362 self._mfnewprojstring = self._mfnewprojstring+ "+" + field
1363 self._mfprojstring = self._mfnewprojstring
1364
1365
1366 class MF_Style:
1367 """
1368 The following parameters, which the mapscript style obj
1369 contains, are used:
1370 color, backgroundcolor, outlinecolor, size, symbolname
1371
1372 The following are not used:
1373 symbol, sizescaled, minsize, maxsize, offsetx, offsety,
1374 antialias
1375 """
1376
1377 def __init__(self, mf_style):
1378 """
1379 Create a style object from the given mapscript style object.
1380 The color Object from the color and the outlinecolor parameter
1381 will be created. if the color (red, green or blue) is -1 there
1382 is no definition in the mapfile and so there is no color object,
1383 it will set to 'None'.
1384 """
1385 self._style = mf_style
1386 if self._style.color.red == -1:
1387 self._color = None
1388 else:
1389 self._color = MF_Color(self._style.color)
1390 if self._style.outlinecolor.red == -1:
1391 self._outlinecolor = None
1392 else:
1393 self._outlinecolor = MF_Color(self._style.outlinecolor)
1394
1395 def get_color(self):
1396 return self._color
1397
1398 def get_outlinecolor(self):
1399 return self._outlinecolor
1400
1401 def get_size(self):
1402 return self._style.size
1403
1404 def set_linecolor(self, tb_color):
1405 self._color = tb_color
1406 new_linecolor = MF_Color(colorObj())
1407 new_linecolor.set_thubancolor(tb_color)
1408 self._outlinecolor = new_linecolor
1409 self._style.outlinecolor = new_linecolor.get_mfcolor()
1410
1411 def set_color(self, tb_color):
1412 self._color = tb_color
1413 new_color = MF_Color(colorObj())
1414 new_color.set_thubancolor(tb_color)
1415 self._color = new_color
1416 self._style.color = new_color.get_mfcolor()
1417
1418 def set_size(self, newsize):
1419 self._style.size = newsize
1420
1421 def set_symbolname(self, newsymbol):
1422 # its possible to use stringnames instead of numbers
1423 self._style.symbolname = newsymbol
1424

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26