Class: Map::RgeoService

Inherits:
Object
  • Object
show all
Defined in:
lib/map/rgeo_service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRgeoService

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

#factoryObject (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

Returns:

  • (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