Class: OGR::Field

Inherits:
Object
  • Object
show all
Defined in:
lib/ogr/field.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ogr_field_struct = nil) ⇒ Field

Returns a new instance of Field.

Parameters:

  • ogr_field_struct (FFI::OGR::Point, FFI::Pointer) (defaults to: nil)


13
14
15
# File 'lib/ogr/field.rb', line 13

def initialize(ogr_field_struct = nil)
  @c_struct = ogr_field_struct || ::FFI::OGR::Field.new
end

Instance Attribute Details

#c_structFFI::OGR::Point (readonly)

Returns:

  • (FFI::OGR::Point)


10
11
12
# File 'lib/ogr/field.rb', line 10

def c_struct
  @c_struct
end

Instance Method Details

#binaryString

Returns 8-bit, unsigned data (uchar). Unpack with String#unpack(‘C*’).

Returns:

  • (String)

    8-bit, unsigned data (uchar). Unpack with String#unpack(‘C*’).



150
151
152
153
154
# File 'lib/ogr/field.rb', line 150

def binary
  b = @c_struct[:binary]

  b[:count].positive? ? b[:data].read_bytes(b[:count]) : ""
end

#binary=(new_binary) ⇒ Object

Parameters:

  • new_binary (String)

    Binary string of 8-bit, unsigned data (uchar). Pack with Array#pack(‘C*’).



158
159
160
161
162
163
164
165
166
167
# File 'lib/ogr/field.rb', line 158

def binary=(new_binary)
  data = FFI::MemoryPointer.new(:uchar, new_binary.length)
  data.put_bytes(0, new_binary)

  b = FFI::OGR::FieldTypes::Binary.new
  b[:data] = data
  b[:count] = new_binary.length

  @c_struct[:binary] = b
end

#c_pointerFFI::Pointer

Returns:

  • (FFI::Pointer)


18
19
20
# File 'lib/ogr/field.rb', line 18

def c_pointer
  @c_struct.to_ptr
end

#dateDateTime

Returns:

  • (DateTime)


184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/ogr/field.rb', line 184

def date
  c_date = @c_struct[:date]
  return if c_date[:year].zero? || c_date[:month].zero? || c_date[:day].zero?

  formatted_tz = OGR._format_time_zone_for_ruby(c_date[:tz_flag].to_i)
  DateTime.new(c_date[:year],
               c_date[:month],
               c_date[:day],
               c_date[:hour],
               c_date[:minute],
               c_date[:second],
               formatted_tz)
end

#date=(new_date) ⇒ Object

Parameters:

  • new_date (Date, Time, DateTime)


199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/ogr/field.rb', line 199

def date=(new_date)
  # All of Date's Time methods are private. Using #send to accomdate Date.
  zone = OGR._format_time_zone_for_ogr(new_date.send(:zone))

  date = FFI::OGR::FieldTypes::Date.new
  date[:year] = new_date.year
  date[:month] = new_date.month
  date[:day] = new_date.day
  date[:hour] = new_date.hour
  date[:minute] = new_date.send(:min)
  date[:second] = new_date.send(:sec) + (new_date.to_time.usec / 1_000_000.to_f)
  date[:tz_flag] = zone

  @c_struct[:date] = date
end

#integerInteger Also known as: to_i

Returns:



23
24
25
# File 'lib/ogr/field.rb', line 23

def integer
  @c_struct[:integer]
end

#integer64Integer

Returns:



34
35
36
# File 'lib/ogr/field.rb', line 34

def integer64
  @c_struct[:integer64]
end

#integer64=(new_int64) ⇒ Object

Parameters:



39
40
41
# File 'lib/ogr/field.rb', line 39

def integer64=(new_int64)
  @c_struct[:integer64] = new_int64
end

#integer64_listArray<Integer> Also known as: to_bignum

Returns:



86
87
88
89
90
91
# File 'lib/ogr/field.rb', line 86

def integer64_list
  il = @c_struct[:integer_list]
  return [] if il[:count].zero?

  il[:list].read_array_of_int64(il[:count])
end

#integer64_list=(new_integer64_list) ⇒ Object Also known as: bignum_list=

Parameters:

  • new_integer64_list (Array<Integer>)


95
96
97
98
99
100
101
102
103
104
# File 'lib/ogr/field.rb', line 95

