/[thuban]/branches/WIP-pyshapelib-bramz/Extensions/drawshape/drawshape.py
ViewVC logotype

Annotation of /branches/WIP-pyshapelib-bramz/Extensions/drawshape/drawshape.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2734 - (hide annotations)
Thu Mar 1 12:42:59 2007 UTC (18 years ago) by bramz
File MIME type: text/x-python
File size: 5026 byte(s)
made a copy
1 bh 1974 # Copyright (C) 2003 by Intevation GmbH
2     # Authors:
3     # Bernhard Herzog <[email protected]>
4     #
5     # This program is free software under the GPL (>=v2)
6     # Read the file COPYING coming with the software for details.
7    
8     """Extension to draw polygons
9    
10     *** Warning: ***
11    
12     This extension is very experimental and may corrupt your data. Use at
13     your own peril.
14     """
15    
16     __version__ = "$Revision$"
17     # $Source$
18     # $Id$
19    
20    
21     import os
22    
23     from wxPython.wx import *
24    
25     import shapelib
26 jan 2207 import Thuban
27     from Thuban import _
28 bh 1974 from Thuban.Model.data import SHAPETYPE_POLYGON
29     from Thuban.Model.table import FIELDTYPE_INT, FIELDTYPE_STRING, \
30     FIELDTYPE_DOUBLE
31     from Thuban.UI.command import registry, ToolCommand
32 jan 2207 from Thuban.UI.mainwindow import main_menu, main_toolbar
33 bh 1974 from Thuban.UI.viewport import Tool
34    
35    
36     def write_empty_row(table, row):
37     """Write an empty record to the row
38    
39     The values in the record will be set to suitable default values
40     depending on the type: 0 for numeric types and the empty string for
41     strings.
42     """
43     values = {}
44     for col in table.Columns():
45     if col.type == FIELDTYPE_INT:
46     value = 0
47     elif col.type == FIELDTYPE_DOUBLE:
48     value = 0.0
49     elif col.type == FIELDTYPE_STRING:
50     value = ""
51     else:
52     print "write_empty_row: Unhandled col.type", col.type
53     values[col.name] = value
54     table.write_record(row, values)
55    
56    
57     def write_shape(shapestore, points):
58     """Addd the points as a new shape to the shapestore
59    
60     The points argument should be a list of the same structure as that
61     returned by the shapelib bindings for a polygon. It is passed
62     directly to the SHPObject constructor.
63     """
64     shapefile = shapelib.ShapeFile(shapestore.FileName(), "r+b")
65     obj = shapelib.SHPObject(shapelib.SHPT_POLYGON, 1, points)
66     newid = shapefile.write_object(-1, obj)
67     write_empty_row(shapestore.Table(), newid)
68     shapefile.close()
69     shapestore._open_shapefile()
70    
71    
72     class ShapeDrawTool(Tool):
73    
74     def __init__(self, view):
75     Tool.__init__(self, view)
76     self.points = []
77    
78     def Name(self):
79     return "ShapeDrawTool"
80    
81     def find_shapestore(self):
82     """Return the shapestore into which to write and the projection
83    
84     If the currently selected layer is a layer with polygons return
85     a tuple of the shapestore and the layer's projection.
86     Otherwise return a tuple of Nones.
87     """
88     layer = self.view.SelectedLayer()
89     if layer is not None and layer.HasShapes() \
90     and layer.ShapeType() == SHAPETYPE_POLYGON:
91     return layer.ShapeStore(), layer.GetProjection()
92     return None, None
93    
94     def MouseDown(self, event):
95     Tool.MouseDown(self, event)
96     if event.RightDown():
97     map_proj = self.view.Map().GetProjection()
98     shapestore, proj = self.find_shapestore()
99     if shapestore is not None and len(self.points) > 2:
100     points = self.points[:]
101     if map_proj is not None:
102     points = [tuple(map_proj.Inverse(*p)) for p in points]
103     if proj is not None:
104     points = [tuple(proj.Forward(*p)) for p in points]
105     points.append(points[0])
106     write_shape(shapestore, [points])
107     self.points = []
108     self.view.full_redraw()
109     else:
110     if not self.points:
111     self.points.append(self.view.win_to_proj(*self.current))
112    
113     def MouseUp(self, event):
114     Tool.MouseUp(self, event)
115     self.points.append(self.view.win_to_proj(*self.current))
116    
117     def draw(self, dc):
118     points = [self.view.proj_to_win(*p) for p in self.points] \
119     + [self.current]
120     if len(points) == 2:
121     dc.DrawLines(points)
122     else:
123     dc.DrawPolygon(points)
124    
125     def DrawPermanent(self, dc):
126     dc.SetPen(wxPen(wxColor(255, 128, 0), 2))
127     dc.SetBrush(wxTRANSPARENT_BRUSH)
128     dc.DrawPolygon([self.view.proj_to_win(*p) for p in self.points])
129    
130    
131     def shape_draw_tool(context):
132     canvas = context.mainwindow.canvas
133     canvas.SelectTool(ShapeDrawTool(canvas))
134    
135     def check_shape_draw_tool(context):
136     return context.mainwindow.canvas.CurrentTool() == "ShapeDrawTool"
137    
138    
139     iconfile = os.path.join(os.path.abspath(Thuban.__path__[0]),
140     "..", "Resources", "Bitmaps", "identify")
141     registry.Add(ToolCommand("shape_draw_tool", "Shape Draw Tool",
142     shape_draw_tool, icon = iconfile,
143     helptext = "Draw a shape",
144     checked = check_shape_draw_tool))
145    
146     # Add the command to the toolbar
147 jan 2207 main_toolbar.InsertSeparator()
148     main_toolbar.InsertItem("shape_draw_tool")
149    
150     # find the experimental menu (create it anew if not found)
151     experimental_menu = main_menu.FindOrInsertMenu('experimental',
152     _('Experimenta&l'))
153    
154     # finally add the new command to the experimental menu
155     experimental_menu.InsertItem('shape_draw_tool')

Properties

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26