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.


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

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 = @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

Instance Attribute Details

#exact_factoryObject (readonly)

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


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

def exact_factory
  @exact_factory
end

#factory_generatorObject (readonly)

Returns the factory generator. See WKTParser for details.


75
76
77
# File 'lib/rgeo/wkrep/wkt_parser.rb', line 75

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)

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

def ignore_extra_tokens?
  @ignore_extra_tokens
end

#parse(str) ⇒ Object

Parse the given string, and return a geometry object.


117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/rgeo/wkrep/wkt_parser.rb', line 117

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 =~ /^srid=(\d+);/i
    str = $'
    @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

#propertiesObject


105
106
107
108
109
110
111
112
113
# File 'lib/rgeo/wkrep/wkt_parser.rb', line 105

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)

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

def strict_wkt11?
  @strict_wkt11
end

#support_ewkt?Boolean

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

Returns:

  • (Boolean)

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

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)

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

def support_wkt12?
  @support_wkt12
end