def integer64_list=(new_integer64_list)
  list_ptr = FFI::MemoryPointer.new(:int64, new_integer64_list.size)
  list_ptr.write_array_of_int64(new_integer64_list)

  il = FFI::OGR::FieldTypes::Integer64List.new
  il[:count] = new_integer64_list.size
  il[:list] = list_ptr

  @c_struct[:integer64_list] = il
end

#integer=(new_int) ⇒ Object

Parameters:



29
30
31
# File 'lib/ogr/field.rb', line 29

def integer=(new_int)
  @c_struct[:integer] = new_int
end

#integer_listArray<Integer>

Returns:



66
67
68
69
70
71
# File 'lib/ogr/field.rb', line 66

def integer_list
  il = @c_struct[:integer_list]
  return [] if il[:count].zero?

  il[:list].read_array_of_int(il[:count])
end

#integer_list=(new_integer_list) ⇒ Object

Parameters:

  • new_integer_list (Array<Integer>)


74
75
76
77
78
79
80
81
82
83
# File 'lib/ogr/field.rb', line 74

def integer_list=(new_integer_list)
  list_ptr = FFI::MemoryPointer.new(:int, new_integer_list.length)
  list_ptr.write_array_of_int(new_integer_list)

  il = FFI::OGR::FieldTypes::IntegerList.new
  il[:count] = new_integer_list.size
  il[:list] = list_ptr

  @c_struct[:integer_list] = il
end

#realFloat Also known as: to_f

Returns:



44
45
46
# File 'lib/ogr/field.rb', line 44

def real
  @c_struct[:real]
end

#real=(new_real) ⇒ Object

Parameters:



50
51
52
# File 'lib/ogr/field.rb', line 50

def real=(new_real)
  @c_struct[:real] = new_real
end

#real_listArray<Float> Also known as: float_list

Returns:



108
109
110
111
112
113
# File 'lib/ogr/field.rb', line 108

def real_list
  rl = @c_struct[:real_list]
  return [] if rl[:count].zero?

  rl[:list].read_array_of_double(rl[:count])
end

#real_list=(new_real_list) ⇒ Object Also known as: float_list=

Parameters:

  • new_real_list (Array<Float>)


117
118
119
120
121
122
123
124
125
126
# File 'lib/ogr/field.rb', line 117

def real_list=(new_real_list)
  list_ptr = FFI::MemoryPointer.new(:double, new_real_list.size)
  list_ptr.write_array_of_double(new_real_list)

  rl = FFI::OGR::FieldTypes::RealList.new
  rl[:count] = new_real_list.size
  rl[:list] = list_ptr

  @c_struct[:real_list] = rl
end

#setHash

Returns:

  • (Hash)


170
171
172
# File 'lib/ogr/field.rb', line 170

def set
  { marker1: @c_struct[:set][:marker1], marker2: @c_struct[:set][:marker2] }
end

#set=(new_set) ⇒ Object

Parameters:



175
176
177
178
179
180
181
# File 'lib/ogr/field.rb', line 175

def set=(new_set)
  set = FFI::OGR::FieldTypes::Set.new
  set[:marker1] = new_set[:marker1]
  set[:marker2] = new_set[:marker2]

  @c_struct[:set] = set
end

#stringObject

TODO: This blows up when another value type has been set.



55
56
57
58
59
# File 'lib/ogr/field.rb', line 55

def string
  return "" if @c_struct[:string]&.null?

  @c_struct[:string].read_string
end

#string=(new_string) ⇒ Object



61
62
63
# File 'lib/ogr/field.rb', line 61

def string=(new_string)
  @c_struct[:string] = FFI::MemoryPointer.from_string(new_string)
end

#string_listArray<String>

Returns:



130
131
132
133
134
135
# File 'lib/ogr/field.rb', line 130

def string_list
  sl = @c_struct[:string_list]
  return [] if sl[:count].zero?

  sl[:list].read_array_of_pointer(sl[:count]).map(&:read_string)
end

#string_list=(new_string_list) ⇒ Object

Parameters:

  • new_string_list (Array<String>)


138
139
140
141
142
143
144
145
146
# File 'lib/ogr/field.rb', line 138

def string_list=(new_string_list)
  list_ptr = GDAL._string_array_to_pointer(new_string_list)

  sl = FFI::OGR::FieldTypes::StringList.new
  sl[:count] = new_string_list.size
  sl[:list] = list_ptr

  @c_struct[:string_list] = sl
end