How do I find the lat/long that is x km east/west of a given lat/long? - position

I have two given lat and lon points. For example, lets assume I have two positions (point_1 and point_2) at coordinates (lat1, lon1) and (lat2, lon2).
I would like to calculate a third point, that is on the same latitude as that of point_2, but x km to the east or west of point_2. So the third point will have the same latitude as point_2, but a different longitude depending on distance x (in kilometers), in other words point_3 will be (lat2, lon?).
I am writing this in IDL, but any other language or formulas would be very welcome.
Thanks

You don't use point 1 anywhere, do you? Let's say our point is P = (lat, lon)
The first rule of problems like this: draw a picture! From a cross-section of the earth, you can see that the radius of the circle centered on the earth's axis, going through your two points, is R*cos(lat). (R is the earth's radius. I hope you don't need to consider the earth an ellipsoid here.) The length x therefore takes up an angle (in degrees) of 360*x/(2*pi*R*cos(lat)). The new point you want is then:
P' = ( lat, lon +- 180*x/(2Rcos(lat)) )
I'm assuming you're using -180 to 0 for west longitude, so you have +/- for east/west. You'll have to check if you need to wrap around. Pseudo-code:
if (lon < -180)
lon += 360
else if (long > 180)
lon -= 360
Just for fun: if you do care about the earth being ellipsoidal, the radius of the circle is (instead of R*cos(lat)):
1/sqrt(tan^2 lat / Rp^2 + 1 / Re^2)
where Rp is the polar radius and Re is the equatorial radius. If Rp = Re, this reduces to the original formula, since 1 + tan^2 lat = sec^2 lat

import math
R = 6378.1 #Radius of the Earth
brng = 1.57 #Bearing is 90 degrees converted to radians.
d = 15 #Distance in km
#lat2 52.20444 - the lat result I'm hoping for
#lon2 0.36056 - the long result I'm hoping for.
lat1 = math.radians(52.20472) #Current lat point converted to radians
lon1 = math.radians(0.14056) #Current long point converted to radians
lat2 = math.asin( math.sin(lat1)*math.cos(d/R) +
math.cos(lat1)*math.sin(d/R)*math.cos(brng))
lon2 = lon1 + math.atan2(math.sin(brng)*math.sin(d/R)*math.cos(lat1),
math.cos(d/R)-math.sin(lat1)*math.sin(lat2))
lat2 = math.degrees(lat2)
lon2 = math.degrees(lon2)
print(lat2)
print(lon2)

Related

random lat lng coordinates within x km's of another lat lng

We have a location specified by a lat & lng
We want to generate some other random lat & lng locations within say 20km of this location
Does anyone have a good formula for this
Generate two uniform random values r and Fi in range 0..1
Calculate distance as d = Radius * Sqrt(r) (description here for plane circle)
Calculate bearing as Theta=2 * Pi * Fi
Find lat/lon coordinates for given central point and calculated d and Theta as described here in section Destination point given distance and bearing from start point
JavaScript:
(all angles
in radians)
var φ2 = Math.asin( Math.sin(φ1)*Math.cos(d/R) +
Math.cos(φ1)*Math.sin(d/R)*Math.cos(θ) );
var λ2 = λ1 + Math.atan2(Math.sin(θ)*Math.sin(d/R)*Math.cos(φ1),
Math.cos(d/R)-Math.sin(φ1)*Math.sin(φ2));
where φ is latitude, λ is longitude,
θ is the bearing (clockwise from north), d being the distance travelled,
R the earth’s radius

Calculate distance between 2 objects by calculating the mid points

I have labelled image objects with their respective height ,width and coordinates of bounding box around them in pixels. I have calculated the mid point of both the bounding box.how to compare the mid point of both the objects and calculate distance between two them.
I assume you mean the 2D distance within the image? In this case, find the Euclidean distance:
const double dx = a.x - b.x;
const double dy = a.y - b.y;
const double distance = sqrt(dx * dx + dy * dy);
where a and b are your mid-points.

How to transform a distance from degrees to metres?

