Class: RGeo::WKRep::WKTGenerator

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

Overview

This class provides the functionality of serializing a geometry as WKT (well-known text) format. You may also customize the serializer to generate PostGIS EWKT extensions to the output, or to follow the Simple Features Specification 1.2 extensions for Z and M.

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

Configuration options

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

:tag_format

The format for tags. Possible values are :wkt11, indicating SFS 1.1 WKT (i.e. no Z or M markers in the tags) but with Z and/or M values added in if they are present; :wkt11_strict, indicating SFS 1.1 WKT with Z and M dropped from the output (since WKT strictly does not support the Z or M dimensions); :ewkt, indicating the PostGIS EWKT extensions (i.e. “M” appended to tag names if M but not Z is present); or :wkt12, indicating SFS 1.2 WKT tags that indicate the presence of Z and M in a separate token. Default is :wkt11. This option can also be specified as :type_format.

:emit_ewkt_srid

If true, embed the SRID of the toplevel geometry. Available only if :tag_format is :ewkt. Default is false.

:square_brackets

If true, uses square brackets rather than parentheses. Default is false.

:convert_case

Possible values are :upper, which changes all letters in the output to ALL CAPS; :lower, which changes all letters to lower case; or nil, indicating no case changes from the default (which is not specified exactly, but is chosen by the generator to emphasize readability.) Default is nil.

Instance Method Summary collapse

Constructor Details

#initialize(opts_ = {}) ⇒ WKTGenerator

Create and configure a WKT generator. See the WKTGenerator documentation for the options that can be passed.


86
87
88
89
90
91
92
# File 'lib/rgeo/wkrep/wkt_generator.rb', line 86

def initialize(opts_={})
  @tag_format = opts_[:tag_format] || opts_[:type_format] || :wkt11
  @emit_ewkt_srid = @tag_format == :ewkt ?
    (opts_[:emit_ewkt_srid] ? true : false) : nil
  @square_brackets = opts_[:square_brackets] ? true : false
  @convert_case = opts_[:convert_case]
end

Instance Method Details

#_generate_coords(obj_) ⇒ Object

:nodoc:


195
196
197
198
199
200
# File 'lib/rgeo/wkrep/wkt_generator.rb', line 195

def _generate_coords(obj_)  # :nodoc:
  str_ = "#{obj_.x.to_s} #{obj_.y.to_s}"
  str_ << " #{obj_.z.to_s}" if @cur_support_z
  str_ << " #{obj_.m.to_s}" if @cur_support_m
  str_
end

#_generate_feature(obj_, toplevel_ = false) ⇒ Object

:nodoc:


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
188
189
190
191
192
# File 'lib/rgeo/wkrep/wkt_generator.rb', line 153

def _generate_feature(obj_, toplevel_=false)  # :nodoc:
  type_ = obj_.geometry_type
  type_ = Feature::LineString if type_.subtype_of?(Feature::LineString)
  tag_ = type_.type_name
  if @tag_format == :ewkt
    if @cur_support_m && !@cur_support_z
      tag_ << 'M'
    end
    if toplevel_ && @emit_ewkt_srid
      tag_ = "SRID=#{obj_.srid};#{tag_}"
    end
  elsif @tag_format == :wkt12
    if @cur_support_z
      if @cur_support_m
        tag_ << ' ZM'
      else
        tag_ << ' Z'
      end
    elsif @cur_support_m
      tag_ << ' M'
    end
  end
  if type_ == Feature::Point
    "#{tag_} #{_generate_point(obj_)}"
  elsif type_ == Feature::LineString
    "#{tag_} #{_generate_line_string(obj_)}"
  elsif type_ == Feature::Polygon
    "#{tag_} #{_generate_polygon(obj_)}"
  elsif type_ == Feature::GeometryCollection
    "#{tag_} #{_generate_geometry_collection(obj_)}"
  elsif type_ == Feature::MultiPoint
    "#{tag_} #{_generate_multi_point(obj_)}"
  elsif type_ == Feature::MultiLineString
    "#{tag_} #{_generate_multi_line_string(obj_)}"
  elsif type_ == Feature::MultiPolygon
    "#{tag_} #{_generate_multi_polygon(obj_)}"
  else
    raise Error::ParseError, "Unrecognized geometry type: #{type_}"
  end
end

#_generate_geometry_collection(obj_) ⇒ Object

:nodoc:


226
227
228
229
230
231
232
# File 'lib/rgeo/wkrep/wkt_generator.rb', line 226

def _generate_geometry_collection(obj_)  # :nodoc:
  if obj_.is_empty?
    'EMPTY'
  else
    "#{@begin_bracket}#{obj_.map{ |f_| _generate_feature(f_) }.join(', ')}#{@end_bracket}"
  end
end

#_generate_line_string(obj_) ⇒ Object

:nodoc:


