Forum Moderators: phranque

Message Too Old, No Replies

longitude/latitude question

how to determine if a point falls within a certain range?

         

thewaiter

8:06 am on Dec 14, 2008 (gmt 0)

10+ Year Member



Hello and thanks for taking the time to read my question.

I was wondering how I can determine if a point using it's longitude and latitude, falls within a range of other points using their longitude and latitude.

Math and I have a great understanding, I don't harm it and it doesn't harm me so I am not really sure how to go about figuring this out.

Say my point is at: -87.700382,41.997543
And the range I want to compare it to is this:
87.6739609156651,41.9615320224265
87.6741899156652,41.9615270224267
87.6745139156657,41.9615200224266
87.6751639156662,41.9615030224266
87.6752379156663,41.9615010224266

I'm just using this as an example, I'm not entirely sure if this range would form much of a shape but I just wanted to show what I am doing. If anyone could give me an idea of what to do, I would really appreciate it.

kaled

1:54 pm on Dec 14, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is a non-trivial problem i.e. it requires a method rather than the application of a simple formula.

Try searching for point in polygon.

Kaled.

vincevincevince

3:31 pm on Dec 14, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Start with exclusion:
1) Find the lowest 'x' value - is your point lower than that? If so = not in shape
2) Find the highest 'x' value - is your point higher? If so = not in shape
3) Find the lowest 'y' value and test likewise
4) Find the highest 'y' value and test likewise

If you are still in the shape, then the next stage is to find the points which are nearest vertically and horizontally. From that your problem is reduced to either a triangle or a quadrilateral - both of these are much easier to solve than an abstract polygon.

If you aren't really very fussy about accuracy, just do the exclusion part at the beginning and leave it at that.

londrum

4:41 pm on Dec 14, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



here's the thing that i use in my php scripts... don't know if it helps.
you'll have to change latitude1 and latitude2 to whatever the 2 latitudes are that you are trying to compare (and likewise with longitude1 and longitude2)
the 0.5 bit is the bit that you have to change. 0.5 checks everything within a radius of about half-a-mile (or about that, i'm not too sure on the precise distance)

if(sqrt((69.1 * (latitude1 - latitude2)) * (69.1 * (latitude1 - latitude2)) + (53.0 * (longitude1 - longitude2)) * (53.0 * (longitude1 - longitude2))) < 0.5) {

   do some stuff

} else {

   don't do some stuff

}

thewaiter

2:45 am on Dec 15, 2008 (gmt 0)

10+ Year Member



Kaled, thanks for the name of that, I never would have figured that out.

I found a couple of semi-solutions. One was a php solution where it takes points and compares them, however it doesn't seem to work for latitude and longitude in that I can't compare 1 point against multiple ones.

The other solution I found uses MS Virtual Earth. It takes all my points and makes a polygon, then if I click inside the polygon in the map, an alert box returns true, if I click outside, it returns false.

This one in theory work great however a JavaScript master, I am not. I am going to take this to the Javascript board and see if there is a way for me to instead give the point and let it do it's thing and just write to the screen if it's true or not.

Thanks for everyone's help, I really appreciate it.

Megaclinium

11:02 pm on Dec 31, 2008 (gmt 0)

10+ Year Member



If you want to exclude by distance between two lat/lon pairs, this is fairly easy with a two line function of spherical trig. melissadata has the formula in their literature and you can google for distance calculations also.

several websites can verify distances you calculate via online form. counties, cities and closer can also be correlated to lat/lon pairs to use in calcs. for counties we get one pair which is the centroid of the county. street level DBs can be bought with address lat/lon

g1smd

12:28 am on Jan 1, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Great circle distances between two points on a sphere is a fairly trivial problem. There's a couple of sin and cos in the formula. [en.wikipedia.org...]

You do need to be aware when the shortest distance between the two points passes through the Equator or passes through either 0 degrees E/W or 180 degrees E/W longitude as you'll need to cater for that.

Also, you'll need to decide which Earth Model you want to use... assume a prefect sphere with which radius distance (several are quoted), or use the corrections for an oblate spheroid (which is what the Earth actually is)?