Class: OGR::DataSource

Inherits:
Object
  • Object
show all
Includes:
FFIOGR
Defined in:
lib/ffi-ogr/data_source.rb

Direct Known Subclasses

CSV, GeoJSON, KML, Shapefile

Constant Summary

Constants included from FFIOGR

FFIOGR::OGR_FUNCTIONS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from FFIOGR

find_lib, gdal_library_path, search_paths

Constructor Details

#initialize(ptr) ⇒ DataSource

Returns a new instance of DataSource.


7
8
9
10
# File 'lib/ffi-ogr/data_source.rb', line 7

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

Instance Attribute Details

#ptrObject

Returns the value of attribute ptr


5
6
7
# File 'lib/ffi-ogr/data_source.rb', line 5

def ptr
  @ptr
end

Class Method Details

.release(ptr) ⇒ Object


12
# File 'lib/ffi-ogr/data_source.rb', line 12

def self.release(ptr);end

Instance Method Details

#add_layer(name, geometry_type, spatial_ref = nil, options = nil) ⇒ Object


97
98
99
100
# File 'lib/ffi-ogr/data_source.rb', line 97

def add_layer(name, geometry_type, spatial_ref=nil, options=nil)
  layer = FFIOGR.OGR_DS_CreateLayer(@ptr, name, spatial_ref, geometry_type.to_sym, options)
  OGR::Tools.cast_layer(layer)
end

#copy(driver_name, output_path, driver_options = nil) ⇒ Object


18
19
20
21
22
# File 'lib/ffi-ogr/data_source.rb', line 18

def copy(driver_name, output_path, driver_options=nil)
  driver = OGRGetDriverByName driver_name
  new_ds = FFIOGR.OGR_Dr_CopyDataSource(driver, @ptr, File.expand_path(output_path), driver_options)
  FFIOGR.OGR_DS_Destroy(new_ds)
end

#copy_with_transform(driver_name, output_path, spatial_ref = nil, driver_options = nil) ⇒ Object


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/ffi-ogr/data_source.rb', line 24

def copy_with_transform(driver_name, output_path, spatial_ref=nil, driver_options=nil)
  writer = OGR::Writer.new driver_name
  writer.set_output(output_path)
  out = writer.ptr

  layers.each do |layer|
    name = layer.name
    geometry_type = layer.geometry_type
    old_sr = layer.spatial_ref

    ct = OGR::CoordinateTransformation.find_transformation(old_sr, spatial_ref) unless spatial_ref.nil? || (spatial_ref == old_sr)

    sr = spatial_ref.nil? ? nil : spatial_ref.ptr

    new_layer = out.add_layer name, geometry_type, sr, driver_options

    ptr = layer.ptr

    layer_definition = FFIOGR.OGR_L_GetLayerDefn(ptr)
    field_count = FFIOGR.OGR_FD_GetFieldCount(layer_definition)

    for i in (0...field_count)
      fd = FFIOGR.OGR_FD_GetFieldDefn(layer_definition, i)
      name = FFIOGR.OGR_Fld_GetNameRef(fd)
      type = FFIOGR.OGR_Fld_GetType(fd)

      opts = {}.tap do |o|
        case type
        when :real
          o[:precision] = FFIOGR.OGR_Fld_GetPrecision fd
        when :string
          o[:width] = FFIOGR.OGR_Fld_GetWidth fd
        end
      end

      new_layer.add_field name, type, opts
    end

    layer.features.each do |feature|
      geometry = OGR::Tools.cast_geometry(feature.geometry)
      geometry.transform ct if ct

      new_feature = new_layer.create_feature
      new_feature.add_geometry geometry

      ptr = feature.ptr
      field_count = FFIOGR.OGR_F_GetFieldCount(ptr)

      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

        new_feature.set_field_value field_name, field_value, field_type
      end

      new_layer.add_feature new_feature
    end

    new_layer.sync
  end
  out.free
end

#freeObject


14
15
16
# File 'lib/ffi-ogr/data_source.rb', line 14

def free
  FFIOGR.OGR_DS_Destroy(@ptr)
end

#get_featuresObject Also known as: features


117
118
119
# File 'lib/ffi-ogr/data_source.rb', line 117

def get_features
  layers.map {|l| l.features}
end

#get_fieldsObject Also known as: fields


