rob-murray/london-bike-hire-cli

View on GitHub
lib/london_bike_hire_cli/repository/spatial_search_adapter.rb

Summary

Maintainability
A
0 mins
Test Coverage
require "kdtree"

module LondonBikeHireCli
  module Repository
    class SpatialSearchAdapter
      def initialize(stations = [])
        init_spatial_store(stations)
      end

      # Public: Return the IDs of the nearest n stations
      # lat, long - latitude, longitude of point to search
      # limit - The maximum number of results
      #
      def nearest(lat, long, limit)
        spatial_store.nearestk(lat, long, limit)
      end

      private

      attr_reader :spatial_store

      def init_spatial_store(stations)
        stations_triples = stations.map { |station| [station.lat, station.long, station.id] }
        @spatial_store = Kdtree.new(stations_triples)
      end
    end
  end
end