class Terraformer::Geometry

Constants

MULTI_REGEX

Attributes

coordinates[RW]
crs[RW]

Public Class Methods

iter_coordinate(obj, meth, &block) click to toggle source
# File lib/terraformer/geometry.rb, line 33
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
new(*args) click to toggle source
Calls superclass method Terraformer::Primitive::new
# File lib/terraformer/geometry.rb, line 12
def initialize *args
  case
  when args.length > 1
    self.coordinates = Coordinate.from_array args
  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

Public Instance Methods

==(obj) { |obj| ... } click to toggle source
# File lib/terraformer/geometry.rb, line 119
def == obj
  return false unless obj.class == self.class
  if block_given?
    yield obj
  else
    self.coordinates == obj.coordinates
  end
end
contains?(other) click to toggle source
# File lib/terraformer/geometry.rb, line 96
def contains? other
  raise NotImplementedError
end
convex_hull() click to toggle source
# File lib/terraformer/geometry.rb, line 92
def convex_hull
  ConvexHull.for coordinates
end
each_coordinate(&block) click to toggle source
# File lib/terraformer/geometry.rb, line 25
def each_coordinate &block
  Geometry.iter_coordinate coordinates, :each, &block
end
first_coordinate() click to toggle source
# File lib/terraformer/geometry.rb, line 71
def first_coordinate
  raise NotImplementedError
end
geographic?() click to toggle source
# File lib/terraformer/geometry.rb, line 79
def geographic?
  first_coordinate.geographic?
end
get(index) click to toggle source
# File lib/terraformer/geometry.rb, line 83
def get index
  if MULTI_REGEX.match type
    sub = type.sub MULTI_REGEX, ''
    Terraformer.const_get(sub).new *coordinates[index]
  else
    raise NotImplementedError
  end
end
intersects?(other) click to toggle source
# File lib/terraformer/geometry.rb, line 104
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

  begin
    return true if within? other or other.within? self
  rescue ArgumentError
    false
  end
  Terraformer::Geometry.arrays_intersect_arrays? coordinates, other.coordinates
end
map_coordinate(&block) click to toggle source
# File lib/terraformer/geometry.rb, line 29
def map_coordinate &block
  Geometry.iter_coordinate coordinates, :map, &block
end
mercator?() click to toggle source
# File lib/terraformer/geometry.rb, line 75
def mercator?
  first_coordinate.mercator?
end
to_feature() click to toggle source
# File lib/terraformer/geometry.rb, line 65
def to_feature
  f = Feature.new
  f.geometry = self
  f
end
to_geographic() click to toggle source
# File lib/terraformer/geometry.rb, line 61
def to_geographic
  self.class.new *map_coordinate(&:to_geographic)
end
to_hash(*args) click to toggle source
# File lib/terraformer/geometry.rb, line 47
def to_hash *args
  h = {
    type: type,
    coordinates: coordinates
  }
  h[:crs] = crs if crs
  h[:bbox] = bbox if Hash === args.last and args.last[:include_bbox]
  h
end
to_mercator() click to toggle source
# File lib/terraformer/geometry.rb, line 57
def to_mercator
  self.class.new *map_coordinate(&:to_mercator)
end
within?(other) click to toggle source
# File lib/terraformer/geometry.rb, line 100
def within? other
  raise NotImplementedError
end