Class: CartoJson::WKTParser

Inherits:
Object
  • Object
show all
Defined in:
lib/carto_json/wkt_parser.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ WKTParser

Returns a new instance of WKTParser.



8
9
10
# File 'lib/carto_json/wkt_parser.rb', line 8

def initialize(input)
  @input = input
end

Class Method Details

.parse(input) ⇒ Object



4
5
6
# File 'lib/carto_json/wkt_parser.rb', line 4

def self.parse(input)
  new(input).parse
end

Instance Method Details

#parseObject

Raises:



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/carto_json/wkt_parser.rb', line 12

def parse
  type = @input.match /(\w+) \(/i

  raise InputError, "invalid WKT input" if type.nil?

  type = type.captures.first

  case type.downcase.to_sym
  when :polygon
    points = xy_to_points(@input.match(/polygon \(\((.+)\)\)/i).captures.first)
    Polygon.new :points => points[0...points.length-1]
  when :linestring
    LineString.new :points => xy_to_points(@input.match(/linestring \((.+)\)/i).captures.first)
  when :point
    Point.new xy_to_points(@input.match(/point \((.+)\)/i).captures.first).first
  else
    raise NotImplementedError, 'multi types are not implemented yet' if MULTI_TYPES.include? type.downcase
    raise InvalidTypeError, type, "invalid type: #{type}"
  end
end