Class: RGeo::WKRep::WKBParser

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

Overview

This class provides the functionality of parsing a geometry from WKB (well-known binary) format. You may also customize the parser to recognize PostGIS EWKB extensions to the input, or 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 parse method.

Configuration options

You must provide each parser with an RGeo::Feature::FactoryGenerator. It should understand the configuration options :srid, :has_z_coordinate, and :has_m_coordinate. You may also pass a specific RGeo::Feature::Factory, or nil to specify the default Cartesian FactoryGenerator.

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

:support_ewkb

Activate support for PostGIS EWKB type codes, which use high order bits in the type code to signal the presence of Z, M, and SRID values in the data. Default is false.

:support_wkb12

Activate support for SFS 1.2 extensions to the type codes, which use values greater than 1000 to signal the presence of Z and M values in the data. SFS 1.2 types such as triangle, tin, and polyhedralsurface are NOT yet supported. Default is false.

:ignore_extra_bytes

If true, extra bytes at the end of the data are ignored. If false (the default), extra bytes will trigger a parse error.

:default_srid

A SRID to pass to the factory generator if no SRID is present in the input. Defaults to nil (i.e. don’t specify a SRID).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(factory_generator = nil, opts = {}) ⇒ WKBParser

Create and configure a WKB parser. See the WKBParser documentation for the options that can be passed.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/rgeo/wkrep/wkb_parser.rb', line 49

def initialize(factory_generator = nil, opts = {})
  if factory_generator.is_a?(Feature::Factory::Instance)
    @factory_generator = Feature::FactoryGenerator.single(factory_generator)
    @exact_factory = factory_generator
  elsif factory_generator.respond_to?(:call)
    @factory_generator = factory_generator
    @exact_factory = nil
  else
    @factory_generator = Cartesian.method(:preferred_factory)
    @exact_factory = nil
  end
  @support_ewkb = opts[:support_ewkb] ? true : false
  @support_wkb12 = opts[:support_wkb12] ? true : false
  @ignore_extra_bytes = opts[:ignore_extra_bytes] ? true : false
  @default_srid = opts[:default_srid]
  @mutex = Mutex.new
end

Instance Attribute Details

#exact_factoryObject (readonly)

If this parser was given an exact factory, returns it; otherwise returns nil.



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

def exact_factory
  @exact_factory
end

#factory_generatorObject (readonly)

Returns the factory generator. See WKBParser for details.



68
69
70
# File 'lib/rgeo/wkrep/wkb_parser.rb', line 68

def factory_generator
  @factory_generator
end

Instance Method Details

#ignore_extra_bytes?Boolean

Returns true if this parser ignores extra bytes. See WKBParser for details.

Returns:

  • (Boolean)


88
89
90
# File 'lib/rgeo/wkrep/wkb_parser.rb', line 88

def ignore_extra_bytes?
  @ignore_extra_bytes
end

#parse(data) ⇒ Object Also known as: parse_hex

Parse the given binary data or hexadecimal string, and return a geometry object.

The #parse_hex method is a synonym, present for historical reasons but deprecated. Use #parse instead.



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/rgeo/wkrep/wkb_parser.rb', line 107

def parse(data)
  @mutex.synchronize do
    data = [data].pack("H*") if data[0, 1] =~ /[0-9a-fA-F]/
    @cur_has_z = nil
    @cur_has_m = nil
    @cur_srid = nil
    @cur_dims = 2
    @cur_factory = nil
    begin
      start_scanner(data)
      obj = parse_object(false)
      unless @ignore_extra_bytes
        bytes = bytes_remaining
        raise Error::ParseError, "Found #{bytes} extra bytes at the end of the stream." if bytes > 0
      end
    ensure
      @data = nil
    end
    obj
  end
end

#propertiesObject



92
93
94
95
96
97
98
99
# File 'lib/rgeo/wkrep/wkb_parser.rb', line 92

def properties
  {
    "support_ewkb" => @support_ewkb,
    "support_wkb12" => @support_wkb12,
    "ignore_extra_bytes" => @ignore_extra_bytes,
    "default_srid" => @default_srid
  }
end

#support_ewkb?Boolean

Returns true if this parser supports EWKB. See WKBParser for details.

Returns:

  • (Boolean)


76
77
78
# File 'lib/rgeo/wkrep/wkb_parser.rb', line 76

def support_ewkb?
  @support_ewkb
end

#support_wkb12?Boolean

Returns true if this parser supports SFS 1.2 extensions. See WKBParser for details.

Returns:

  • (Boolean)


82
83
84
# File 'lib/rgeo/wkrep/wkb_parser.rb', line 82

def support_wkb12?
  @support_wkb12
end