Class: Shapes::ShapeFile

Inherits:
Object
  • Object
show all
Defined in:
lib/shapes/shape_file.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(afile = nil, stype = nil) ⇒ ShapeFile



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/shapes/shape_file.rb', line 5

def initialize(afile = nil,stype = nil)
  if afile and stype
    self.handle = ShapesLib::create_shape afile, stype.to_s
    self.dbf_handle = ShapesLib::create_dbf afile
  elsif afile
    self.handle = ShapesLib::open_shape afile
    self.dbf_handle = Shapeslib::open_dbf afile
  else
    return self # get out of the function if no handle was passed
  end
  
  self.fields = Hash.new
  
  # but if we attempted to create a handle, make sure
  # it actually returned something valid
  return self.handle unless self.handle
  return self.dbf_handle unless self.dbf_handle
end

Instance Attribute Details

#dbf_handleObject

Returns the value of attribute dbf_handle.



3
4
5
# File 'lib/shapes/shape_file.rb', line 3

def dbf_handle
  @dbf_handle
end

#fieldsObject

Returns the value of attribute fields.



3
4
5
# File 'lib/shapes/shape_file.rb', line 3

def fields
  @fields
end

#handleObject

Returns the value of attribute handle.



3
4
5
# File 'lib/shapes/shape_file.rb', line 3

def handle
  @handle
end

Class Method Details

.create(filename, shape_type, &block) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/shapes/shape_file.rb', line 24

def self.create(filename,shape_type,&block)
  inst = new filename, shape_type
  return inst unless inst
  
  inst.define(&block)
  
  inst.close
  return true
end

.open(filename, &block) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/shapes/shape_file.rb', line 34

def self.open(filename,&block)
  inst = new filename
  return inst unless inst
  
  inst.define(&block)
  
  inst.close
  return true
end

Instance Method Details

#add_field(field_name, field_type, width = 255, decimals = 0) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/shapes/shape_file.rb', line 44

def add_field(field_name,field_type,width = 255,decimals = 0)
  rv = ShapesLib::dbf_add_field self.dbf_handle, field_name.to_s, field_type.to_s, width, decimals
  self.fields[field_name] =  {
    name: field_name,
    type: field_type,
    width: width,
    decimals: decimals,
    field_number: rv
  }
  return rv
end

#arc(a) ⇒ Object



101
102
103
104
105
106
107
# File 'lib/shapes/shape_file.rb', line 101

def arc(a)
  i = ShapesLib::create_object self.handle, 'arc', a.num_vertices, a.xs, p.ys, p.zs
  attrs = a.attributes
  
  attrs.each {|key,val| save_attribute(key,val,i)}
  return i
end

#closeObject



56
57
58
59
# File 'lib/shapes/shape_file.rb', line 56

def close
  ShapesLib::close_shape self.handle
  ShapesLib::close_dbf self.dbf_handle
end

#define(&block) ⇒ Object



61
62
63
# File 'lib/shapes/shape_file.rb', line 61

def define(&block)
  instance_eval(&block)
end

#multipoint(mp) ⇒ Object



109
110
111
112
113
114
115
# File 'lib/shapes/shape_file.rb', line 109

def multipoint(mp)
  i = ShapesLib::create_object self.handle, 'multipoint', mp.num_vertices, mp.xs, mp.ys, mp.zs
  attrs = mp.attributes
  
  attrs.each {|key,val| save_attribute(key,val,i) }
  return i
end

#point(p) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/shapes/shape_file.rb', line 85

def point(p)
  i = ShapesLib::create_object self.handle, 'point', 1, [p.longitude], [p.latitude], [p.altitude]
  attrs = p.attributes
  
  attrs.each {|key,val| save_attribute(key,val,i) }
  return i
end

#polygon(p) ⇒ Object



93
94
95
96
97
98
99
# File 'lib/shapes/shape_file.rb', line 93

def polygon(p)
  i = ShapesLib::create_object self.handle, 'polygon', p.num_vertices, p.xs, p.ys, p.zs
  attrs = p.attributes
  
  attrs.each {|key,val| save_attribute(key,val,i) }
  return i
end

#save_attribute(key, value, row) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/shapes/shape_file.rb', line 65

def save_attribute(key,value,row)
  return false if self.fields[key].nil?
  
  d = self.dbf_handle
  field_info = self.fields[key]
  field_name = key.to_s
  field_type = field_info[:type]
  field_number = field_info[:field_number]
  width = field_info[:width]
  decimals = field_info[:decimals]
  
  saved = case field_type
  when :string then ShapesLib::dbf_write_string d, row, field_number, value.to_s
  when :integer then ShapesLib::dbf_write_integer d, row, field_number, value.to_i
  when :double then ShapesLib::dbf_write_double d, row, field_number, value.to_f
  end
  
  return saved
end