/[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 2197 by frank, Tue May 4 17:17:00 2004 UTC
# Line 16  in this case the bounding box is a union Line 16  in this case the bounding box is a union
16  __version__ = '$Revision$'  __version__ = '$Revision$'
17    
18  import os, sys  import os, sys
19    import string
20    
 # only import GUI when not called as command line tool  
21  from wxPython.wx import *  from wxPython.wx import *
22  from wxPython.lib.dialogs import wxScrolledMessageDialog  from wxPython.lib.dialogs import wxScrolledMessageDialog
23    
# Line 29  from Thuban import _ Line 29  from Thuban import _
29  import shapelib  import shapelib
30  import dbflib  import dbflib
31    
32    # Widget IDs
33  ID_FILENAME = 4001  ID_FILENAME = 4001
34  ID_ATTRIBUTES = 4002  ID_ATTRIBUTES = 4002
35  ID_SELFN = 4003  ID_SELFN = 4003
# Line 121  class BBoxDumpDialog(wxDialog): Line 122  class BBoxDumpDialog(wxDialog):
122          if bboxmessage:          if bboxmessage:
123              dlg = wxScrolledMessageDialog(              dlg = wxScrolledMessageDialog(
124                                  self.parent, bboxmessage,                                  self.parent, bboxmessage,
125                                  _("Bounding Box Dump %s") % self.layer.Title()                                  _("Bounding Box Dump %s") % self.layer.Title())
                                 )  
126              dlg.ShowModal()              dlg.ShowModal()
127    
128      def OnSelectFilename(self, event):      def OnSelectFilename(self, event):
# Line 149  def bboxdump(layer, column, filename): Line 149  def bboxdump(layer, column, filename):
149      layer    - Layer of shapes to be dumped      layer    - Layer of shapes to be dumped
150      column   - optional column to group shapes (else None)      column   - optional column to group shapes (else None)
151      filename - optional filename to dump into (else empty string, i.e. dump      filename - optional filename to dump into (else empty string, i.e. dump
152                 to stdio)             to message dialog)
153      """      """
154      # Preparation      # Preparation
155      shapelist = {}      shapelist = {}
156      bboxmessage = ""      bboxmessage = []
157    
158        dlg= wxProgressDialog(_("Bounding Box Dump"),
159                              _("Collecting shapes ..."),
160                              layer.ShapeStore().NumShapes(),
161                              None)
162    
163        cnt = 0
164        step =  int(layer.ShapeStore().NumShapes() / 100.0)
165        if step == 0:
166            step = 1
167    
168      # Collect shape ids to be dumped      # Collect shape ids to be dumped
169      if column is None:      if column is None:
170          # A simple dump of shapes bbox is required          # A simple dump of shapes bbox is required
171          for i in xrange(layer.NumShapes()):          for s in layer.ShapeStore().AllShapes():
172                i = s.ShapeID()
173              shapelist[i] = (i,)              shapelist[i] = (i,)
174                if cnt % step == 0:
175                    dlg.Update(cnt)
176                cnt = cnt + 1
177      else:      else:
178          # group them by column ...          # group them by column ...
179          for i in xrange(layer.NumShapes()):          for s in layer.ShapeStore().AllShapes():
180                i = s.ShapeID()
181              row = layer.ShapeStore().Table().ReadRowAsDict(i)              row = layer.ShapeStore().Table().ReadRowAsDict(i)
182              att = row[column.name]              att = row[column.name]
183              if not shapelist.has_key(att):              if not shapelist.has_key(att):
184                  shapelist[att] = []                  shapelist[att] = []
185              shapelist[att].append(i)              shapelist[att].append(i)
186                if cnt % step == 0:
187                    dlg.Update(cnt)
188                cnt = cnt + 1
189    
190        dlg.Destroy()
191        dlg= wxProgressDialog(_("Bounding Box Dump"),
192                              _("Dump bounding boxes of selected shapes ..."),
193                              len(shapelist),
194                              None)
195        cnt = 0
196        step = int(len(shapelist) / 100.0)
197        if step == 0:
198            step = 1
199    
200      # Dump them, sorted      # Dump them, sorted
201      keys = shapelist.keys()      keys = shapelist.keys()
202      keys.sort()      keys.sort()
203      for key in keys:      for key in keys:
204          bbox = layer.ShapesBoundingBox(shapelist[key])          bbox = layer.ShapesBoundingBox(shapelist[key])
205          bboxmessage = bboxmessage + "%.3f,%.3f,%.3f,%.3f,%s\n" % (          bboxmessage.append("%.3f,%.3f,%.3f,%.3f,%s\n" % (
206                      bbox[0],bbox[1],bbox[2],bbox[3],key)                              bbox[0], bbox[1], bbox[2], bbox[3], key))
207            if cnt % step == 0:
208                dlg.Update(cnt)
209            cnt = cnt + 1
210        dlg.Destroy()
211    
212      # finally      # finally
213      if filename != '':      if filename != '':
214          bboxfile = file(filename,'w+')          bboxfile = file(filename, 'w+')
215          bboxfile.write(bboxmessage)          bboxfile.write(string.join(bboxmessage))
216          bboxfile.close()          bboxfile.close()
217          return None          return None
218      else:      else:
219          return bboxmessage          return string.join(bboxmessage)
220    
221  def LayerBBoxDump(context):  def LayerBBoxDump(context):
222      """Menu Handler BBoxDump      """Menu Handler BBoxDump
# Line 196  def LayerBBoxDump(context): Line 228  def LayerBBoxDump(context):
228          dlg.ShowModal()          dlg.ShowModal()
229    
230    
231  # gns2shp executed as an extension to Thuban  # bboxdump executed as an extension to Thuban
232    
233  # register the new command  # register the new command
234  registry.Add(Command('bboxdump', _('BBox Dump'), LayerBBoxDump,  registry.Add(Command('bboxdump', _('BBox Dump'), LayerBBoxDump,

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26