lib/terraformer/geometry.rb in terraformer-0.1.0 vs lib/terraformer/geometry.rb in terraformer-0.2.0
- old
+ new
@@ -11,19 +11,41 @@
def initialize *args
case
when args.length > 1
self.coordinates = Coordinate.from_array args
- when Array === args[0]
+ when Array === args[0] || Coordinate === args[0]
self.coordinates = Coordinate.from_array args[0]
else
super *args do |arg|
self.coordinates = Coordinate.from_array arg['coordinates']
end
end
end
+ def each_coordinate &block
+ Geometry.iter_coordinate coordinates, :each, &block
+ end
+
+ def map_coordinate &block
+ Geometry.iter_coordinate coordinates, :map, &block
+ end
+
+ def self.iter_coordinate obj, meth, &block
+ if Terraformer::Coordinate === obj
+ block.call obj
+ elsif Array === obj
+ obj.__send__ meth do |pair|
+ if Array === pair
+ Geometry.iter_coordinate pair, meth, &block
+ else
+ block.call pair
+ end
+ end
+ end
+ end
+
def to_hash *args
h = {
type: type,
coordinates: coordinates
}
@@ -31,15 +53,15 @@
h[:bbox] = bbox if Hash === args.last and args.last[:include_bbox]
h
end
def to_mercator
- self.class.new *coordinates.map_coordinate(&:to_mercator)
+ self.class.new *map_coordinate(&:to_mercator)
end
def to_geographic
- self.class.new *coordinates.map_coordinate(&:to_geographic)
+ self.class.new *map_coordinate(&:to_geographic)
end
def to_feature
f = Feature.new
f.geometry = self
@@ -83,11 +105,12 @@
[self, other].each do |e|
if [Point, MultiPoint].include? e.class
raise ArgumentError.new "unsupported type: #{e.type rescue e.class}"
end
end
- return true if begin
- within? other or other.within? self
+
+ begin
+ return true if within? other or other.within? self
rescue ArgumentError
false
end
Terraformer::Geometry.arrays_intersect_arrays? coordinates, other.coordinates
end