Class: RGeo::WKRep::WKTParser

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

Overview

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).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(factory_generator = nil, opts = {}) ⇒ WKTParser

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



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/rgeo/wkrep/wkt_parser.rb', line 55

def initialize(factory_generator = nil, opts = {})
  if factory_generator.is_a?(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 =
    if @support_ewkt || @support_wkt12
      false
    else
      opts[:strict_wkt11] ? true : false
    end
  @ignore_extra_tokens = opts[:ignore_extra_tokens] ? true : false
  @default_srid = opts[:default_srid]
  @mutex = Mutex.new
end

Instance Attribute Details

#exact_factoryObject (readonly)

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



84
85
86
# File 'lib/rgeo/wkrep/wkt_parser.rb', line 84

def exact_factory
  @exact_factory
end

#factory_generatorObject (readonly)

Returns the factory generator. See WKTParser for details.



80
81
82
# File 'lib/rgeo/wkrep/wkt_parser.rb', line 80

def factory_generator
  @factory_generator
end

Instance Method Details

#ignore_extra_tokens?Boolean

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

Returns:

  • (Boolean)


106
107
108
# File 'lib/rgeo/wkrep/wkt_parser.rb', line 106

def ignore_extra_tokens?
  @ignore_extra_tokens
end

#parse(str) ⇒ Object

Parse the given string, and return a geometry object.



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/rgeo/wkrep/wkt_parser.rb', line 122

def parse(str)
  @mutex.synchronize do
    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 =~ /^srid=(\d+);/i
      str = Regexp.last_match&.post_match
      @cur_srid = Regexp.last_match(1).to_i
    end
    begin
      start_scanner(str)
      obj = parse_type_tag

      if @cur_token && !@ignore_extra_tokens
        raise Error::ParseError, "Extra tokens beginning with #{@cur_token.inspect}."
      end
    ensure
      clean_scanner
    end
    obj
  end
end

#propertiesObject



110
111
112
113
114
115
116
117
118
# File 'lib/rgeo/wkrep/wkt_parser.rb', line 110

def properties
  {
    "support_ewkt" => @support_ewkt,
    "support_wkt12" => @support_wkt12,
    "strict_wkt11" => @strict_wkt11,
    "ignore_extra_tokens" => @ignore_extra_tokens,
    "default_srid" => @default_srid
  }
end

#strict_wkt11?Boolean

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

Returns:

  • (Boolean)


100
101
102
# File 'lib/rgeo/wkrep/wkt_parser.rb', line 100

def strict_wkt11?
  @strict_wkt11
end

#support_ewkt?Boolean

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

Returns:

  • (Boolean)


88
89
90
# File 'lib/rgeo/wkrep/wkt_parser.rb', line 88

def support_ewkt?
  @support_ewkt
end

#support_wkt12?Boolean

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

Returns:

  • (Boolean)


94
95
96
# File 'lib/rgeo/wkrep/wkt_parser.rb', line 94

def support_wkt12?
  @support_wkt12
end