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.


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

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_caseObject (readonly)

Returns the case for output. See WKTGenerator for details.


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

def convert_case
  @convert_case
end

#tag_formatObject (readonly) Also known as: type_format

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


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

def tag_format
  @tag_format
end

Instance Method Details

#emit_ewkt_srid?Boolean

Returns whether SRID is embedded. See WKTGenerator for details.

Returns:

  • (Boolean)

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

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.


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

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

#propertiesObject


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

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)

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

def square_brackets?
  @square_brackets
end