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 Attribute Summary collapse

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.



52
53
54
55
56
57
58
59
60
# File 'lib/rgeo/wkrep/wkt_generator.rb', line 52

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

Instance Attribute Details

#convert_caseObject (readonly)

Returns the case for output. See WKTGenerator for details.



78
79
80
# File 'lib/rgeo/wkrep/wkt_generator.rb', line 78

def convert_case
  @convert_case
end

#tag_formatObject (readonly) Also known as: type_format

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



63
64
65
# File 'lib/rgeo/wkrep/wkt_generator.rb', line 63

def tag_format
  @tag_format
end

Instance Method Details

#emit_ewkt_srid?Boolean

Returns whether SRID is embedded. See WKTGenerator for details.

Returns:

  • (Boolean)


67
68
69
# File 'lib/rgeo/wkrep/wkt_generator.rb', line 67

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.



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/rgeo/wkrep/wkt_generator.rb', line 92

def generate(obj)
  @begin_bracket = @square_brackets ? "[" : "("
  @end_bracket = @square_brackets ? "]" : ")"
  factory = obj.factory
  if @tag_format == :wkt11_strict
    support_z = false
    support_m = false
  else
    support_z = factory.property(:has_z_coordinate)
    support_m = factory.property(:has_m_coordinate)
  end
  str = generate_feature(obj, support_z, support_m, toplevel: true)
  case @convert_case
  when :upper
    str.upcase
  when :lower
    str.downcase
  else
    str
  end
end

#propertiesObject



80
81
82
83
84
85
86
87
# File 'lib/rgeo/wkrep/wkt_generator.rb', line 80

def properties
  {
    "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

#square_brackets?Boolean

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

Returns:

  • (Boolean)


73
74
75
# File 'lib/rgeo/wkrep/wkt_generator.rb', line 73

def square_brackets?
  @square_brackets
end