Class: Terraformer::Geometry

Inherits:
Primitive show all
Extended by:
ClassMethods
Defined in:
lib/terraformer/geometry.rb,
lib/terraformer/geometry/class_methods.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

MULTI_REGEX =
/^Multi/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ClassMethods

arrays_intersect_arrays?, coordinates_contain_point?, edge_intersects_edge?, line_contains_point?

Methods inherited from Primitive

#bbox, #envelope, #to_json, #type

Constructor Details

#initialize(*args) ⇒ Geometry

Returns a new instance of Geometry.



12
13
14
15
16
17
18
19
20
21
22
23
# 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

Instance Attribute Details

#coordinatesObject

Returns the value of attribute coordinates.



10
11
12
# File 'lib/terraformer/geometry.rb', line 10

def coordinates
  @coordinates
end

#crsObject

Returns the value of attribute crs.



10
11
12
# File 'lib/terraformer/geometry.rb', line 10

def crs
  @crs
end

Class Method Details

.iter_coordinate(obj, meth, &block) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
# 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

Instance Method Details

#==(obj) ⇒ Object



119
120
121
122
123
124
125
126
# 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) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


96
97
98
# File 'lib/terraformer/geometry.rb', line 96

def contains? other
  raise NotImplementedError
end

#convex_hullObject



92
93
94
# File 'lib/terraformer/geometry.rb', line 92

def convex_hull
  ConvexHull.for coordinates
end

#each_coordinate(&block) ⇒ Object



25
26
27
# File 'lib/terraformer/geometry.rb', line 25

def each_coordinate &block
  Geometry.iter_coordinate coordinates, :each, &block
end

#first_coordinateObject

Raises:

  • (NotImplementedError)


71
72
73
# File 'lib/terraformer/geometry.rb', line 71

def first_coordinate
  raise NotImplementedError
end

#geographic?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/terraformer/geometry.rb', line 79

def geographic?
  first_coordinate.geographic?
end

#get(index) ⇒ Object



83
84
85
86
87
88
89
90
# 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) ⇒ Boolean

Returns:

  • (Boolean)


104
105
106
107
108
109
110
111
112
113
114
115
116
117
# 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) ⇒ Object



29
30
31
# File 'lib/terraformer/geometry.rb', line 29

def map_coordinate &block
  Geometry.iter_coordinate coordinates, :map, &block
end

#mercator?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/terraformer/geometry.rb', line 75

def mercator?
  first_coordinate.mercator?
end

#to_featureObject



65
66
67
68
69
# File 'lib/terraformer/geometry.rb', line 65

def to_feature
  f = Feature.new
  f.geometry = self
  f
end

#to_geographicObject



61
62
63
# File 'lib/terraformer/geometry.rb', line 61

def to_geographic
  self.class.new *map_coordinate(&:to_geographic)
end

#to_hash(*args) ⇒ Object



47
48
49
50
51
52
53
54
55
# 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_mercatorObject



57
58
59
# File 'lib/terraformer/geometry.rb', line 57

def to_mercator
  self.class.new *map_coordinate(&:to_mercator)
end

#within?(other) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


100
101
102
# File 'lib/terraformer/geometry.rb', line 100

def within? other
  raise NotImplementedError
end