lib/terraformer/bounds.rb in terraformer-0.1.0 vs lib/terraformer/bounds.rb in terraformer-0.2.0
- old
+ new
@@ -5,29 +5,23 @@
def bounds obj, format = :bbox
obj = Terraformer.parse obj unless Geometry === obj
- bbox = case obj.type
- when 'Point'
+ bbox = case obj
+ when Point
[ obj.coordinates[0], obj.coordinates[1],
obj.coordinates[0], obj.coordinates[1] ]
- when 'MultiPoint'
+ when MultiPoint, LineString,
+ MultiLineString, Polygon,
+ MultiPolygon
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'
+ when Feature
obj.geometry ? bounds(obj.geometry) : nil
- when 'FeatureCollection'
+ when FeatureCollection
bounds_for_feature_collection obj
- when 'GeometryCollection'
+ when GeometryCollection
bounds_for_geometry_collection obj
else
raise ArgumentError.new 'unknown type: ' + obj.type
end
@@ -43,25 +37,22 @@
end
end
X1, Y1, X2, Y2 = 0, 1, 2, 3
- 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
+ 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[lon, X1, :<]
- set[lon, X2, :>]
- set[lat, Y1, :<]
- set[lat, Y2, :>]
+ set[a[0], X1, :<]
+ set[a[0], X2, :>]
+ set[a[1], Y1, :<]
+ set[a[1], Y2, :>]
b
+ else
+ bounds_for_array a, box
end
- bbox
end
end
def bounds_for_feature_collection fc
bounds_for_collection fc.features, &:geometry