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.



367
368
369
370
371
372
# File 'lib/geo_ruby/shp4r/shp.rb', line 367

def initialize(shp, dbf)
  @deleted = {}
  @added = []
  @shp = shp
  @dbf = dbf
end

Instance Attribute Details

#rollbackedObject (readonly)

Returns the value of attribute rollbacked.



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

def rollbacked
  @rollbacked
end

Instance Method Details

#add(record) ⇒ Object

add a ShpRecord at the end



387
388
389
390
391
# File 'lib/geo_ruby/shp4r/shp.rb', line 387

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

#commitObject

updates the physical files



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

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 = {}
 @added = []

 @shp.reload!
end

#delete(i) ⇒ Object

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



375
376
377
378
# File 'lib/geo_ruby/shp4r/shp.rb', line 375

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

#rollbackObject

prevents the udpate from taking place



420
421
422
423
424
# File 'lib/geo_ruby/shp4r/shp.rb', line 420

def rollback
  @deleted = {}
  @added = []
  @rollbacked = true
end

#update(i, record) ⇒ Object

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



381
382
383
384
# File 'lib/geo_ruby/shp4r/shp.rb', line 381

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