lib/terraformer/point.rb in terraformer-0.0.2 vs lib/terraformer/point.rb in terraformer-0.0.3

- old
+ new

@@ -5,27 +5,47 @@ def first_coordinate coordinates end def distance_and_bearing_to obj - case obj - when Point - first_coordinate.distance_and_bearing_to obj.first_coordinate + dabt = case obj + when Point + [first_coordinate.distance_and_bearing_to(obj.first_coordinate)] - # todo other cases - end + when MultiPoint + obj.coordinates.map {|c| first_coordinate.distance_and_bearing_to c} + + when LineString + obj.coordinates.map {|c| first_coordinate.distance_and_bearing_to c} + + when MultiLineString + obj.line_strings.map {|ls| distance_and_bearing_to ls} + + when Polygon + obj.line_strings[0].coordinates.map {|c| first_coordinate.distance_and_bearing_to c} + + when MultiPolygon + obj.polygons.map {|p| distance_and_bearing_to p} + + # todo other cases + + else + raise ArgumentError.new "unsupported type: #{obj.type rescue obj.class}" + end + dabt.flatten! + dabt.minmax_by {|db| db[:distance]} end - def distance_to obj - distance_and_bearing_to(obj)[:distance] + def distance_to obj, minmax = :min + distance_and_bearing_to(obj)[minmax == :min ? 0 : 1][:distance] end def initial_bearing_to obj - distance_and_bearing_to(obj)[:bearing][:initial] + distance_and_bearing_to(obj)[0][:bearing][:initial] end def final_bearing_to obj - distance_and_bearing_to(obj)[:bearing][:final] + distance_and_bearing_to(obj)[0][:bearing][:final] end def contains? obj case obj when Point