Class: NoBrainer::GeoSpatial::Polygon

Inherits:
Object
  • Object
show all
Defined in:
lib/nobrainer_geospatial/types/geo_polygon.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(values) ⇒ Polygon

Returns a new instance of Polygon.

Raises:

  • (NoBrainer::Error::InvalidType)


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/nobrainer_geospatial/types/geo_polygon.rb', line 6

def initialize(values)
  # OK, values is either going to be a pair of coords like:
  #    [ [lon1, lat1], [lon2, lat2] ], ...
  #              OR
  #    [point1, point2], ...
  # so we should have either all numeric or an array of arrays...
  raise NoBrainer::Error::InvalidType.new('You must supply at least 4 coordinates') if values.length < 4

  # check to make sure we're not getting invalid parameters or mixed parameters...
  unless values.all? {|c| c.is_a? Numeric} || values.all? {|c| c.is_a? Array}
    raise NoBrainer::Error::InvalidType.new('Points must be an Array or list of numeric')
  end

  self.coordinates = []

  # convert from list of coords to a paired list...
  if values.all? {|c| c.is_a? Numeric}
    raise NoBrainer::Error::InvalidType.new('Must have even number of coordinates!') if values.length.odd?
    values.each_slice(2) do |coords|
      self.coordinates << [coords[0], coords[1]]
    end
  else
    # we already had a paired list... just assign it straight across...
    self.coordinates = values
  end

  check_coordinates(self.coordinates)

end

Instance Attribute Details

#coordinatesObject

Returns the value of attribute coordinates.



4
5
6
# File 'lib/nobrainer_geospatial/types/geo_polygon.rb', line 4

def coordinates
  @coordinates
end

Class Method Details

.nobrainer_cast_db_to_model(value) ⇒ Object

This class method translates a value from the database to the proper type. It is used when reading from the database.



66
67
68
# File 'lib/nobrainer_geospatial/types/geo_polygon.rb', line 66

def nobrainer_cast_db_to_model(value)
  NoBrainer::GeoSpatial::Polygon.new(*value['coordinates'])
end

.nobrainer_cast_model_to_db(value) ⇒ Object



60
61
62
# File 'lib/nobrainer_geospatial/types/geo_polygon.rb', line 60

def nobrainer_cast_model_to_db(value)
  RethinkDB::RQL.new.polygon(*value.coordinates)
end

.nobrainer_cast_user_to_model(value) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/nobrainer_geospatial/types/geo_polygon.rb', line 51

def nobrainer_cast_user_to_model(value)
  case value
    when NoBrainer::GeoSpatial::Polygon then value
    when Array then
      new(value)
    else raise NoBrainer::Error::InvalidType
  end
end