Class: RGeo::Cartesian::Factory

Inherits:
Object
  • Object
show all
Includes:
Feature::Factory::Instance
Defined in:
lib/rgeo/cartesian/factory.rb

Overview

This class implements the factory for the simple cartesian implementation.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts_ = {}) ⇒ Factory

Create a new simple cartesian factory.

See ::RGeo::Cartesian.simple_factory for a list of supported options.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/rgeo/cartesian/factory.rb', line 19

def initialize(opts_ = {})
  @has_z = opts_[:has_z_coordinate] ? true : false
  @has_m = opts_[:has_m_coordinate] ? true : false
  @proj4 = opts_[:proj4]
  if CoordSys::Proj4.supported?
    if @proj4.is_a?(::String) || @proj4.is_a?(::Hash)
      @proj4 = CoordSys::Proj4.create(@proj4)
    end
  else
    @proj4 = nil
  end
  srid_ = opts_[:srid]
  @coord_sys = opts_[:coord_sys]
  if @coord_sys.is_a?(::String)
    @coord_sys = begin
                   CoordSys::CS.create_from_wkt(@coord_sys)
                 rescue
                   nil
                 end
  end
  if (!@proj4 || !@coord_sys) && srid_ && (db_ = opts_[:srs_database])
    entry_ = db_.get(srid_.to_i)
    if entry_
      @proj4 ||= entry_.proj4
      @coord_sys ||= entry_.coord_sys
    end
  end
  srid_ ||= @coord_sys.authority_code if @coord_sys
  @srid = srid_.to_i
  @lenient_assertions = opts_[:uses_lenient_assertions] ? true : false
  @buffer_resolution = opts_[:buffer_resolution].to_i
  @buffer_resolution = 1 if @buffer_resolution < 1

  wkt_generator_ = opts_[:wkt_generator]
  case wkt_generator_
  when ::Hash
    @wkt_generator = WKRep::WKTGenerator.new(wkt_generator_)
  else
    @wkt_generator = WKRep::WKTGenerator.new(convert_case: :upper)
  end
  wkb_generator_ = opts_[:wkb_generator]
  case wkb_generator_
  when ::Hash
    @wkb_generator = WKRep::WKBGenerator.new(wkb_generator_)
  else
    @wkb_generator = WKRep::WKBGenerator.new
  end
  wkt_parser_ = opts_[:wkt_parser]
  case wkt_parser_
  when ::Hash
    @wkt_parser = WKRep::WKTParser.new(self, wkt_parser_)
  else
    @wkt_parser = WKRep::WKTParser.new(self)
  end
  wkb_parser_ = opts_[:wkb_parser]
  case wkb_parser_
  when ::Hash
    @wkb_parser = WKRep::WKBParser.new(self, wkb_parser_)
  else
    @wkb_parser = WKRep::WKBParser.new(self)
  end
end

Instance Attribute Details

#coord_sysObject (readonly)

See ::RGeo::Feature::Factory#coord_sys



304
305
306
# File 'lib/rgeo/cartesian/factory.rb', line 304

def coord_sys
  @coord_sys
end

#proj4Object (readonly)

See ::RGeo::Feature::Factory#proj4



300
301
302
# File 'lib/rgeo/cartesian/factory.rb', line 300

def proj4
  @proj4
end

#sridObject (readonly)

Returns the SRID.



195
196
197
# File 'lib/rgeo/cartesian/factory.rb', line 195

def srid
  @srid
end

Instance Method Details

#_generate_wkb(obj_) ⇒ Object

:nodoc:



310
311
312
# File 'lib/rgeo/cartesian/factory.rb', line 310

def _generate_wkb(obj_)  # :nodoc:
  @wkb_generator.generate(obj_)
end

#_generate_wkt(obj_) ⇒ Object

:nodoc:



306
307
308
# File 'lib/rgeo/cartesian/factory.rb', line 306

def _generate_wkt(obj_)  # :nodoc:
  @wkt_generator.generate(obj_)
end

#_marshal_wkb_generatorObject

:nodoc:



314
315
316
317
318
319
320
# File 'lib/rgeo/cartesian/factory.rb', line 314

def _marshal_wkb_generator # :nodoc:
  unless defined?(@marshal_wkb_generator)
    @marshal_wkb_generator = ::RGeo::WKRep::WKBGenerator.new(
      type_format: :wkb12)
  end
  @marshal_wkb_generator
end

#_marshal_wkb_parserObject

:nodoc:



322
323
324
325
326
327
328
# File 'lib/rgeo/cartesian/factory.rb', line 322

def _marshal_wkb_parser # :nodoc:
  unless defined?(@marshal_wkb_parser)
    @marshal_wkb_parser = ::RGeo::WKRep::WKBParser.new(self,
      support_wkb12: true)
  end
  @marshal_wkb_parser
