Class: GeoRuby::SimpleFeatures::GeometryCollection

Inherits:
Geometry
  • Object
show all
Defined in:
lib/geo_ruby/simple_features/geometry_collection.rb

Overview

Represents a collection of arbitrary geometries

Direct Known Subclasses

MultiLineString, MultiPoint, MultiPolygon

Instance Attribute Summary collapse

Attributes inherited from Geometry

#srid, #with_m, #with_z

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Geometry

#as_ewkb, #as_ewkt, #as_georss, #as_hex_ewkb, #as_hex_wkb, #as_kml, #as_wkb, #as_wkt, #envelope, from_ewkb, from_ewkt, from_geojson, from_georss, from_georss_with_tags, from_hex_ewkb, from_kml, #to_json

Constructor Details

#initialize(srid = DEFAULT_SRID, with_z = false, with_m = false) ⇒ GeometryCollection

Returns a new instance of GeometryCollection.



9
10
11
12
# File 'lib/geo_ruby/simple_features/geometry_collection.rb', line 9

def initialize(srid = DEFAULT_SRID, with_z = false, with_m = false)
  super(srid, with_z, with_m)
  @geometries = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &b) ⇒ Object

Delegate the unknown methods to the geometries array



15
16
17
# File 'lib/geo_ruby/simple_features/geometry_collection.rb', line 15

def method_missing(method_name, *args, &b)
  @geometries.send(method_name, *args, &b)
end

Instance Attribute Details

#geometriesObject (readonly)

Returns the value of attribute geometries.



7
8
9
# File 'lib/geo_ruby/simple_features/geometry_collection.rb', line 7

def geometries
  @geometries
end

Class Method Details

.from_geometries(geometries, srid = DEFAULT_SRID, z = false, m = false) ⇒ Object

creates a new GeometryCollection from an array of geometries



136
137
138
139
140
# File 'lib/geo_ruby/simple_features/geometry_collection.rb', line 136

def self.from_geometries(geometries, srid = DEFAULT_SRID, z = false, m = false)
  geometry_collection = new(srid, z, m)
  geometry_collection.concat(geometries)
  geometry_collection
end

Instance Method Details

#==(other) ⇒ Object

tests the equality of geometry collections



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/geo_ruby/simple_features/geometry_collection.rb', line 67

def ==(other)
  if (other.class != self.class)
    false
  elsif length != other.length
    false
  else
    index = 0
    while index < length
      return false if self[index] != other[index]
      index += 1
    end
    true
  end
end

#as_json(_options = {}) ⇒ Object



108
109
110
# File 'lib/geo_ruby/simple_features/geometry_collection.rb', line 108

def as_json(_options = {})
  { type: 'GeometryCollection', geometries: geometries }
end

#binary_geometry_typeObject

WKB geometry type of the collection



92
93
94
# File 'lib/geo_ruby/simple_features/geometry_collection.rb', line 92

def binary_geometry_type #:nodoc:
  7
end

#binary_representation(allow_z = true, allow_m = true) ⇒ Object

Binary representation of the collection



83
84
85
86
87
88
89
# File 'lib/geo_ruby/simple_features/geometry_collection.rb', line 83

def binary_representation(allow_z = true, allow_m = true) #:nodoc:
  rep = [length].pack('V')
  # output the list of geometries without outputting the SRID first
  # and with the same setting regarding Z and M
  each { |geometry| rep << geometry.as_ewkb(false, allow_z, allow_m) }
  rep
end

#bounding_boxObject

Bounding box in 2D/3D. Returns an array of 2 points



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
# File 'lib/geo_ruby/simple_features/geometry_collection.rb', line 20

def bounding_box
  max_x, min_x, max_y, min_y = -Float::MAX, Float::MAX, -Float::MAX, Float::MAX
  if with_z
    max_z, min_z = -Float::MAX, Float::MAX
    each do |geometry|
      bbox = geometry.bounding_box
      sw = bbox[0]
      ne = bbox[1]

      max_y = ne.y if ne.y > max_y
      min_y = sw.y if sw.y < min_y
      max_x = ne.x if ne.x > max_x
      min_x = sw.x if sw.x < min_x
      max_z = ne.z if ne.z > max_z
      min_z = sw.z if sw.z < min_z
    end
    [Point.from_x_y_z(min_x, min_y, min_z), Point.from_x_y_z(max_x, max_y, max_z)]
  else
    each do |geometry|
      bbox = geometry.bounding_box
      sw = bbox[0]
      ne = bbox[1]

      max_y = ne.y if ne.y > max_y
      min_y = sw.y if sw.y < min_y
      max_x = ne.x if ne.x > max_x
      min_x = sw.x if sw.x < min_x
    end
    [Point.from_x_y(min_x, min_y), Point.from_x_y(max_x, max_y)]
  end
end

#georss_gml_representation(options) ⇒ Object

georss gml representation : outputs only the first geometry of the collection



121
122
123
# File 'lib/geo_ruby/simple_features/geometry_collection.rb', line 121

def georss_gml_representation(options) #:nodoc:
  self[0].georss_gml_representation(options)
end

#georss_simple_representation(options) ⇒ Object

georss simple representation : outputs only the first geometry of the collection



113
114
115
# File 'lib/geo_ruby/simple_features/geometry_collection.rb', line 113

def georss_simple_representation(options) #:nodoc:
  self[0].georss_simple_representation(options)
end

#georss_w3cgeo_representation(options) ⇒ Object

georss w3c representation : outputs the first point of the outer ring



117
118
119
# File 'lib/geo_ruby/simple_features/geometry_collection.rb', line 117

def georss_w3cgeo_representation(options) #:nodoc:
  self[0].georss_w3cgeo_representation(options)
end

#kml_representation(options = {}) ⇒ Object

outputs the geometry in kml format



126
127
128
129
130
131
132
133
# File 'lib/geo_ruby/simple_features/geometry_collection.rb', line 126

def kml_representation(options = {}) #:nodoc:
  result = "<MultiGeometry#{options[:id_attr]}>\n"
  options[:id_attr] = '' # the subgeometries do not have an ID
  each do |geometry|
    result += geometry.kml_representation(options)
  end
  result += "</MultiGeometry>\n"
end

#m_rangeObject



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/geo_ruby/simple_features/geometry_collection.rb', line 52

def m_range
  if with_m
    max_m, min_m = -Float::MAX, Float::MAX
    each do |lr|
      lrmr = lr.m_range
      max_m = lrmr[1] if lrmr[1] > max_m
      min_m = lrmr[0] if lrmr[0] < min_m
    end
    [min_m, max_m]
  else
    [0, 0]
  end
end

#text_geometry_typeObject

WKT geometry type



104
105
106
# File 'lib/geo_ruby/simple_features/geometry_collection.rb', line 104

def text_geometry_type #:nodoc:
  'GEOMETRYCOLLECTION'
end

#text_representation(allow_z = true, allow_m = true) ⇒ Object

Text representation of a geometry collection



97
98
99
100
101
# File 'lib/geo_ruby/simple_features/geometry_collection.rb', line 97

def text_representation(allow_z = true, allow_m = true) #:nodoc:
  @geometries.collect do |geometry|
    geometry.as_ewkt(false, allow_z, allow_m)
  end.join(',')
end