Parent

Included Modules

Files

GeoRuby::Shp4r::ShpFile

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

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(file,shp_type,fields,&proc) click to toggle source

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.

# File lib/geo_ruby/shp4r/shp.rb, line 64
def self.create(file,shp_type,fields,&proc)
  file_root = file.gsub(/.shp$/,"")
  shx_io = File.open(file_root + ".shx","wb")
  shp_io = File.open(file_root + ".shp","wb")
  dbf_io = File.open(file_root + ".dbf","wb")
  str = [9994,0,0,0,0,0,50,1000,shp_type,0,0,0,0,0,0,0,0].pack("N7V2E8")
  shp_io << str
  shx_io << str
  rec_length = 1 + fields.inject(0) {|s,f| s + f.length} #+1 for the prefixed space (active record marker)

  dbf_io << [3,107,7,7,0,33 + 32 * fields.length,rec_length ].pack("c4Vv2x20") #32 bytes for first part of header

  fields.each do |field|
    dbf_io << [field.name,field.type,field.length,field.decimal].pack("a10xax4CCx14")
  end
  dbf_io << ['0d'].pack("H2")
  
  shx_io.close
  shp_io.close
  dbf_io.close

  open(file,&proc)

end
new(file) click to toggle source

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

# File lib/geo_ruby/shp4r/shp.rb, line 33
def initialize(file)
  #strip the shp out of the file if present

  @file_root = file.gsub(/.shp$/,"")
  #check existence of shp, dbf and shx files       

  unless File.exists?(@file_root + ".shp") and File.exists?(@file_root + ".dbf") and File.exists?(@file_root + ".shx")
    raise MalformedShpException.new("Missing one of shp, dbf or shx for: #{@file}")
  end

  @dbf = Dbf::Reader.open(@file_root + ".dbf")
  @shx = File.open(@file_root + ".shx","rb")
  @shp = File.open(@file_root + ".shp","rb")
  read_index
end
open(file) click to toggle source

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(...).

# File lib/geo_ruby/shp4r/shp.rb, line 53
def self.open(file)
  shpfile = ShpFile.new(file)
  if block_given?
    yield shpfile
    shpfile.close
  else
    shpfile
  end
end

Public Instance Methods

[](i) click to toggle source

Returns record i

# File lib/geo_ruby/shp4r/shp.rb, line 128
def [](i)
  get_record(i)
end
close() click to toggle source

Closes a shapefile

# File lib/geo_ruby/shp4r/shp.rb, line 88
def close
  @dbf.close
  @shx.close
  @shp.close
end
each() click to toggle source

Goes through each record

# File lib/geo_ruby/shp4r/shp.rb, line 120
def each
  (0...record_count).each do |i|
    yield get_record(i)
  end
end
Also aliased as: each_record
each_record() click to toggle source
Alias for: each
empty?() click to toggle source

Tests if the file has no record

# File lib/geo_ruby/shp4r/shp.rb, line 115
def empty?
  record_count == 0
end
fields() click to toggle source

return the description of data fields

# File lib/geo_ruby/shp4r/shp.rb, line 110
def fields
  @dbf.fields
end
records() click to toggle source

Returns all the records

# File lib/geo_ruby/shp4r/shp.rb, line 133
def records
  Array.new(record_count) do |i|
    get_record(i)
  end
end
reload!() click to toggle source

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

# File lib/geo_ruby/shp4r/shp.rb, line 48
def reload!
  initialize(@file_root)
end
transaction() click to toggle source

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

# File lib/geo_ruby/shp4r/shp.rb, line 95
def transaction
  trs = ShpTransaction.new(self,@dbf)
  if block_given?
    answer = yield trs
    if answer == :rollback
      trs.rollback
    elsif !trs.rollbacked
      trs.commit
    end
  else
    trs
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.