I'm looking for the R function (or code) equivalent to the MATLAB function scxsc
This gives the Intersection points for pairs of small circles on a sphere.
The application is "a vessel is at bearing X1 and distance d1 from point 1 and bearing X2 and distance d2 from point 2. What is it's position in Lat, lon?"
I see plenty of examples for the intersection of great circles but not with small circles
I found the answer on https://gis.stackexchange.com/questions/48937/calculating-intersection-of-two-circles/273855#273855
NB I have a suspicion that in the two (unmodified) lines below ...
lat1 = rad2deg(atan2(point1[2] ,point1[1]))
lon1= rad2deg(asin(point1[3]))
and
lat2 = rad2deg(atan2(point2[2] ,point2[1]))
lon2 = rad2deg(asin(point2[3]))
... the lat and lon are inverted !
It worked much better for me with these changes!
Related
In the past, in MATLAB, I've calculated the index of a point given its lat and lon using a great circle distance calculation. I'll share my code with you. I'm fairly stumped as to what the equivalent function in R would look like or whether on exists? I've found some code that shows the distance between two points, but none that help me to index my data.
This is my MATLAB code!
%% Define latlon grid and coordinates (lon follows lat)
lon_grid = transpose([40.1 40.12 40.14; 40.3 40.32 40.34; 40.5 40.52 40.54]);
lat_grid = transpose([30 30.2 30.4;30.02 30.22 30.42; 30.04 30.24 30.44]);
coord = [30.4125 40.4043];
%% Compute great circle distance
dist = distance('gc',coord(1),coord(2),lat_grid,lon_grid);
%% Retrieve index of minimum distance
[value,array_index] = min(distance(:));
[i,j] = ind2sub(size(dist),array_index);
The "dist" calculation is sort of the party piece here. You should be able to use the provided code to reproduce the results and see what I'm hoping to achieve in R.
Again, what might a comparable function in R be given I have the following:
A grid of latitude points
A grid of longitude points
Two points, in degrees, for the latitude and longitude of my position.
maybe this works:
library(geoshpere)
dist<-apply(coord, 1, FUN=function(p) distHaversine(p, lonlat_matrix))
It is well known that the Haversine formula is a useful mechanism in which to calculate distances between geographic coordinates. Obviously, there are certain packages in R which provider further refinements; accounting for the ellipsoidal nature of the earth.
What I cannot find, however, is any method for finding the straight-line distance between NED coordinates: that is, latitude, longitude and depth.
This type of distance calculation is essential for research on events which occur deep under oceans, or under the earth's crust in general.
Is anyone aware of a method to assist with this type of distance calculation on a sphere? On the earth? If not, does anyone have any ideas what the best trigonometric approach to this problem would be?
Linear distances are much easier to compute in a Cartesian coordinate system, so the first step is to convert your NED coordinates (i.e. long-lat-height) coordinates to some such system. In the open source world, PROJ4's "geocent" projection, which gives locations in meters in an orthogonal x-y-z coordinate system, is a good choice.
Simon Urbanek's proj4 package provides a good lightweight means of accomplishing that conversion. To demonstrate it's use, I'll write a little wrapper function that takes two points in NED coordinates and computes their separation in meters. (The one complication is that the ptransform() expects its latitude and longitude coordinate in radians; hence the divisions by 180/pi in the function's first and second lines.)
library(proj4)
findDist <- function(x=c(0,0,0), y=c(0,0,0)) {
x <- matrix(x/c(180/pi, 180/pi, 1), ncol=3)
y <- matrix(y/c(180/pi, 180/pi, 1), ncol=3)
xx <- ptransform(x, src.proj="+proj=longlat +ellps=WGS84",
dst.proj="+proj=geocent +ellps=WGS84")
yy <- ptransform(y, src.proj="+proj=longlat +ellps=WGS84",
dst.proj="+proj=geocent +ellps=WGS84")
sqrt(sum((yy-xx)^2))
}
## A sanity check. (Find distance between two points at 0°N 0°E, one on the
## surface and one 100m below the surface (i.e. at a height of -100m).)
findDist(c(0,0,0), c(0,0,-100))
## [1] 100
## Distance from North to South Pole
findDist(c(0,90,0), c(0,-90,0))
## [1] 12713505
## Diameter of the earth at its equator
findDist(c(0,0,0), c(180,0,0))
## [1] 12756274
You can use Matlab function ecefOffset to compute distance between two geographical locations in 3D.
https://www.mathworks.com/help/map/ref/ecefoffset.html
I have a set of latitudes and longitudes , so this is the data for an animal as it moves in time. what i want to do is to calculate turning angle, that is by what angle it turns between every movement. so say i have point 1, point 2 and point 3 with latitude and longitude value corresponding to each point(animal moves from point 1 to point 2 to point 3 and so on) and i want to calculate the angle between these 3 points, point 2 being the middle point. what should i do? my OS is windows and i am using R for analysis.
so here is my sample data:
longitude latitude
36.89379547 0.290166977
36.89384037 0.290194109
36.88999724 0.286821044
36.88708721 0.288339411
36.88650313 0.29010232
36.88563203 0.289939416
36.88545224 0.290924863
they are in decimal degrees
Using the function trackAzimuth in maptools:
library(maptools)
trackAngle <- function(xy) {
angles <- abs(c(trackAzimuth(xy), 0) -
c(0, rev(trackAzimuth(xy[nrow(xy):1, ]))))
angles <- ifelse(angles > 180, 360 - angles, angles)
angles[is.na(angles)] <- 180
angles[-c(1, length(angles))]
}
The trackAzimuth function is a simple loop wrapper around gzAzimuth. See ?gzAzimuth for references on calculating directions on the sphere.
Using your data:
x <- read.table(text = "longitude latitude
36.89379547 0.290166977
36.89384037 0.290194109
36.88999724 0.286821044
36.88708721 0.288339411
36.88650313 0.29010232
36.88563203 0.289939416
36.88545224 0.290924863", header = TRUE)
trackAngle(as.matrix(x))
[1] 10.12946 111.17211 135.88514 97.73801 89.74684
EDIT: I had to remove first/last angles from the function, something I was doing after the fact with this function elsewhere. Should be right now. :)
Also, the packages adehabitatLT and argosfilter contain functions to calculate track directions and angles.
Your data points vary over only a small range. We can look at one small patch of Earth's surface and pretend it's flat, two dimensional. You have to figure out the scale of how many km, meters, miles, whatever your favorite unit is, corresponds to one degree of latitude, and for one degree of longitude. The latter depends on latitude - it'll be the same as the scale for latitude when near the equator, but if you are standing within arm's length of the north pole, one step will take you through fifty degrees. Set up x,y coordinates where x=0 is at longitude 36.88000, and y=0 is latitude 0.29000.
So, now you have a series of (x,y) points. Take the differences from each point to the next: P2-P1, P3-P2, etc. These could be called "displacement vectors" but other terms may be used in other fields than where i'm from. Call them V1, V2, etc. Use dot products and norms: dot(V1,V2) = magnitude(V1)*magnitude(V2)*cos(a) where a is the angle by which V2 deviates from the direction of V1. Repeat for V3 and V2, and so on.
R has all the tools to do this, but I don't know enough syntax of R to give examples.
I'm trying to translate latitude and longitude coordinates into coordinates of my own defined map. For instance:
51.876324 maps to 659.33 in my map
0.943395 maps to 2585.17 in my map other such examples might be:
51.875737 - 505.77
0.943564 - 2055.39
51.875883 - 1090.58
0.944658 - 1935.42
My math skills/knowledge is kind of rusted, but should I use substitution to find out what is the factor by which the coordinates can be mapped into mine?
Any hints are appreciated
Cheers!
It is not quite possible to answer this question exactly without knowing details about the map projection algorithm used. While lat and lon are angular coordinates on the earth (almost) ellipsoid the other seem to be map (x,y) coordinates.
If your map covers only a small part of the surface you might approximate this by a simple transformation
x = lon * factor_x + offset_x
y = lat * factor_y + offset_y
with constants factor_x, factor_y and offset_x and offset_y. From two distinct points on your map you may derive these constants by inversion:
factor_x = (x1-x2) / (lon1-lon2)
offset_x = x1 - lon1 * factor_x
and same for y.
If I have the coordinates of a point (lat lon) and the azimuth angle how can I calculate what points are at "the end' of a distance of 10 miles.
Ex. I am watching North , I know I am at a certain point ... At 10 miles apart what coordinates has that geo point ?
This site has a pretty good collection of formulae. For your case,
Let lon1,lat1 be the starting point, θ the azimuth angle (also often referred to as the "bearing") in radians,
d the distance traveled (km), and R the earth's radius (approx 6371 km). Then you can find
the final coordinates lon2, lat2 :
lat2 = asin(sin(lat1)*cos(d/R) + cos(lat1)*sin(d/R)*cos(θ))
lon2 = lon1 + atan2(sin(θ)*sin(d/R)*cos(lat1), cos(d/R)−sin(lat1)*sin(lat2))
Note: d/R represents an angle in radians corresponding to the arc length d.
θ is measured such that North=0 degrees, East=90 degrees, and so forth.
That doesn't make much sense. Let's take the first formula
lat2 = asin(sin(lat1)*cos(d/R) + cos(lat1)*sin(d/R)*cos(θ))
sin(lat1)*cos(d/R) -> as sin and cos will never be larger than 1, the largest result can be 1
cos(lat1)*sin(d/R)*cos(θ) -> same as above: the biggest possible result is 1
=> the result is that lat2 according to that formula can be 2 at most.
You need to also have a bearing to calculate this distance as well. For very short distances, great circle distance (the distance along the path of the earth) will be very close to cartesian distance, but the site provided by Jim Lewis' answer is a nice interactive site. This site also has a very extensive set of lat/lon formulas http://williams.best.vwh.net/avform.htm.