Class: Map::RgeoService
- Inherits:
-
Object
- Object
- Map::RgeoService
- Defined in:
- lib/map/rgeo_service.rb
Instance Attribute Summary collapse
-
#factory ⇒ Object
readonly
Returns the value of attribute factory.
Instance Method Summary collapse
- #centroid(points) ⇒ Object
- #create_point(point) ⇒ Object
- #create_polygon(points) ⇒ Object
-
#initialize ⇒ RgeoService
constructor
A new instance of RgeoService.
- #polygon_inside_any_other?(base, other) ⇒ Boolean
Constructor Details
#initialize ⇒ RgeoService
Returns a new instance of RgeoService.
6 7 8 |
# File 'lib/map/rgeo_service.rb', line 6 def initialize @factory = RGeo::Geos.factory(srid: 3361) end |
Instance Attribute Details
#factory ⇒ Object (readonly)
Returns the value of attribute factory.
4 5 6 |
# File 'lib/map/rgeo_service.rb', line 4 def factory @factory end |
Instance Method Details
#centroid(points) ⇒ Object
34 35 36 37 38 39 40 41 |
# File 'lib/map/rgeo_service.rb', line 34 def centroid(points) polygon = create_polygon(points) centroid = polygon.centroid { latitude: centroid.x, longitude: centroid.y } end |
#create_point(point) ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/map/rgeo_service.rb', line 20 def create_point(point) if point.is_a?(Array) point.first < point.second ? factory.point(point.second.to_f, point.first.to_f) : factory.point(point.first.to_f, point.second.to_f) elsif point.is_a?(Hash) factory.point((point['latitude'] || point[:latitude]).to_f, (point['longitude'] || point[:longitude]).to_f) end end |
#create_polygon(points) ⇒ Object
10 11 12 13 14 15 16 17 18 |
# File 'lib/map/rgeo_service.rb', line 10 def create_polygon(points) raise 'Parâmetro inválido' unless points.is_a?(Array) transformed = points.collect do |point| create_point(point) end factory.polygon(factory.linear_ring(transformed)) end |
#polygon_inside_any_other?(base, other) ⇒ Boolean
28 29 30 31 32 |
# File 'lib/map/rgeo_service.rb', line 28 def polygon_inside_any_other?(base, other) return false if base.nil? || other.nil? return true if base.overlaps?(other) || other.overlaps?(base) return true if base.contains?(other) || other.contains?(base) end |