How to calculate x kilometers from a lat/lon - algorithm

I have a lat/lon and I'd like to calculate X kilometers North South East and West from that location. How would I go about doing that?
I read that "The number of kilometers per degree of longitude is approximately"
(2*pi/360) * r_earth * cos(theta)
Where theta is the latitude in degrees and r_earth is approximately 6378 km.
I'm not sure, for instance, how to calculate 3km North of a 37.875942,-122.3156416

Related

Find points in four directions of a latitude and longitude at certain distance

I am working on showing four pointers around a location (a pointer with certain latitude and longitude), which can be selected on a map.
So i get the location's lat and long values from https://nominatim.org/ api. What i'm trying to work on is get four point's lat and long value from the selected location. These four points are simply 100 meters in distance from the current point.
So now the issue here is how do i calculate or find a point east, west, north and south of the selected location's point (100 meters from the current lat long position). i have checked different options like point in polygon algorithm here https://assemblysys.com/php-point-in-polygon-algorithm/, or trying to draw a circle around the selected point and then getting four points on that circle's circumference, however the issue is still that i'm not able to know if the point which i have got is in which direct from the selected location. Is it east of location, south or west etc.
Any help is appreciated about identifying that a certain point is in which direction of the selected point.
Once you (or someone) select the location on the map, you get its latitude and longitude. Let's say it is:
lat = 44.78
lon = 20.45
Then, take a look at this answer: Calculating new longitude, latitude from old + n meters.
It should give you the distance of 100 meters, but as latitude/longitude offset. Once you apply the formula from SO answer, you would get:
lat_offset = 0.0008983152841195215
lon_offset = 0.001265559599380766 // note that this lon_offset contains lat in formula (take a look at the link above)
Finally, apply the calculated in your formula:
west_lat = lat
west_lon = lon - lon_offset
east_lat = lat
east_lon = lon + lon_offset
north_lat = lat + lat_offset
north_lon = lon
south_lat = lat - lat_offset
south_lon = lon
Note: These formulas does not work for positions that are very close to south or north pole

is it possible with ruby geocoder to get a location 1 mile due south of a given latitude and longitude

If given a specified latitude and longitude, can I get a location 1 mile due south of it. Say I am given:
event lat: 34.0522342
event lon: -118.2436849
and I want a location 1 mile south?
I have seen this: https://gis.stackexchange.com/questions/5821/calculating-latitude-longitude-x-miles-from-point but just want to do this in geocoder.
The longitude doesn't change when you're moving south.
One degree of latitude has the same length everywhere : 69.1 miles.
As long as you're not exactly on the North Pole and you're more than 1 mile away from the South Pole, you basically need :
lat -= 1/69.1
Moving west or east, only the longitude would change but you'd need to take the latitude into account. One degree of longitude is 69.1 miles on the equator but 0 mile on the Poles.

Geolocations - How to check if 2 circles are overlapping

Let's say we have 2 locations (latitude,longitude) and each location has a radius (it may be different from each other), making a circle. How to check if these 2 circles are overlapping?
Check if the distance between the centers is smaller than the sum of the radii.
Say for circles A and B with radius Ar and Br respectively, and coordinates (Ax, Ay) and (Bx, By) respectively, the distance between the circles is
D = sqrt( (Ax - Bx)2 + (Ay - By)2 )
They overlap when
D < Ar + Br
There's a catch, however: the centers of the circles are placed on a sphere. The shortest distance between them is a straight line, beneath the sphere's surface. The distance between them following the surface will be larger. For instance, the distance between the North and South pole is 2 Earth radii, but the path on the surface will be 2π Earth radii. Also, these circles don't overlap. So, the above equations only hold when the distances are relatively small.

Compute equidistant GPS point around a center

I have a question about some GPS calculations.
My problem is as follow :
I have a specific point P, and I want to compute N points around P.
Here is the algorithm :
P = (x, y) // latitude, longitude
N = 8
angle_size = 360/N
points = []
for i in 1..N
points.push compute_100meter(P, angle_size*i)
end
In this example, I'm trying to compute 8 equidistant point within 100 meter from P.
Is anyone know a ruby gem allowing me to do so ?
My problem is to write the content of compute_100meter
EDIT:
I have to take into account the earth curvature and get the point coordinates in degree (latitude, longitude).
As long as the radius is small enough (and 100 meters should be, unless you're right next to the north or south pole), a simple linear approximation should do well enough:
def perimeter_point(lat, lon, angle, radius)
# convert angle from degrees to radians
angle *= Math::PI / 180
# convert meters to degrees approximately, assuming spherical Earth
radius /= 6371000 * Math::PI / 180
# calculate relative length of the circle of longitude compared to equator
scale = Math.cos( lat * Math::PI / 180 );
# add offsets to longitude and latitude and return them
# (I'm assuming that angle = 0 means due east)
lat += radius * Math.sin(angle)
lon += radius * Math.cos(angle) / scale
return lat, lon
end
Note that, if your center point is near the 180th meridian, this could return longitudes below -180 or above +180. If that's a problem, check for it and normalize as needed. (Output latitudes outside the &pm;90 range are also technically possible, if the center point is near the north or south pole, but the approximation I used breaks down close to the poles anyway.)

Convert degree to radians in ruby

I have a latitude and longitude in degrees and I want to convert these into radians. How can I do that?
P.S. I am using sphinx search engine and it requires values in radians
Just the same way you convert degrees to radians in real life:
radians = degrees * Math::PI / 180
There are 360 degrees and 2 pi radians in a circle. So to convert degrees to radians, divide by 360 and multiply by 2 * pi (approx. 6.28).
Or equivalently, divide by 180 and multiply by pi.

Resources