Class: Terraformer::Geometry
Defined Under Namespace
Modules: ClassMethods
Constant Summary
collapse
- MULTI_REGEX =
/^Multi/
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
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.
Instance Attribute Details
#coordinates ⇒ Object
Returns the value of attribute coordinates.
10
11
12
|
# File 'lib/terraformer/geometry.rb', line 10
def coordinates
@coordinates
end
|
#crs ⇒ Object
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
96
97
98
|
# File 'lib/terraformer/geometry.rb', line 96
def contains? other
raise NotImplementedError
end
|
#convex_hull ⇒ Object
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_coordinate ⇒ Object
71
72
73
|
# File 'lib/terraformer/geometry.rb', line 71
def first_coordinate
raise NotImplementedError
end
|
#geographic? ⇒ 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
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
75
76
77
|
# File 'lib/terraformer/geometry.rb', line 75
def mercator?
first_coordinate.mercator?
end
|
#to_feature ⇒ Object
65
66
67
68
69
|
# File 'lib/terraformer/geometry.rb', line 65
def to_feature
f = Feature.new
f.geometry = self
f
end
|
#to_geographic ⇒ Object
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_mercator ⇒ Object
57
58
59
|
# File 'lib/terraformer/geometry.rb', line 57
def to_mercator
self.class.new *map_coordinate(&:to_mercator)
end
|
#within?(other) ⇒ Boolean
100
101
102
|
# File 'lib/terraformer/geometry.rb', line 100
def within? other
raise NotImplementedError
end
|