Sha256: ed29616e9559fec5c01f56c2e118dfca096ac8318d0d770b75c93ef795707e60

Contents?: true

Size: 1.72 KB

Versions: 1

Compression:

Stored size: 1.72 KB

Contents

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'
          self.geometry = Terraformer.parse arg['geometry']
        end
      end
    end

    def properties
      @properties ||= {}
    end

    def to_hash
      h = {
        type: type,
        properties: properties,
        geometry: geometry.to_hash
      }
      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

    def geojson_io
      Terraformer.geojson_io self
    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 unless Primitive === f}
        end
      end
    end

    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

    def geojson_io
      Terraformer.geojson_io self
    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
terraformer-0.0.6 lib/terraformer/feature.rb