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.


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

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

Instance Attribute Details

#rollbackedObject (readonly)

Returns the value of attribute rollbacked


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

def rollbacked
  @rollbacked
end

Instance Method Details

#add(record) ⇒ Object

add a ShpRecord at the end


389
390
391
392
393
# File 'lib/geo_ruby/shp4r/shp.rb', line 389

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


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

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


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

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


422
423
424
425
426
# File 'lib/geo_ruby/shp4r/shp.rb', line 422

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

#update(i, record) ⇒ Object

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


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

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