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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2196 by frank, Thu Apr 22 17:11:54 2004 UTC revision 2203 by jan, Tue May 11 22:34:49 2004 UTC
# Line 14  in this case the bounding box is a union Line 14  in this case the bounding box is a union
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    
# Line 29  from Thuban import _ Line 31  from Thuban import _
31  import shapelib  import shapelib
32  import dbflib  import dbflib
33    
34    # Widget IDs
35  ID_FILENAME = 4001  ID_FILENAME = 4001
36  ID_ATTRIBUTES = 4002  ID_ATTRIBUTES = 4002
37  ID_SELFN = 4003  ID_SELFN = 4003
# Line 121  class BBoxDumpDialog(wxDialog): Line 124  class BBoxDumpDialog(wxDialog):
124          if bboxmessage:          if bboxmessage:
125              dlg = wxScrolledMessageDialog(              dlg = wxScrolledMessageDialog(
126                                  self.parent, bboxmessage,                                  self.parent, bboxmessage,
127                                  _("Bounding Box Dump %s") % self.layer.Title()                                  _("Bounding Box Dump %s") % self.layer.Title())
                                 )  
128              dlg.ShowModal()              dlg.ShowModal()
129    
130      def OnSelectFilename(self, event):      def OnSelectFilename(self, event):
# Line 149  def bboxdump(layer, column, filename): Line 151  def bboxdump(layer, column, filename):
151      layer    - Layer of shapes to be dumped      layer    - Layer of shapes to be dumped
152      column   - optional column to group shapes (else None)      column   - optional column to group shapes (else None)
153      filename - optional filename to dump into (else empty string, i.e. dump      filename - optional filename to dump into (else empty string, i.e. dump
154                 to stdio)             to message dialog)
155      """      """
156      # Preparation      # Preparation
157      shapelist = {}      shapelist = {}
158      bboxmessage = ""      bboxmessage = []
159    
160        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      # Collect shape ids to be dumped      # Collect shape ids to be dumped
171      if column is None:      if column is None:
172          # A simple dump of shapes bbox is required          # A simple dump of shapes bbox is required
173          for i in xrange(layer.NumShapes()):          for s in layer.ShapeStore().AllShapes():
174                i = s.ShapeID()
175              shapelist[i] = (i,)              shapelist[i] = (i,)
176                if cnt % step == 0:
177                    dlg.Update(cnt)
178                cnt = cnt + 1
179      else:      else:
180          # group them by column ...          # group them by column ...
181          for i in xrange(layer.NumShapes()):          for s in layer.ShapeStore().AllShapes():
182                i = s.ShapeID()
183              row = layer.ShapeStore().Table().ReadRowAsDict(i)              row = layer.ShapeStore().Table().ReadRowAsDict(i)
184              att = row[column.name]              att = row[column.name]
185              if not shapelist.has_key(att):              if not shapelist.has_key(att):
186                  shapelist[att] = []                  shapelist[att] = []
187              shapelist[att].append(i)              shapelist[att].append(i)
188                if cnt % step == 0:
189                    dlg.Update(cnt)
190                cnt = cnt + 1
191    
192        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      # Dump them, sorted      # Dump them, sorted
203      keys = shapelist.keys()      keys = shapelist.keys()
204      keys.sort()      keys.sort()
205      for key in keys:      for key in keys:
206          bbox = layer.ShapesBoundingBox(shapelist[key])          bbox = layer.ShapesBoundingBox(shapelist[key])
207          bboxmessage = bboxmessage + "%.3f,%.3f,%.3f,%.3f,%s\n" % (          bboxmessage.append("%.3f,%.3f,%.3f,%.3f,%s\n" % (
208                      bbox[0],bbox[1],bbox[2],bbox[3],key)                              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    
214      # finally      # finally
215      if filename != '':      if filename != '':
216          bboxfile = file(filename,'w+')          bboxfile = file(filename, 'w+')
217          bboxfile.write(bboxmessage)          bboxfile.write(string.join(bboxmessage))
218          bboxfile.close()          bboxfile.close()
219          return None          return None
220      else:      else:
221          return bboxmessage          return string.join(bboxmessage)
222    
223  def LayerBBoxDump(context):  def LayerBBoxDump(context):
224      """Menu Handler BBoxDump      """Menu Handler BBoxDump
# Line 196  def LayerBBoxDump(context): Line 230  def LayerBBoxDump(context):
230          dlg.ShowModal()          dlg.ShowModal()
231    
232    
233  # gns2shp executed as an extension to Thuban  # bboxdump executed as an extension to Thuban
234    
235  # register the new command  # register the new command
236  registry.Add(Command('bboxdump', _('BBox Dump'), LayerBBoxDump,  registry.Add(Command('bboxdump', _('BBox Dump'), LayerBBoxDump,
# Line 204  registry.Add(Command('bboxdump', _('BBox Line 238  registry.Add(Command('bboxdump', _('BBox
238                       sensitive = _has_selected_shape_layer))                       sensitive = _has_selected_shape_layer))
239    
240  # find the extensions menu (create it anew if not found)  # find the extensions menu (create it anew if not found)
241  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'))  
242    
243  # finally add the new entry to the extensions menu  # finally add the new entry to the extensions menu
244  extensions_menu.InsertItem('bboxdump')  extensions_menu.InsertItem('bboxdump')

Legend:
Removed from v.2196  
changed lines
  Added in v.2203

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26