How to get the nearest X points from point with latitude and longitude? - asp.net

I have a list of points with latitude and longitude coordinates, from which I want to enter a point say X. I need help coming up with an algorithm to determine the closest 3 list members to that point x.

You'll probably have to use the Haversine Formula. It calculates Great Circle distance between two points on the Earth's surface. Here's a good article explaining that, and here's an answer to a question similar to yours. Hope that helps!

See the method computeDistanceBetween() in the google.maps.geometry.spherical namespace:
https://developers.google.com/maps/documentation/javascript/reference#spherical
Then, here's an old V2 demo that uses Array.sort():
http://maps.forum.nu/gm_array_sort.html
You could combine the two, implementing
computeDistanceBetween() instead of compareDistance() as the sorting function.
Note that this is all done client side.

Related

Mathematic way to know 50 km around an specific coordinate

I'm investigating about the best way to know what coordinates are around an specific coordinate.
I mean:
If am in the coordinate:
#29.817178,-95.4012915
How I can know if the coordinate: #30.00121, -93.13213 has 50km (or less) of my actual position?
You should use the Haversine formula, a good explanation can be found here with a javascript example.
Since you're dealing with latitude and longitude, you need to calculate the great circle distance between your two points.
The formula is a bit detailed so I won't reproduce it here, but it's explained in the above linked Wikipedia article.

Check which side of a plane points are on

I'm trying to take an array of 3D points and a plane and divide the points up into 2 arrays based on which side of the plane they are on. Before I get to heavily into debugging I wanted to post what I'm planning on doing to make sure my understanding of how to do this will work.
Basically I have the plane with 3 points and I use (pseudo code):
var v1 = new vector(plane.b.x-plane.a.x, plane.b.y-plane.a.y, plane.b.z-plane.a.z);
var v2 = new vector(plane.c.x-plane.a.x, plane.c.y-plane.a.y, plane.c.z-plane.a.z);
I take the cross product of these two vectors to get the normal vector.
Then I loop through my array of points and turn them into vectors and calculate the dot product against the normal.
Then i use the dot product to determine the side that the point is on.
Does this sound like it would work?
Let a*x+b*y+c*z+d=0 be the equation determining your plane.
Substitute the [x,y,z] coordinates of a point into the left hand side of the equation (I mean the a*x+b*y+c*z+d) and look at the sign of the result.
The points having the same sign are on the same side of the plane.
Honestly, I did not examine the details of what you wrote. I guess you agree that what I propose is simpler.
Following the 'put points into the plane's equation and check the sign' approach given previously. The equation can be easily obtained using SymPy. I used it to find location of points (saved as numpy arrays) in a list of points.
from sympy import Point3D, Plane
plane=Plane(Point3D(point1), Point3D(point2), Point3D(point3))
for point in pointList:
if plane.equation(x=point[0], y=point[1],z=point[2]) > 0:
print "point is on side A"
else:
print "point is on side B"
I haven't tested its speed compared to other methods mentioned above but is definitely the easiest method.
Your approach sounds good. However, when you say "and turn them into vectors", it might not be good (depending on the meaning of your sentence).
You should "turn your points into vector" by computing the difference in terms of coordinates between the current point and one of the points in the plane (for example, one of the 3 points defining the plane). As you wrote it, it sounds like you might have misunderstood that ; but apart from that, it's ok!
take into account the normal vector of the plane
example: for the point A=[-243.815437431962, -41.7407630281635, 10.0]
equation= -2663.1860000000006*Z +21305.488000000005=0
RESULt POSITIVE
but if equation= 2663.1860000000006*Z -21305.488000000005=0
RESULT NEGATIVE

Is there a library or framework for geodesic lat/long coordinate calculations?

