Method: Shapefile#each_shape

Defined in:
lib/shapefile.rb

#each_shape(&block) ⇒ Object



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

def each_shape(&block)
  @shp.seek(HEADER_BYTES, IO::SEEK_SET)
  until @shp.eof?
    # Read the next shape record.
    nbr, len, shp_type = @shp.read(12).unpack("NNV")
    record = @shp.read(len * 2 - 4)
    next if shp_type == Shape::Null.code
    shape = @shape_class.new(nbr, record)
    # Load the shape's attributes. DBF's record method is ridiculously slow
    # (it rereads every single row each time you call it), so we cheat a bit
    # and use some of its private methods.
    if @dbf
      @dbf.send(:seek_to_record, nbr - 1)
      unless @dbf.send(:deleted_record?)
        shape.attributes = DBF::Record.new(@dbf).attributes
      end
    end
    # Run the shape through the block.
    block.call(shape)
  end
end