1 |
# Copyright (C) 2003 by Intevation GmbH |
# Copyright (C) 2003-2004 by Intevation GmbH |
2 |
# Authors: |
# Authors: |
3 |
# Frank Koormann <[email protected]> |
# Frank Koormann <[email protected]> |
4 |
# |
# |
14 |
""" |
""" |
15 |
|
|
16 |
__version__ = '$Revision$' |
__version__ = '$Revision$' |
17 |
|
# $Source$ |
18 |
|
# $Id$ |
19 |
|
|
20 |
import os, sys |
import os, sys |
21 |
|
import string |
22 |
|
|
|
# only import GUI when not called as command line tool |
|
23 |
from wxPython.wx import * |
from wxPython.wx import * |
24 |
from wxPython.lib.dialogs import wxScrolledMessageDialog |
from wxPython.lib.dialogs import wxScrolledMessageDialog |
25 |
|
|
26 |
from Thuban.UI.common import ThubanBeginBusyCursor, ThubanEndBusyCursor |
from Thuban.UI.common import ThubanBeginBusyCursor, ThubanEndBusyCursor |
27 |
from Thuban.UI.command import registry, Command |
from Thuban.UI.command import registry, Command |
28 |
from Thuban.UI.mainwindow import main_menu, _has_selected_shape_layer |
from Thuban.UI.mainwindow import main_menu, _has_selected_shape_layer |
29 |
|
from Thuban.UI.extensionregistry import ExtensionDesc, ext_registry |
30 |
from Thuban import _ |
from Thuban import _ |
31 |
|
|
32 |
import shapelib |
import shapelib |
33 |
import dbflib |
import dbflib |
34 |
|
|
35 |
|
ext_registry.add(ExtensionDesc( |
36 |
|
name = 'bboxdump', |
37 |
|
version = '1.0.0', |
38 |
|
authors= [ 'Frank Koormann' ], |
39 |
|
copyright = '2003-2004 Intevation GmbH', |
40 |
|
desc = _("Dumps the bounding boxes of all\n" \ |
41 |
|
"shapes of the selected layer."))) |
42 |
|
|
43 |
|
# Widget IDs |
44 |
ID_FILENAME = 4001 |
ID_FILENAME = 4001 |
45 |
ID_ATTRIBUTES = 4002 |
ID_ATTRIBUTES = 4002 |
46 |
ID_SELFN = 4003 |
ID_SELFN = 4003 |
133 |
if bboxmessage: |
if bboxmessage: |
134 |
dlg = wxScrolledMessageDialog( |
dlg = wxScrolledMessageDialog( |
135 |
self.parent, bboxmessage, |
self.parent, bboxmessage, |
136 |
_("Bounding Box Dump %s") % self.layer.Title() |
_("Bounding Box Dump %s") % self.layer.Title()) |
|
) |
|
137 |
dlg.ShowModal() |
dlg.ShowModal() |
138 |
|
|
139 |
def OnSelectFilename(self, event): |
def OnSelectFilename(self, event): |
160 |
layer - Layer of shapes to be dumped |
layer - Layer of shapes to be dumped |
161 |
column - optional column to group shapes (else None) |
column - optional column to group shapes (else None) |
162 |
filename - optional filename to dump into (else empty string, i.e. dump |
filename - optional filename to dump into (else empty string, i.e. dump |
163 |
to stdio) |
to message dialog) |
164 |
""" |
""" |
165 |
# Preparation |
# Preparation |
166 |
shapelist = {} |
shapelist = {} |
167 |
bboxmessage = "" |
bboxmessage = [] |
168 |
|
|
169 |
|
dlg= wxProgressDialog(_("Bounding Box Dump"), |
170 |
|
_("Collecting shapes ..."), |
171 |
|
layer.ShapeStore().NumShapes(), |
172 |
|
None) |
173 |
|
|
174 |
|
cnt = 0 |
175 |
|
step = int(layer.ShapeStore().NumShapes() / 100.0) |
176 |
|
if step == 0: |
177 |
|
step = 1 |
178 |
|
|
179 |
# Collect shape ids to be dumped |
# Collect shape ids to be dumped |
180 |
if column is None: |
if column is None: |
181 |
# A simple dump of shapes bbox is required |
# A simple dump of shapes bbox is required |
182 |
for i in xrange(layer.NumShapes()): |
for s in layer.ShapeStore().AllShapes(): |
183 |
|
i = s.ShapeID() |
184 |
shapelist[i] = (i,) |
shapelist[i] = (i,) |
185 |
|
if cnt % step == 0: |
186 |
|
dlg.Update(cnt) |
187 |
|
cnt = cnt + 1 |
188 |
else: |
else: |
189 |
# group them by column ... |
# group them by column ... |
190 |
for i in xrange(layer.NumShapes()): |
for s in layer.ShapeStore().AllShapes(): |
191 |
|
i = s.ShapeID() |
192 |
row = layer.ShapeStore().Table().ReadRowAsDict(i) |
row = layer.ShapeStore().Table().ReadRowAsDict(i) |
193 |
att = row[column.name] |
att = row[column.name] |
194 |
if not shapelist.has_key(att): |
if not shapelist.has_key(att): |
195 |
shapelist[att] = [] |
shapelist[att] = [] |
196 |
shapelist[att].append(i) |
shapelist[att].append(i) |
197 |
|
if cnt % step == 0: |
198 |
|
dlg.Update(cnt) |
199 |
|
cnt = cnt + 1 |
200 |
|
|
201 |
|
dlg.Destroy() |
202 |
|
dlg= wxProgressDialog(_("Bounding Box Dump"), |
203 |
|
_("Dump bounding boxes of selected shapes ..."), |
204 |
|
len(shapelist), |
205 |
|
None) |
206 |
|
cnt = 0 |
207 |
|
step = int(len(shapelist) / 100.0) |
208 |
|
if step == 0: |
209 |
|
step = 1 |
210 |
|
|
211 |
# Dump them, sorted |
# Dump them, sorted |
212 |
keys = shapelist.keys() |
keys = shapelist.keys() |
213 |
keys.sort() |
keys.sort() |
214 |
for key in keys: |
for key in keys: |
215 |
bbox = layer.ShapesBoundingBox(shapelist[key]) |
bbox = layer.ShapesBoundingBox(shapelist[key]) |
216 |
bboxmessage = bboxmessage + "%.3f,%.3f,%.3f,%.3f,%s\n" % ( |
bboxmessage.append("%.3f,%.3f,%.3f,%.3f,%s\n" % ( |
217 |
bbox[0],bbox[1],bbox[2],bbox[3],key) |
bbox[0], bbox[1], bbox[2], bbox[3], key)) |
218 |
|
if cnt % step == 0: |
219 |
|
dlg.Update(cnt) |
220 |
|
cnt = cnt + 1 |
221 |
|
dlg.Destroy() |
222 |
|
|
223 |
# finally |
# finally |
224 |
if filename != '': |
if filename != '': |
225 |
bboxfile = file(filename,'w+') |
bboxfile = file(filename, 'w+') |
226 |
bboxfile.write(bboxmessage) |
bboxfile.write(string.join(bboxmessage)) |
227 |
bboxfile.close() |
bboxfile.close() |
228 |
return None |
return None |
229 |
else: |
else: |
230 |
return bboxmessage |
return string.join(bboxmessage) |
231 |
|
|
232 |
def LayerBBoxDump(context): |
def LayerBBoxDump(context): |
233 |
"""Menu Handler BBoxDump |
"""Menu Handler BBoxDump |
239 |
dlg.ShowModal() |
dlg.ShowModal() |
240 |
|
|
241 |
|
|
242 |
# gns2shp executed as an extension to Thuban |
# bboxdump executed as an extension to Thuban |
243 |
|
|
244 |
# register the new command |
# register the new command |
245 |
registry.Add(Command('bboxdump', _('BBox Dump'), LayerBBoxDump, |
registry.Add(Command('bboxdump', _('BBox Dump'), LayerBBoxDump, |
247 |
sensitive = _has_selected_shape_layer)) |
sensitive = _has_selected_shape_layer)) |
248 |
|
|
249 |
# find the extensions menu (create it anew if not found) |
# find the extensions menu (create it anew if not found) |
250 |
extensions_menu = main_menu.find_menu('extensions') |
extensions_menu = main_menu.FindOrInsertMenu('extensions', _('E&xtensions')) |
|
if extensions_menu is None: |
|
|
extensions_menu = main_menu.InsertMenu('extensions', _('E&xtensions')) |
|
251 |
|
|
252 |
# finally add the new entry to the extensions menu |
# finally add the new entry to the extensions menu |
253 |
extensions_menu.InsertItem('bboxdump') |
extensions_menu.InsertItem('bboxdump') |