I'm making a core-location driven app where I must calculate lots of things for a given latitude/longitude pair, such as:
Distance to another lat/long coordinate
The target lat/long coordinate when traveling a distance x into direction y
Is there something open sourced which can be used?
Since you are using CoreLocation, you can use CoreLocation's distanceFromLocation as #progrmr points out.
On the other hand, since you specified open source, and that ain't open sourced, you can look at GeographicLib implementations as suggested by #MikeT.
(My original answer, suggesting the Haversine formula, was flawed. As #MikeT points out, the Haversine formula is only valid for spheres. And the Earth is not a perfect sphere.)
Original, flawed answer:
It sounds like you want the Haversine formula.
The Wikipedia page for the Haversine formula explains what it is and (at the bottom, under "External links") contains links to many implementations. I haven't checked, but I have to imagine that at least some of them are open source projects.
There are some C functions on github that does heading from a coordinate pair, or destination coordinates given start and heading. Distance between coordinates you can do with CLLocation.
These are based on the Spherical Law of Cosines and derived from the algorithms here and here.
GeographicLib has been implemented in several programming languages, including Java. The library calculates lengths and related mathematical properties of geodesics on ellipsoids of revolution (or spheroid). The calculation errors are generally in the range of micrometers.
To find the distance between two coordinates:
GeodesicData g = Geodesic.WGS84.Inverse(lat1, lon1, lat2, lon2,
GeodesicMask.DISTANCE);
then get g.s12 for the distance between the two points.
The second example in the question, to project a location given distance and direction, is found using the Direct methods.

Calculate area of a object in cartesian plane

I wonder if someone can help me to find the area of a 2-D object in Cartesian plane , when we know coordinates of every points.
Eg : I want to calculate the area of a triangular. A(12,34) B(45,89) C(25,35)
I want a common algorithm to find any 2-D object's area.
Thank you.
Here you go, uses triangulation. This was literally the top result off Google when I searched "area of polygon given set of points". Please do your research before posting.
If your object is a simple polygon, there's no need to triangulate it to compute its area. There's a simple formula that depends only on the coordinates of the vertices. See http://en.wikipedia.org/wiki/Polygon#Area_and_centroid

Determine the centroid of multiple points

I'm writing a mapping application that I am writing in python and I need to get the lat/lon centroid of N points.
Say I have two locations
a.lat = 101
a.lon = 230
b.lat = 146
b.lon = 200
Getting the center of two points is fairly easy using a euclidean formula. I would like
to be able to do it for more then two points.
Fundamentally I'm looking to do something like http://a.placebetween.us/ where one can enter multiple addresses and find a the spot that is equidistant for everyone.
Have a look at the pdf document linked below. It explains how to apply the plane figure algorithm that Bill the Lizard mentions, but on the surface of a sphere.
poster thumbnail and some details http://img51.imageshack.us/img51/4093/centroidspostersummary.jpg
Source: http://www.jennessent.com/arcgis/shapes_poster.htm
There is also a 25 MB full-size PDF available for download.
Credit goes to mixdev for finding the link to the original source, and of course to Jenness Enterprises for making the information available. Note: I am in no way affiliated with the author of this material.
Adding to Andrew Rollings' answer.
You will also need to make sure that if you have points on either side of the 0/360 longitude line that you are measuring in the "right direction"
Is the center of (0,359) and (0, 1) at (0,0) or (0,180)?
If you are averaging angles and have to deal with them crossing the 0/360 then it is safer to sum the sin and cos of each value and then Average = atan2(sum of sines,sum of cosines)
(be careful of the argument order in your atan2 function)
The math is pretty simple if the points form a plane figure. There's no guarantee, however, that a set of latitudes and longitudes are that simple, so it may first be necessary to find the convex hull of the points.
EDIT: As eJames points out, you have to make corrections for the surface of a sphere. My fault for assuming (without thinking) that this was understood. +1 to him.
The below PDF has a bit more detail than the poster from Jenness Enterprises. It also handles conversion in both directions and for a spheroid (such as the Earth) rather than a perfect sphere.
Converting between 3-D Cartesian and ellipsoidal latitude, longitude and height coordinates
Separately average the latitudes and longitudes.

Resources