lib/terraformer/feature.rb in terraformer-0.0.1 vs lib/terraformer/feature.rb in terraformer-0.0.2
- old
+ new
@@ -1,12 +1,15 @@
module Terraformer
class Feature < Primitive
+ extend Forwardable
attr_accessor :id, :geometry
attr_writer :properties
+ def_delegator :@geometry, :convex_hull
+
def initialize *args
unless args.empty?
super *args do |arg|
self.id = arg['id'] if arg.key? 'id'
self.properties = arg['properties'] if arg.key? 'properties'
@@ -27,16 +30,28 @@
}
h.merge! id: id if id
h
end
+ def great_circle_distance other
+ other = other.geometry if Feature === other
+ self.geometry.great_circle_distance other
+ end
+
end
class FeatureCollection < Primitive
attr_writer :features
+ def self.with_features *f
+ raise ArgumentError unless f.all? {|e| Feature === e}
+ fc = FeatureCollection.new
+ fc.features = f
+ fc
+ end
+
def initialize *args
unless args.empty?
super *args do |arg|
self.features = arg['features'].map {|f| Terraformer.parse f}
end
@@ -45,14 +60,23 @@
def features
@features ||= []
end
+ def << feature
+ raise ArgumentError unless Feature === feature
+ features << feature
+ end
+
def to_hash
{
type: type,
features: features.map(&:to_hash)
}
+ end
+
+ def convex_hull
+ ConvexHull.for features.map(&:geometry).map(&:coordinates)
end
end
end