Class: Gull::Polygon

Inherits:
Object
  • Object
show all
Defined in:
lib/gull/polygon.rb

Overview

Represents the geographic boundary of an alert as a series of lat/lon coordinate pairs. Coordinates are stored in [lat, lon] order.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(coords) ⇒ Polygon

Accepts GeoJSON coordinates ([lon, lat]) and stores them as [lat, lon].



12
13
14
# File 'lib/gull/polygon.rb', line 12

def initialize(coords)
  self.coordinates = coords.map { |point| [point[1], point[0]] }
end

Instance Attribute Details

#coordinatesObject

Returns the value of attribute coordinates.



8
9
10
# File 'lib/gull/polygon.rb', line 8

def coordinates
  @coordinates
end

Instance Method Details

#to_sObject

Returns coordinates as "lat,lon lat,lon ..." string.



17
18
19
# File 'lib/gull/polygon.rb', line 17

def to_s
  coordinates.map { |pair| pair.join(',') }.join(' ')
end

#to_wktObject

Returns coordinates in Well-Known Text format.



22
23
24
25
26
# File 'lib/gull/polygon.rb', line 22

def to_wkt
  pairs = coordinates.map { |pair| "#{pair.last} #{pair.first}" }
                     .join(', ')
  "POLYGON((#{pairs}))"
end