Class: RGeo::Geographic::Factory

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

Overview

This class implements the various factories for geography features. See methods of the RGeo::Geographic module for the API for creating geography factories.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(impl_prefix_, opts_ = {}) ⇒ Factory

:nodoc:



16
17
18
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
# File 'lib/rgeo/geographic/factory.rb', line 16

def initialize(impl_prefix_, opts_ = {}) # :nodoc:
  @impl_prefix = impl_prefix_
  @point_class = Geographic.const_get("#{impl_prefix_}PointImpl")
  @line_string_class = Geographic.const_get("#{impl_prefix_}LineStringImpl")
  @linear_ring_class = Geographic.const_get("#{impl_prefix_}LinearRingImpl")
  @line_class = Geographic.const_get("#{impl_prefix_}LineImpl")
  @polygon_class = Geographic.const_get("#{impl_prefix_}PolygonImpl")
  @geometry_collection_class = Geographic.const_get("#{impl_prefix_}GeometryCollectionImpl")
  @multi_point_class = Geographic.const_get("#{impl_prefix_}MultiPointImpl")
  @multi_line_string_class = Geographic.const_get("#{impl_prefix_}MultiLineStringImpl")
  @multi_polygon_class = Geographic.const_get("#{impl_prefix_}MultiPolygonImpl")
  @support_z = opts_[:has_z_coordinate] ? true : false
  @support_m = opts_[:has_m_coordinate] ? true : false
  @srid = (opts_[:srid] || 4326).to_i
  @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
  @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
  @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
  @projector = nil
end

Instance Attribute Details

#coord_sysObject (readonly)

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



407
408
409
# File 'lib/rgeo/geographic/factory.rb', line 407

def coord_sys
  @coord_sys
end

#proj4Object (readonly)

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



403
404
405
# File 'lib/rgeo/geographic/factory.rb', line 403

def proj4
  @proj4
end

#sridObject (readonly)

Returns the srid reported by this factory.



233
234
235
# File 'lib/rgeo/geographic/factory.rb', line 233

def srid
  @srid
end

Instance Method Details

#_generate_wkb(obj_) ⇒ Object

:nodoc:



413
414
415
# File 'lib/rgeo/geographic/factory.rb', line 413

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

#_generate_wkt(obj_) ⇒ Object

:nodoc:



409
410
411
# File 'lib/rgeo/geographic/factory.rb', line 409

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

#_marshal_wkb_generatorObject

:nodoc:



417
418
419
420
421
422
423
# File 'lib/rgeo/geographic/factory.rb', line 417

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:



425
426
427
428
429
430
431
# File 'lib/rgeo/geographic/factory.rb', line 425

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:



433
434
435
436
437
438
439
# File 'lib/rgeo/geographic/factory.rb', line 433

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:



441
442
443
444
445
446
447
# File 'lib/rgeo/geographic/factory.rb', line 441

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

#_set_projector(projector_) ⇒ Object

:nodoc:



81
82
83
# File 'lib/rgeo/geographic/factory.rb', line 81

def _set_projector(projector_) # :nodoc:
  @projector = projector_
end

#collection(elems_) ⇒ Object

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



371
372
373
374
375
# File 'lib/rgeo/geographic/factory.rb', line 371

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

#encode_with(coder_) ⇒ Object

Psych support



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/rgeo/geographic/factory.rb', line 167

def encode_with(coder_) # :nodoc:
  coder_["impl_prefix"] = @impl_prefix
  coder_["has_z_coordinate"] = @support_z
  coder_["has_m_coordinate"] = @support_m
  coder_["srid"] = @srid
  coder_["wkt_generator"] = @wkt_generator._properties
  coder_["wkb_generator"] = @wkb_generator._properties
  coder_["wkt_parser"] = @wkt_parser._properties
  coder_["wkb_parser"] = @wkb_parser._properties
  coder_["lenient_assertions"] = @lenient_assertions
  coder_["buffer_resolution"] = @buffer_resolution
  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
  if @projector
    coder_["projector_class"] = @projector.class.name.sub(/.*::/, "")
    coder_["projection_factory"] = @projector.projection_factory
  end
end

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

Equivalence test.

Returns:

  • (Boolean)


87
88
89
90
91
92
93
# File 'lib/rgeo/geographic/factory.rb', line 87

def eql?(rhs_)
  rhs_.is_a?(Geographic::Factory) &&
    @impl_prefix == rhs_.instance_variable_get(:@impl_prefix) &&
    @support_z == rhs_.instance_variable_get(:@support_z) &&
    @support_m == rhs_.instance_variable_get(:@support_m) &&
    @proj4 == rhs_.instance_variable_get(:@proj4)
end

#has_projection?Boolean

Returns true if this factory supports a projection.

Returns:

  • (Boolean)


237
238
239
# File 'lib/rgeo/geographic/factory.rb', line 237

def has_projection?
  !@projector.nil?
end

#hashObject

Standard hash code



98
99
100
# File 'lib/rgeo/geographic/factory.rb', line 98

def hash
  @hash ||= [@impl_prefix, @support_z, @support_m, @proj4].hash
end

#init_with(coder_) ⇒ Object

