lib/terraformer/geometry.rb in terraformer-0.0.1 vs lib/terraformer/geometry.rb in terraformer-0.0.2
- old
+ new
@@ -1,16 +1,22 @@
+require 'terraformer/geometry/class_methods'
+
module Terraformer
class Geometry < Primitive
+ extend Terraformer::Geometry::ClassMethods
MULTI_REGEX = /^Multi/
attr_accessor :coordinates
def initialize *args
- if args.length > 1 or Array === args[0]
+ case
+ when args.length > 1
self.coordinates = Coordinate.from_array args
+ when Array === args[0]
+ self.coordinates = Coordinate.from_array args[0]
else
super *args do |arg|
self.coordinates = Coordinate.from_array arg['coordinates']
end
end
@@ -29,10 +35,16 @@
def to_geographic
self.class.new *coordinates.map_coordinate(&:to_geographic)
end
+ def to_feature
+ f = Feature.new
+ f.geometry = self
+ f
+ end
+
def first_coordinate
raise NotImplementedError
end
def mercator?
@@ -51,25 +63,44 @@
raise NotImplementedError
end
end
def convex_hull
- raise NotImplementedError
+ ConvexHull.for coordinates
end
- def contains other
+ def contains? other
raise NotImplementedError
end
- def within other
+ def within? other
raise NotImplementedError
end
- def intersects other
- raise NotImplementedError
+ def intersects? other
+ [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
+ rescue ArgumentError
+ false
+ end
+ Terraformer::Geometry.arrays_intersect_arrays? coordinates, other.coordinates
end
+ def == obj
+ return false unless obj.class == self.class
+ if block_given?
+ yield obj
+ else
+ self.coordinates == obj.coordinates
+ end
+ end
+
end
class GeometryCollection < Primitive
attr_writer :geometries
@@ -89,9 +120,13 @@
def to_hash
{
type: type,
geometries: geometries.map(&:to_hash)
}
+ end
+
+ def convex_hull
+ ConvexHull.for geometries.map &:coordinates
end
end
end