How do I determine if a lat/lon pair falls within the radius of another lat/lon pair? - geolocation

Here's an example of what I want to figure out:
Device A is at 40.7128 / -74.0060 (lat/lon)
Location B is at 40.730610 / -73.935242
Radius = 10 miles
And the question:
Is Device A within the radius around Location B?
I don't care about the language or technology, just want to get the job done. I know some Python, JS, and Java.
Anyone know a good/efficient way to approach this?

I would calculate the distance between the two points and check if it's inferior to the radius.
This thread gives some ideas on how to implement it in python: Getting distance between two points based on latitude/longitude

Related

How to convert geo coordinates to meters with a specific starting point?

I need to convert geo coordinates to meters. I already learned about proj4 and etc.
But I need to be able to specify the "zero" coordinate from which the countdown of meters will begin. And the error should be less than 500 meters.
Is it possible?
Two approaches:
1) UTM: This system has predefined origin and all the meters grids are calculated from there. However the calculation are almost accurate
https://en.wikipedia.org/wiki/Universal_Transverse_Mercator_coordinate_system
2)
meters_XY=LatLonToMeters(x,y)
meters_xOrigin_yOrigin=LatLonToMeters(xOrigin,yOrigin)
Take difference of meters_XY and meters_xOrigin_yOrigin. This way you will have meters from your origin.
Code for LatLonToMeters is available at:
https://github.com/Prafulljohri/gmap-tile-generator/blob/master/gmaps-tile-creator/src/gov/ca/maps/tile/geom/GlobalMercator.java

How to calculate minimum distance between point and line in 3D space

I need to calculate the minimum distance between a 3D point (latitude, longitude, elevation) and a line (defined as two points).
The elevation is not necessary on the ground, I need to consider flying objects.
I only found an article that explains how to do that on a generic space but my points are defined with lat/lon/altitude(meters).
Thank you for pointing in the right direction, in my case I need to do that in Javascript but couldn't find any library that takes into consideration the altitude.
Point-Line Distance--3-Dimensional
If you want to compare a 3d point to a 2d line, I suppose you mean a "line" on our earth, at elevation 0. Take a look at st_distance in postgis.
If I understand you correctly, that'll give you what you want.
https://postgis.net/docs/ST_Distance.html

Calculate location by distance from other location

I know there are tons of questions (and answers) on how to get the distance between two CLLocations. But I didn't find one single hint on how to do this the other way round.
Here's the concrete situation: I have one CLLocation, one distance (let's say 200 meters) and one direction. Now I need to know how to calculate the location that is 200 meters away (in a specified direction) from the first location. I don't know what could be the best format for the direction. Maybe the best would be in degrees (north = 0, east = 90, south = 180, west = 270).
In a nutshell, I need a method that could be defined like
-(CLLocation*)locationWithDistance:(int)meters inDirection:(int)degrees fromLocation:(CLLocation*)origin;
Thanks a lot for your help.
For more accurate method use this formula in the link
http://www.movable-type.co.uk/scripts/latlong.html
Please find the section "Destination point given distance and bearing from start point". Apply it in your code
There is C code already written to do exactly that. It is posted on github here. There is a degrees and radians version of the function in UtilitiesGeo.c/.h files. It could have been written with an Objective-C style but I wanted it to be portable to any C based project.
/*-------------------------------------------------------------------------
* Given a starting lat/lon point on earth, distance (in meters)
* and bearing, calculates destination coordinates lat2/lon2.
*
* all params in degrees
*-------------------------------------------------------------------------*/
void destCoordsInDegrees(double lat1, double lon1,
double distanceMeters, double bearing,
double* lat2, double* lon2);

Detecting a certain Latitude / Longitude is in a US State

I know that most people will view this question and point me to Google Geocode - but I'm looking for a mathematical formula that allows someone to take a Lat/Lng point and see if its inside a US state (or a bounding box). Is there a way via PHP, that I can do a calculation to see if a point is in a certain Box (such as California)?
Well, there's no formula that'll tell you anything about what states is where (it would have totally been a spoiler as to the outcome of the US-Mexico war if there was!) So you'll need to get that data from somewhere.
This then turns into one of two problems, depending on the degree of accuracy you want.
If you have details of a bounding box that is rectangular when shown on a Mercator or similar projection (that is, it has degrees of latitude for north and south, and of longitude for east and west), then the formula is simply:
inBox = latitude <= north && latitude >= south && longitude <= west && longitude >= east
If you have more detail, and have a series of points that defines the border of the state (obviously, the more points, the more precision) then it becomes a variant of the point-in-polygon problem, with a guarantee of only involving simple polygons (no US state has a border that crosses itself, nor completely surrounds that used in this C code. It's possible that there would be edge cases affected by the fact that this is a 2D-plane algorithm rather than a spherical one, but I imagine you'd need to have some pretty precise data on the boundaries of the states for the imprecision from the algorithm to be greater than that caused by the data.
The simplest way I would think is using bound box for each state, that can be found from Flicker Geo API, an example for CA- https://www.flickr.com/places/info/2347563

What earth radius should I use to calculate distances near the Poles?

I'm monitoring a GPS unit which is on it's way from Cape Discovery in Canada, to the North Pole. I need to keep track of the distance travelled and distance remaining each day, so I'm using the Haversine Formula, which I'm told is very accurate for smaller distances.
I'm really bad a Math, but I have a sneaking suspicion that the accuracy depends greatly on the radius of the Earth, and since the universe decided to make Earth out of an oblate spheroid, I have a choice of approximations for Earths radius to choose from.
Since I'm monitoring coordinates very close to the north pole, I'm wondering wether anyone knows which radius is going to provide the most accuracy.
Mean Equatorial: 6,378.1370km
Mean Polar: 6,356.7523
Authalic/Volumetric: 6,371km
Meridional: 6367km
Or any other kind of Radius that anyone knows about?
I'm hoping some maths or cartography whizz might know the answer to this one.
You could approximate the actual radius at the point(s) where you're measuring the distance (provided that you calculate a sequence of relative small distances).
Assuming the earth being an ellipsoid with the main axis a being the mean equatorial radius and the second axis b being the mean polar radius, you can calculate the point on the ellipse represented by these two axes by using the current latidude. The calculation is shown and explained here.
(Note: This ellipse can be thought as a cross section of the earth through the poles and the point where you want to calculate the distance)
This gives you a point q=(qx,qy), the radius at this point being r=sqrt(qx^2+qy^2). That's what I'd use for calculating the Haversine formula.
It doesn't really matter - they are all going to be wrong if you just treat the earth as a sphere. I would probably use the polar since you are mostly going north.

Resources