Last active
March 11, 2018 11:59
-
-
Save grvsachdeva/2dcd12e4203ccd3541ca247c6e0cb4bf to your computer and use it in GitHub Desktop.
This ruby code print the names of all people residing in locations within 100 Km (For testing change 100 at line 111 to 10000 as faker gem generates random coordinates which has distance much greater than 100km )
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'json' | |
require 'pp' | |
require 'faker' | |
class Close | |
attr_accessor :id, :name | |
@@array = Array.new | |
def self.all_instances | |
@@array; | |
end | |
def initialize(id,name) | |
@id = id; | |
@name = name; | |
@@array << self | |
end | |
def self.printAll() | |
for i in 0...@@array.length | |
print "ID: ",@@array[i].id ," Name: ", @@array[i].name; | |
puts; | |
end | |
end | |
def self.sorting() | |
arr_size = @@array.length | |
loop do | |
swapped = false | |
(arr_size-1).times do |i| | |
if @@array[i].id > @@array[i+1].id | |
@@array[i].id, @@array[i+1].id = @@array[i+1].id, @@array[i].id | |
@@array[i].name, @@array[i+1].name = @@array[i+1].name, @@array[i].name | |
swapped = true | |
end | |
end | |
break if not swapped | |
end | |
return @@array; | |
end | |
end | |
def to_rad angle | |
angle/180 * Math::PI | |
end | |
def distance_between_points lat1,lat2,lon1,lon2 | |
radius = 6371000; | |
φ1 = to_rad(lat1); | |
φ2 = to_rad(lat2); | |
Δφ = to_rad(lat2-lat1); | |
Δλ = to_rad(lon2-lon1); | |
a = Math.sin(Δφ/2) * Math.sin(Δφ/2) + | |
Math.cos(φ1) * Math.cos(φ2) * | |
Math.sin(Δλ/2) * Math.sin(Δλ/2); | |
c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); | |
distance = radius * c; | |
return (distance/1000); | |
end | |
def generating_fake_data | |
i=0; | |
friend = []; | |
100.times do | |
friend[i] = { | |
:lat => Faker::Number.between(-90, 90)*(Faker::Number.decimal(0, 6).to_f) , | |
:lon => Faker::Number.between(-180, 180)*(Faker::Number.decimal(0, 6).to_f), | |
:name => Faker::Name.name, | |
:id => Faker::Number.number(4).to_i | |
} | |
i = i + 1 | |
end | |
tempHash = {"friend" => friend} | |
File.open("temp.json","w") do |f| | |
f.write(tempHash.to_json) #Writing JSON for testing purpose | |
end | |
end | |
def reading_json | |
json = File.read('temp.json'); #Reading JSON file for fetching information | |
obj = JSON.parse(json); | |
return obj; | |
end | |
generating_fake_data(); | |
obj = reading_json(); | |
latitude_gyandhan = 28.521134; | |
longitude_gyandhan = 77.206567; | |
j=0,count = 0; | |
friend_list_length = obj["friend"].length() | |
for j in 0...friend_list_length | |
dist = distance_between_points(latitude_gyandhan,obj["friend"][j]["lat"],longitude_gyandhan,obj["friend"][j]["lon"]) | |
if( dist <= 100) #For testing with gyandhan location coordinates ,consider changing 100 to 10000 or greater as faker gem generates random coordinates which has distance much greater than 100km | |
Close.new(obj["friend"][j]["id"],obj["friend"][j]["name"]) | |
count = count + 1; | |
end | |
end | |
if count === 0 | |
puts "For testing with gyandhan location coordinates ,consider changing 100 to 10000 or greater as faker gem generates random coordinates which has distance much greater than 100km"; | |
end | |
Close.sorting(); | |
Close.printAll(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment