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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1974 - (show annotations)
Tue Nov 25 14:05:35 2003 UTC (21 years, 3 months ago) by bh
Original Path: trunk/thuban/Extensions/drawshape/drawshape.py
File MIME type: text/x-python
File size: 4710 byte(s)
Add very experimental (and possibly dangerous) extension to draw
polygons:

* Extensions/drawshape/README: New. Brief installation
instructions

* Extensions/drawshape/drawshape.py: New. Implementation of the
drawshape extensions

* Extensions/drawshape/patch.diff: Patch to apply before the
extension can be used.

1 # 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 from Thuban.Model.data import SHAPETYPE_POLYGON
27 from Thuban.Model.table import FIELDTYPE_INT, FIELDTYPE_STRING, \
28 FIELDTYPE_DOUBLE
29 from Thuban.UI.command import registry, ToolCommand
30 import Thuban.UI.mainwindow
31 from Thuban.UI.viewport import Tool
32
33
34 def write_empty_row(table, row):
35 """Write an empty record to the row
36
37 The values in the record will be set to suitable default values
38 depending on the type: 0 for numeric types and the empty string for
39 strings.
40 """
41 values = {}
42 for col in table.Columns():
43 if col.type == FIELDTYPE_INT:
44 value = 0
45 elif col.type == FIELDTYPE_DOUBLE:
46 value = 0.0
47 elif col.type == FIELDTYPE_STRING:
48 value = ""
49 else:
50 print "write_empty_row: Unhandled col.type", col.type
51 values[col.name] = value
52 table.write_record(row, values)
53
54
55 def write_shape(shapestore, points):
56 """Addd the points as a new shape to the shapestore
57
58 The points argument should be a list of the same structure as that
59 returned by the shapelib bindings for a polygon. It is passed
60 directly to the SHPObject constructor.
61 """
62 shapefile = shapelib.ShapeFile(shapestore.FileName(), "r+b")
63 obj = shapelib.SHPObject(shapelib.SHPT_POLYGON, 1, points)
64 newid = shapefile.write_object(-1, obj)
65 write_empty_row(shapestore.Table(), newid)
66 shapefile.close()
67 shapestore._open_shapefile()
68
69
70 class ShapeDrawTool(Tool):
71
72 def __init__(self, view):
73 Tool.__init__(self, view)
74 self.points = []
75
76 def Name(self):
77 return "ShapeDrawTool"
78
79 def find_shapestore(self):
80 """Return the shapestore into which to write and the projection
81
82 If the currently selected layer is a layer with polygons return
83 a tuple of the shapestore and the layer's projection.
84 Otherwise return a tuple of Nones.
85 """
86 layer = self.view.SelectedLayer()
87 if layer is not None and layer.HasShapes() \
88 and layer.ShapeType() == SHAPETYPE_POLYGON:
89 return layer.ShapeStore(), layer.GetProjection()
90 return None, None
91
92 def MouseDown(self, event):
93 Tool.MouseDown(self, event)
94 if event.RightDown():
95 map_proj = self.view.Map().GetProjection()
96 shapestore, proj = self.find_shapestore()
97 if shapestore is not None and len(self.points) > 2:
98 points = self.points[:]
99 if map_proj is not None:
100 points = [tuple(map_proj.Inverse(*p)) for p in points]
101 if proj is not None:
102 points = [tuple(proj.Forward(*p)) for p in points]
103 points.append(points[0])
104 write_shape(shapestore, [points])
105 self.points = []
106 self.view.full_redraw()
107 else:
108 if not self.points:
109 self.points.append(self.view.win_to_proj(*self.current))
110
111 def MouseUp(self, event):
112 Tool.MouseUp(self, event)
113 self.points.append(self.view.win_to_proj(*self.current))
114
115 def draw(self, dc):
116 points = [self.view.proj_to_win(*p) for p in self.points] \
117 + [self.current]
118 if len(points) == 2:
119 dc.DrawLines(points)
120 else:
121 dc.DrawPolygon(points)
122
123 def DrawPermanent(self, dc):
124 dc.SetPen(wxPen(wxColor(255, 128, 0), 2))
125 dc.SetBrush(wxTRANSPARENT_BRUSH)
126 dc.DrawPolygon([self.view.proj_to_win(*p) for p in self.points])
127
128
129 def shape_draw_tool(context):
130 canvas = context.mainwindow.canvas
131 canvas.SelectTool(ShapeDrawTool(canvas))
132
133 def check_shape_draw_tool(context):
134 return context.mainwindow.canvas.CurrentTool() == "ShapeDrawTool"
135
136
137 iconfile = os.path.join(os.path.abspath(Thuban.__path__[0]),
138 "..", "Resources", "Bitmaps", "identify")
139 registry.Add(ToolCommand("shape_draw_tool", "Shape Draw Tool",
140 shape_draw_tool, icon = iconfile,
141 helptext = "Draw a shape",
142 checked = check_shape_draw_tool))
143
144 # Add the command to the toolbar
145 Thuban.UI.mainwindow.main_toolbar.InsertSeparator()
146 Thuban.UI.mainwindow.main_toolbar.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