Class: GeoGraf::IntersectionCalculator

Inherits:
Object
  • Object
show all
Defined in:
lib/geo_graf/intersection_calculator.rb

Instance Method Summary collapse

Constructor Details

#initialize(input_geodata) ⇒ IntersectionCalculator

Returns a new instance of IntersectionCalculator.



8
9
10
11
12
# File 'lib/geo_graf/intersection_calculator.rb', line 8

def initialize(input_geodata)
  @geodata ||= input_geodata
    .map { |geodatum| {id: geodatum[:id], polygon: polygon_from_coords(geodatum[:polygon_coords])} }
    .sort_by { |geodatum| geodatum[:polygon].area } # always have smaller first
end

Instance Method Details

#intersectionsObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/geo_graf/intersection_calculator.rb', line 14

def intersections
  intersections = []

  geodata.combination(2) do |smaller, bigger|
    intersection_area = smaller[:polygon].intersection_area(bigger[:polygon])

    unless intersection_area.zero?
      intersections << {
        id: smaller[:id],
        contained_area_percentage: (intersection_area.to_f / smaller[:polygon].area * 100).round,
        container_id: bigger[:id]
      }
    end
  end

  intersections
end