Module: OGR::LayerMixins::OGRFeatureMethods

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

Instance Method Summary collapse

Instance Method Details

#create_feature(feature) ⇒ Object

Adds the new OGR::Feature to the Layer. The feature should have been created using the Layer’s FeatureDefintion.

feature = OGR::Feature.new(layer.feature_definition)
feature.set_field_integer(123)
layer.create_feature(feature)

Parameters:

Raises:



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

def create_feature(feature)
  unless test_capability("SequentialWrite")
    raise OGR::UnsupportedOperation,
          "This layer does not support feature creation."
  end

  OGR::ErrorHandling.handle_ogr_err("Unable to create feature") do
    FFI::OGR::API.OGR_L_CreateFeature(@c_pointer, feature.c_pointer)
  end
end

#definitionOGR::FeatureDefinition? Also known as: feature_definition

The schema information for this layer.

Returns:



9
10
11
12
13
14
15
16
17
# File 'lib/ogr/layer_mixins/ogr_feature_methods.rb', line 9

def definition
  feature_defn_pointer = FFI::OGR::API.OGR_L_GetLayerDefn(@c_pointer)
  feature_defn_pointer.autorelease = false

  return nil if feature_defn_pointer.null?

  # This object should not be modified.
  OGR::FeatureDefinition.new(feature_defn_pointer)
end

#delete_feature(feature_id) ⇒ Object

Deletes the feature from the layer.

Parameters:

  • feature_id (Integer)

    ID of the Feature to delete.

Raises:

  • (OGR::Failure)

    When trying to delete a feature with an ID that does not exist.



45
46
47
48
49
50
51
52
53
54
# File 'lib/ogr/layer_mixins/ogr_feature_methods.rb', line 45

def delete_feature(feature_id)
  unless test_capability("DeleteFeature")
    raise OGR::UnsupportedOperation,
          "This layer does not support feature deletion."
  end

  OGR::ErrorHandling.handle_ogr_err("Unable to delete feature with ID '#{feature_id}'") do
    FFI::OGR::API.OGR_L_DeleteFeature(@c_pointer, feature_id)
  end
end

#feature(index) ⇒ OGR::Feature?

Parameters:

  • index (Integer)

    The 0-based index of the feature to get. It should be <= feature_count, but no checking is done to ensure.

Returns:



83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/ogr/layer_mixins/ogr_feature_methods.rb', line 83

def feature(index)
  unless test_capability("RandomRead")
    raise OGR::UnsupportedOperation,
          "#feature(index) not supported by this Layer"
  end

  # This feature needs to be Destroyed.
  feature_pointer = FFI::OGR::API.OGR_L_GetFeature(@c_pointer, index)
  return nil if feature_pointer.null?

  OGR::Feature.new(feature_pointer)
end

#feature=(new_feature) ⇒ Object

Rewrites an existing feature using the ID within the given Feature.

Parameters:

Raises:



69
70
71
72
73
74
75
76
77
78
# File 'lib/ogr/layer_mixins/ogr_feature_methods.rb', line 69

def feature=(new_feature)
  raise OGR::UnsupportedOperation, "#feature= not supported by this Layer" unless test_capability("RandomWrite")

  new_feature_ptr = GDAL._pointer(OGR::Feature, new_feature)
  raise OGR::InvalidFeature if new_feature_ptr.nil? || new_feature_ptr.null?

  OGR::ErrorHandling.handle_ogr_err("Unable to set feature") do
    FFI::OGR::API.OGR_L_SetFeature(@c_pointer, new_feature_ptr)
  end
end

#feature_count(force: true) ⇒ Integer

The number of features in this layer. If force is false and it would be expensive to determine the feature count, -1 may be returned.

Parameters:

  • force (Boolean) (defaults to: true)

    Force the calculation even if it’s expensive.

Returns:



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

def feature_count(force: true)
  FFI::OGR::API.OGR_L_GetFeatureCount(@c_pointer, force)
end

#features_readInteger

Returns:



123
124
125
# File 'lib/ogr/layer_mixins/ogr_feature_methods.rb', line 123

def features_read
  FFI::OGR::API.OGR_L_GetFeaturesRead(@c_pointer)
end

#next_featureOGR::Feature?

The next available feature in this layer. Only features matching the current spatial filter will be returned. Use reset_reading to start at the beginning again.

NOTE: You must call OGR::LayerMixins::OGRFeatureMethods.{OGR{OGR::Feature{OGR::Feature#destroy!} on the returned feature, otherwise expect segfaults.

Returns:



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

def next_feature
  feature_pointer = FFI::OGR::API.OGR_L_GetNextFeature(@c_pointer)
  return if feature_pointer.null?

  OGR::Feature.new(feature_pointer)
end

#next_feature_index=(feature_index) ⇒ Object Also known as: set_next_by_index

Sets the index for #next_feature.

Parameters:

Raises:



115
116
117
118
119
# File 'lib/ogr/layer_mixins/ogr_feature_methods.rb', line 115

def next_feature_index=(feature_index)
  OGR::ErrorHandling.handle_ogr_err("Unable to set next feature index to #{feature_index}") do
    FFI::OGR::API.OGR_L_SetNextByIndex(@c_pointer, feature_index)
  end
end

#reset_readingObject

Resets the sequential reading of features for this layer.



128
129
130
# File 'lib/ogr/layer_mixins/ogr_feature_methods.rb', line 128

def reset_reading
  FFI::OGR::API.OGR_L_ResetReading(@c_pointer)
end