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.



11
12
13
14
# File 'lib/rgeo/coord_sys/cs/wkt_parser.rb', line 11

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

Instance Attribute Details

#cur_tokenObject (readonly)

Returns the value of attribute cur_token.



181
182
183
# File 'lib/rgeo/coord_sys/cs/wkt_parser.rb', line 181

def cur_token
  @cur_token
end

Instance Method Details

#consume_token_type(type_) ⇒ Object

:nodoc:



136
137
138
139
140
141
# File 'lib/rgeo/coord_sys/cs/wkt_parser.rb', line 136

def consume_token_type(type_) # :nodoc:
  expect_token_type(type_)
  tok_ = @cur_token
  next_token
  tok_
end

#expect_token_type(type_) ⇒ Object

:nodoc:



143
144
145
146
147
# File 'lib/rgeo/coord_sys/cs/wkt_parser.rb', line 143

def expect_token_type(type_) # :nodoc:
  unless type_ === @cur_token
    raise Error::ParseError, "#{type_.inspect} expected but #{@cur_token.inspect} found."
  end
end

#next_tokenObject

:nodoc:



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
# File 'lib/rgeo/coord_sys/cs/wkt_parser.rb', line 149

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
    if token_ =~ /^[-+]?(\d+(\.\d*)?|\.\d+)(e[-+]?\d+)?$/
      @cur_token = token_.to_f
    else
      raise Error::ParseError, "Bad token: #{token_.inspect}"
    end
  end
  @cur_token
end

#parse(containing_type_ = nil) ⇒ Object

:nodoc:



16
17
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
# File 'lib/rgeo/coord_sys/cs/wkt_parser.rb', line 16

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_token_type(:begin)
  args_ = ArgumentList.new
  args_ << parse(type_)
  loop do
    break unless @cur_token == :comma
    next_token
    args_ << parse(type_)
  end
  consume_token_type(: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"
    case containing_type_
    when "GEOCCS", "VERT_CS", "PROJCS", "SPHEROID"
      klass_ = LinearUnit
    when "GEOGCS"
      klass_ = AngularUnit
    else
      klass_ = 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 "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)
    unless axes_.size > 0
      raise Error::ParseError("Expected at least one AXIS in a LOCAL_CS")
    end
    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