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

An interface to an ESRI shapefile (actually 3 files : shp, shx and dbf). Currently supports only the reading of geometries.

Methods

[]   close   create   each   each_record   empty?   fields   new   open   records   reload!   transaction  

Included Modules

Enumerable

Attributes

file_length  [R] 
file_root  [R] 
mmax  [R] 
mmin  [R] 
record_count  [R] 
shp_type  [R] 
xmax  [R] 
xmin  [R] 
ymax  [R] 
ymin  [R] 
zmax  [R] 
zmin  [R] 

Public Class methods

create a new Shapefile of the specified shp type (see ShpType) and with the attribute specified in the fields array (see Dbf::Field). If a block is given, the ShpFile object newly created is passed to it.

[Source]

    # File lib/geo_ruby/shp4r/shp.rb, line 64
64:       def self.create(file,shp_type,fields,&proc)
65:         file_root = file.gsub(/.shp$/i,"")
66:         shx_io = File.open(file_root + ".shx","wb")
67:         shp_io = File.open(file_root + ".shp","wb")
68:         dbf_io = File.open(file_root + ".dbf","wb")
69:         str = [9994,0,0,0,0,0,50,1000,shp_type,0,0,0,0,0,0,0,0].pack("N7V2E8")
70:         shp_io << str
71:         shx_io << str
72:         rec_length = 1 + fields.inject(0) {|s,f| s + f.length} #+1 for the prefixed space (active record marker)
73:         dbf_io << [3,107,7,7,0,33 + 32 * fields.length,rec_length ].pack("c4Vv2x20") #32 bytes for first part of header
74:         fields.each do |field|
75:           dbf_io << [field.name,field.type,field.length,field.decimal].pack("a10xax4CCx14")
76:         end
77:         dbf_io << ['0d'].pack("H2")
78:         
79:         shx_io.close
80:         shp_io.close
81:         dbf_io.close
82: 
83:         open(file,&proc)
84: 
85:       end

Opens a SHP file. Both "abc.shp" and "abc" are accepted. The files "abc.shp", "abc.shx" and "abc.dbf" must be present

[Source]

    # File lib/geo_ruby/shp4r/shp.rb, line 33
33:       def initialize(file)
34:         #strip the shp out of the file if present
35:         @file_root = file.gsub(/.shp$/i,"")
36:         #check existence of shp, dbf and shx files       
37:         unless File.exists?(@file_root + ".shp") and File.exists?(@file_root + ".dbf") and File.exists?(@file_root + ".shx")
38:           raise MalformedShpException.new("Missing one of shp, dbf or shx for: #{@file}")
39:         end
40: 
41:         @dbf = Dbf::Reader.open(@file_root + ".dbf")
42:         @shx = File.open(@file_root + ".shx","rb")
43:         @shp = File.open(@file_root + ".shp","rb")
44:         read_index
45:       end

opens a SHP "file". If a block is given, the ShpFile object is yielded to it and is closed upon return. Else a call to open is equivalent to ShpFile.new(…).

[Source]

    # File lib/geo_ruby/shp4r/shp.rb, line 53
53:       def self.open(file)
54:         shpfile = ShpFile.new(file)
55:         if block_given?
56:           yield shpfile
57:           shpfile.close
58:         else
59:           shpfile
60:         end
61:       end

Public Instance methods

Returns record i

[Source]

     # File lib/geo_ruby/shp4r/shp.rb, line 128
128:       def [](i)
129:         get_record(i)
130:       end

Closes a shapefile

[Source]

    # File lib/geo_ruby/shp4r/shp.rb, line 88
88:       def close
89:         @dbf.close
90:         @shx.close
91:         @shp.close
92:       end

Goes through each record

[Source]

     # File lib/geo_ruby/shp4r/shp.rb, line 120
120:       def each
121:         (0...record_count).each do |i|
122:           yield get_record(i)
123:         end
124:       end
each_record()

Alias for each

Tests if the file has no record

[Source]

     # File lib/geo_ruby/shp4r/shp.rb, line 115
115:       def empty?
116:         record_count == 0
117:       end

return the description of data fields

[Source]

     # File lib/geo_ruby/shp4r/shp.rb, line 110
110:       def fields
111:         @dbf.fields
112:       end

Returns all the records

[Source]

     # File lib/geo_ruby/shp4r/shp.rb, line 133
133:       def records
134:         Array.new(record_count) do |i|
135:           get_record(i)
136:         end
137:       end

force the reopening of the files compsing the shp. Close before calling this.

[Source]

    # File lib/geo_ruby/shp4r/shp.rb, line 48
48:       def reload!
49:         initialize(@file_root)
50:       end

starts a transaction, to buffer physical file operations on the shapefile components.

[Source]

     # File lib/geo_ruby/shp4r/shp.rb, line 95
 95:       def transaction
 96:         trs = ShpTransaction.new(self,@dbf)
 97:         if block_given?
 98:           answer = yield trs
 99:           if answer == :rollback
100:             trs.rollback
101:           elsif !trs.rollbacked
102:             trs.commit
103:           end
104:         else
105:           trs
106:         end
107:       end

[Validate]