Jump to content

[SOLVED] Sorting distance from a point in a coordinate system (from a mysql table)


mirana

Recommended Posts

Hi, I've found many similar problems and solutions, but they're alot more advanced since they've all applied advanced formulas to calculate distances on earth. Also, way too advanced for me to understand most of it. My situation is in fact a flat 2d coordinate system.

 

My situation is:

I have a mysql db which has ID, X, Y (x and y are the coordinates)

 

goal:

 

write a php page which I can feed whichever x and y coordinate I need, and then sort the 10 closest coordinate points from the sql by distance, then ID if equal distance.

 

As far as I've understood, if I have at all, is that I could use mysql_fetch_array , and somehow sort it within the array.

 

Conjuring a formula to do the calculating of distance is the one thing I believe I AM able to achieve. it's the simple pythegorian theorem. Applying this inside an array to sort them by distance, is a problem.

 

SQRT((inputX - dbX)*(inputX - dbX) + (inputY - dbY)*(inputY - dbY))

 

If anyone is able to help me in the right direction with this, I would be humbly grateful.

Link to comment
Share on other sites

NOTE:

 

You can't use the alias "distance" in a WHERE clause so this will fail

SELECT id, x, y, SQRT((9-x)*(9-x) + (8-y)*(8-y)) as distance
FROM tmp
WHERE distance < $maxdist
ORDER BY distance

 

This is OK

SELECT id, x, y, SQRT((9-x)*(9-x) + (8-y)*(8-y)) as distance
FROM tmp
WHERE SQRT((9-x)*(9-x) + (8-y)*(8-y)) < $maxdist
ORDER BY distance

Link to comment
Share on other sites

Thank you both for your help so far, I'm so much closer now. I'd like to limit the search, yes, as the coordinate system is indeed a handfull.

 

allright, I've got it working with the first example, but one gives a bit weird results, but these two codes give me different results, which I can't even see the difference between:

 

code 1, working as should, sorting by distance:

 $query = "SELECT id, x, y, SQRT(($inputX - x)*($inputX - x) + ($inpuY - y)*($inputY - y)) as distance
FROM mytable
ORDER BY distance"; 

 

and the second one, which sorts in a pseudo-close to correct way:

 

$query = "SELECT id, x, y, SQRT(($inputX - x)*($inputX - x) + ($inputY - y)*($inputY - y)) as distance
FROM mytable
ORDER BY distance";

 

 

Though the one part I really want to get to work, is this , but i can't manage to get it to work, the WHERE line is the problem :

 

 $query = "SELECT id, x, y, SQRT(($inputX - x)*($inputX - x) + ($inputY - y)*($inputX - y)) as distance
FROM mytable
WHERE SQRT(($inputX - x)*($inputX - x) + ($inputY - y)*($inputY - y)) < $maxdist
ORDER BY distance"; 

Link to comment
Share on other sites

Well, as far as i can see, it's the first one that shouldn't be working. You've missing a t and have $inpuY rather than $inputY.

 

You say the past example isn't working. In what way? Do you get an error message? Have you tried echoing the query, to check all the variables are correct?

Link to comment
Share on other sites

Well, as far as i can see, it's the first one that shouldn't be working. You've missing a t and have $inpuY rather than $inputY.

 

You say the past example isn't working. In what way? Do you get an error message? Have you tried echoing the query, to check all the variables are correct?

 

I must have changed it again while trying to figure something out, my bad. it was correct at same point.

 

On the very positive side, You guys have managed to solve my problem. Thank you very much.

 

Here's the code, sorting the points by distance, and limiting it by distance:

 

$query = "SELECT id, x, y, SQRT(($inputX - x)*($inputX - x) + ($inputY - y)*($inputY - y)) as distance
FROM table
WHERE SQRT(($inputX - x)*($inputX - x) + ($inputY - y)*($inputY - y)) < $maxdist
ORDER BY distance"; 

 

 

Just as you guys showed me. Thank you all for you patience, and you expert help. it works!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.