Class: RGeo::WKRep::WKBGenerator

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

Overview

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

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.

:type_format

The format for type codes. Possible values are :wkb11, indicating SFS 1.1 WKB (i.e. no Z or M values); :ewkb, indicating the PostGIS EWKB extensions (i.e. Z and M presence flagged by the two high bits of the type code, and support for embedded SRID); or :wkb12 (indicating SFS 1.2 WKB (i.e. Z and M presence flagged by adding 1000 and/or 2000 to the type code.) Default is :wkb11.

:emit_ewkb_srid

If true, embed the SRID in the toplevel geometry. Available only if :type_format is :ewkb. Default is false.

:hex_format

If true, output a hex string instead of a byte string. Default is false.

:little_endian

If true, output little endian (NDR) byte order. If false, output big endian (XDR), or network byte order. Default is false.

Constant Summary collapse

TYPE_CODES =

:stopdoc:

{
  Feature::Point => 1,
  Feature::LineString => 2,
  Feature::LinearRing => 2,
  Feature::Line => 2,
  Feature::Polygon => 3,
  Feature::MultiPoint => 4,
  Feature::MultiLineString => 5,
  Feature::MultiPolygon => 6,
  Feature::GeometryCollection => 7
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ WKBGenerator

Create and configure a WKB generator. See the WKBGenerator documentation for the options that can be passed.


61
62
63
64
65
66
# File 'lib/rgeo/wkrep/wkb_generator.rb', line 61

def initialize(opts = {})
  @type_format = opts[:type_format] || :wkb11
  @emit_ewkb_srid = @type_format == :ewkb && opts[:emit_ewkb_srid]
  @hex_format = opts[:hex_format] ? true : false
  @little_endian = opts[:little_endian] ? true : false
end

Instance Attribute Details

#type_formatObject (readonly)

Returns the format for type codes. See WKBGenerator for details.


69
70
71
# File 'lib/rgeo/wkrep/wkb_generator.rb', line 69

def type_format
  @type_format
end

Instance Method Details

#emit_ewkb_srid?Boolean

Returns whether SRID is embedded. See WKBGenerator for details.

Returns:

  • (Boolean)

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

def emit_ewkb_srid?
  @emit_ewkb_srid
end

#generate(obj) ⇒ Object

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


100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/rgeo/wkrep/wkb_generator.rb', line 100

def generate(obj)
  factory = obj.factory
  if @type_format == :ewkb || @type_format == :wkb12
    @cur_has_z = factory.property(:has_z_coordinate)
    @cur_has_m = factory.property(:has_m_coordinate)
  else
    @cur_has_z = nil
    @cur_has_m = nil
  end
  @cur_dims = 2 + (@cur_has_z ? 1 : 0) + (@cur_has_m ? 1 : 0)
  start_emitter
  generate_feature(obj, true)
  finish_emitter
end

#hex_format?Boolean

Returns whether output is converted to hex. See WKBGenerator for details.

Returns:

  • (Boolean)

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

def hex_format?
  @hex_format
end

#little_endian?Boolean

Returns whether output is little-endian (NDR). See WKBGenerator for details.

Returns:

  • (Boolean)

84
85
86
# File 'lib/rgeo/wkrep/wkb_generator.rb', line 84

def little_endian?
  @little_endian
end

#propertiesObject


88
89
90
91
92
93
94
95
# File 'lib/rgeo/wkrep/wkb_generator.rb', line 88

def properties
  {
    "type_format" => @type_format.to_s,
    "emit_ewkb_srid" => @emit_ewkb_srid,
    "hex_format" => @hex_format,
    "little_endian" => @little_endian
  }
end