Class: GeoRuby::Shp4r::ShpTransaction

Inherits:
Object
  • Object
show all
Defined in:
lib/geo_ruby/shp4r/shp_transaction.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.



5
6
7
8
9
10
# File 'lib/geo_ruby/shp4r/shp_transaction.rb', line 5

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.



3
4
5
# File 'lib/geo_ruby/shp4r/shp_transaction.rb', line 3

def rollbacked
  @rollbacked
end

Instance Method Details

#add(record) ⇒ Object

add a ShpRecord at the end



25
26
27
28
29
# File 'lib/geo_ruby/shp4r/shp_transaction.rb', line 25

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



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/geo_ruby/shp4r/shp_transaction.rb', line 32

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



13
14
15
16
# File 'lib/geo_ruby/shp4r/shp_transaction.rb', line 13

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



58
59
60
61
62
# File 'lib/geo_ruby/shp4r/shp_transaction.rb', line 58

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.



19
20
21
22
# File 'lib/geo_ruby/shp4r/shp_transaction.rb', line 19

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