jschuengel: thuban/Extensions/umn_mapserver mf_import.py,1.6,1.7
cvs@intevation.de
cvs at intevation.de
Thu Jul 15 16:30:39 CEST 2004
Author: jschuengel
Update of /thubanrepository/thuban/Extensions/umn_mapserver
In directory doto:/tmp/cvs-serv5213
Modified Files:
mf_import.py
Log Message:
Updated the discription.
Split the funktion in to three smaller ones. The new functions are add_rasterlayer, add_polygonlayer and select_layer2import.
Removed the mapfilepath and filepath initialisation, because its know include in the new functions.
Now nothing will be imported if cancel is pressed in the layer choice dialog.
Index: mf_import.py
===================================================================
RCS file: /thubanrepository/thuban/Extensions/umn_mapserver/mf_import.py,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -d -r1.6 -r1.7
--- mf_import.py 14 Jul 2004 10:34:10 -0000 1.6
+++ mf_import.py 15 Jul 2004 14:30:37 -0000 1.7
@@ -64,13 +64,254 @@
#
# ###################################
+def set_projection_to_tb(tb_con, mapobj):
+ """
+ Helpfunction to set the projection in thuban.
+
+ Parameters:
+ tb_con -- The thuban conext (mapobj or layerobj)
+ mapobj -- The mapobject (from mapscript)
+ """
+ if (mapobj.get_projection().get_params() != None):
+ projparams = mapobj.get_projection().get_params()
+ # check if it is an latlong projection, becaues one
+ # more parameter is needed in that case
+ addtometer = False
+ for param in projparams:
+ projkey = param.split("=")
+ if projkey[0] == "proj":
+ if projkey[1] == "latlong":
+ addtometer = True
+ if projkey[0] == "to_meter":
+ addtometer = False
+ if addtometer == True:
+ projparams.append("to_meter=0.017453")
+ tb_con.SetProjection(Projection(projparams))
+ if mapobj.get_projection().get_epsgcode() != None:
+ projepsg = mapobj.get_projection().get_epsgproj()
+ tb_con.SetProjection(projepsg)
+
+
+def add_rasterlayer(context, tb_map, mapobj, maplayer):
+ """
+ add a rasterlayer to thuban
+
+ tb_map = context.mainwindow.canvas.Map()
+
+ mapobj = the Mapobject created from the mapfile
+
+ maplayer = layer obj to add to thuban
+ """
+ imgpath = maplayer.get_data()
+ filepath = mapobj.get_mappath()
+ layertitle = maplayer.get_name()
+ # if there is no imagepath defined, the Raster Layer could not load
+ if imgpath == None:
+ context.mainwindow.RunMessageBox(_('Error Loading Raster Layer'),
+ _("Can't open the rasterlayer '%s'.") % layertitle)
+ else:
+ if os.path.isabs(imgpath):
+ filename = imgpath
+ else:
+ filename = os.path.join(filepath,mapobj.get_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())
+ #add the projection if exists
+ set_projection_to_tb(rasterlayer, maplayer)
+ tb_map.AddLayer(rasterlayer)
+
+
+def add_polygonlayer(context, tb_map, mapobj, maplayer):
+ """
+ add a polygonlayer to thuban
+
+ tb_map = context.mainwindow.canvas.Map()
+
+ mapobj = the Mapobject created from the mapfile
+
+ maplayer = layer obj to add to thuban
+ """
+ filepath = mapobj.get_mappath()
+ layertitle = maplayer.get_name()
+ if maplayer.get_data():
+ if os.path.isabs(maplayer.get_data()):
+ filename = maplayer.get_data() +".shp"
+ else:
+ filename = os.path.join(filepath, mapobj.get_shapepath(), \
+ maplayer.get_data())
+ filename = filename + ".shp"
+ # Normalize the pathname by collapses
+ # redundant separators and up-level references
+ filename = os.path.normpath(filename)
+ else:
+ context.mainwindow.RunMessageBox(_('Error Loading Layer'),
+ _("no shp file definied, maybe used a feature obj '%s'.") % layertitle)
+ # try to open the layer
+ try:
+ store = context.application.Session().OpenShapefile(filename)
+ except IOError:
+ # the layer couldn't be opened
+ context.mainwindow.RunMessageBox(_('Open Shapepath'),
+ _("Can't open the file '%s'.") % filename)
+ else:
+ # create a new layer which will be added to thuban later
+ layer = Layer(layertitle, store)
+ # all classes of the maplayer
+ map_clazzes = maplayer.get_classes()
+ # number of layers loaded vom maplayer
+ map_numclazzes = len(map_clazzes)
+ # defaultclazzset is necessary to know if a default class is set
+ # in thuban. if is not and there are more them one class in the
+ # mapfile the colors of the default layer in thuban musst
+ # set to Transparence
+ defaultclazzset = None
+
+ # create a class in thuban for every class in the maplayer
+ for map_clazznr in range(0, map_numclazzes, 1):
+ # define the color
+ map_clazz = map_clazzes[map_clazznr]
+ layer_style = map_clazz.get_styles()[0]
+ clazz_name = map_clazz.get_name()
+ clazz = layer.GetClassification()
+ prop = ClassGroupProperties()
+
+ # set the layerprojection if given
+ # TODO check this, becaus it is not working correctly
+ # set_projection_to_tb(layer, maplayer)
+
+ # if outline color is defined use it else set transparent
+ if layer_style.get_outlinecolor():
+ tb_color = layer_style.get_outlinecolor().get_thubancolor()
+ prop.SetLineColor(tb_color)
+ else:
+ prop.SetLineColor(Transparent)
+ # if color is defined use it as fillcolor
+ # but if the layer type is line use the color as linecolor
+ if layer_style.get_color():
+ tb_color = layer_style.get_color().get_thubancolor()
+ if maplayer.get_type() == 'line':
+ prop.SetLineColor(tb_color)
+ else:
+ prop.SetFill(tb_color)
+ else:
+ prop.SetFill(Transparent)
+
+ #set size for the line
+ if ((maplayer.get_type() == 'polygon') or
+ (maplayer.get_type() == 'line')):
+ if layer_style.get_size():
+ prop.SetLineWidth(layer_style.get_size())
+
+ # generate special expression classes to show in thuban
+ # not all possibilities from the MapServer are supported now
+ # supporteed:
+ # String comparisons
+ # not supported:
+ # Regular expressions
+ # Logical expressions
+ expressionstring = map_clazz.get_expressionstring()
+ if (((map_numclazzes == 1) & (not expressionstring)) or
+ ((map_numclazzes > 1) & (expressionstring == None)) or
+ (expressionstring == '/./')):
+ if clazz_name == None:
+ clazz_name = ""
+ new_group = ClassGroupDefault(props = prop, label = clazz_name)
+ new_group.SetVisible(map_clazz.get_status())
+ clazz.SetDefaultGroup(new_group)
+ defaultclazzset = True
+ # break, because alle classes behind the one which definies the
+ # default will be ignored by mapserver. See sample
+ # TODO: if possible set it as invisible class in thuban, but
+ # show the parts of that class as default.
+ # not possible at the moment ?
+ break
+
+ # this is the String comparison.
+ # the expressionstring is enclosed by the ' or " indications
+ elif ((expressionstring[0] == "'") or
+ (expressionstring[0] == '"')):
+ expressionclassitem = maplayer.get_classitem().upper()
+ layer.SetClassificationColumn(expressionclassitem)
+ try:
+ theexpression = int(expressionstring[1:-1])
+ except:
+ theexpression = expressionstring[1:-1]
+ if clazz_name == None:
+ clazz_name = str(theexpression)
+ 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()))
+ else:
+ new_group = ClassGroupSingleton(props = prop,label = clazz_name)
+ new_group.SetVisible(map_clazz.get_status())
+ clazz.AppendGroup(new_group)
+
+ # if there is no default layer set,
+ # the color and linecolor will set as Transparent.
+ if (not defaultclazzset):
+ proptp = ClassGroupProperties()
+ proptp.SetLineColor(Transparent)
+ new_group = ClassGroupDefault(props = proptp)
+ clazz.SetDefaultGroup(new_group)
+ defaultclazzset = None
+
+ # set the visible status
+ layer.SetVisible(maplayer.get_status())
+ # associate the maplayer object to the layer in thuban.
+ layer.extension_umn_layerobj = maplayer
+
+ #add the projection if exists
+ set_projection_to_tb(layer, maplayer)
+
+ #add the layer into thuban
+ tb_map.AddLayer(layer)
+
+def select_layer2import(context, mapobj):
+ """
+ shows a dialog to select the layers to import.
+
+ mapobj = the Mapobject created from the mapfile
+ """
+ lst = []
+ selectedlayer = []
+ numlayers = len(mapobj.get_layers())
+ for layernr in range(0, numlayers,1):
+ lst.append(mapobj.get_layers()[layernr].get_name() +
+ " (" + mapobj.get_layers()[layernr].get_type() +")" )
+
+ dlgsize = (300,130+len(lst)*20)
+ if dlgsize[1] >= 300:
+ dlgsize = (300,300)
+ dlg = wxMultipleChoiceDialog(context.mainwindow,
+ "Select the layers from the\n" +
+ "list to load into thuban.\n" +
+ "annotation not supported !",
+ "Select Layer.", lst, size = dlgsize)
+ if (dlg.ShowModal() == wxID_OK):
+ selectedlayer = dlg.GetValue()
+ return selectedlayer
+
+
def import_mapfile(context):
"""
Import the mapfile from a file given by the user. After that parse
the mapfile with the mapscript parser to create all necessary objects.
- Loaded polygon layer like polygon, line or point into thuban. Raster layer
- are supported also, but with some limitations.
+ Loaded polygon layer like polygon, line or point into thuban.
+ Raster layer are supported also.
context - the thuban context
"""
@@ -85,15 +326,9 @@
else:
return
- # get the path to the file
- mapfilepath = os.path.dirname(filename)
# Parse mapfile
mapobj = parse_mapfile(filename)
- # set the filepath empty if shapepath is absolute
- if os.path.isabs(mapobj.get_shapepath()):
- filepath = ""
- else:
- filepath = mapfilepath
+
# Show number of Layer found in file
numlayers = len(mapobj.get_layers())
if numlayers == 0:
@@ -102,256 +337,60 @@
else:
context.mainwindow.RunMessageBox(_('Loading Layer'),
_("%s Layer loaded from file.") % numlayers)
-
- selectedlayer = []
- # Show a dialog to select layers to load into thuban
+ # Show a dialog to select layers to load into thuban only
+ # if the mapfile contains any layer
if numlayers != 0:
- lst = []
- for layernr in range(0, numlayers,1):
- lst.append(mapobj.get_layers()[layernr].get_name() +
- " (" + mapobj.get_layers()[layernr].get_type() +")" )
-
- dlgsize = (300,130+len(lst)*20)
- if dlgsize[1] >= 300:
- dlgsize = (300,300)
- dlg = wxMultipleChoiceDialog(context.mainwindow,
- "Select the layers from the\n" +
- "list to load into thuban.\n" +
- "annotation not supported !",
- "Select Layer.", lst, size = dlgsize)
- if (dlg.ShowModal() == wxID_OK):
- selectedlayer = dlg.GetValue()
-
+ selectedlayer = select_layer2import(context,mapobj)
+ else:
+ selectedlayer = []
# counter to show the numer of layer loaded into Tuban
layer_count = 0
- # thuban map context
- tb_map = context.mainwindow.canvas.Map()
- # set the titel an projection
- tb_map.SetTitle(mapobj.get_name())
- set_projection_to_tb(tb_map,mapobj)
-
- # Check for each Layer if it is possible to show in Thuban
- for layernr in selectedlayer:
- maplayer = mapobj.get_layers()[layernr]
- layertitle = maplayer.get_name()
-
- #check if rasterlayer
- if maplayer.get_type() == 'raster':
- imgpath = maplayer.get_data()
- # if there is no imagepath defined, the Raster Layer could not load
- if imgpath == None:
- context.mainwindow.RunMessageBox(_('Error Loading Raster Layer'),
- _("Can't open the rasterlayer '%s'.") % layertitle)
- else:
- if os.path.isabs(imgpath):
- filename = imgpath
- else:
- filename = os.path.join(filepath,mapobj.get_shapepath(),imgpath)
- # Normalize the pathname by collapses
- # redundant separators and up-level references
- filename = os.path.normpath(filename)
-
- rasterlayer = RasterLayer(maplayer.get_name(), filename)
- # set the visible status
- rasterlayer.SetVisible(maplayer.get_status())
- #add the projection if exists
- set_projection_to_tb(rasterlayer, maplayer)
- tb_map.AddLayer(rasterlayer)
- layer_count += 1
-
- #check if polygonlayer
- if ((maplayer.get_type() == 'polygon') or
- (maplayer.get_type() == 'line') or
- (maplayer.get_type() == 'circle') or
- (maplayer.get_type() == 'point')):
+
+ # import settings to thuban only if one layer is selected
+ if len(selectedlayer) != 0:
+ # thuban map context
+ tb_map = context.mainwindow.canvas.Map()
+ # set the titel and projection
+ tb_map.SetTitle(mapobj.get_name())
+ set_projection_to_tb(tb_map,mapobj)
- if mapobj.get_layers()[layernr].get_data():
- if os.path.isabs(mapobj.get_layers()[layernr].get_data()):
- filename = mapobj.get_layers()[layernr].get_data() +".shp"
- else:
- filename = os.path.join(filepath, mapobj.get_shapepath(), \
- mapobj.get_layers()[layernr].get_data())
- filename = filename + ".shp"
- # Normalize the pathname by collapses
- # redundant separators and up-level references
- filename = os.path.normpath(filename)
- else:
- context.mainwindow.RunMessageBox(_('Error Loading Layer'),
- _("no shp file definied, maybe used a feature obj '%s'.") % layertitle)
- # try to open the layer
- try:
- store = context.application.Session().OpenShapefile(filename)
+ # Check for each Layer if it is possible to show in Thuban
+ for layernr in selectedlayer:
+ maplayer = mapobj.get_layers()[layernr]
+
+ #check if rasterlayer type
+ if maplayer.get_type() == 'raster':
+ add_rasterlayer(context, tb_map, mapobj, maplayer)
layer_count += 1
- except IOError:
- # the layer couldn't be opened
- context.mainwindow.RunMessageBox(_('Open Shapepath'),
- _("Can't open the file '%s'.") % filename)
- else:
- # create a new layer which will be added to thuban later
- layer = Layer(layertitle, store)
- # all classes of the maplayer
- map_clazzes = maplayer.get_classes()
- # number of layers loaded vom maplayer
- map_numclazzes = len(map_clazzes)
- # defaultclazzset is necessary to know if a default class is set
- # in thuban. if is not and there are more them one class in the
- # mapfile the colors of the default layer in thuban musst
- # set to Transparence
- defaultclazzset = None
-
- # create a class in thuban for every class in the maplayer
- for map_clazznr in range(0, map_numclazzes, 1):
- # define the color
- map_clazz = map_clazzes[map_clazznr]
- layer_style = map_clazz.get_styles()[0]
- clazz_name = map_clazz.get_name()
- clazz = layer.GetClassification()
- prop = ClassGroupProperties()
-
- # set the layerprojection if given
- # TODO check this, becaus it is not working correctly
- # set_projection_to_tb(layer, maplayer)
-
- # if outline color is defined use it else set transparent
- if layer_style.get_outlinecolor():
- tb_color = layer_style.get_outlinecolor().get_thubancolor()
- prop.SetLineColor(tb_color)
- else:
- prop.SetLineColor(Transparent)
- # if color is defined use it as fillcolor
- # but if the layer type is line use the color as linecolor
- if layer_style.get_color():
- tb_color = layer_style.get_color().get_thubancolor()
- if maplayer.get_type() == 'line':
- prop.SetLineColor(tb_color)
- else:
- prop.SetFill(tb_color)
- else:
- prop.SetFill(Transparent)
-
- #set size for the line
- if ((maplayer.get_type() == 'polygon') or
- (maplayer.get_type() == 'line')):
- if layer_style.get_size():
- prop.SetLineWidth(layer_style.get_size())
-
- # generate special expression classes to show in thuban
- # not all possibilities from the MapServer are supported now
- # supporteed:
- # String comparisons
- # not supported:
- # Regular expressions
- # Logical expressions
- expressionstring = map_clazz.get_expressionstring()
- if (((map_numclazzes == 1) & (not expressionstring)) or
- ((map_numclazzes > 1) & (expressionstring == None)) or
- (expressionstring == '/./')):
- if clazz_name == None:
- clazz_name = ""
- new_group = ClassGroupDefault(props = prop, label = clazz_name)
- new_group.SetVisible(map_clazz.get_status())
- clazz.SetDefaultGroup(new_group)
- defaultclazzset = True
- # break, because alle classes behind the one which definies the
- # default will be ignored by mapserver. See sample
- # TODO: if possible set it as invisible class in thuban, but
- # show the parts of that class as default.
- # not possible at the moment ?
- break
-
- # this is the String comparison.
- # the expressionstring is enclosed by the ' or " indications
- elif ((expressionstring[0] == "'") or
- (expressionstring[0] == '"')):
- expressionclassitem = maplayer.get_classitem().upper()
- layer.SetClassificationColumn(expressionclassitem)
- try:
- theexpression = int(expressionstring[1:-1])
- except:
- theexpression = expressionstring[1:-1]
- if clazz_name == None:
- clazz_name = str(theexpression)
- new_group = ClassGroupSingleton(value = theexpression,
- 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())
- clazz.AppendGroup(new_group)
-
- # if there is no default layer set,
- # the color and linecolor will set as Transparent.
- if (not defaultclazzset):
- proptp = ClassGroupProperties()
- proptp.SetLineColor(Transparent)
- new_group = ClassGroupDefault(props = proptp)
- clazz.SetDefaultGroup(new_group)
- defaultclazzset = None
-
- # set the visible status
- layer.SetVisible(maplayer.get_status())
- # associate the maplayer object to the layer in thuban.
- layer.extension_umn_layerobj = maplayer
-
- #add the projection if exists
- set_projection_to_tb(layer, maplayer)
-
- #add the layer into thuban
- tb_map.AddLayer(layer)
-
- # maybe remove all used layers
- # for nr in range(len(selectedlayer)-1, -1, -1):
- # print selectedlayer[nr]
- # mapobj.remove_layer(selectedlayer[nr])
- # remove alle layer from the mapobj, because the layer will be
- # created new in export, and the unused layers are not needed
- # TODO: Save the layers which are not used somewhere, to edit
- # them later and export it.
- for nr in range(len(mapobj.get_layers())-1, -1, -1):
- mapobj.remove_layer(nr)
+ #check if polygonlayer type
+ if ((maplayer.get_type() == 'polygon') or
+ (maplayer.get_type() == 'line') or
+ (maplayer.get_type() == 'circle') or
+ (maplayer.get_type() == 'point')):
+ add_polygonlayer(context, tb_map, mapobj, maplayer)
+ layer_count += 1
- # add the map object to thuban, to use it later
- tb_map.extension_umn_mapobj = mapobj
+ # remove alle layer from the mapobj, because the layer will be
+ # created new in export, and the unused layers are not needed
+ # TODO: Save the layers which are not used somewhere, to edit
+ # them later and export it.
+ for nr in range(len(mapobj.get_layers())-1, -1, -1):
+ mapobj.remove_layer(nr)
- # fit the new map to the window
- context.mainwindow.canvas.FitMapToWindow()
+ # add the map object to thuban, to use it later
+ tb_map.extension_umn_mapobj = mapobj
+
+ # show a message how many layer were loaded into thuban
+ # if the number of layers is not null
+ if numlayers != 0:
+ # fit the new map to the window
+ context.mainwindow.canvas.FitMapToWindow()
+
+ context.mainwindow.RunMessageBox(_('Layer loaded'),
+ _("%s Layer loaded into Thuban") % layer_count)
- # show a message how many layer were loaded into thuban
- # if the number of layers is not null
- if numlayers != 0:
- context.mainwindow.RunMessageBox(_('Layer loaded'),
- _("%s Layer loaded into Thuban") % layer_count)
-def set_projection_to_tb(tb_con, mapobj):
- """
- Helpfunction to set the projection in thuban.
-
- Parameters:
- tb_con -- The thuban conext (mapobj or layerobj)
- mapobj -- The mapobject (from mapscript)
- """
- if (mapobj.get_projection().get_params() != None):
- projparams = mapobj.get_projection().get_params()
- # check if it is an latlong projection, becaues one
- # more parameter is needed in that case
- addtometer = False
- for param in projparams:
- projkey = param.split("=")
- if projkey[0] == "proj":
- if projkey[1] == "latlong":
- addtometer = True
- if projkey[0] == "to_meter":
- addtometer = False
- if addtometer == True:
- projparams.append("to_meter=0.017453")
- tb_con.SetProjection(Projection(projparams))
- if mapobj.get_projection().get_epsgcode() != None:
- projepsg = mapobj.get_projection().get_epsgproj()
- tb_con.SetProjection(projepsg)
-
def parse_mapfile(filename):
"""
Parse the mapfile.
More information about the Thuban-devel
mailing list
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)