:nodoc:



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/rgeo/geographic/factory.rb', line 189

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(coder_["impl_prefix"],
    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_
            )
  if (projklass_ = coder_["projector_class"]) && (projfactory_ = coder_["projection_factory"])
    klass_ = begin
               ::RGeo::Geographic.const_get(projklass_)
             rescue
               nil
             end
    if klass_
      projector_ = klass_.allocate
      projector_._set_factories(self, projfactory_)
      _set_projector(projector_)
    end
  end
end

#line(start_, end_) ⇒ Object

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



347
348
349
350
351
# File 'lib/rgeo/geographic/factory.rb', line 347

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

#line_string(points_) ⇒ Object

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



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

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

#linear_ring(points_) ⇒ Object

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



355
356
357
358
359
# File 'lib/rgeo/geographic/factory.rb', line 355

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

#marshal_dumpObject

Marshal support



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/rgeo/geographic/factory.rb', line 104

def marshal_dump # :nodoc:
  hash_ = {
    "pref" => @impl_prefix,
    "hasz" => @support_z,
    "hasm" => @support_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
  if @projector
    hash_["prjc"] = @projector.class.name.sub(/.*::/, "")
    hash_["prjf"] = @projector.projection_factory
  end
  hash_
end

#marshal_load(data_) ⇒ Object

:nodoc:



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/rgeo/geographic/factory.rb', line 126

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(data_["pref"],
    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_
            )
  if (projklass_ = data_["prjc"]) && (projfactory_ = data_["prjf"])
    klass_ = begin
               ::RGeo::Geographic.const_get(projklass_)
             rescue
               nil
             end
    if klass_
      projector_ = klass_.allocate
      projector_._set_factories(self, projfactory_)
      _set_projector(projector_)
    end
  end
end

#multi_line_string(elems_) ⇒ Object

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



387
388
389
390
391
# File 'lib/rgeo/geographic/factory.rb', line 387

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

#multi_point(elems_) ⇒ Object

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



379
380
381
382
383
# File 'lib/rgeo/geographic/factory.rb', line 379

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

#multi_polygon(elems_) ⇒ Object

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



395
396
397
398
399
# File 'lib/rgeo/geographic/factory.rb', line 395

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

#parse_wkb(str_) ⇒ Object

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



325
326
327
# File 'lib/rgeo/geographic/factory.rb', line 325

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

#parse_wkt(str_) ⇒ Object

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



319
320
321
# File 'lib/rgeo/geographic/factory.rb', line 319

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

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

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



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

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

#polygon(outer_ring_, inner_rings_ = nil) ⇒ Object

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



363
364
365
366
367
# File 'lib/rgeo/geographic/factory.rb', line 363

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

#project(geometry_) ⇒ Object

Projects the given geometry into the projected coordinate space, and returns the projected geometry. Returns nil if this factory does not support a projection. Raises Error::InvalidGeometry if the given geometry is not of this factory.



254
255
256
257
258
259
260
# File 'lib/rgeo/geographic/factory.rb', line 254

def project(geometry_)
  return nil unless @projector && geometry_
  unless geometry_.factory == self
    raise Error::InvalidGeometry, "Wrong geometry type"
  end
  @projector.project(geometry_)
end

#projection_factoryObject

Returns the factory for the projected coordinate space, or nil if this factory does not support a projection.



244
245
246
# File 'lib/rgeo/geographic/factory.rb', line 244

def projection_factory
  @projector ? @projector.projection_factory : nil
end

#projection_limits_windowObject

Returns a ProjectedWindow specifying the limits of the domain of the projection space. Returns nil if this factory does not support a projection, or the projection limits are not known.



291
292
293
294
295
296
297
298
# File 'lib/rgeo/geographic/factory.rb', line 291

def projection_limits_window
  if @projector
    unless defined?(@projection_limits_window)
      @projection_limits_window = @projector.limits_window
    end
    @projection_limits_window
  end
end

#projection_wraps?Boolean

Returns true if this factory supports a projection and the projection wraps its x (easting) direction. For example, a Mercator projection wraps, but a local projection that is valid only for a small area does not wrap. Returns nil if this factory does not support or a projection, or if it is not known whether or not it wraps.

Returns:

  • (Boolean)


282
283
284
# File 'lib/rgeo/geographic/factory.rb', line 282

def projection_wraps?
  @projector ? @projector.wraps? : nil
end

#property(name_) ⇒ Object

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



302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'lib/rgeo/geographic/factory.rb', line 302

def property(name_)
  case name_
  when :has_z_coordinate
    @support_z
  when :has_m_coordinate
    @support_m
  when :uses_lenient_assertions
    @lenient_assertions
  when :buffer_resolution
    @buffer_resolution
  when :is_geographic
    true
  end
end

#unproject(geometry_) ⇒ Object

Reverse-projects the given geometry from the projected coordinate space into lat-long space. Raises Error::InvalidGeometry if the given geometry is not of the projection defined by this factory.



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

def unproject(geometry_)
  return nil unless geometry_
  unless @projector && @projector.projection_factory == geometry_.factory
    raise Error::InvalidGeometry, "You can unproject only features that are in the projected coordinate space."
  end
  @projector.unproject(geometry_)
end