Class: NSWTopo::ESRIHdr

Inherits:
Object
  • Object
show all
Defined in:
lib/nswtopo/gis/esri_hdr.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path_or_object, *args) ⇒ ESRIHdr

Returns a new instance of ESRIHdr.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/nswtopo/gis/esri_hdr.rb', line 3

def initialize(path_or_object, *args)
  @header = case path_or_object
  when Pathname then path_or_object.sub_ext(".hdr").each_line.map(&:upcase).map(&:split).to_h
  when ESRIHdr then path_or_object.header.dup
  end

  @format = case @header.values_at "PIXELTYPE", "NBITS", "BYTEORDER"
  when %w[SIGNEDINT 8 I]    then "c*"
  when %w[SIGNEDINT 8 M]    then "c*"
  when %w[SIGNEDINT 16 I]   then "s<*"
  when %w[SIGNEDINT 16 M]   then "s>*"
  when %w[SIGNEDINT 32 I]   then "l<*"
  when %w[SIGNEDINT 32 M]   then "l>*"
  when %w[UNSIGNEDINT 8 I]  then "C*"
  when %w[UNSIGNEDINT 8 M]  then "C*"
  when %w[UNSIGNEDINT 16 I] then "S<*"
  when %w[UNSIGNEDINT 16 M] then "S>*"
  when %w[UNSIGNEDINT 32 I] then "L<*"
  when %w[UNSIGNEDINT 32 M] then "L>*"
  when %w[FLOAT 32 I]       then "e*"
  when %w[FLOAT 32 M]       then "g*"
  end

  @nodata = case path_or_object
  when Pathname
    case @header.values_at "PIXELTYPE", "NBITS"
    when %w[SIGNEDINT 8]    then args.take(1).pack("c").unpack("c").first
    when %w[UNSIGNEDINT 8]  then args.take(1).pack("C").unpack("C").first
    when %w[SIGNEDINT 16]   then args.take(1).pack("s").unpack("s").first
    when %w[UNSIGNEDINT 16] then args.take(1).pack("S").unpack("S").first
    when %w[SIGNEDINT 32]   then args.take(1).pack("l").unpack("l").first
    when %w[UNSIGNEDINT 32] then args.take(1).pack("L").unpack("L").first
    when %w[FLOAT 32]       then args.first
    else abort @header.inspect
    end if args.any?
  when ESRIHdr then path_or_object.nodata
  end

  @values = case path_or_object
  when Pathname
    data = []
    path_or_object.sub_ext(".bil").open("rb") do |file|
      while !file.eof?
        data += file.read(32*1024*1024).unpack(@format).map do |value|
          value == @nodata ? nil : value
        end
      end
    end
    data
  when ESRIHdr then args[0]
  end
end

Instance Attribute Details

#headerObject (readonly)

Returns the value of attribute header.



69
70
71
# File 'lib/nswtopo/gis/esri_hdr.rb', line 69

def header
  @header
end

#nodataObject (readonly)

Returns the value of attribute nodata.



69
70
71
# File 'lib/nswtopo/gis/esri_hdr.rb', line 69

def nodata
  @nodata
end

#valuesObject (readonly)

Returns the value of attribute values.



69
70
71
# File 'lib/nswtopo/gis/esri_hdr.rb', line 69

def values
  @values
end

Instance Method Details

#ncolsObject



75
76
77
# File 'lib/nswtopo/gis/esri_hdr.rb', line 75

def ncols
  @ncols ||= @header["NCOLS"].to_i
end

#nrowsObject



71
72
73
# File 'lib/nswtopo/gis/esri_hdr.rb', line 71

def nrows
  @nrows ||= @header["NROWS"].to_i
end

#rowsObject



79
80
81
# File 'lib/nswtopo/gis/esri_hdr.rb', line 79

def rows
  @values.each_slice ncols
end

#write(path) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/nswtopo/gis/esri_hdr.rb', line 56

def write(path)
  @header.map do |pair|
    "%-#{@header.keys.map(&:length).max}s  %s\n" % pair
  end.join('').tap do |text|
    path.sub_ext(".hdr").write text
  end
  @values.map do |value|
    value || @nodata
  end.pack(@format).tap do |data|
    path.sub_ext(".bil").binwrite data
  end
end