Class: OGR::Feature

Inherits:
Object
  • Object
show all
Defined in:
lib/ffi-ogr/feature.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ptr) ⇒ Feature

Returns a new instance of Feature.



5
6
7
8
9
# File 'lib/ffi-ogr/feature.rb', line 5

def initialize(ptr)
  @ptr = FFI::AutoPointer.new(ptr, self.class.method(:release))
  #@ptr = FFI::AutoPointer.new(ptr)
  @ptr.autorelease = false
end

Instance Attribute Details

#ptrObject

Returns the value of attribute ptr.



3
4
5
# File 'lib/ffi-ogr/feature.rb', line 3

def ptr
  @ptr
end

Class Method Details

.release(ptr) ⇒ Object



11
# File 'lib/ffi-ogr/feature.rb', line 11

def self.release(ptr);end

Instance Method Details

#add_geometry(geometry) ⇒ Object



42
43
44
45
# File 'lib/ffi-ogr/feature.rb', line 42

def add_geometry(geometry)
  #FFIOGR.OGR_F_SetGeometry(@ptr, geometry.ptr)
  FFIOGR.OGR_F_SetGeometryDirectly(@ptr, geometry.ptr)
end

#freeObject



13
14
15
# File 'lib/ffi-ogr/feature.rb', line 13

def free
  FFIOGR.OGR_F_Destroy(@ptr)
end

#get_field_countObject Also known as: field_count



52
53
54
# File 'lib/ffi-ogr/feature.rb', line 52

def get_field_count
  FFIOGR.OGR_F_GetFieldCount(@ptr)
end

#get_fieldsObject Also known as: fields



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/ffi-ogr/feature.rb', line 57

def get_fields
  fields = {}

  for i in (0...field_count)
    fd = FFIOGR.OGR_F_GetFieldDefnRef(@ptr, i)
    field_name = FFIOGR.OGR_Fld_GetNameRef(fd)
    field_type = FFIOGR.OGR_Fld_GetType(fd)

    case field_type
    when :integer
      field_value = FFIOGR.OGR_F_GetFieldAsInteger(@ptr, i)
    when :real
      field_value = FFIOGR.OGR_F_GetFieldAsDouble(@ptr, i)
    else
      field_value = FFIOGR.OGR_F_GetFieldAsString(@ptr, i)
    end

    fields[field_name] = field_value
  end

  fields
end

#get_geometryObject Also known as: geometry



47
48
49
# File 'lib/ffi-ogr/feature.rb', line 47

def get_geometry
  FFIOGR.OGR_F_GetGeometryRef(@ptr)
end

#set_field_value(name, value, field_type = nil) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ffi-ogr/feature.rb', line 17

def set_field_value(name, value, field_type=nil)
  field_index = FFIOGR.OGR_F_GetFieldIndex(@ptr, name)

  unless field_type.nil?
    field_type = field_type.to_sym
  else
    field_definition = FFIOGR.OGR_F_GetFieldDefnRef(@ptr, field_index)
    field_type = FFIOGR.OGR_Fld_GetType(field_definition)
  end

  case field_type
  when :integer
    FFIOGR.OGR_F_SetFieldInteger(@ptr, field_index, Integer(value))
  when :real
    #dbl_ptr = FFI::MemoryPointer.new(:double, 1)
    #dbl_ptr.write_double(Float(value))

    FFIOGR.OGR_F_SetFieldDouble(@ptr, field_index, value)
  when :string
    FFIOGR.OGR_F_SetFieldString(@ptr, field_index, String(value))
  when :binary
    FFIOGR.OGR_F_SetFieldBinary(@ptr, field_index, to_binary(value))
  end
end

#to_geojsonObject



88
89
90
# File 'lib/ffi-ogr/feature.rb', line 88

def to_geojson
  {type: 'Feature', geometry: OGR::Tools.cast_geometry(geometry).to_geojson, properties: fields}
end

#transform_to(out_sr) ⇒ Object



81
82
83
84
85
86
# File 'lib/ffi-ogr/feature.rb', line 81

def transform_to(out_sr)
  geom = OGR::Tools.cast_geometry(geometry)
  geom.transform(out_sr)
  p geom.ptr
  add_geometry(geom)
end