Module: OceanNames

Defined in:
lib/ocean_names.rb,
lib/ocean_names/polygon.rb,
lib/ocean_names/version.rb

Defined Under Namespace

Classes: Error, Polygon

Constant Summary collapse

GET_POINTS =

drill down to main geometry points array

->(arg) {
  return arg unless arg.first.first.is_a?(Array)

  GET_POINTS.(arg.first)
}
WITHIN_BOUNDS =

check bounds to avoid

->(record, x, y) {
  record["min_lng"] <= x &&
  record["max_lng"] >= x &&
  record["min_lat"] <= y &&
  record["max_lat"] >= y
}
VERSION =
"1.0.1"

Class Method Summary collapse

Class Method Details

.dataObject



47
48
49
# File 'lib/ocean_names.rb', line 47

def self.data
  @data ||= Oj.load(File.read(file))
end

.fileObject



43
44
45
# File 'lib/ocean_names.rb', line 43

def self.file
  File.expand_path("../data/water.json", File.dirname(__FILE__))
end

.reverse_geocode(lat:, lng:) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ocean_names.rb', line 25

def self.reverse_geocode(lat:, lng:)
  rec = data.find do |record|
    # avoid check of obviously false geometries
    next unless WITHIN_BOUNDS.(record, lng, lat)

    record["geometry"].any? do |geometry|
      # get points from nested arrays
      points = GET_POINTS.(geometry)
      polygon = OceanNames::Polygon.new(points)
      polygon.contains?(lat: lat, lng: lng)
    end
  end

  rec&.reject do |key|
    key == "geometry"
  end
end