Module: Terraformer::Bounds
- Defined in:
- lib/terraformer/bounds.rb
Class Method Summary collapse
- .bounds(obj, format = :bbox) ⇒ Object
- .bounds_for_array(array, nesting = 0, box = Array.new(4)) ⇒ Object
- .bounds_for_collection(collection) ⇒ Object
- .bounds_for_feature_collection(fc) ⇒ Object
- .bounds_for_geometry_collection(gc) ⇒ Object
- .envelope(geometry) ⇒ Object
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 39 40 41 42 43 44 |
# File 'lib/terraformer/bounds.rb', line 6 def bounds obj, format = :bbox obj = Terraformer.parse obj unless Geometry === obj bbox = case obj.type when 'Point' [ obj.coordinates[0], obj.coordinates[1], obj.coordinates[0], obj.coordinates[1] ] when 'MultiPoint' bounds_for_array obj.coordinates when 'LineString' bounds_for_array obj.coordinates when 'MultiLineString' bounds_for_array obj.coordinates, 1 when 'Polygon' bounds_for_array obj.coordinates, 1 when 'MultiPolygon' bounds_for_array obj.coordinates, 2 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, nesting = 0, box = Array.new(4)) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/terraformer/bounds.rb', line 48 def bounds_for_array array, nesting = 0, box = Array.new(4) if nesting > 0 array.reduce box do |b, a| bounds_for_array a, (nesting - 1), b end else bbox = array.reduce box do |b, lonlat| lon, lat = *lonlat set = ->(d, i, t){ b[i] = d if b[i].nil? or d.send(t, b[i])} set[lon, X1, :<] set[lon, X2, :>] set[lat, Y1, :<] set[lat, Y2, :>] b end bbox end end |
.bounds_for_collection(collection) ⇒ Object
75 76 77 |
# File 'lib/terraformer/bounds.rb', line 75 def bounds_for_collection collection bounds_for_array collection.map {|e| bounds(block_given? ? yield(e) : e)} end |
.bounds_for_feature_collection(fc) ⇒ Object
67 68 69 |
# File 'lib/terraformer/bounds.rb', line 67 def bounds_for_feature_collection fc bounds_for_collection fc.features, &:geometry end |
.bounds_for_geometry_collection(gc) ⇒ Object
71 72 73 |
# File 'lib/terraformer/bounds.rb', line 71 def bounds_for_geometry_collection gc bounds_for_collection gc end |
.envelope(geometry) ⇒ Object
80 81 82 83 84 85 86 87 88 |
# File 'lib/terraformer/bounds.rb', line 80 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 |