/[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 2301 - (show annotations)
Wed Jul 28 12:35:32 2004 UTC (20 years, 7 months ago) by jschuengel
File MIME type: text/x-python
File size: 47123 byte(s)
Changed all class functions. Now all metadata will handle by the function and its not needed to initialize it from outside. Therefor the associated mapobj will be stored in the Metadata Object. So we can use thespecial functions from the associated mapobj to get the Metadata. Therefor all initialization code for the metadata is removed from the other classes.
(MF_Layer): Added a function to get the metadata object.
(MF_Map): Added a function to set the mappath, the path where the mapfile ist stored.

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

Properties

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26