I have a list of cities, each of them obviously has a longitude and latitude.
Now selecting one of these cities, i want to obtain all the other cities that have a longitude / latitude in a range of 50 km from the selected city.
What formula should I use?
I am only interested in the mathematical formula to convert km to latidutine and longitude from a know city position
Then i will calculate the maximum and minimum latitude and longitude, for considering an acceptable range. (like a Square)
tks
I don't want to calculate the distance between two points!+
I want to calculate min e max latitude and longitude and then filter my cities by this coordinates.
I've found a sample in Php that worked for me.
(i've ported it to C#)
http://blog.fedecarg.com/2009/02/08/geo-proximity-search-the-haversine-equation/
You'd probably want to use the Haversine formula.
You may want to check out the following articles for further reading and for a few implementations in various languages:
Calculate distance, bearing and more between Latitude/Longitude points by Chris Veness.
Calculate Distance Between Two Points on a Globe in 11 languages.
You are looking for the Great Circle calculation. It returns the shortest distance across the surface of the planet given two points and their respective latitude and longitude. There is a great Wikipedia article here:
http://en.wikipedia.org/wiki/Great-circle_distance
There is a pretty decent implementation - source code in javascript here:
http://trac.osgeo.org/openlayers/wiki/GreatCircleAlgorithms
Related
I am trying to calculate the direction vector and distance between two points with a short distance in between (for example 1 meter). Normally I get those coordinates from two rovers in the RTK system with high accuracy.
So, what is the best and most accurate formula for this purpose as I went through too much mathematics and was confused about which to use?
Thanks
This question is more about math than programming. I am programming a function which takes a square of geographical distance between 2 points with known latitude and longitude as an argument. There's a straightforward way to compute it: calculate dot-product, then take arccos, and multiply by Earth radius. Then square the result and you get the square of geographical distance assuming Earth is a sphere (which is acceptable approximation in my case).
However I would like, if possible, to avoid an expensive arccos() call, especially given that I can easily obtain the square of the tunnel distance (by either Pythagorean theorem or the dot product).
I also read here http://en.wikipedia.org/wiki/Geographical_distance#Tunnel_distance about underestimation formula which I can use to get tunnel distance from geographical distance. In my case however, I need the opposite (tunnel to geographical), and for the square. I played with Taylor series and got a rough approximation:
G square = T2 / (1 - (T2/R2)/12.0) // here G2 is square of geographical distance, T2-square of tunnel, R2-square of Earth radius. I also was able to get a more accurate formula:
G square = T2 / (1 - (T2/R2)/12.0 - ((T2/R2)^2)/240.0).
This last formula gives error of only 3.8mm for G=1000 km, and less than 50cm for G=2000 km.
However, I still cannot mathematically prove this formula, at least when using Taylor series. Wonder if it's possible to get the mathematical proof and also expansion of this formula for larger values of G/T. Thanks!
Why tunnel distance from geo distance?. There is no geo distance. There are many possibilities to calculate a distance between two points on earth.
Just take the two lat/lon cooridnates, and then calculate the distance between them using a simmple cyclindrical projection.
This needs only a cos(centerLatitude), and a multiplication with a factor. (meters_per_degree)
See also Cyclindrical equi distant projection. Up to some kilomters (abou 10 to 100) this gives sufficient accuracy.
I am looking to calculate the distance between points (about 47K) and the closest X countries (of all world countries). I have imported the lat/long of points as SpatialPoints, and loaded a world map as a SpatialPolygons. I think I could build off of the advice given here:
SpatialLinesDataFrame: how to calculate the min. distance between a point and a line
It looks like I have to calculate the distance between all countries and all points and then extract the X closest, which is a bit intense with so many points.
In short, is there a way to impose a polygon limit? If not, what would you suggest- my only thought is to import a smaller number of points and then loop through this code (I am a new R user).
Thanks!
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.
Spatialite has a the ability to calculate the distance between 2 geometries with it's Distance() function. There are other functions that work on LINESTRINGs. However I can't find out what units it returns it in. Is it metres? If I have 2 points, how do I calculate the distance between them in a spatialite query?
(For the record I'm using SRID 4326, i.e. WSG 86, i.e. the old traditional degrees of latitude and longitude).
the unit returned by ST_Distance(), ST_Length() and ST_Area()
exactly is the one defined by the corresponding SRID.
consequently, if you are using latitude and longitude (SRID=4326,
WGS 84), any length will be measured in DEGREES, and any area in
SQUARE DEGREES.
if you are interested in giving a more conventional
unit (METERS, SQUARE METERS), you simply have to project
your geometries into some appropriate 'planar' CRS (e.g. UTM)
using ST_Transform()
In version 2.4.4 and higher there is a function PtDistWithin() which returns meters for a distance query. See doc. section "SQL functions for distance relationships":
http://www.gaia-gis.it/spatialite-2.4.0-4/spatialite-sql-2.4-4.html#p13