Sha256: ddb6e7231b9ceed96aede39e314c32d0ae202d54311a37a0ee63d83a42ab07d5

Contents?: true

Size: 1.14 KB

Versions: 1

Compression:

Stored size: 1.14 KB

Contents

# TODO: Write in a real parsing library like Parslet, finish multi type support
module CartoJson
  class WKTParser
    def self.parse(input)
      new(input).parse
    end

    def initialize(input)
      @input = input
    end

    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 Input Error, "invalid type: #{type}"
      end
    end

    private

    def xy_to_points(capture)
      capture.split(',').collect { |coordinates| coordinates.split(' ') }.collect {|c| {LAT => c.last, LNG => c.first}}
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
carto_json-0.0.6 lib/carto_json/wkt_parser.rb