I am trying to calculate distance between two points in kilometers but it is giving me wrong.
location.each do |loc|
distance = Haversine.distance(28.6139, 77.209, loc[:lat].to_f, loc[:long].to_f).to_km
puts " #{distance}"
end
Here is the distance which I am getting and which is wrong
7385.99072855455
7383.795725224046
7392.13122601482
7391.537885880786
The data of the location is in my database
[{"id":1,"lat":28.6139,"lng":77.209,"location":"","object_id":1,"created_at":"2016-06-07T05:46:53.000Z","updated_at":"2016-06-07T05:46:53.000Z","object_type":"abc"},{"id":2,"lat":28.6692,"lng":77.4538,"location":"","object_id":2,"created_at":"2016-06-07T05:49:23.000Z","updated_at":"2016-06-07T05:49:23.000Z","object_type":"cde"},{"id":3,"lat":28.4595,"lng":77.0266,"location":"","object_id":3,"created_at":"2016-06-07T05:50:22.000Z","updated_at":"2016-06-07T05:50:22.000Z","object_type":"ggg"},{"id":4,"lat":28.4744,"lng":77.504,"location":"","object_id":4,"created_at":"2016-06-07T05:50:24.000Z","updated_at":"2016-06-07T05:50:24.000Z","object_type":"eerr"}]
You have a typo: loc[:long] instead of loc[:lng], the line should read:
Haversine.distance(28.6139, 77.209, loc[:lat].to_f, loc[:lng].to_f).to_km
You passed nil.to_f as _longitude of the second point. This is equivalent to passing 0
Related
This program is written to find area of a rectangle.
l = float(input("Lenght of rectangle:"))
b = float(input("Breadht of rectangle:"))
area = l*b
print ("Area of square = l*b")
print (" =",l,"*",b)
print (" =",area)
The input length is 12.4 and the input breadth is 13 So the answer must be 161.2
however the answer coming is 161.20000000000002 What's going wrong?
Check out https://medium.com/code-85/how-to-stop-floating-point-arithmetic-errors-in-python-a98d3a63ccc8.
"This happens because decimal values are actually stored as a formula and do not have an exact representation."
Use the following program instead of the above one
l = float(input("Lenght of rectangle:"))
b = float(input("Breadht of rectangle:"))
area = (l*b)
a_float = "{:.2f}".format(area)
print ("Area of square = l*b")
print (" =",l,"*",b)
print (" =",area)
Here, we have format the program so that we only get two digits after the decimal. in this way the outcome will be similar to what we exactly wanted.
For more information, visit
https://www.kite.com/python/answers/how-to-print-a-float-with-two-decimal-places-in-python#:~:text=Use%20str.,number%20with%20two%20decimal%20places.
reference:- www.kite.com
I'm using geokit to give me distances between two post codes. I need to determine which post code is the nearest.
point_a = Geokit::Geocoders::GoogleGeocoder.geocode "se18 7hp"
alpha = ["cr0 3rl", "W2 1AA"]
miles = alpha.map do |m| point_a.distance_to(m) end
miles.min # => 11.005310790913377
How do I do the reverse of miles.min to get to know which post code was the nearest from point_a?
To get the index of an array element, use Array#index
So, in your case, it will be
alpha[miles.index(miles.min)]
I am writing a simple code in matlab which has the purpose of creating the histogram of a grayscale image without using the function hist. I am stuck at the point in which mathlab displays the error "Subscript indices must either be real positive integers or logicals." Can you help me finding where is the wrong indices?
indirizzo='file.jpg';
immagine=imread(indirizzo);
immaginebn=rgb2gray(immagine);
n=zerps(0,255);
for x=0:255;
numeroennesimo=sum(sum(immaginebn==x));
n(x)=numeroennesimo;
end
plot(x,n)
you cant use 0 as index. Either make n(x+1) or for x = 1:256 and substract the 1 in your comparison. And there is a typo, I guess it means zeros instead of zerps, which also doesnt work with a 0. And one more, your plot will also not work as the x has only a size of 1 while n is an array of 266 and for a histogram I would use a barplot instead.
indirizzo='file.jpg';
immagine=imread(indirizzo);
immaginebn=rgb2gray(immagine);
n=zeros(1,256);
for x=0:255;
numeroennesimo=sum(sum(immaginebn==x-1));
n(x+1)=numeroennesimo;
end
bar(0:255,n)
or
indirizzo='file.jpg';
immagine=imread(indirizzo);
immaginebn=rgb2gray(immagine);
n=zeros(1,256);
xplot=zeros(1,256);
for x=1:256;
numeroennesimo=sum(sum(immaginebn==x-1));
n(x)=numeroennesimo;
xplot(x) = x-1;
end
plot(xplot,n)
I have a 250*2001 matrix. I want to find the location for the maximum value for a(:,i) where i takes 5 different values: i = i + 256
a(:,256)
a(:,512)
a(:,768)
a(:,1024)
a(:,1280)
I tried using MAXLOC, but since I'm new to fortran, I couldn't get it right.
Try this
maxloc(a(:,256:1280:256))
but be warned, this call will return a value in the range 1..5 for the second dimension. The call will return the index of the maxloc in the 2001*5 array section that you pass to it. So to get the column index of the location in the original array you'll have to do some multiplication. And note that since the argument in the call to maxloc is a rank-2 array section the call will return a 2-element vector.
Your question is a little unclear: it could be either of two things you want.
One value for the maximum over the entire 250-by-5 subarray;
One value for the maximum in each of the 5 250-by-1 subarrays.
Your comments suggest you want the latter, and there is already an answer for the former.
So, in case it is the latter:
b(1:5) = MAXLOC(a(:,256:1280:256), DIM=1)
I am using VBScript in a Manifold GIS Database to verify the distance between two longitude and latitude points using Trig function. The script for finding the distance run without any problems but the script for verifying whether the O-D is valid had syntax error. I really hope any of you could help me with this problem.
There were 2 new active columns that I created : Distance and VerifyDistance. Distance column was created to find the distance using Trig function. It ran successfuly but may need improvements in its messy structure. The VerifyDistance used the IF conditions and checked the value in another column named "Valid O/D". If the value is "OK", it will return the Distance value; otherwise, it will return a text value saying "O-D points are invalid" in this column.
As you can see, I am a newb in programming language. The VBScript can be seen below:
Function Distance
Distance = sqr((111.21*Record.Data("Work Y-coord") - 111.21*Record.Data("Home Y-coord"))^2 + (85.30*Record.Data("Work X-coord") - 85.30*Record.Data("Home X-coord"))^2)
End Function
Function VerifyDistance
If Record.Data("Valid O/D") = "OK"
VerifyDistance = Record.Data("Distance")
'document.write("Invalid O-D Points")
Else
VerifyDistance = "O-D Points are invalid."
End If
End Function
You are missing Then here.
If Record.Data("Valid O/D") = "OK" Then