class RGeo::WKRep::WKTParser

This class provides the functionality of parsing a geometry from WKT (well-known text) format. You may also customize the parser to recognize PostGIS EWKT extensions to the input, or Simple Features Specification 1.2 extensions for Z and M coordinates.

To use this class, create an instance with the desired settings and customizations, and call the parse method.

Configuration options

You must provide each parser with an RGeo::Feature::FactoryGenerator. It should understand the configuration options :srid, :has_z_coordinate, and :has_m_coordinate. You may also pass a specific RGeo::Feature::Factory, or nil to specify the default Cartesian FactoryGenerator.

The following additional options are recognized. These can be passed to the constructor, or set on the object afterwards.

:support_ewkt

Activate support for PostGIS EWKT type tags, which appends an “M” to tags to indicate the presence of M but not Z, and also recognizes the SRID prefix. Default is false.

:support_wkt12

Activate support for SFS 1.2 extensions to the type codes, which use a “M”, “Z”, or “ZM” token to signal the presence of Z and M values in the data. SFS 1.2 types such as triangle, tin, and polyhedralsurface are NOT yet supported. Default is false.

:strict_wkt11

If true, parsing will proceed in SFS 1.1 strict mode, which disallows any values other than X or Y. This has no effect if support_ewkt or support_wkt12 are active. Default is false.

:ignore_extra_tokens

If true, extra tokens at the end of the data are ignored. If false (the default), extra tokens will trigger a parse error.

:default_srid

A SRID to pass to the factory generator if no SRID is present in the input. Defaults to nil (i.e. don’t specify a SRID).

Public Class Methods

new(factory_generator_=nil, opts_={}) click to toggle source

Create and configure a WKT parser. See the WKTParser documentation for the options that can be passed.

# File lib/rgeo/wkrep/wkt_parser.rb, line 90
def initialize(factory_generator_=nil, opts_={})
  if factory_generator_.kind_of?(Feature::Factory::Instance)
    @factory_generator = Feature::FactoryGenerator.single(factory_generator_)
    @exact_factory = factory_generator_
  elsif factory_generator_.respond_to?(:call)
    @factory_generator = factory_generator_
    @exact_factory = nil
  else
    @factory_generator = Cartesian.method(:preferred_factory)
    @exact_factory = nil
  end
  @support_ewkt = opts_[:support_ewkt] ? true : false
  @support_wkt12 = opts_[:support_wkt12] ? true : false
  @strict_wkt11 = @support_ewkt || @support_wkt12 ? false : opts_[:strict_wkt11] ? true : false
  @ignore_extra_tokens = opts_[:ignore_extra_tokens] ? true : false
  @default_srid = opts_[:default_srid]
end

Public Instance Methods

exact_factory() click to toggle source

If this parser was given an exact factory, returns it; otherwise returns nil.

# File lib/rgeo/wkrep/wkt_parser.rb, line 116
def exact_factory
  @exact_factory
end
factory_generator() click to toggle source

Returns the factory generator. See WKTParser for details.

# File lib/rgeo/wkrep/wkt_parser.rb, line 110
def factory_generator
  @factory_generator
end
ignore_extra_tokens?() click to toggle source

Returns true if this parser ignores extra tokens. See WKTParser for details.

# File lib/rgeo/wkrep/wkt_parser.rb, line 140
def ignore_extra_tokens?
  @ignore_extra_tokens
end
parse(str_) click to toggle source

Parse the given string, and return a geometry object.

# File lib/rgeo/wkrep/wkt_parser.rb, line 158
def parse(str_)
  str_ = str_.downcase
  @cur_factory = @exact_factory
  if @cur_factory
    @cur_factory_support_z = @cur_factory.property(:has_z_coordinate) ? true : false
    @cur_factory_support_m = @cur_factory.property(:has_m_coordinate) ? true : false
  end
  @cur_expect_z = nil
  @cur_expect_m = nil
  @cur_srid = @default_srid
  if @support_ewkt && str_ =~ %r^srid=(\d+);/
    str_ = $'
    @cur_srid = $1.to_i
  end
  begin
    _start_scanner(str_)
    obj_ = _parse_type_tag(false)
    if @cur_token && !@ignore_extra_tokens
      raise Error::ParseError, "Extra tokens beginning with #{@cur_token.inspect}."
    end
  ensure
    _clean_scanner
  end
  obj_
end
strict_wkt11?() click to toggle source

Returns true if this parser strictly adheres to WKT 1.1. See WKTParser for details.

# File lib/rgeo/wkrep/wkt_parser.rb, line 134
def strict_wkt11?
  @strict_wkt11
end
support_ewkt?() click to toggle source

Returns true if this parser supports EWKT. See WKTParser for details.

# File lib/rgeo/wkrep/wkt_parser.rb, line 122
def support_ewkt?
  @support_ewkt
end
support_wkt12?() click to toggle source

Returns true if this parser supports SFS 1.2 extensions. See WKTParser for details.

# File lib/rgeo/wkrep/wkt_parser.rb, line 128
def support_wkt12?
  @support_wkt12
end