module CartoJson class LineString include Shape type :linestring attr_accessor :points def initialize(input) super @points = [] input[:points].each do |p| if p.is_a?(Point) @points << p else @points << Point.new(LAT => p[LAT], LNG => p[LNG]) end end validate end def to_hash {:type => self.class.type, :points => points.collect {|p| p.to_hash_for_array}} end def to_wkt "#{type.to_s.upcase} ((#{@points.dup.push(@points.first).collect {|p| "#{p.send(LNG)} #{p.send(LAT)}"}.join(', ')}))" end protected def validate if @points.length < 2 raise InsufficientPointsError, "a minimum of 2 points is required to make a linestring, you provided #{@points.length}" end end end end