end

#_psych_wkt_generatorObject

:nodoc:



330
331
332
333
334
335
336
# File 'lib/rgeo/cartesian/factory.rb', line 330

def _psych_wkt_generator # :nodoc:
  unless defined?(@psych_wkt_generator)
    @psych_wkt_generator = ::RGeo::WKRep::WKTGenerator.new(
      tag_format: :wkt12)
  end
  @psych_wkt_generator
end

#_psych_wkt_parserObject

:nodoc:



338
339
340
341
342
343
344
# File 'lib/rgeo/cartesian/factory.rb', line 338

def _psych_wkt_parser # :nodoc:
  unless defined?(@psych_wkt_parser)
    @psych_wkt_parser = ::RGeo::WKRep::WKTParser.new(self,
      support_wkt12: true, support_ewkt: true)
  end
  @psych_wkt_parser
end

#collection(elems_) ⇒ Object

See ::RGeo::Feature::Factory#collection



268
269
270
271
272
# File 'lib/rgeo/cartesian/factory.rb', line 268

def collection(elems_)
  GeometryCollectionImpl.new(self, elems_)
rescue
  nil
end

#encode_with(coder_) ⇒ Object

Psych support



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/rgeo/cartesian/factory.rb', line 146

def encode_with(coder_) # :nodoc:
  coder_["has_z_coordinate"] = @has_z
  coder_["has_m_coordinate"] = @has_m
  coder_["srid"] = @srid
  coder_["lenient_assertions"] = @lenient_assertions
  coder_["buffer_resolution"] = @buffer_resolution
  coder_["wkt_generator"] = @wkt_generator._properties
  coder_["wkb_generator"] = @wkb_generator._properties
  coder_["wkt_parser"] = @wkt_parser._properties
  coder_["wkb_parser"] = @wkb_parser._properties
  if @proj4
    str_ = @proj4.original_str || @proj4.canonical_str
    coder_["proj4"] = @proj4.radians? ? { "proj4" => str_, "radians" => true } : str_
  end
  coder_["coord_sys"] = @coord_sys.to_wkt if @coord_sys
end

#eql?(rhs_) ⇒ Boolean Also known as: ==

Equivalence test.

Returns:

  • (Boolean)


84
85
86
87
88
89
# File 'lib/rgeo/cartesian/factory.rb', line 84

def eql?(rhs_)
  rhs_.is_a?(self.class) && @srid == rhs_.srid &&
    @has_z == rhs_.property(:has_z_coordinate) &&
    @has_m == rhs_.property(:has_m_coordinate) &&
    @proj4.eql?(rhs_.proj4)
end

#hashObject

Standard hash code



94
95
96
# File 'lib/rgeo/cartesian/factory.rb', line 94

def hash
  @hash ||= [@srid, @has_z, @has_m, @proj4].hash
end

#init_with(coder_) ⇒ Object

:nodoc:



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/rgeo/cartesian/factory.rb', line 163

def init_with(coder_) # :nodoc:
  if (proj4_data_ = coder_["proj4"])
    if proj4_data_.is_a?(::Hash)
      proj4_ = CoordSys::Proj4.create(proj4_data_["proj4"], radians: proj4_data_["radians"])
    else
      proj4_ = CoordSys::Proj4.create(proj4_data_.to_s)
    end
  else
    proj4_ = nil
  end
  if (coord_sys_data_ = coder_["cs"])
    coord_sys_ = CoordSys::CS.create_from_wkt(coord_sys_data_.to_s)
  else
    coord_sys_ = nil
  end
  initialize(
    has_z_coordinate: coder_["has_z_coordinate"],
    has_m_coordinate: coder_["has_m_coordinate"],
    srid: coder_["srid"],
    wkt_generator: ImplHelper::Utils.symbolize_hash(coder_["wkt_generator"]),
    wkb_generator: ImplHelper::Utils.symbolize_hash(coder_["wkb_generator"]),
    wkt_parser: ImplHelper::Utils.symbolize_hash(coder_["wkt_parser"]),
    wkb_parser: ImplHelper::Utils.symbolize_hash(coder_["wkb_parser"]),
    uses_lenient_assertions: coder_["lenient_assertions"],
    buffer_resolution: coder_["buffer_resolution"],
    proj4: proj4_,
    coord_sys: coord_sys_
  )
end

#line(start_, end_) ⇒ Object

See ::RGeo::Feature::Factory#line



244
245
246
247
248
# File 'lib/rgeo/cartesian/factory.rb', line 244

def line(start_, end_)
  LineImpl.new(self, start_, end_)
rescue
  nil
end

#line_string(points_) ⇒ Object

See ::RGeo::Feature::Factory#line_string



236
237
238
239
240
# File 'lib/rgeo/cartesian/factory.rb', line 236

def line_string(points_)
  LineStringImpl.new(self, points_)
