Class: GeoRuby::SimpleFeatures::Polygon

Inherits:
Object
  • Object
show all
Defined in:
lib/georuby-ext/georuby/polygon.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.circle(center, radius, sides_number = 24) ⇒ Object



3
4
5
6
7
8
9
10
11
# File 'lib/georuby-ext/georuby/polygon.rb', line 3

def self.circle(center, radius, sides_number = 24)
  points = sides_number.times.map do |side|
    2 * 180 / sides_number * side
  end.map! do |angle|
    center.endpoint angle, radius
  end

  from_points [points], center.srid
end

.intersection(georuby_polygons) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/georuby-ext/georuby/polygon.rb', line 42

def self.intersection(georuby_polygons)
  if !georuby_polygons.empty?
    polygon_intersection = georuby_polygons.first.to_rgeo
    georuby_polygons.shift
  end
  
  georuby_polygons.each do |polygon|
    polygon_intersection = polygon_intersection.intersection(polygon.to_rgeo)
  end

  polygon_intersection.to_georuby
end

.union(georuby_polygons) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/georuby-ext/georuby/polygon.rb', line 29

def self.union(georuby_polygons)
  return nil if georuby_polygons.empty?             

  rgeo_polygons = georuby_polygons.collect(&:to_rgeo)      
  rgeo_polygon_union = rgeo_polygons.first    
   
  rgeo_polygons[1..(rgeo_polygons.size - 1)].each do |rgeo_polygon|
     rgeo_polygon_union = rgeo_polygon_union.union(rgeo_polygon)
  end
  
  rgeo_polygon_union.to_georuby
end

Instance Method Details

#centroidObject



25
26
27
# File 'lib/georuby-ext/georuby/polygon.rb', line 25

def centroid
  to_rgeo.centroid.to_georuby
end

#change(options) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/georuby-ext/georuby/polygon.rb', line 66

def change(options)
  self.class.from_linear_rings(options[:rings] || rings, 
                               options[:srid] || srid,
                               options[:with_z] || with_z, 
                               options[:with_m] || with_m)
  # or instead of || requires parenthesis
end

#difference(georuby_polygon) ⇒ Object



55
56
57
58
# File 'lib/georuby-ext/georuby/polygon.rb', line 55

def difference(georuby_polygon)
  polygon_difference = self.to_rgeo.difference(georuby_polygon.to_rgeo)
  polygon_difference.to_georuby
end

#perimeterObject



21
22
23
# File 'lib/georuby-ext/georuby/polygon.rb', line 21

def perimeter
  rings.sum(&:spherical_distance)
end

#pointsObject



17
18
19
# File 'lib/georuby-ext/georuby/polygon.rb', line 17

def points
  rings.collect(&:points).flatten
end

#project_to(target_srid) ⇒ Object



74
75
76
77
# File 'lib/georuby-ext/georuby/polygon.rb', line 74

def project_to(target_srid)
  return self if srid == target_srid
  change :rings => rings.map { |ring| ring.project_to(target_srid) }, :srid => target_srid
end

#side_countObject



13
14
15
# File 'lib/georuby-ext/georuby/polygon.rb', line 13

def side_count
  rings.sum(&:side_count)
end

#to_rgeoObject



60
61
62
63
64
# File 'lib/georuby-ext/georuby/polygon.rb', line 60

def to_rgeo
  outer_ring = rings.first.to_rgeo
  rings.size > 1 ? inner_rings = rings[1..(rings.size - 1)].collect(&:to_rgeo) : inner_rings = nil
  rgeo_factory.polygon(outer_ring, inner_rings)
end