Class: RGeo::CoordSys::CS::WKTParser

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

Overview

:nodoc:

Defined Under Namespace

Classes: ArgumentList, AuthorityClause, ExtensionClause, QuotedString, TypeString

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(str) ⇒ WKTParser

Returns a new instance of WKTParser.



13
14
15
16
# File 'lib/rgeo/coord_sys/cs/wkt_parser.rb', line 13

def initialize(str)
  @scanner = StringScanner.new(str)
  next_token
end

Instance Attribute Details

#cur_tokenObject (readonly)

Returns the value of attribute cur_token.



235
236
237
# File 'lib/rgeo/coord_sys/cs/wkt_parser.rb', line 235

def cur_token
  @cur_token
end

Instance Method Details

#consume_tokentype(type) ⇒ Object

:nodoc:



189
190
191
192
193
194
# File 'lib/rgeo/coord_sys/cs/wkt_parser.rb', line 189

def consume_tokentype(type) # :nodoc:
  expect_tokentype(type)
  tok = @cur_token
  next_token
  tok
end

#expect_tokentype(type) ⇒ Object

:nodoc:

Raises:



196
197
198
199
200
# File 'lib/rgeo/coord_sys/cs/wkt_parser.rb', line 196

def expect_tokentype(type) # :nodoc:
  return if type === @cur_token

  raise Error::ParseError, "#{type.inspect} expected but #{@cur_token.inspect} found."
end

#next_tokenObject

:nodoc:



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/rgeo/coord_sys/cs/wkt_parser.rb', line 202