131
132
133
# File 'lib/ffi-ogr/data_source.rb', line 131

def get_fields
  features.map {|feature| feature.map {|f| f.fields}}
end

#get_geometries(as_geojson = false) ⇒ Object Also known as: geometries


122
123
124
125
126
127
128
# File 'lib/ffi-ogr/data_source.rb', line 122

def get_geometries(as_geojson=false)
  unless as_geojson
    features.map {|feature| feature.map {|f| OGR::Tools.cast_geometry(f.geometry)}}
  else
    features.map {|feature| feature.map {|f| OGR::Tools.cast_geometry(f.geometry).to_geojson}}
  end
end

#get_layersObject Also known as: layers


106
107
108
109
110
111
112
113
114
# File 'lib/ffi-ogr/data_source.rb', line 106

def get_layers
  layers = []

  for i in (0...num_layers) do
    layers << OGR::Tools.cast_layer(OGR_DS_GetLayer(@ptr, i))
  end

  layers
end

#num_layersObject


102
103
104
# File 'lib/ffi-ogr/data_source.rb', line 102

def num_layers
  FFIOGR.OGR_DS_GetLayerCount(@ptr)
end

#parse_driver_options(options) ⇒ Object


176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/ffi-ogr/data_source.rb', line 176

def parse_driver_options(options)
  tf_values = {
    true => "YES",
    false => "NO"
  }

  pointers = [].tap do |ptrs|
    options.each do |k,v|
      tf_value = tf_values[v] || v
      ptrs << FFI::MemoryPointer.from_string("#{k.to_s.upcase}=#{tf_value.upcase}")
    end
  end

  pointers << nil

  driver_options = FFI::MemoryPointer.new :pointer, pointers.size

  pointers.each_with_index do |ptr, i|
    driver_options[i].put_pointer 0, ptr
  end

  driver_options
end

#to_csv(output_path, options = {}) ⇒ Object


160
161
162
# File 'lib/ffi-ogr/data_source.rb', line 160

def to_csv(output_path, options={})
  to_format('csv', output_path, options)
end

#to_format(format, output_path, options = {}) ⇒ Object

Raises:

  • (RuntimeError)

136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/ffi-ogr/data_source.rb', line 136

def to_format(format, output_path, options={})
  raise RuntimeError.new("Output path not specified.") if output_path.nil?

  spatial_ref = options.delete :spatial_ref

  driver_options = parse_driver_options options

  driver_name = OGR::DRIVER_TYPES[format]

  unless spatial_ref
    copy driver_name, output_path, driver_options
  else
    if spatial_ref.instance_of? OGR::SpatialReference
      copy_with_transform driver_name, output_path, spatial_ref, driver_options
    else
      raise RuntimeError.new("Invalid spatial reference specified.")
    end
  end
end

#to_geojson(output_path, options = {}) ⇒ Object


172
173
174
# File 'lib/ffi-ogr/data_source.rb', line 172

def to_geojson(output_path, options={})
  to_format('geojson', output_path, options)
end

#to_json(pretty = false) ⇒ Object


200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/ffi-ogr/data_source.rb', line 200

def to_json(pretty=false)
  h = {
    type: 'FeatureCollection',
    features: []
  }

  layers.each do |layer|
    h[:features].tap do |features|
      layer.features.each do |feature|
        features << {
          type: 'Feature',
          geometry: OGR::Tools.cast_geometry(feature.geometry).to_geojson,
          properties: feature.fields
        }
      end
    end
  end

  unless pretty
    MultiJson.dump(h)
  else
    MultiJson.dump(h, pretty: true)
  end
end

#to_kml(output_path, options = {}) ⇒ Object


164
165
166
167
168
169
170
# File 'lib/ffi-ogr/data_source.rb', line 164

def to_kml(output_path, options={})
  format = OGR.drivers.include?('LIBKML') ? 'kml' : 'kml_lite'

  warn "GDAL is compiled without LIBKML support. Without LIBKML support KML output will always be in EPSG:4326" if format == 'kml_lite'

  to_format(format, output_path, options)
end

#to_shp(output_path, options = {}) ⇒ Object


156
157
158
# File 'lib/ffi-ogr/data_source.rb', line 156

def to_shp(output_path, options={})
  to_format('shapefile', output_path, options)
end