Sha256: a9f619137a6d03a2d25a4c03c07022acfa0551a1628e9f1571d5181ff5a7cf25

Contents?: true

Size: 1.15 KB

Versions: 3

Compression:

Stored size: 1.15 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 InvalidTypeError, type, "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

3 entries across 3 versions & 1 rubygems

Version Path
carto_json-0.0.9 lib/carto_json/wkt_parser.rb
carto_json-0.0.8 lib/carto_json/wkt_parser.rb
carto_json-0.0.7 lib/carto_json/wkt_parser.rb