Class: GeoAPI::Geometry

Inherits:
Object
  • Object
show all
Defined in:
lib/geoapi/geometry.rb

Direct Known Subclasses

Multipoint, Point, Polygon

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs) ⇒ Geometry

Returns a new instance of Geometry.



76
77
78
# File 'lib/geoapi/geometry.rb', line 76

def initialize attrs
  @geometry_type = self.class.name.split('::').last
end

Class Attribute Details

.coordsObject

Returns the value of attribute coords.



5
6
7
# File 'lib/geoapi/geometry.rb', line 5

def coords
  @coords
end

.geometry_typeObject

Returns the value of attribute geometry_type.



5
6
7
# File 'lib/geoapi/geometry.rb', line 5

def geometry_type
  @geometry_type
end

Class Method Details

.from_hash(hash) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/geoapi/geometry.rb', line 42

def self.from_hash hash
  
  unless hash.blank?
    geom = Geometry.new_from_class_name hash['type']
  
    unless geom.blank?
    
      geom.geometry_type = hash['type']
  
      geom.coords = hash['coordinates']
      
      puts "Got geometry: #{geom}"
      
      geom
  
    end
  end
end

.from_json(json) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/geoapi/geometry.rb', line 24

def self.from_json json
  unless json.blank?
    attrs = Crack::JSON.parse(json)
  
    geom = Geometry.new_from_class_name json['type']
  
    unless geom.blank?
    
      geom.geometry_type = attrs['type']
  
      geom.coords = attrs['coordinates']
    
      geom
    
    end
  end
end

.new_from_class_name(class_name) ⇒ Object

Class methods



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/geoapi/geometry.rb', line 11

def self.new_from_class_name class_name
  geom = case class_name.downcase.gsub('_','')
    
    when "point" then GeoAPI::Point.new
    
    when "polygon" then GeoAPI::Polygon.new
    
    when "multipoint" then GeoAPI::Polygon.new
      
  end

end

Instance Method Details

#coordinatesObject

Instance methods



64
65
66
# File 'lib/geoapi/geometry.rb', line 64

def coordinates
  coords
end

#to_json(options = nil) ⇒ Object



72
73
74
# File 'lib/geoapi/geometry.rb', line 72

def to_json options=nil
  {:type=>self.geometry_type, :coordinates=>self.coords}.to_json
end

#typeObject



68
69
70
# File 'lib/geoapi/geometry.rb', line 68

def type
  geometry_type
end