I'm using OpenLayers with an ordinary mercator map and I'm trying to sample a bounding box by finding a grid of points in latlong.
The bbox is expressed in latlon, e.g.
48.1388,-15.3616,55.2057,-3.9359
I can define a distance in degrees (e.g. x: 2.5, y: 2.4) and work out the points from there.
But I'd like to express this distance in metres (e.g. 50000) in order to relate it to the user mindset (people understand metres, not degrees).
How can I convert this distance? I know how to reproject a point, but not a distance.
Thanks for any hints!
Mulone
Use the haversine formula to get the distance between two points of lat/long. This assumes the earth is a sphere (which is, for most cases, "good enough").
A Javascript implementation of it (shamelessly stolen from here) looks like this:
var R = 6371; // km
var dLat = (lat2-lat1).toRad();
var dLon = (lon2-lon1).toRad();
var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(lat1.toRad()) * Math.cos(lat2.toRad()) *
Math.sin(dLon/2) * Math.sin(dLon/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c;
Without allowing for the slightly non-spherical shape of the earth,
One minute of latitude North to south = 1 Nautical Mile = 6075 feet
So One degree = 60 Minutes = 60 * 6075 feet
There are 3.28 Feet in a meter so
One degree = 60 * 6075 / 3.28 Meters = 111,128 meters
Alternatively, one minute of Latitude = 1,852 Meters
So One degree = 60 * 1852 meters = 111,120 meters
I'm not sure which is more accurate...
For One degree of Longitude, do the same thing, but multiply by the Cosine(Latitude) since the Longitude lines converge, (get closer together), as you move north.
Note: if you're using a calculator or computer, make sure you use the right units for the latitude (degrees or radians) that your device requires or is set to use.
The transformation between degrees and metres varies across the Earth's surface.
Assuming a spherical Earth, degrees latitude = distance * 360 / (2*PI * 6400000)
Note that longitude will vary according to the latitude:
Degrees longitude = distance *360 * / (2*PI* cos(latitude) )
The above is for the Earth's surface, and does not use the Mercator projection. If you wish to work with projected linear distance, then you will need to use the Mercator projection.

algorithm of calculating the slope between line relatively to center coordinate

Please help with the algorithm of calculating the slope
So we have a Cartesian coordinate system. X right at Y the top. There is a line which passes through the center of coordinates.
Needed to determine the angle relatively to the axis OX.
So here's what I'm doing
Certain functions transferred to the origin (top line) and end of line
Determine dx, dy
Hildren releases two parameters in atan2 (dy, dx)
Returns the result in radians.
But! I atan2 works only within 180 degrees. After 180 goes in another direction.
So the question: what is the correct algorithm for finding the angle? Do I need to take dy, dx values in magnitude? How to make the arctangent calculated for all 360 and more? I would be glad to hear specific algorithms, or pieces of code comments.
Thanx!
static inline CGFloat angleBetweenLinesInRadians2 (CGPoint line1Start, CGPoint line1End)
{
CGFloat dx = 0, dy = 0;
dx = line1End.x - line1Start.x; / / whether to do fabs (line1End.x - line1Start.x);
dy = line1End.y - line1Start.y;
CGFloat rads = atan2 (dy, dx); / / whether to do fabs (rads)
return rads;
}
atan2() is supposed to return a value in the interval [-pi,pi] (i.e. [-180, 180] ), and works with the signs of x and y to figure out the quadrant. (C++ ref)
So technically, you have 360 degrees.
A formula to calculate an angle from 0 to 360 degrees :
f(x,y)=180-90*(1+sign(x))* (1-sign(y^2))-45*(2+sign(x))*sign(y)
-180/pi()*sign(x*y)*atan((abs(x)-abs(y))/(abs(x)+abs(y)))
x=x2-x1 and y=y2-y1 .

Calculating co-ordinate of a point on a path given a distance

I'm working on a project that surveys the condition of a road or highway using a calibrated trip computer connected to a rugged-PC. An operator keys in defect codes as they travel along a pre-defined route.
I need to show an indicator on the map screen that shows the vehicles current position, taking into account the distance data from the trip computer.
I know the exact lat lon co-ordinates at the starting point of each section of road, and the road is made up of a series of points.
The question is: how can I calculate the lat lon co-ordinates of the vehicle assuming that it has continued on the route and traveled a certain distance (e.g. 1.4km). The co-ordinates would be 'locked onto' the road line, as shown in blue on the diagram below.
Thanks,
Alex
Here is some Java-ish pseudocode, giving a solution using linear interpolation between points.
inputs: distance, points
// construct a list of segments from the points
segments = [];
for(point in points) {
if(not first point) {
seg = new segment(last_point, point)
add seg to segments
}
last_point = point
}
// calculate current lat and lon
for(segment in segments) {
if(distance < segment.length) {
alpha = distance / segment.length
lat = segment.start.lat * (1.0 - alpha) + segment.end.lat * alpha
lon = segment.start.lon * (1.0 - alpha) + segment.end.lon * alpha
return (lat, lon)
} else {
distance = distance - segment.length
}
}
You might also want to consider spline interpolation, which could be more accurate. It will require some more maths, but the above idea can still be applied.

Resources