Class: GeoRuby::Shp4r::ShpTransaction

Inherits:
Object
  • Object
show all
Defined in:
lib/geo_ruby/shp4r/shp.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(shp, dbf) ⇒ ShpTransaction

Returns a new instance of ShpTransaction.



364
365
366
367
368
369
# File 'lib/geo_ruby/shp4r/shp.rb', line 364

def initialize(shp, dbf)
  @deleted = Hash.new
  @added = Array.new
  @shp = shp
  @dbf = dbf
end

Instance Attribute Details

#rollbackedObject (readonly)

Returns the value of attribute rollbacked.



362
363
364
# File 'lib/geo_ruby/shp4r/shp.rb', line 362

def rollbacked
  @rollbacked
end

Instance Method Details

#add(record) ⇒ Object

add a ShpRecord at the end



384
385
386
387
388
# File 'lib/geo_ruby/shp4r/shp.rb', line 384

def add(record)
  record_type = to_shp_type(record.geometry)
  raise IncompatibleGeometryException.new("Incompatible type") unless record_type==@shp.shp_type
  @added << record
end

#commitObject

updates the physical files



391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
# File 'lib/geo_ruby/shp4r/shp.rb', line 391

def commit
 @shp.close
 @shp_r = open(@shp.file_root + ".shp", "rb")
 @dbf_r = open(@shp.file_root + ".dbf", "rb")
 @shp_io = open(@shp.file_root + ".shp.tmp.shp", "wb")
 @shx_io = open(@shp.file_root + ".shx.tmp.shx", "wb")
 @dbf_io = open(@shp.file_root + ".dbf.tmp.dbf", "wb")
 index = commit_delete
 min_x,max_x,min_y,max_y,min_z,max_z,min_m,max_m = commit_add(index)
 commit_finalize(min_x,max_x,min_y,max_y,min_z,max_z,min_m,max_m)
 @shp_r.close
 @dbf_r.close
 @dbf_io.close
 @shp_io.close
 @shx_io.close
 FileUtils.move(@shp.file_root + ".shp.tmp.shp", @shp.file_root + ".shp")
 FileUtils.move(@shp.file_root + ".shx.tmp.shx", @shp.file_root + ".shx")
 FileUtils.move(@shp.file_root + ".dbf.tmp.dbf", @shp.file_root + ".dbf")

 @deleted = Hash.new
 @added = Array.new

 @shp.reload!
end

#delete(i) ⇒ Object

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



372
373
374
375
# File 'lib/geo_ruby/shp4r/shp.rb', line 372

def delete(i)
  raise UnexistantRecordException.new("Invalid index : #{i}") if @shp.record_count <= i
  @deleted[i] = true
end

#rollbackObject

prevents the udpate from taking place



417
418
419
420
421
# File 'lib/geo_ruby/shp4r/shp.rb', line 417

def rollback
  @deleted = Hash.new
  @added = Array.new
  @rollbacked = true
end

#update(i, record) ⇒ Object

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



378
379
380
381
# File 'lib/geo_ruby/shp4r/shp.rb', line 378

def update(i, record)
  delete(i)
  add(record)
end