Class GeoRuby::Shp4r::ShpTransaction
In: lib/geo_ruby/shp4r/shp.rb
Parent: Object

An object returned from ShpFile#transaction. Buffers updates to a Shapefile

Methods

add   commit   delete   new   rollback   update  

Attributes

rollbacked  [R] 

Public Class methods

[Source]

     # File lib/geo_ruby/shp4r/shp.rb, line 343
343:       def initialize(shp, dbf)
344:         @deleted = Hash.new
345:         @added = Array.new
346:         @shp = shp
347:         @dbf = dbf
348:       end

Public Instance methods

add a ShpRecord at the end

[Source]

     # File lib/geo_ruby/shp4r/shp.rb, line 363
363:       def add(record)
364:         record_type = to_shp_type(record.geometry)
365:         raise IncompatibleGeometryException.new("Incompatible type") unless record_type==@shp.shp_type
366:         @added << record
367:       end

updates the physical files

[Source]

     # File lib/geo_ruby/shp4r/shp.rb, line 370
370:       def commit
371:         @shp.close
372:         @shp_r = open(@shp.file_root + ".shp", "rb")
373:         @dbf_r = open(@shp.file_root + ".dbf", "rb")
374:         @shp_io = open(@shp.file_root + ".shp.tmp.shp", "wb")
375:         @shx_io = open(@shp.file_root + ".shx.tmp.shx", "wb")
376:         @dbf_io = open(@shp.file_root + ".dbf.tmp.dbf", "wb")
377:         index = commit_delete
378:         min_x,max_x,min_y,max_y,min_z,max_z,min_m,max_m = commit_add(index)
379:         commit_finalize(min_x,max_x,min_y,max_y,min_z,max_z,min_m,max_m)
380:         @shp_r.close
381:         @dbf_r.close
382:         @dbf_io.close
383:         @shp_io.close
384:         @shx_io.close
385:         FileUtils.move(@shp.file_root + ".shp.tmp.shp", @shp.file_root + ".shp")
386:         FileUtils.move(@shp.file_root + ".shx.tmp.shx", @shp.file_root + ".shx")
387:         FileUtils.move(@shp.file_root + ".dbf.tmp.dbf", @shp.file_root + ".dbf")
388:         
389:         @deleted = Hash.new
390:         @added = Array.new
391:         
392:         @shp.reload!
393:        end

delete a record. Does not take into account the records added in the current transaction

[Source]

     # File lib/geo_ruby/shp4r/shp.rb, line 351
351:       def delete(i)
352:         raise UnexistantRecordException.new("Invalid index : #{i}") if @shp.record_count <= i 
353:         @deleted[i] = true
354:       end

prevents the udpate from taking place

[Source]

     # File lib/geo_ruby/shp4r/shp.rb, line 396
396:       def rollback
397:         @deleted = Hash.new
398:         @added = Array.new
399:         @rollbacked = true
400:       end

Update a record. In effect just a delete followed by an add.

[Source]

     # File lib/geo_ruby/shp4r/shp.rb, line 357
357:       def update(i, record)
358:         delete(i)
359:         add(record)
360:       end

[Validate]