1 |
jan |
2402 |
# Copyright (C) 2004 by Intevation GmbH |
2 |
frank |
2196 |
# Authors: |
3 |
jan |
2402 |
# Frank Koormann <[email protected]> (2004) |
4 |
frank |
2196 |
# |
5 |
|
|
# This program is free software under the GPL (>=v2) |
6 |
|
|
# Read the file COPYING coming with Thuban for details. |
7 |
|
|
|
8 |
|
|
""" |
9 |
|
|
Extend thuban with a bounding box dump. |
10 |
|
|
|
11 |
|
|
Dumps the bounding boxes of all shapes of the selected layer. |
12 |
|
|
An optional column can be specified to group the objects, |
13 |
|
|
in this case the bounding box is a union of the separate boxes. |
14 |
|
|
""" |
15 |
|
|
|
16 |
|
|
__version__ = '$Revision$' |
17 |
jan |
2203 |
# $Source$ |
18 |
|
|
# $Id$ |
19 |
frank |
2196 |
|
20 |
|
|
import os, sys |
21 |
frank |
2197 |
import string |
22 |
frank |
2196 |
|
23 |
dpinte |
2721 |
import wx |
24 |
|
|
from wx.lib.dialogs import ScrolledMessageDialog |
25 |
frank |
2196 |
|
26 |
|
|
from Thuban.UI.common import ThubanBeginBusyCursor, ThubanEndBusyCursor |
27 |
|
|
from Thuban.UI.command import registry, Command |
28 |
|
|
from Thuban.UI.mainwindow import main_menu, _has_selected_shape_layer |
29 |
|
|
from Thuban import _ |
30 |
|
|
|
31 |
|
|
import shapelib |
32 |
|
|
import dbflib |
33 |
|
|
|
34 |
frank |
2197 |
# Widget IDs |
35 |
frank |
2196 |
ID_FILENAME = 4001 |
36 |
|
|
ID_ATTRIBUTES = 4002 |
37 |
|
|
ID_SELFN = 4003 |
38 |
|
|
|
39 |
dpinte |
2721 |
class BBoxDumpDialog(wx.Dialog): |
40 |
frank |
2196 |
"""Bounding Box Dump Dialog |
41 |
|
|
|
42 |
|
|
Specify a filename for the dump and optionally a layer's column |
43 |
|
|
field to group objects. |
44 |
|
|
""" |
45 |
|
|
|
46 |
|
|
def __init__(self, parent, title, layer = None): |
47 |
dpinte |
2721 |
wx.Dialog.__init__(self, parent, -1, title, |
48 |
|
|
style = wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER) |
49 |
frank |
2196 |
|
50 |
|
|
if layer is None: |
51 |
dpinte |
2721 |
return wx.ID_CANCEL |
52 |
frank |
2196 |
|
53 |
|
|
# Store the layer |
54 |
|
|
self.layer = layer |
55 |
|
|
|
56 |
|
|
# Filename selection elements |
57 |
dpinte |
2721 |
self.filename = wx.TextCtrl(self, ID_FILENAME, "") |
58 |
|
|
self.button_selectfile = wx.Button(self, ID_SELFN, _('Select...')) |
59 |
|
|
wx.EVT_BUTTON(self, ID_SELFN, self.OnSelectFilename) |
60 |
frank |
2196 |
|
61 |
|
|
# Column choice elements |
62 |
dpinte |
2721 |
self.choice_column = wx.Choice(self, ID_ATTRIBUTES) |
63 |
frank |
2196 |
self.choice_column.Append(_('Select...'), None) |
64 |
|
|
for col in self.layer.ShapeStore().Table().Columns(): |
65 |
|
|
self.choice_column.Append(col.name, col) |
66 |
|
|
self.choice_column.SetSelection(0) |
67 |
|
|
|
68 |
|
|
# Dialog button elements |
69 |
dpinte |
2721 |
self.button_dump = wx.Button(self, wx.ID_OK, _("OK")) |
70 |
|
|
wx.EVT_BUTTON(self, wx.ID_OK, self.OnDump) |
71 |
frank |
2196 |
self.button_dump.SetDefault() |
72 |
|
|
# TODO: Disable the OK button until a filename is entered ... |
73 |
|
|
# self.button_dump.Enable(False) |
74 |
dpinte |
2721 |
self.button_cancel = wx.Button(self, wx.ID_CANCEL, _("Cancel")) |
75 |
frank |
2196 |
|
76 |
|
|
|
77 |
|
|
# Dialog Layout: three horizontal box sizers. |
78 |
dpinte |
2721 |
topbox = wx.BoxSizer(wx.VERTICAL) |
79 |
frank |
2196 |
|
80 |
dpinte |
2721 |
hbox = wx.BoxSizer(wx.HORIZONTAL) |
81 |
|
|
topbox.Add(hbox, 0, wx.ALL|wx.EXPAND) |
82 |
|
|
hbox.Add(wx.StaticText(self, -1, _("File:")), |
83 |
|
|
0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 4) |
84 |
|
|
hbox.Add(self.filename, 1, wx.ALL|wx.EXPAND, 4) |
85 |
|
|
hbox.Add(self.button_selectfile, 0, wx.ALL, 4) |
86 |
frank |
2196 |
|
87 |
dpinte |
2721 |
hbox = wx.BoxSizer(wx.HORIZONTAL) |
88 |
|
|
topbox.Add(hbox, 0, wx.ALL|wx.EXPAND) |
89 |
|
|
hbox.Add(wx.StaticText(self, -1, _("Group by:")), |
90 |
|
|
0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 4) |
91 |
|
|
hbox.Add(self.choice_column, 1, wx.ALL|wx.EXPAND, 4) |
92 |
frank |
2196 |
|
93 |
dpinte |
2721 |
hbox = wx.BoxSizer(wx.HORIZONTAL) |
94 |
|
|
topbox.Add(hbox, 0, wx.ALL|wx.EXPAND) |
95 |
|
|
hbox.Add(self.button_dump, 0, wx.ALL|wx.ALIGN_CENTER, |
96 |
frank |
2196 |
10) |
97 |
dpinte |
2721 |
hbox.Add(self.button_cancel, 0, wx.ALL|wx.ALIGN_CENTER, |
98 |
frank |
2196 |
10) |
99 |
|
|
|
100 |
|
|
# Finalize ... |
101 |
|
|
self.SetAutoLayout(True) |
102 |
|
|
self.SetSizer(topbox) |
103 |
|
|
topbox.Fit(self) |
104 |
|
|
topbox.SetSizeHints(self) |
105 |
|
|
|
106 |
|
|
# Store for later use |
107 |
|
|
self.parent = parent |
108 |
|
|
|
109 |
|
|
def OnDump(self, event): |
110 |
|
|
"""Bounding Box Dump Dialog event handler OK button. |
111 |
|
|
|
112 |
|
|
Prepare the inputs from the dialog and call processing. |
113 |
|
|
""" |
114 |
|
|
i = self.choice_column.GetSelection() |
115 |
|
|
column = self.choice_column.GetClientData(i) |
116 |
|
|
self.Close() |
117 |
|
|
|
118 |
|
|
ThubanBeginBusyCursor() |
119 |
|
|
try: |
120 |
|
|
bboxmessage = bboxdump(self.layer, column, self.filename.GetValue()) |
121 |
|
|
finally: |
122 |
|
|
ThubanEndBusyCursor() |
123 |
|
|
|
124 |
|
|
if bboxmessage: |
125 |
dpinte |
2721 |
dlg = ScrolledMessageDialog( |
126 |
frank |
2196 |
self.parent, bboxmessage, |
127 |
frank |
2197 |
_("Bounding Box Dump %s") % self.layer.Title()) |
128 |
frank |
2196 |
dlg.ShowModal() |
129 |
|
|
|
130 |
|
|
def OnSelectFilename(self, event): |
131 |
|
|
"""Bounding Box Dump Dialog event handler File Selection. |
132 |
|
|
|
133 |
|
|
Opens a file dialog to specify a file to dump into. |
134 |
|
|
""" |
135 |
dpinte |
2721 |
dlg = wx.FileDialog(self, _("Dump Bounding Boxes To"), |
136 |
frank |
2196 |
os.path.dirname(self.filename.GetValue()), |
137 |
|
|
os.path.basename(self.filename.GetValue()), |
138 |
|
|
_("CSV Files (*.csv)|*.csv|") + |
139 |
|
|
_("All Files (*.*)|*.*"), |
140 |
dpinte |
2721 |
wx.SAVE|wx.OVERWRITE_PROMPT) |
141 |
|
|
if dlg.ShowModal() == wx.ID_OK: |
142 |
frank |
2196 |
self.filename.SetValue(dlg.GetPath()) |
143 |
|
|
dlg.Destroy() |
144 |
|
|
else: |
145 |
|
|
dlg.Destroy() |
146 |
|
|
|
147 |
|
|
|
148 |
|
|
def bboxdump(layer, column, filename): |
149 |
|
|
"""Bounding Box Dump Processing |
150 |
|
|
|
151 |
|
|
layer - Layer of shapes to be dumped |
152 |
|
|
column - optional column to group shapes (else None) |
153 |
|
|
filename - optional filename to dump into (else empty string, i.e. dump |
154 |
frank |
2197 |
to message dialog) |
155 |
frank |
2196 |
""" |
156 |
|
|
# Preparation |
157 |
|
|
shapelist = {} |
158 |
frank |
2197 |
bboxmessage = [] |
159 |
frank |
2196 |
|
160 |
dpinte |
2721 |
dlg= wx.ProgressDialog(_("Bounding Box Dump"), |
161 |
frank |
2197 |
_("Collecting shapes ..."), |
162 |
|
|
layer.ShapeStore().NumShapes(), |
163 |
|
|
None) |
164 |
|
|
|
165 |
|
|
cnt = 0 |
166 |
|
|
step = int(layer.ShapeStore().NumShapes() / 100.0) |
167 |
|
|
if step == 0: |
168 |
|
|
step = 1 |
169 |
|
|
|
170 |
frank |
2196 |
# Collect shape ids to be dumped |
171 |
|
|
if column is None: |
172 |
|
|
# A simple dump of shapes bbox is required |
173 |
frank |
2197 |
for s in layer.ShapeStore().AllShapes(): |
174 |
|
|
i = s.ShapeID() |
175 |
frank |
2196 |
shapelist[i] = (i,) |
176 |
frank |
2197 |
if cnt % step == 0: |
177 |
|
|
dlg.Update(cnt) |
178 |
|
|
cnt = cnt + 1 |
179 |
frank |
2196 |
else: |
180 |
|
|
# group them by column ... |
181 |
frank |
2197 |
for s in layer.ShapeStore().AllShapes(): |
182 |
|
|
i = s.ShapeID() |
183 |
frank |
2196 |
row = layer.ShapeStore().Table().ReadRowAsDict(i) |
184 |
|
|
att = row[column.name] |
185 |
|
|
if not shapelist.has_key(att): |
186 |
|
|
shapelist[att] = [] |
187 |
|
|
shapelist[att].append(i) |
188 |
frank |
2197 |
if cnt % step == 0: |
189 |
|
|
dlg.Update(cnt) |
190 |
|
|
cnt = cnt + 1 |
191 |
frank |
2196 |
|
192 |
frank |
2197 |
dlg.Destroy() |
193 |
dpinte |
2721 |
dlg= wx.ProgressDialog(_("Bounding Box Dump"), |
194 |
frank |
2197 |
_("Dump bounding boxes of selected shapes ..."), |
195 |
|
|
len(shapelist), |
196 |
|
|
None) |
197 |
|
|
cnt = 0 |
198 |
|
|
step = int(len(shapelist) / 100.0) |
199 |
|
|
if step == 0: |
200 |
|
|
step = 1 |
201 |
|
|
|
202 |
frank |
2196 |
# Dump them, sorted |
203 |
|
|
keys = shapelist.keys() |
204 |
|
|
keys.sort() |
205 |
|
|
for key in keys: |
206 |
|
|
bbox = layer.ShapesBoundingBox(shapelist[key]) |
207 |
frank |
2197 |
bboxmessage.append("%.3f,%.3f,%.3f,%.3f,%s\n" % ( |
208 |
|
|
bbox[0], bbox[1], bbox[2], bbox[3], key)) |
209 |
|
|
if cnt % step == 0: |
210 |
|
|
dlg.Update(cnt) |
211 |
|
|
cnt = cnt + 1 |
212 |
|
|
dlg.Destroy() |
213 |
frank |
2196 |
|
214 |
|
|
# finally |
215 |
|
|
if filename != '': |
216 |
frank |
2197 |
bboxfile = file(filename, 'w+') |
217 |
|
|
bboxfile.write(string.join(bboxmessage)) |
218 |
frank |
2196 |
bboxfile.close() |
219 |
|
|
return None |
220 |
|
|
else: |
221 |
frank |
2197 |
return string.join(bboxmessage) |
222 |
frank |
2196 |
|
223 |
|
|
def LayerBBoxDump(context): |
224 |
|
|
"""Menu Handler BBoxDump |
225 |
|
|
""" |
226 |
|
|
layer = context.mainwindow.canvas.SelectedLayer() |
227 |
|
|
if layer is not None: |
228 |
|
|
dlg = BBoxDumpDialog(context.mainwindow, _("Bounding Box Dump"), |
229 |
|
|
layer = layer) |
230 |
|
|
dlg.ShowModal() |
231 |
|
|
|
232 |
|
|
|
233 |
frank |
2197 |
# bboxdump executed as an extension to Thuban |
234 |
frank |
2196 |
|
235 |
|
|
# register the new command |
236 |
|
|
registry.Add(Command('bboxdump', _('BBox Dump'), LayerBBoxDump, |
237 |
|
|
helptext = _('Dump Bounding Boxes of Layer Objects'), |
238 |
|
|
sensitive = _has_selected_shape_layer)) |
239 |
|
|
|
240 |
|
|
# find the extensions menu (create it anew if not found) |
241 |
jan |
2203 |
extensions_menu = main_menu.FindOrInsertMenu('extensions', _('E&xtensions')) |
242 |
frank |
2196 |
|
243 |
|
|
# finally add the new entry to the extensions menu |
244 |
|
|
extensions_menu.InsertItem('bboxdump') |