/[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 2287 - (show annotations)
Thu Jul 15 14:31:37 2004 UTC (20 years, 7 months ago) by jschuengel
File MIME type: text/x-python
File size: 48245 byte(s)
Added a function to get the mappath directly from the mapobj. Is needed becaus of the changes in mf_import.py.
(MF_Layer.add_thubanclass): Added a new comment.
(MF_Map.set_extent): Fixed a bug with exporting empty mapobj. If the mapobj is empty there is no extent get from thuban an so no one can set to th mapobj.

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

Properties

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26