rescue
  nil
end

#linear_ring(points_) ⇒ Object

See ::RGeo::Feature::Factory#linear_ring



252
253
254
255
256
# File 'lib/rgeo/cartesian/factory.rb', line 252

def linear_ring(points_)
  LinearRingImpl.new(self, points_)
rescue
  nil
end

#marshal_dumpObject

Marshal support



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

def marshal_dump # :nodoc:
  hash_ = {
    "hasz" => @has_z,
    "hasm" => @has_m,
    "srid" => @srid,
    "wktg" => @wkt_generator._properties,
    "wkbg" => @wkb_generator._properties,
    "wktp" => @wkt_parser._properties,
    "wkbp" => @wkb_parser._properties,
    "lena" => @lenient_assertions,
    "bufr" => @buffer_resolution
  }
  hash_["proj4"] = @proj4.marshal_dump if @proj4
  hash_["cs"] = @coord_sys.to_wkt if @coord_sys
  hash_
end

#marshal_load(data_) ⇒ Object

:nodoc:



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
# File 'lib/rgeo/cartesian/factory.rb', line 117

def marshal_load(data_) # :nodoc:
  if CoordSys::Proj4.supported? && (proj4_data_ = data_["proj4"])
    proj4_ = CoordSys::Proj4.allocate
    proj4_.marshal_load(proj4_data_)
  else
    proj4_ = nil
  end
  if (coord_sys_data_ = data_["cs"])
    coord_sys_ = CoordSys::CS.create_from_wkt(coord_sys_data_)
  else
    coord_sys_ = nil
  end
  initialize(
    has_z_coordinate: data_["hasz"],
    has_m_coordinate: data_["hasm"],
    srid: data_["srid"],
    wkt_generator: ImplHelper::Utils.symbolize_hash(data_["wktg"]),
    wkb_generator: ImplHelper::Utils.symbolize_hash(data_["wkbg"]),
    wkt_parser: ImplHelper::Utils.symbolize_hash(data_["wktp"]),
    wkb_parser: ImplHelper::Utils.symbolize_hash(data_["wkbp"]),
    uses_lenient_assertions: data_["lena"],
    buffer_resolution: data_["bufr"],
    proj4: proj4_,
    coord_sys: coord_sys_
  )
end

#multi_line_string(elems_) ⇒ Object

See ::RGeo::Feature::Factory#multi_line_string



284
285
286
287
288
# File 'lib/rgeo/cartesian/factory.rb', line 284

def multi_line_string(elems_)
  MultiLineStringImpl.new(self, elems_)
rescue
  nil
end

#multi_point(elems_) ⇒ Object

See ::RGeo::Feature::Factory#multi_point



276
277
278
279
280
# File 'lib/rgeo/cartesian/factory.rb', line 276

def multi_point(elems_)
  MultiPointImpl.new(self, elems_)
rescue
  nil
end

#multi_polygon(elems_) ⇒ Object

See ::RGeo::Feature::Factory#multi_polygon



292
293
294
295
296
# File 'lib/rgeo/cartesian/factory.rb', line 292

def multi_polygon(elems_)
  MultiPolygonImpl.new(self, elems_)
rescue
  nil
end

#parse_wkb(str_) ⇒ Object

See ::RGeo::Feature::Factory#parse_wkb



222
223
224
# File 'lib/rgeo/cartesian/factory.rb', line 222

def parse_wkb(str_)
  @wkb_parser.parse(str_)
end

#parse_wkt(str_) ⇒ Object

See ::RGeo::Feature::Factory#parse_wkt



216
217
218
# File 'lib/rgeo/cartesian/factory.rb', line 216

def parse_wkt(str_)
  @wkt_parser.parse(str_)
end

#point(x_, y_, *extra_) ⇒ Object

See ::RGeo::Feature::Factory#point



228
229
230
231
232
# File 'lib/rgeo/cartesian/factory.rb', line 228

def point(x_, y_, *extra_)
  PointImpl.new(self, x_, y_, *extra_)
rescue
  nil
end

#polygon(outer_ring_, inner_rings_ = nil) ⇒ Object

See ::RGeo::Feature::Factory#polygon



260
261
262
263
264
# File 'lib/rgeo/cartesian/factory.rb', line 260

def polygon(outer_ring_, inner_rings_ = nil)
  PolygonImpl.new(self, outer_ring_, inner_rings_)
rescue
  nil
end

#property(name_) ⇒ Object

See ::RGeo::Feature::Factory#property



199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/rgeo/cartesian/factory.rb', line 199

def property(name_)
  case name_
  when :has_z_coordinate
    @has_z
  when :has_m_coordinate
    @has_m
  when :uses_lenient_assertions
    @lenient_assertions
  when :buffer_resolution
    @buffer_resolution
  when :is_cartesian
    true
  end
end