Class: OGR::DataSource

Inherits:
Object
  • Object
show all
Includes:
GDAL::Logger, GDAL::MajorObject, Extensions, OGR::DataSourceMixins::CapabilityMethods
Defined in:
lib/ogr/data_source.rb,
lib/ogr/extensions/data_source/data_source_extensions.rb

Defined Under Namespace

Modules: Extensions

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Extensions

#layers

Methods included from OGR::DataSourceMixins::CapabilityMethods

#can_create_geometry_field_after_create_layer?, #can_create_layer?, #can_delete_layer?, #supports_curve_geometries?

Methods included from GDAL::MajorObject

#all_metadata, #description, #description=, #metadata, #metadata_domain_list, #metadata_item, #null?, #set_metadata_item

Constructor Details

#initialize(path_or_pointer, access_flag) ⇒ DataSource



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/ogr/data_source.rb', line 52

def initialize(path_or_pointer, access_flag)
  @c_pointer =
    if path_or_pointer.is_a?(String)
      FFI::OGR::API.OGROpen(path_or_pointer, OGR._boolean_access_flag(access_flag), nil)
    else
      path_or_pointer
    end

  if @c_pointer.null?
    error_msg, ptr = FFI::CPL::Error.CPLGetLastErrorMsg
    ptr.autorelease = false

    error_type = FFI::CPL::Error.CPLGetLastErrorType
    FFI::CPL::Error.CPLErrorReset

    raise OGR::OpenFailure, "#{error_type}: #{error_msg} (#{path_or_pointer})"
  end

  @layers = []
end

Instance Attribute Details

#c_pointerFFI::Pointer (readonly)



47
48
49
# File 'lib/ogr/data_source.rb', line 47

def c_pointer
  @c_pointer
end

Class Method Details

.open(path, access_flag = "r") ⇒ OGR::DataSource

Same as .new.



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ogr/data_source.rb', line 17

def self.open(path, access_flag = "r")
  ds = new(path, access_flag)

  if block_given?
    result = yield ds
    ds.close
    result
  else
    ds
  end
end

.release(pointer) ⇒ Object



30
31
32
33
34
# File 'lib/ogr/data_source.rb', line 30

def self.release(pointer)
  return unless pointer && !pointer.null?

  FFI::OGR::API.OGR_DS_Destroy(pointer)
end

.release_result_set(data_source_pointer, layer_pointer) ⇒ Object

Use to release the resulting data pointer from #execute_sql.



40
41
42
43
44
# File 'lib/ogr/data_source.rb', line 40

def self.release_result_set(data_source_pointer, layer_pointer)
  return unless data_source_pointer && !data_source_pointer.null? && layer_pointer && !layer_pointer.null?

  FFI::OGR::API.OGR_DS_ReleaseResultSet(data_source_pointer, layer_pointer)
end

Instance Method Details

#copy_layer(source_layer, new_name, **options) ⇒ OGR::Layer?



163
164
165
166
167
168
169
170
171
172
# File 'lib/ogr/data_source.rb', line 163

def copy_layer(source_layer, new_name, **options)
  source_layer_ptr = GDAL._pointer(OGR::Layer, source_layer)
  options_ptr = GDAL::Options.pointer(options)

  layer_ptr = FFI::OGR::API.OGR_DS_CopyLayer(@c_pointer, source_layer_ptr,
                                             new_name, options_ptr)
  return nil if layer_ptr.null?

  OGR::Layer.new(layer_ptr)
end

#create_layer(name, geometry_type: :wkbUnknown, spatial_reference: nil, **options) ⇒ OGR::Layer

Raises:



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/ogr/data_source.rb', line 140

def create_layer(name, geometry_type: :wkbUnknown, spatial_reference: nil, **options)
  unless test_capability("CreateLayer")
    raise OGR::UnsupportedOperation,
          "This data source does not support creating layers."
  end

  spatial_ref_ptr = GDAL._pointer(OGR::SpatialReference, spatial_reference, autorelease: false) if spatial_reference
  options_obj = GDAL::Options.pointer(options)

  layer_ptr =
    FFI::OGR::API.OGR_DS_CreateLayer(@c_pointer, name, spatial_ref_ptr, geometry_type, options_obj)

  raise OGR::InvalidLayer, "Unable to create layer '#{name}'." unless layer_ptr

  @layers << OGR::Layer.new(layer_ptr)

  @layers.last
end

#delete_layer(index) ⇒ Object

Raises:



176
177
178
179
180
181
182
183
184
185
# File 'lib/ogr/data_source.rb', line 176

