Class: EsriShapefile::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/esri_shapefile/reader.rb

Constant Summary collapse

MAIN_FILE_HEADER_BYTESIZE =
100
RECORD_HEADER_BYTESIZE =
8

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path_to_shapefile) ⇒ Reader

Returns a new instance of Reader.



12
13
14
15
16
17
# 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

Instance Attribute Details

#main_file_headerObject (readonly)

Returns the value of attribute main_file_header.



10
11
12
# File 'lib/esri_shapefile/reader.rb', line 10

def main_file_header
  @main_file_header
end

Instance Method Details

#each_recordObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# 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. = (dbf, record_header.record_number)

      yield record_header, shape

      file.pos
    end
  end
end