def next_token # :nodoc:
  @scanner.skip(/\s+/)
  case @scanner.peek(1)
  when '"'
    @scanner.getch
    @cur_token = QuotedString.new(@scanner.scan(/[^"]*/))
    @scanner.getch
  when ","
    @scanner.getch
    @cur_token = :comma
  when "(", "["
    @scanner.getch
    @cur_token = :begin
  when "]", ")"
    @scanner.getch
    @cur_token = :end
  when /[a-zA-Z]/
    @cur_token = TypeString.new(@scanner.scan(/[a-zA-Z]\w*/))
  when "", nil
    @cur_token = nil
  else
    @scanner.scan_until(/[^\s()\[\],"]+/)
    token = @scanner.matched

    unless token =~ /^[-+]?(\d+(\.\d*)?|\.\d+)(e[-+]?\d+)?$/
      raise Error::ParseError, "Bad token: #{token.inspect}"
    end

    @cur_token = token.to_f
  end
  @cur_token
end

#parse(containing_type = nil) ⇒ Object

:nodoc:



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/rgeo/coord_sys/cs/wkt_parser.rb', line 18

def parse(containing_type = nil) # :nodoc:
  if @cur_token.is_a?(QuotedString) ||
    @cur_token.is_a?(Numeric) ||
    (containing_type == "AXIS" && @cur_token.is_a?(TypeString))
    value = @cur_token
    next_token
    return value
  end

  unless @cur_token.is_a?(TypeString)
    raise Error::ParseError, "Found token #{@cur_token} when we expected a value"
  end

  type = @cur_token
  next_token
  consume_tokentype(:begin)
  args = ArgumentList.new
  args << parse(type)
  loop do
    break unless @cur_token == :comma
    next_token
    args << parse(type)
  end
  consume_tokentype(:end)
  obj = nil
  case type
  when "AUTHORITY"
    obj = AuthorityClause.new(args.shift(QuotedString), args.shift(QuotedString))
  when "EXTENSION"
    obj = ExtensionClause.new(args.shift(QuotedString), args.shift(QuotedString))
  when "AXIS"
    obj = AxisInfo.create(args.shift(QuotedString), args.shift(TypeString))
  when "TOWGS84"
    bursa_wolf_params = args.find_all(Numeric)

    unless bursa_wolf_params.size == 7
      raise Error::ParseError, "Expected 7 Bursa Wolf parameters but found #{bursa_wolf_params.size}"
    end

    obj = WGS84ConversionInfo.create(*bursa_wolf_params)
  when "UNIT"
    klass = case containing_type
            when "GEOCCS", "VERT_CS", "PROJCS", "SPHEROID"
              LinearUnit
            when "GEOGCS"
              AngularUnit
            else
              Unit
            end
    obj = klass.create(args.shift(QuotedString), args.shift(Numeric), *args.create_optionals)
  when "PARAMETER"
    obj = ProjectionParameter.create(args.shift(QuotedString), args.shift(Numeric))
  when "PRIMEM"
    obj = PrimeMeridian.create(args.shift(QuotedString), nil, args.shift(Numeric), *args.create_optionals)
  when "SPHEROID"
    obj = Ellipsoid.create_flattened_sphere(
      args.shift(QuotedString),
      args.shift(Numeric),
      args.shift(Numeric),
      args.find_first(LinearUnit),
      *args.create_optionals
    )
  when "PROJECTION"
    name = args.shift(QuotedString)
    obj = Projection.create(name, name, args.find_all(ProjectionParameter), *args.create_optionals)
  when "DATUM"
    name = args.shift(QuotedString)
    ellipsoid = args.find_first(Ellipsoid)
    to_wgs84 = args.find_first(WGS84ConversionInfo)
    obj = HorizontalDatum.create(name, HD_GEOCENTRIC, ellipsoid, to_wgs84, *args.create_optionals)
  when "VERT_DATUM"
    obj = VerticalDatum.create(args.shift(QuotedString), args.shift(Numeric), *args.create_optionals)
  when "LOCAL_DATUM"
    obj = LocalDatum.create(args.shift(QuotedString), args.shift(Numeric), *args.create_optionals)
  when "CS"
    # Not actually valid WKT, but necessary to load and dump factories
    # with placeholder coord_sys objects
    defn = args.shift(QuotedString)
    dim = args.shift(Float).to_i
    optionals = args.create_optionals
    obj = CoordinateSystem.create(defn, dim, *optionals)
  when "COMPD_CS"
    obj = CompoundCoordinateSystem.create(
      args.shift(QuotedString),
      args.shift(CoordinateSystem),
      args.shift(CoordinateSystem),
      *args.create_optionals
    )
  when "LOCAL_CS"
    name = args.shift(QuotedString)
    local_datum = args.find_first(LocalDatum)
    unit = args.find_first(Unit)
    axes = args.find_all(AxisInfo)
    raise Error::ParseError, "Expected at least one AXIS in a LOCAL_CS" unless axes.size > 0
    obj = LocalCoordinateSystem.create(name, local_datum, unit, axes, *args.create_optionals)
  when "GEOCCS"
    name = args.shift(QuotedString)
    horizontal_datum = args.find_first(HorizontalDatum)
    prime_meridian = args.find_first(PrimeMeridian)
    linear_unit = args.find_first(LinearUnit)
    axes = args.find_all(AxisInfo)

    unless axes.size == 0 || axes.size == 3
      raise Error::ParseError, "GEOCCS must contain either 0 or 3 AXIS parameters"
    end

    obj = GeocentricCoordinateSystem.create(
      name,
      horizontal_datum,
      prime_meridian,
      linear_unit,
      axes[0],
      axes[1],
      axes[2],
      *args.create_optionals
    )
  when "VERT_CS"
    name = args.shift(QuotedString)
    vertical_datum = args.find_first(VerticalDatum)
    linear_unit = args.find_first(LinearUnit)
    axis = args.find_first(AxisInfo)
    obj = VerticalCoordinateSystem.create(name, vertical_datum, linear_unit, axis, *args.create_optionals)
  when "GEOGCS"
    name = args.shift(QuotedString)
    horizontal_datum = args.find_first(HorizontalDatum)
    prime_meridian = args.find_first(PrimeMeridian)
    angular_unit = args.find_first(AngularUnit)
    axes = args.find_all(AxisInfo)

    unless axes.size == 0 || axes.size == 2
      raise Error::ParseError, "GEOGCS must contain either 0 or 2 AXIS parameters"
    end

    obj = GeographicCoordinateSystem.create(
      name,
      angular_unit,
      horizontal_datum,
      prime_meridian,
      axes[0],
      axes[1],
      *args.create_optionals
    )
  when "PROJCS"
    name = args.shift(QuotedString)
    geographic_coordinate_system = args.find_first(GeographicCoordinateSystem)
    projection = args.find_first(Projection)
    parameters = args.find_all(ProjectionParameter)
    projection.instance_variable_get(:@parameters).concat(parameters)
    linear_unit = args.find_first(LinearUnit)
    axes = args.find_all(AxisInfo)

    unless axes.size == 0 || axes.size == 2
      raise Error::ParseError, "PROJCS must contain either 0 or 2 AXIS parameters"
    end

    obj = ProjectedCoordinateSystem.create(
      name,
      geographic_coordinate_system,
      projection,
      linear_unit,
      axes[0],
      axes[1],
      *args.create_optionals
    )
  else
    raise Error::ParseError, "Unrecognized type: #{type}"
  end
  args.assert_empty
  obj
end