Class: Terraformer::MultiPolygon

Inherits:
Geometry show all
Defined in:
lib/terraformer/multi_polygon.rb

Constant Summary

Constants inherited from Geometry

Geometry::MULTI_REGEX

Instance Attribute Summary

Attributes inherited from Geometry

#coordinates, #crs

Instance Method Summary collapse

Methods inherited from Geometry

#convex_hull, #each_coordinate, #geographic?, #get, #intersects?, iter_coordinate, #map_coordinate, #mercator?, #to_feature, #to_geographic, #to_hash, #to_mercator

Methods included from Geometry::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) ⇒ MultiPolygon

Returns a new instance of MultiPolygon.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/terraformer/multi_polygon.rb', line 5

def initialize *args
  case

  # arg is an array of arrays of polygon, holes
  when Array === args[0] && Array === args[0][0] && Array === args[0][0][0] && Array === args[0][0][0][0]
    self.coordinates = Coordinate.from_array(*args)

  when Coordinate === args[0] # only one
    self.coordinates = [[Coordinate.from_array(args)]]

  when Array === args[0] # multiple?
    self.coordinates = [Coordinate.from_array(args)]

  when Polygon === args[0]
    self.coordinates = args.map &:coordinates

  else
    super *args
  end

  # must be an array of arrays of arrays of coordinates (whew!)
  unless Array === coordinates &&
         Array === coordinates[0] &&
         Array === coordinates[0][0] &&
         Terraformer::Coordinate === coordinates[0][0][0]
    raise ArgumentError.new 'invalid coordinates for Terraformer::MultiPolygon'
  end
end

Instance Method Details

#==(obj) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/terraformer/multi_polygon.rb', line 42

def == obj
  super obj do |o|
    equal = true
    ps = polygons
    ops = o.polygons
    ps.each_with_index do |p, i|
      equal = p == ops[i] rescue false
      break unless equal
    end
    equal
  end
end

#contains?(obj) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/terraformer/multi_polygon.rb', line 55

def contains? obj
  polygons.any? {|p| p.contains? obj}
end

#first_coordinateObject



34
35
36
# File 'lib/terraformer/multi_polygon.rb', line 34

def first_coordinate
  coordinates[0][0][0]
end

#polygonsObject



38
39
40
# File 'lib/terraformer/multi_polygon.rb', line 38

def polygons
  coordinates.map {|p| Polygon.new *p}
end

#within?(obj) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
62
63
64
65
66
67
68
# File 'lib/terraformer/multi_polygon.rb', line 59

def within? obj
  case obj
  when Polygon
    polygons.all? {|p| p.within? obj}
  when MultiPolygon
    polygons.all? {|p| obj.polygons.any? {|op| op.contains? p}}
  else
    raise ArgumentError.new "unsupported type: #{obj.type rescue obj.class}"
  end
end