lib/gull/polygon.rb in gull-0.3.4 vs lib/gull/polygon.rb in gull-0.4.0

- old
+ new

@@ -1,37 +1,14 @@ module Gull class Polygon attr_accessor :coordinates def initialize(polygon) - self.coordinates = polygon.split(' ').collect do |coords| - coords.split(',').collect(&:to_f) - end + self.coordinates = polygon.split(' ') + .map { |point| point.split(',').map(&:to_f) } end - def centroid - low_x = 0 - low_y = 0 - high_x = 0 - high_y = 0 - - coordinates.each do |pair| - x_bounds = bounds pair.first, low_x, high_x - low_x = x_bounds.first - high_x = x_bounds.last - - y_bounds = bounds pair.last, low_y, high_y - low_y = y_bounds.first - high_y = y_bounds.last - end - - center_x = low_x + ((high_x - low_x) / 2) - center_y = low_y + ((high_y - low_y) / 2) - - [center_x, center_y] - end - def image_url(api_key, options = {}) options = { width: 640, height: 640, color: '0xff0000', @@ -49,22 +26,19 @@ def to_s coordinates.map { |pair| pair.join(',') }.join(' ') end - private - - def bounds(point, low, high) - if point < low || low == 0 - low = point - elsif point > high || high == 0 - high = point - end - - [low, high] + # Returns well-known text (WKT) formatted polygon + def to_wkt + pairs = coordinates.map { |pair| "#{pair.last} #{pair.first}" } + .join(', ') + "POLYGON((#{pairs}))" end + private + def coordinates_piped - coordinates.collect { |pair| pair.join ',' }.join '|' + coordinates.map { |pair| pair.join ',' }.join '|' end end end