class EsriShapefile::Reader

Constants

MAIN_FILE_HEADER_BYTESIZE
RECORD_HEADER_BYTESIZE

Attributes

main_file_header[R]

Public Class Methods

new(path_to_shapefile) click to toggle source
# File lib/esri_shapefile/reader.rb, line 12
def initialize(path_to_shapefile)
  @path_to_shapefile = Pathname.new(path_to_shapefile)
  @path_to_dbf = @path_to_shapefile.sub_ext(".dbf")

  @main_file_header = parse_main_file_header
end

Public Instance Methods

each_record() { |record_header, shape| ... } click to toggle source
# File lib/esri_shapefile/reader.rb, line 19
def each_record
  dbf = @path_to_dbf.file? ? DBF::Table.new(@path_to_dbf.to_s) : nil

  File.open(@path_to_shapefile) do |file|
    file.pos = MAIN_FILE_HEADER_BYTESIZE

    (0..Float::INFINITY).lazy.reduce(file.pos) do |current_offset, _|
      break if current_offset >= main_file_header.file_length_bytes

      record_header_bytes = file.read(RECORD_HEADER_BYTESIZE)
      record_header = RecordHeader.from_bytes(record_header_bytes)

      shape_bytes = file.read(record_header.content_length_bytes)
      shape_class = Shapes.find_by_bytes(shape_bytes)

      shape = shape_class.from_bytes(shape_bytes)
      shape.metadata = get_record_metadata(dbf, record_header.record_number)

      yield record_header, shape

      file.pos
    end
  end
end

Private Instance Methods

get_record_metadata(dbf, record_number) click to toggle source

Retrieve the metadata for the shape from the associated .dbf file. Record numbers are one based, while the dbf file reader is 0 based. So we subtract one from the record number when performing the lookup.

# File lib/esri_shapefile/reader.rb, line 49
def get_record_metadata(dbf, record_number)
  dbf.nil? ? {} : dbf.find(record_number - 1).attributes
end
parse_main_file_header() click to toggle source
# File lib/esri_shapefile/reader.rb, line 53
def parse_main_file_header
  main_file_header_bytes = File.binread(@path_to_shapefile, 100, 0)
  MainFileHeader.from_bytes(main_file_header_bytes)
end