Module: OGR::LayerMixins::OGRFieldMethods

Included in:
OGR::Layer
Defined in:
lib/ogr/layer_mixins/ogr_field_methods.rb

Instance Method Summary collapse

Instance Method Details

#alter_field_definition(field_index, new_field_definition, flags) ⇒ Object

Parameters:

  • field_index (Integer)
  • new_field_definition (OGR::FieldDefinition)

    The definition for which to base the Field at field_index off of.

  • flags (Integer)

    ALTER_NAME_FLAG, ALTER_TYPE_FLAG, ALTER_WIDTH_PRECISION_FLAG, or ALTER_ALL_FLAG.

Raises:



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/ogr/layer_mixins/ogr_field_methods.rb', line 82

def alter_field_definition(field_index, new_field_definition, flags)
  unless test_capability("AlterFieldDefn")
    raise OGR::UnsupportedOperation, "This layer does not support field definition altering."
  end

  new_field_definition_ptr = GDAL._pointer(OGR::FieldDefinition, new_field_definition)

  OGR::ErrorHandling.handle_ogr_err("Unable to alter field definition at field index #{field_index}") do
    FFI::OGR::API.OGR_L_AlterFieldDefn(
      @c_pointer,
      field_index,
      new_field_definition_ptr,
      flags
    )
  end
end

#create_field(field_definition, approx_ok: false) ⇒ Object

Creates and writes a new field to the layer. This adds the field to the internal FeatureDefinition; C API says not to update the FeatureDefinition directly.

Parameters:

  • field_definition (OGR::FieldDefinition)
  • approx_ok (Boolean) (defaults to: false)

    If true the field may be created in a slightly different form, depending on the limitations of the format driver.

Raises:



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/ogr/layer_mixins/ogr_field_methods.rb', line 14

def create_field(field_definition, approx_ok: false)
  unless test_capability("CreateField")
    raise OGR::UnsupportedOperation,
          "This layer does not support field creation."
  end

  field_definition_ptr = GDAL._pointer(OGR::FieldDefinition, field_definition)

  OGR::ErrorHandling.handle_ogr_err("Unable to create field") do
    FFI::OGR::API.OGR_L_CreateField(@c_pointer, field_definition_ptr, approx_ok)
  end
end

#create_geometry_field(geometry_field_def, approx_ok: false) ⇒ Object

Creates and writes a new geometry to the layer. Note: not all drivers support this.

Parameters:

  • geometry_field_def (OGR::GeometryFieldDefinition)

    The definition to use for creating the new field.

  • approx_ok (Boolean) (defaults to: false)

Raises:



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/ogr/layer_mixins/ogr_field_methods.rb', line 118

def create_geometry_field(geometry_field_def, approx_ok: false)
  unless test_capability("CreateGeomField")
    raise OGR::UnsupportedOperation, "This layer does not support geometry field creation"
  end

  geometry_field_definition_ptr = GDAL._pointer(OGR::GeometryFieldDefinition, geometry_field_def)

  OGR::ErrorHandling.handle_ogr_err("Unable to create geometry field") do
    FFI::OGR::API.OGR_L_CreateGeomField(
      @c_pointer,
      geometry_field_definition_ptr,
      approx_ok
    )
  end
end

#delete_field(field_id) ⇒ Object

Deletes the field definition from the layer.

Raises:



30
31
32
33
34
35
36
37
38
39
# File 'lib/ogr/layer_mixins/ogr_field_methods.rb', line 30

def delete_field(field_id)
  unless test_capability("DeleteField")
    raise OGR::UnsupportedOperation,
          "This driver does not support field deletion."
  end

  OGR::ErrorHandling.handle_ogr_err("Unable to delete field") do
    FFI::OGR::API.OGR_L_DeleteField(@c_pointer, field_id)
  end
end

#find_field_index(field_name, exact_match: true) ⇒ Integer

Finds the index of a field in this Layer.

Parameters:

  • field_name (String)
  • exact_match (Boolean) (defaults to: true)

    If false and the field doesn’t exist in the given form, the driver will try to make changes to make a match.

Returns:

  • (Integer)

    Index of the field or nil if the field doesn’t exist.



105
106
107
108
109
# File 'lib/ogr/layer_mixins/ogr_field_methods.rb', line 105

def find_field_index(field_name, exact_match: true)
  result = FFI::OGR::API.OGR_L_FindFieldIndex(@c_pointer, field_name, exact_match)

  result.negative? ? nil : result
end

#reorder_field(old_position, new_position) ⇒ Object

Puts the field whose index is old_position into index at new_position and shuffles the other indexes accordingly.

Parameters:

Raises:



65
66
67
68
69
70
71
72
73
74
# File 'lib/ogr/layer_mixins/ogr_field_methods.rb', line 65

def reorder_field(old_position, new_position)
  unless test_capability("ReorderFields")
    raise OGR::UnsupportedOperation,
          "This driver does not support field reordering."
  end

  OGR::ErrorHandling.handle_ogr_err("Unable to reorder field: #{old_position} to #{new_position}") do
    FFI::OGR::API.OGR_L_ReorderField(@c_pointer, old_position, new_position)
  end
end

#reorder_fields(*new_order) ⇒ Object

Parameters:

  • new_order (Array<Integer>)

    An array that orders field indexes by which they should be reordered. I.e. [0, 2, 3, 1, 4].

Raises:



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ogr/layer_mixins/ogr_field_methods.rb', line 44

def reorder_fields(*new_order)
  unless test_capability("ReorderFields")
    raise OGR::UnsupportedOperation,
          "This driver does not support field reordering."
  end

  return false if new_order.empty? || new_order.any? { |i| i > feature_definition.field_count }

  map_array_ptr = FFI::MemoryPointer.new(:int, new_order.size).write_array_of_int(new_order)

  OGR::ErrorHandling.handle_ogr_err("Unable to reorder fields using order: #{new_order}") do
    FFI::OGR::API.OGR_L_ReorderFields(@c_pointer, map_array_ptr)
  end
end

#set_ignored_fields(*field_names) ⇒ Object

If the driver supports this functionality, it will not fetch the specified fields in subsequent calls to #feature / #next_feature and thus save some processing time and/or bandwidth.

Parameters:

Raises:



140
141
142
143
144
145
146
147
148
# File 'lib/ogr/layer_mixins/ogr_field_methods.rb', line 140

def set_ignored_fields(*field_names)
  return false if field_names.empty?

  fields_ptr = GDAL._string_array_to_pointer(field_names)

  OGR::ErrorHandling.handle_ogr_err("Unable to ignore fields with names: #{field_names}") do
    FFI::OGR::API.OGR_L_SetIgnoredFields(@c_pointer, fields_ptr)
  end
end