Class: CartoJson::Point

Inherits:
Object
  • Object
show all
Includes:
Shape
Defined in:
lib/carto_json/point.rb

Direct Known Subclasses

Circle

Instance Method Summary collapse

Methods included from Shape

included, #to_json, #to_pretty_json, #to_s, #to_wkt, #type

Constructor Details

#initialize(input) ⇒ Point

Returns a new instance of Point.

Raises:



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/carto_json/point.rb', line 9

def initialize(input)
  raise InputError, 'cannot create a shape with an array' if input.is_a?(Array)

  if input[LAT].nil? || input[LNG].nil?
    raise InputError, "#{LAT} and #{LNG} are required to instantiate a point"
  end

  input[LAT] = input[LAT].to_f
  input[LNG] = input[LNG].to_f

  super input

  unless (-90..90).include? input[LAT]
    raise InputError, "latitude must be between -90 and 90, \"#{input[LAT]}\" was provided"
  end

  unless (-180..180).include? input[LNG]
    raise InputError, "longitude must be between -180 and 180, \"#{input[LNG]}\" was provided"
  end
end

Instance Method Details

#==(point) ⇒ Object



41
42
43
# File 'lib/carto_json/point.rb', line 41

def ==(point)
  send(LAT) == point.send(LAT) && send(LNG) == point.send(LNG)
end

#to_hashObject



30
31
32
33
34
# File 'lib/carto_json/point.rb', line 30

def to_hash
  {:type => self.class.type,
   LAT  => send(LAT),
   LNG  => send(LNG)}
end

#to_hash_for_arrayObject



36
37
38
39
# File 'lib/carto_json/point.rb', line 36

def to_hash_for_array
  {LAT  => send(LAT),
   LNG  => send(LNG)}
end