def delete_layer(index)
  unless test_capability("DeleteLayer")
    raise OGR::UnsupportedOperation,
          "This data source does not support deleting layers."
  end

  OGR::ErrorHandling.handle_ogr_err("Unable to delete layer at index #{index}") do
    FFI::OGR::API.OGR_DS_DeleteLayer(@c_pointer, index)
  end
end

#destroy!Object Also known as: close

Closes opened data source and releases allocated resources.



74
75
76
77
78
# File 'lib/ogr/data_source.rb', line 74

def destroy!
  DataSource.release(@c_pointer)

  @c_pointer = nil
end

#driverOGR::Driver



93
94
95
96
97
98
# File 'lib/ogr/data_source.rb', line 93

def driver
  driver_ptr = FFI::OGR::API.OGR_DS_GetDriver(@c_pointer)
  return nil if driver_ptr.nil?

  OGR::Driver.new(driver_ptr)
end

#execute_sql(command, spatial_filter = nil, dialect = nil) ⇒ OGR::Layer?



193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/ogr/data_source.rb', line 193

def execute_sql(command, spatial_filter = nil, dialect = nil)
  geometry_ptr = GDAL._pointer(OGR::Geometry, spatial_filter) if spatial_filter

  layer_ptr = FFI::OGR::API.OGR_DS_ExecuteSQL(@c_pointer, command, geometry_ptr, dialect)
  layer_ptr.autorelease = false

  return nil if layer_ptr.null?

  OGR::Layer.new(FFI::AutoPointer.new(layer_ptr, lambda { |ptr|
    DataSource.release_result_set(@c_pointer, ptr)
  }))
end

#layer(index) ⇒ OGR::Layer



107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/ogr/data_source.rb', line 107

def layer(index)
  @layers.fetch(index) do
    # The returned layer remains owned by the OGRDataSource and should not be deleted by the application.
    layer_pointer = FFI::OGR::API.OGR_DS_GetLayer(@c_pointer, index)
    layer_pointer.autorelease = false

    return nil if layer_pointer.null?

    l = OGR::Layer.new(layer_pointer)
    @layers.insert(index, l)

    l
  end
end

#layer_by_name(name) ⇒ OGR::Layer



124
125
126
127
128
129
130
131
132
# File 'lib/ogr/data_source.rb', line 124

def layer_by_name(name)
  # The returned layer remains owned by the OGRDataSource and should not be deleted by the application.
  layer_pointer = FFI::OGR::API.OGR_DS_GetLayerByName(@c_pointer, name)
  layer_pointer.autorelease = false

  return nil if layer_pointer.null?

  OGR::Layer.new(layer_pointer)
end

#layer_countInteger



101
102
103
# File 'lib/ogr/data_source.rb', line 101

def layer_count
  FFI::OGR::API.OGR_DS_GetLayerCount(@c_pointer)
end

#nameString

Name of the file represented by this object.



84
85
86
87
88
89
90
# File 'lib/ogr/data_source.rb', line 84

def name
  # This is an internal string and should not be modified or freed.
  name_ptr = FFI::OGR::API.OGR_DS_GetName(@c_pointer)
  name_ptr.autorelease = false

  name_ptr.read_string_to_null
end

#style_tableOGR::StyleTable?



207
208
209
210
211
212
213
214
# File 'lib/ogr/data_source.rb', line 207

def style_table
  style_table_ptr = FFI::OGR::API.OGR_DS_GetStyleTable(@c_pointer)
  style_table_ptr.autorelease = false

  return nil if style_table_ptr.null?

  OGR::StyleTable.new(style_table_ptr)
end

#style_table=(new_style_table) ⇒ Object



217
218
219
220
221
222
223
224
225
226
227
# File 'lib/ogr/data_source.rb', line 217

def style_table=(new_style_table)
  new_style_table_ptr = GDAL._pointer(OGR::StyleTable, new_style_table)

  FFI::OGR::API.OGR_DS_SetStyleTable(@c_pointer, new_style_table_ptr)

  if new_style_table.instance_of? OGR::StyleTable
    new_style_table
  else
    OGR::StyleTable.new(new_style_table_ptr)
  end
end

#sync_to_diskObject

Raises:



238
239
240
241
242
# File 'lib/ogr/data_source.rb', line 238

def sync_to_disk
  OGR::ErrorHandling.handle_ogr_err("Unable to syn datasource to disk") do
    FFI::OGR::API.OGR_DS_SyncToDisk(@c_pointer)
  end
end

#test_capability(capability) ⇒ Boolean



233
234
235
# File 'lib/ogr/data_source.rb', line 233

def test_capability(capability)
  FFI::OGR::API.OGR_DS_TestCapability(@c_pointer, capability)
end