208
209
210
211
212
213
214
# File 'lib/rgeo/wkrep/wkt_generator.rb', line 208

def _generate_line_string(obj_)  # :nodoc:
  if obj_.is_empty?
    'EMPTY'
  else
    "#{@begin_bracket}#{obj_.points.map{ |p_| _generate_coords(p_) }.join(', ')}#{@end_bracket}"
  end
end

#_generate_multi_line_string(obj_) ⇒ Object

:nodoc:


244
245
246
247
248
249
250
# File 'lib/rgeo/wkrep/wkt_generator.rb', line 244

def _generate_multi_line_string(obj_)  # :nodoc:
  if obj_.is_empty?
    'EMPTY'
  else
    "#{@begin_bracket}#{obj_.map{ |f_| _generate_line_string(f_) }.join(', ')}#{@end_bracket}"
  end
end

#_generate_multi_point(obj_) ⇒ Object

:nodoc:


235
236
237
238
239
240
241
# File 'lib/rgeo/wkrep/wkt_generator.rb', line 235

def _generate_multi_point(obj_)  # :nodoc:
  if obj_.is_empty?
    'EMPTY'
  else
    "#{@begin_bracket}#{obj_.map{ |f_| _generate_point(f_) }.join(', ')}#{@end_bracket}"
  end
end

#_generate_multi_polygon(obj_) ⇒ Object

:nodoc:


253
254
255
256
257
258
259
# File 'lib/rgeo/wkrep/wkt_generator.rb', line 253

def _generate_multi_polygon(obj_)  # :nodoc:
  if obj_.is_empty?
    'EMPTY'
  else
    "#{@begin_bracket}#{obj_.map{ |f_| _generate_polygon(f_) }.join(', ')}#{@end_bracket}"
  end
end

#_generate_point(obj_) ⇒ Object

:nodoc:


203
204
205
# File 'lib/rgeo/wkrep/wkt_generator.rb', line 203

def _generate_point(obj_)  # :nodoc:
  "#{@begin_bracket}#{_generate_coords(obj_)}#{@end_bracket}"
end

#_generate_polygon(obj_) ⇒ Object

:nodoc:


217
218
219
220
221
222
223
# File 'lib/rgeo/wkrep/wkt_generator.rb', line 217

def _generate_polygon(obj_)  # :nodoc:
  if obj_.is_empty?
    'EMPTY'
  else
    "#{@begin_bracket}#{([_generate_line_string(obj_.exterior_ring)] + obj_.interior_rings.map{ |r_| _generate_line_string(r_) }).join(', ')}#{@end_bracket}"
  end
end

#_propertiesObject

:nodoc:


118
119
120
121
122
123
124
125
# File 'lib/rgeo/wkrep/wkt_generator.rb', line 118

def _properties  # :nodoc:
  {
    'tag_format' => @tag_format.to_s,
    'emit_ewkt_srid' => @emit_ewkt_srid,
    'square_brackets' => @square_brackets,
    'convert_case' => @convert_case ? @convert_case.to_s : nil,
  }
end

#convert_caseObject

Returns the case for output. See WKTGenerator for details.


113
114
115
# File 'lib/rgeo/wkrep/wkt_generator.rb', line 113

def convert_case
  @convert_case
end

#emit_ewkt_srid?Boolean

Returns whether SRID is embedded. See WKTGenerator for details.

Returns:

  • (Boolean)

102
103
104
# File 'lib/rgeo/wkrep/wkt_generator.rb', line 102

def emit_ewkt_srid?
  @emit_ewkt_srid
end

#generate(obj_) ⇒ Object

Generate and return the WKT format for the given geometry object, according to the current settings.


131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/rgeo/wkrep/wkt_generator.rb', line 131

def generate(obj_)
  @begin_bracket = @square_brackets ? '[' : '('
  @end_bracket = @square_brackets ? ']' : ')'
  factory_ = obj_.factory
  if @tag_format == :wkt11_strict
    @cur_support_z = nil
    @cur_support_m = nil
  else
    @cur_support_z = factory_.property(:has_z_coordinate)
    @cur_support_m = factory_.property(:has_m_coordinate)
  end
  str_ = _generate_feature(obj_, true)
  if @convert_case == :upper
    str_.upcase
  elsif @convert_case == :lower
    str_.downcase
  else
    str_
  end
end

#square_brackets?Boolean

Returns whether square brackets rather than parens are output. See WKTGenerator for details.

Returns:

  • (Boolean)

108
109
110
# File 'lib/rgeo/wkrep/wkt_generator.rb', line 108

def square_brackets?
  @square_brackets
end

#tag_formatObject Also known as: type_format

Returns the format for type tags. See WKTGenerator for details.


96
97
98
# File 'lib/rgeo/wkrep/wkt_generator.rb', line 96

def tag_format
  @tag_format
end