lib/terraformer/feature.rb in terraformer-0.0.6 vs lib/terraformer/feature.rb in terraformer-0.0.7
- old
+ new
@@ -1,21 +1,21 @@
module Terraformer
class Feature < Primitive
extend Forwardable
- attr_accessor :id, :geometry
+ attr_accessor :id, :geometry, :crs
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'
- self.geometry = Terraformer.parse arg['geometry']
+ self.geometry = Terraformer.parse arg['geometry'] if arg['geometry']
end
end
end
def properties
@@ -24,13 +24,13 @@
def to_hash
h = {
type: type,
properties: properties,
- geometry: geometry.to_hash
}
- h.merge! id: id if id
+ h[:geometry] = geometry.to_hash if geometry
+ h[:id] = id if id
h
end
def great_circle_distance other
other = other.geometry if Feature === other
@@ -39,14 +39,20 @@
def geojson_io
Terraformer.geojson_io self
end
+ def == obj
+ return false unless Feature === obj
+ to_hash == obj.to_hash
+ end
+
end
class FeatureCollection < Primitive
+ attr_accessor :crs
attr_writer :features
def self.with_features *f
raise ArgumentError unless f.all? {|e| Feature === e}
fc = FeatureCollection.new
@@ -70,13 +76,20 @@
raise ArgumentError unless Feature === feature
features << feature
end
def to_hash
- {
+ h = {
type: type,
features: features.map(&:to_hash)
}
+ h[:crs] = crs if crs
+ h
+ end
+
+ def == obj
+ return false unless FeatureCollection === obj
+ to_hash == obj.to_hash
end
def convex_hull
ConvexHull.for features.map(&:geometry).map(&:coordinates)
end