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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2203 - (hide annotations)
Tue May 11 22:34:49 2004 UTC (20 years, 9 months ago) by jan
Original Path: trunk/thuban/Extensions/bboxdump/bboxdump.py
File MIME type: text/x-python
File size: 7838 byte(s)
Use FindOrInsertMenu() instead of finding menu on its own.

1 frank 2196 # Copyright (C) 2003 by Intevation GmbH
2     # Authors:
3     # Frank Koormann <[email protected]>
4     #
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     from wxPython.wx import *
24     from wxPython.lib.dialogs import wxScrolledMessageDialog
25    
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     class BBoxDumpDialog(wxDialog):
40     """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     wxDialog.__init__(self, parent, -1, title,
48     style = wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER)
49    
50     if layer is None:
51     return wxID_CANCEL
52    
53     # Store the layer
54     self.layer = layer
55    
56     # Filename selection elements
57     self.filename = wxTextCtrl(self, ID_FILENAME, "")
58     self.button_selectfile = wxButton(self, ID_SELFN, _('Select...'))
59     EVT_BUTTON(self, ID_SELFN, self.OnSelectFilename)
60    
61     # Column choice elements
62     self.choice_column = wxChoice(self, ID_ATTRIBUTES)
63     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     self.button_dump = wxButton(self, wxID_OK, _("OK"))
70     EVT_BUTTON(self, wxID_OK, self.OnDump)
71     self.button_dump.SetDefault()
72     # TODO: Disable the OK button until a filename is entered ...
73     # self.button_dump.Enable(False)
74     self.button_cancel = wxButton(self, wxID_CANCEL, _("Cancel"))
75    
76    
77     # Dialog Layout: three horizontal box sizers.
78     topbox = wxBoxSizer(wxVERTICAL)
79    
80     hbox = wxBoxSizer(wxHORIZONTAL)
81     topbox.Add(hbox, 0, wxALL|wxEXPAND)
82     hbox.Add(wxStaticText(self, -1, _("File:")),
83     0, wxALL|wxALIGN_CENTER_VERTICAL, 4)
84     hbox.Add(self.filename, 1, wxALL|wxEXPAND, 4)
85     hbox.Add(self.button_selectfile, 0, wxALL, 4)
86    
87     hbox = wxBoxSizer(wxHORIZONTAL)
88     topbox.Add(hbox, 0, wxALL|wxEXPAND)
89     hbox.Add(wxStaticText(self, -1, _("Group by:")),
90     0, wxALL|wxALIGN_CENTER_VERTICAL, 4)
91     hbox.Add(self.choice_column, 1, wxALL|wxEXPAND, 4)
92    
93     hbox = wxBoxSizer(wxHORIZONTAL)
94     topbox.Add(hbox, 0, wxALL|wxEXPAND)
95     hbox.Add(self.button_dump, 0, wxALL|wxALIGN_CENTER,
96     10)
97     hbox.Add(self.button_cancel, 0, wxALL|wxALIGN_CENTER,
98     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     dlg = wxScrolledMessageDialog(
126     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     dlg = wxFileDialog(self, _("Dump Bounding Boxes To"),
136     os.path.dirname(self.filename.GetValue()),
137     os.path.basename(self.filename.GetValue()),
138     _("CSV Files (*.csv)|*.csv|") +
139     _("All Files (*.*)|*.*"),
140     wxSAVE|wxOVERWRITE_PROMPT)
141     if dlg.ShowModal() == wxID_OK:
142     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 frank 2197 dlg= wxProgressDialog(_("Bounding Box Dump"),
161     _("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     dlg= wxProgressDialog(_("Bounding Box Dump"),
194     _("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')

Properties

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26