Module: Terraformer::Bounds

Defined in:
lib/terraformer/bounds.rb

Class Method Summary collapse

Class Method Details

.bounds(obj, format = :bbox) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/terraformer/bounds.rb', line 6

def bounds obj, format = :bbox

  obj = Terraformer.parse obj unless Geometry === obj

  bbox = case obj
         when Point
           [ obj.coordinates[0], obj.coordinates[1],
             obj.coordinates[0], obj.coordinates[1] ]
         when MultiPoint, LineString,
              MultiLineString, Polygon,
              MultiPolygon
           bounds_for_array obj.coordinates
         when Feature
           obj.geometry ? bounds(obj.geometry) : nil
         when FeatureCollection
           bounds_for_feature_collection obj
         when GeometryCollection
           bounds_for_geometry_collection obj
         else
           raise ArgumentError.new 'unknown type: ' + obj.type
         end

  case format
  when :bbox
    bbox
  when :polygon
    Polygon.new [[bbox[0], bbox[1]],
                 [bbox[0], bbox[3]],
                 [bbox[2], bbox[3]],
                 [bbox[2], bbox[1]],
                 [bbox[0], bbox[1]]]
  end
end

.bounds_for_array(array, box = Array.new(4)) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/terraformer/bounds.rb', line 42

def bounds_for_array array, box = Array.new(4)
  array.reduce box do |b, a|
    case a
    when Coordinate
      set = ->(d, i, t){ b[i] = d if b[i].nil? or d.send(t, b[i])}
      set[a[0], X1, :<]
      set[a[0], X2, :>]
      set[a[1], Y1, :<]
      set[a[1], Y2, :>]
      b
    else
      bounds_for_array a, box
    end
  end
end

.bounds_for_collection(collection) ⇒ Object



66
67
68
# File 'lib/terraformer/bounds.rb', line 66

def bounds_for_collection collection
  bounds_for_array collection.map {|e| bounds(block_given? ? yield(e) : e)}
end

.bounds_for_feature_collection(fc) ⇒ Object



58
59
60
# File 'lib/terraformer/bounds.rb', line 58

def bounds_for_feature_collection fc
  bounds_for_collection fc.features, &:geometry
end

.bounds_for_geometry_collection(gc) ⇒ Object



62
63
64
# File 'lib/terraformer/bounds.rb', line 62

def bounds_for_geometry_collection gc
  bounds_for_collection gc
end

.envelope(geometry) ⇒ Object



71
72
73
74
75
76
77
78
79
# File 'lib/terraformer/bounds.rb', line 71

def envelope geometry
  b = bounds geometry
  {
    x: b[0],
    y: b[1],
    w: (b[0] - b[2]).abs,
    h: (b[1] - b[3]).abs
  }
end