jschuengel: thuban/Extensions/umn_mapserver mf_import.py,1.7,1.8
cvs@intevation.de
cvs at intevation.de
Thu Jul 22 14:38:40 CEST 2004
Author: jschuengel
Update of /thubanrepository/thuban/Extensions/umn_mapserver
In directory doto:/tmp/cvs-serv29811
Modified Files:
mf_import.py
Log Message:
Added the import module "re". Also added Range and the ClassGroupRange import from Thuban. Both are needed for the new range expression import.
(create_rangeexpression): Added. Creates a Range Expression in Thuban style from a given mapfile expression.
(added_rasterlayer): Make some small code changes. The shapepath is now stored in an extra variable and the clazz_name is set empty if no class name set in the mapfile.
Changed the Error message for Range Expressions, becaus the new function create a error string which will be shown in the dialog.
Index: mf_import.py
===================================================================
RCS file: /thubanrepository/thuban/Extensions/umn_mapserver/mf_import.py,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -d -r1.7 -r1.8
--- mf_import.py 15 Jul 2004 14:30:37 -0000 1.7
+++ mf_import.py 22 Jul 2004 12:38:38 -0000 1.8
@@ -16,13 +16,14 @@
# $Id$
+
# ###################################
#
# import necessary modules
#
# ###################################
-import os
+import os, re
from mapscript import mapObj
@@ -49,9 +50,12 @@
from Thuban.UI.mainwindow import main_menu
from Thuban.Model.classification import ClassGroupSingleton, \
+ ClassGroupRange, \
ClassGroupProperties, \
ClassGroupDefault
+from Thuban.Model.range import Range
+
# The Thuban color objects
from Thuban.Model.color import Transparent
@@ -91,6 +95,106 @@
projepsg = mapobj.get_projection().get_epsgproj()
tb_con.SetProjection(projepsg)
+def create_rangeexpression(mapexprstr):
+ """
+ create a expressionstring in thuban style from an given
+ mapfile expressionstring
+
+ mapexprstr = the expressionstring from the mapfile
+ """
+ attribut_expr = "\[\w+\]"
+ attribut = re.compile(attribut_expr)
+ operator_expr = ">=|<=|<|>|="
+ operator = re.compile(operator_expr)
+ #only and now supported
+ link_expr = "AND"
+ link = re.compile(link_expr)
+ oneexpr = "\s*\[\w+\]\s*>*<*=*\s*\w+"
+ theexpr = re.compile(oneexpr)
+
+ # init the string variables to build the string later
+ thestring = ""
+ strbegin = None
+ strmin = None
+ strmax = None
+ strend = None
+
+ link_result = link.findall(mapexprstr)
+ attr = attribut.findall(mapexprstr)
+ if len(link_result) == 0 and len(attr) == 1:
+ strattr = attr[0][1:-1]
+ expression = theexpr.findall(mapexprstr)
+ if expression:
+ operator_result = operator.search(mapexprstr)
+ if operator_result.group(0) == ">=":
+ strbegin = "["
+ strmin = operator_result.string[operator_result.start()+2:].replace(" ","")
+ strmax = "oo"
+ strend = "]"
+ elif operator_result.group(0) == "<=":
+ strbegin = "["
+ strmin = "-oo"
+ strmax = operator_result.string[operator_result.start()+2:].replace(" ","")
+ strend = "]"
+ elif operator_result.group(0) == ">":
+ strbegin = "]"
+ strmin = operator_result.string[operator_result.start()+1:].replace(" ","")
+ strmax = "oo"
+ strend = "]"
+ elif operator_result.group(0) == "<":
+ strbegin = "["
+ strmin = "-oo"
+ strmax = operator_result.string[operator_result.start()+1:].replace(" ","")
+ strend = "["
+ elif operator_result.group(0) == "=":
+ # create a singleton, not implemented yet
+ errorstring = "singleton creation not implemented yet"
+ else:
+ errorstring = "no operator found"
+ else:
+ errorstring = "no expression found"
+ elif len(link_result) == 1:
+ if attr[0] == attr[1]:
+ strattr = attr[0][1:-1]
+ first_expr,second_expr = mapexprstr.split("AND")
+ oneexpr_result = theexpr.findall(mapexprstr)
+ operator1_result = operator.search(first_expr)
+ operator2_result = operator.search(second_expr)
+ errorstring = "operators are wrong"
+ if operator1_result.group(0)[0] == "<" and \
+ operator2_result.group(0)[0] == ">":
+ optemt = operator1_result
+ operator1_result = operator2_result
+ operator2_result = optemt
+ if operator1_result.group(0) == ">=":
+ strbegin = "["
+ strmin = operator1_result.string[operator1_result.start()+2:].replace(" ","")
+ elif operator1_result.group(0) == ">":
+ strbegin = "]"
+ strmin = operator1_result.string[operator1_result.start()+1:].replace(" ","")
+ if operator2_result.group(0) == "<=":
+ strend = "]"
+ strmax = operator2_result.string[operator2_result.start()+2:].replace(" ","")
+ elif operator2_result.group(0) == "<" and operator1_result != ">" :
+ strend = "["
+ strmax = operator2_result.string[operator2_result.start()+1:].replace(" ","")
+ if strmax < strmin:
+ errorstring = "values are wrong"
+ strbegin = None
+ else:
+ errorstring = "Attributes not equal"
+ elif len(link_result) == 0:
+ errorstring = "Link expression not supported"
+ else:
+ errorstring = "More then two expressions"
+
+ #create the expression thestring
+ if strbegin and strmin and strmax and strend:
+ thestring = strbegin +strmin+";"+strmax + strend
+ return (strattr, thestring)
+ else:
+ return (None,errorstring)
+
def add_rasterlayer(context, tb_map, mapobj, maplayer):
"""
@@ -103,6 +207,7 @@
maplayer = layer obj to add to thuban
"""
imgpath = maplayer.get_data()
+ shapepath = mapobj.get_shapepath()
filepath = mapobj.get_mappath()
layertitle = maplayer.get_name()
# if there is no imagepath defined, the Raster Layer could not load
@@ -113,11 +218,10 @@
if os.path.isabs(imgpath):
filename = imgpath
else:
- filename = os.path.join(filepath,mapobj.get_shapepath(),imgpath)
+ filename = os.path.join(filepath, shapepath,imgpath)
# Normalize the pathname by collapses
# redundant separators and up-level references
filename = os.path.normpath(filename)
-
rasterlayer = RasterLayer(layertitle, filename)
# set the visible status
rasterlayer.SetVisible(maplayer.get_status())
@@ -242,18 +346,37 @@
except:
theexpression = expressionstring[1:-1]
if clazz_name == None:
- clazz_name = str(theexpression)
+ clazz_name = str("")
new_group = ClassGroupSingleton(value = theexpression,
props = prop,
label = clazz_name)
new_group.SetVisible(map_clazz.get_status())
clazz.AppendGroup(new_group)
elif (expressionstring[0] == "("):
- context.mainwindow.RunMessageBox(_('Error Loading Layer'),
- _("could not import the expression\n"+ \
- "%s \n from layer '%s', class %d (%s)'.")\
- %(expressionstring, layertitle, \
- map_clazznr, map_clazz.get_name()))
+ expressionclassitem, thubanexpr \
+ = create_rangeexpression(expressionstring[1:-1])
+ if expressionclassitem == None:
+ if clazz_name == None:
+ clazz_showtxt = "Nr. " + str(map_clazznr+1)
+ else:
+ clazz_showtxt = clazz_name
+ context.mainwindow.RunMessageBox(_('Error Loading Expression'), \
+ _("%s \n" + \
+ "Can't load the Expression from layer \n" + \
+ "Layer: %s ; Class: %s\n" + \
+ "Expression: %s")\
+ %(thubanexpr, layer.title, clazz_showtxt, expressionstring[1:-1]) )
+ else:
+ layer.SetClassificationColumn(expressionclassitem)
+ if clazz_name == None:
+ clazz_name = str("")
+ expressionrange = Range(thubanexpr)
+ new_group = ClassGroupRange(expressionrange,
+ props = prop,
+ label = clazz_name)
+ new_group.SetVisible(map_clazz.get_status())
+ clazz.AppendGroup(new_group)
+
else:
new_group = ClassGroupSingleton(props = prop,label = clazz_name)
new_group.SetVisible(map_clazz.get_status())
@@ -345,7 +468,6 @@
selectedlayer = []
# counter to show the numer of layer loaded into Tuban
layer_count = 0
-
# import settings to thuban only if one layer is selected
if len(selectedlayer) != 0:
# thuban map context
@@ -386,10 +508,11 @@
if numlayers != 0:
# fit the new map to the window
context.mainwindow.canvas.FitMapToWindow()
-
+ # get the extent from the map and set it in thuban
+ #extentrect = mapobj.get_extent().get_rect()
+ #context.mainwindow.canvas.FitRectToWindow(extentrect)
context.mainwindow.RunMessageBox(_('Layer loaded'),
_("%s Layer loaded into Thuban") % layer_count)
-
def parse_mapfile(filename):
"""
More information about the Thuban-devel
mailing list
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)