Class: RGeo::WKRep::WKTGenerator
- Inherits:
-
Object
- Object
- RGeo::WKRep::WKTGenerator
- 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 Attribute Summary collapse
-
#convert_case ⇒ Object
readonly
Returns the case for output.
-
#tag_format ⇒ Object
(also: #type_format)
readonly
Returns the format for type tags.
Instance Method Summary collapse
-
#_generate_coords(obj_) ⇒ Object
:nodoc:.
-
#_generate_feature(obj_, toplevel_ = false) ⇒ Object
:nodoc:.
-
#_generate_geometry_collection(obj_) ⇒ Object
:nodoc:.
-
#_generate_line_string(obj_) ⇒ Object
:nodoc:.
-
#_generate_multi_line_string(obj_) ⇒ Object
:nodoc:.
-
#_generate_multi_point(obj_) ⇒ Object
:nodoc:.
-
#_generate_multi_polygon(obj_) ⇒ Object
:nodoc:.
-
#_generate_point(obj_) ⇒ Object
:nodoc:.
-
#_generate_polygon(obj_) ⇒ Object
:nodoc:.
-
#_properties ⇒ Object
:nodoc:.
-
#emit_ewkt_srid? ⇒ Boolean
Returns whether SRID is embedded.
-
#generate(obj_) ⇒ Object
Generate and return the WKT format for the given geometry object, according to the current settings.
-
#initialize(opts_ = {}) ⇒ WKTGenerator
constructor
Create and configure a WKT generator.
-
#square_brackets? ⇒ Boolean
Returns whether square brackets rather than parens are output.
Constructor Details
#initialize(opts_ = {}) ⇒ WKTGenerator
Create and configure a WKT generator. See the WKTGenerator documentation for the options that can be passed.
51 52 53 54 55 56 57 |
# File 'lib/rgeo/wkrep/wkt_generator.rb', line 51 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 Attribute Details
#convert_case ⇒ Object (readonly)
Returns the case for output. See WKTGenerator for details.
75 76 77 |
# File 'lib/rgeo/wkrep/wkt_generator.rb', line 75 def convert_case @convert_case end |
#tag_format ⇒ Object (readonly) Also known as: type_format
Returns the format for type tags. See WKTGenerator for details.
60 61 62 |
# File 'lib/rgeo/wkrep/wkt_generator.rb', line 60 def tag_format @tag_format end |
Instance Method Details
#_generate_coords(obj_) ⇒ Object
:nodoc:
147 148 149 150 151 152 |
# File 'lib/rgeo/wkrep/wkt_generator.rb', line 147 def _generate_coords(obj_) # :nodoc: str_ = "#{obj_.x} #{obj_.y}" str_ << " #{obj_.z}" if @cur_support_z str_ << " #{obj_.m}" if @cur_support_m str_ end |
#_generate_feature(obj_, toplevel_ = false) ⇒ Object
:nodoc:
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 |
# File 'lib/rgeo/wkrep/wkt_generator.rb', line 110 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 tag_ << "M" if @cur_support_m && !@cur_support_z tag_ = "SRID=#{obj_.srid};#{tag_}" if toplevel_ && @emit_ewkt_srid 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:
174 175 176 177 178 179 180 |
# File 'lib/rgeo/wkrep/wkt_generator.rb', line 174 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:
158 159 160 161 162 163 164 |
# File 'lib/rgeo/wkrep/wkt_generator.rb', line 158 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:
190 191 192 193 194 195 196 |
# File 'lib/rgeo/wkrep/wkt_generator.rb', line 190 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:
182 183 184 185 186 187 188 |
# File 'lib/rgeo/wkrep/wkt_generator.rb', line 182 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:
198 199 200 201 202 203 204 |
# File 'lib/rgeo/wkrep/wkt_generator.rb', line 198 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:
154 155 156 |
# File 'lib/rgeo/wkrep/wkt_generator.rb', line 154 def _generate_point(obj_) # :nodoc: "#{@begin_bracket}#{_generate_coords(obj_)}#{@end_bracket}" end |
#_generate_polygon(obj_) ⇒ Object
:nodoc:
166 167 168 169 170 171 172 |
# File 'lib/rgeo/wkrep/wkt_generator.rb', line 166 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 |
#_properties ⇒ Object
:nodoc:
77 78 79 80 81 82 83 84 |
# File 'lib/rgeo/wkrep/wkt_generator.rb', line 77 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 |
#emit_ewkt_srid? ⇒ Boolean
Returns whether SRID is embedded. See WKTGenerator for details.
64 65 66 |
# File 'lib/rgeo/wkrep/wkt_generator.rb', line 64 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.
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/rgeo/wkrep/wkt_generator.rb', line 89 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.
70 71 72 |
# File 'lib/rgeo/wkrep/wkt_generator.rb', line 70 def square_brackets? @square_brackets end |