Class: RGdal::Base

Inherits:
Object
  • Object
show all
Includes:
API
Defined in:
lib/rgdal/base.rb

Direct Known Subclasses

CSV, GeoJSON, KML, SHP

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from API

included

Constructor Details

#initialize(source) ⇒ Base

Returns a new instance of Base.



13
14
15
16
17
18
# File 'lib/rgdal/base.rb', line 13

def initialize(source)
  @source = source
  @data_source = driver.create_data_source(@source)
  @current_layer = layers.first
  @columns = @current_layer ? @current_layer.fields.map(&:name) : []
end

Instance Attribute Details

#current_layerObject (readonly)

Returns the value of attribute current_layer.



6
7
8
# File 'lib/rgdal/base.rb', line 6

def current_layer
  @current_layer
end

#data_sourceObject (readonly)

Returns the value of attribute data_source.



5
6
7
# File 'lib/rgdal/base.rb', line 5

def data_source
  @data_source
end

#driverObject (readonly)

Returns the value of attribute driver.



7
8
9
# File 'lib/rgdal/base.rb', line 7

def driver
  @driver
end

Instance Method Details

#add_column(name, options = {}) ⇒ Object Also known as: layer_field_definition



52
53
54
55
56
57
58
# File 'lib/rgdal/base.rb', line 52

def add_column(name, options={})
  options, name = {type: Gdal::Ogr::OFTSTRING, width: 254}.merge(options), header(name)
  @columns.push(name)
  field_definition = create_field_definition(name, options)
  @current_layer.create_field(field_definition)
  field_definition = nil
end

#closeObject



45
46
47
48
49
50
# File 'lib/rgdal/base.rb', line 45

def close
  @driver = nil
  @data_source = nil
  @current_layer = nil
  @columns = nil
end

#create_field_definition(name, options) ⇒ Object



62
63
64
65
66
67
# File 'lib/rgdal/base.rb', line 62

def create_field_definition(name, options)
  Gdal::Ogr::FieldDefn.new(name, options[:type]).tap do |field|
    field.set_width(options[:width]) if options[:width]
    field.set_precision(options[:precision]) if options[:precision]
  end
end

#feature(longitude, latitude, attributes = {}) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/rgdal/base.rb', line 69

def feature(longitude, latitude, attributes={})
  feat = Gdal::Ogr::Feature.new(@current_layer.definition)
  set_geometry(latitude, longitude, feat)
  set_attributes(attributes, feat)

  @current_layer.create_feature(feat)
  @current_layer.reset_reading
end

#layer(filename) ⇒ Object



41
42
43
# File 'lib/rgdal/base.rb', line 41

def layer(filename)
  @data_source.create_layer(filename, reference, format)
end

#layersObject



20
21
22
# File 'lib/rgdal/base.rb', line 20

def layers
  @layers = @data_source.layers
end

#new_layer(filename = 'export') ⇒ Object



33
34
35
36
37
38
39
# File 'lib/rgdal/base.rb', line 33

def new_layer(filename='export')
  layer(filename).tap do |layer|
    @current_layer = layer
    @layers << layer
    @columns = []
  end
end

#switch_layer(name) ⇒ Object



28
29
30
31
# File 'lib/rgdal/base.rb', line 28

def switch_layer(name)
  @current_layer = @layers.select { |layer| layer.name == name }.first
  @columns = @current_layer ? @current_layer.fields.map(&:name) : []
end