bernhard: thuban/Extensions/svgexport svgmapwriter.py, 1.2, 1.3 svgsaver.py, 1.2, 1.3

cvs@intevation.de cvs at intevation.de
Tue Sep 21 23:30:38 CEST 2004


Author: bernhard

Update of /thubanrepository/thuban/Extensions/svgexport
In directory doto:/tmp/cvs-serv28184/Extensions/svgexport

Modified Files:
	svgmapwriter.py svgsaver.py 
Log Message:
Improved the svgexport to only use unique ids. Will issues 
an error message dialoge when two layer names are the same.
ShapeIDs are now added with a dash within the svg ids.

* Extensions/svgexport/svgmapwriter.py (SVGMapWriterError): New.
* Extensions/svgexport/test/test_svgmapwriter.py: Added imports
(TestSVGRenderer): New test class with test_make_in() and
test_check_for_layer_name_clash()
* Extensions/svgexport/svgmapwriter.py (SVGRenderer): Fixed __init__()
and draw_shape_layer_incrementally() to not use a baseid twice, 
satisfying test_check_for_layer_name_clash()
(VirtualDC.make_id): Use a dash between baseit and id, satisfies
test_make_in().
* Extensions/svexport/svgsaver.py: Import SVGMapWriterError, wxOK
and wxICON_HAND. 
(write_to_svg): Put dc and rendering in a try statement and on
catching SVGmapWriterError notify the user and delete the target file.
-------------------------------------------------------------------


Index: svgmapwriter.py
===================================================================
RCS file: /thubanrepository/thuban/Extensions/svgexport/svgmapwriter.py,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- svgmapwriter.py	20 Feb 2004 14:33:22 -0000	1.2
+++ svgmapwriter.py	21 Sep 2004 21:30:36 -0000	1.3
@@ -25,6 +25,8 @@
 from string import join
 # We need to determine some object types
 from types import ListType
+
+from Thuban import _
 # VirtualDC extends XMLWriter
 from Thuban.Model.xmlwriter import XMLWriter, escape
 # Color related classes from the model of thuban
@@ -147,6 +149,14 @@
 # Instantiate a solid pattern.
 SOLID = Pattern()
 
+class SVGMapWriterError(Exception):
+    """Get raised for problems when writing map-svg files.
+    
+    Occasion when this exception is raised:
+        Two layers have the same name to be used as BaseId: Name Clash 
+    """
+
+
 class SVGRenderer(BaseRenderer):
     """Class to render a map onto a VirtualDC.
     
@@ -163,7 +173,8 @@
                 resolution, honor_visibility)
         #
         self.factor = (abs(region[2]) + abs(region[3])) / (2.0 * 1000.0)
-        
+        self.used_baseids=[]   # needed for name clash check
+
     def make_point(self, x, y):
         """Return a Point object from two values."""
         return Point(x, y)
@@ -231,7 +242,7 @@
         dc = self.dc
         brush = TRANSPARENT_BRUSH
         pen   = TRANSPARENT_PEN
-        
+
         value = None
         field = None
         lc = layer.GetClassification()
@@ -251,8 +262,12 @@
                 self.low_level_renderer(layer)
         tool_cache = {}
         
-        # Set baseid - prefix of a shape id to be unique
+        if layer.title in self.used_baseids:
+            raise SVGMapWriterError(_("Clash of layer names!\n")+ \
+                _("Two layers probably have the same name, try renaming one."))
+        # prefix of a shape id to be unique
         dc.SetBaseID(layer.title)
+        self.used_baseids.append(layer.title)
         # Titel of current layer to the groups meta informations
         dc.BeginGroup(meta={'Layer':layer.Title(), })
         # Delete all MetaData
@@ -558,10 +573,13 @@
         return 'meta="%s"' % (join(result, '; '))
     
     def make_id(self):
-        """Get the ID for the next object - if current ID is valid."""
+        """Return id= string for object out of currently set baseid and id.
+        
+        Return the empty string if no id was set.
+        """
         if self.id < 0:
             return ''
-        else: return 'id="%s%s"' % (self.baseid, self.id)
+        return 'id="%s-%s"' % (self.baseid, self.id)
 
     def DrawEllipse(self, x, y, dx, dy):
         """Draw an ellipse."""

Index: svgsaver.py
===================================================================
RCS file: /thubanrepository/thuban/Extensions/svgexport/svgsaver.py,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- svgsaver.py	16 May 2004 09:33:04 -0000	1.2
+++ svgsaver.py	21 Sep 2004 21:30:36 -0000	1.3
@@ -16,7 +16,8 @@
 
 
 # Needed wx-toolkit classes
-from wxPython.wx import wxFileDialog, wxSAVE, wxOVERWRITE_PROMPT, wxID_OK
+from wxPython.wx import wxFileDialog, wxSAVE, wxOVERWRITE_PROMPT, wxID_OK, \
+        wxOK, wxICON_HAND
 
 # We need os.path
 import os
@@ -25,7 +26,7 @@
 from Thuban import _
 
 # Import SVG related classes
-from svgmapwriter import VirtualDC, SVGRenderer
+from svgmapwriter import VirtualDC, SVGRenderer, SVGMapWriterError
 
 
 def write_to_svg(context):
@@ -64,15 +65,22 @@
         # Get all selected layers and shapes that should be written as SVG
         selected_layer = canvas.selection.SelectedLayer()
         selected_shapes = canvas.selection.SelectedShapes()
-        dc = VirtualDC(file, (mapwidth, mapheight, ))
-        dc.BeginExport()
-        # map scale offset region
-        renderer = SVGRenderer(dc, map, 1.0, (0 - min(llx, urx),
-                0 + max(lly, ury)), mapregion)
-        # Render the map
-        renderer.RenderMap(selected_layer, selected_shapes)
-        #
-        dc.EndExport()
+        try:
+            dc = VirtualDC(file, (mapwidth, mapheight, ))
+            dc.BeginExport()
+            # map scale offset region
+            renderer = SVGRenderer(dc, map, 1.0, (0 - min(llx, urx),
+                    0 + max(lly, ury)), mapregion)
+            # Render the map
+            renderer.RenderMap(selected_layer, selected_shapes)
+            dc.EndExport()
+        except SVGMapWriterError, inst:
+            context.mainwindow.RunMessageBox(_("Error: SVG not written!"),
+                text=_("Could not write SVG because: ")+ str(inst),
+                flags= wxOK | wxICON_HAND)
+            # delete partly writting file
+            os.remove(file)
+
 
 
 # Thuban has named commands which can be registered in the central





More information about the Thuban-devel mailing list

This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)