Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You're first filtering your data by city and tag, and then want to <em>order</em> the result by distance, but not <em>filter</em> by distance. I.e. even the farthest points should be retrieved from the database. In that case, there shoulnd't be too much of a penalty if you compute the distance from the given location when you execute the request. After all, it's just one formula evaluation per line, and then sorting the result by that value.</p> <p>If your data is given using latitude and longitude coordinates, then theoretically this would mean computating lengths of geodesics on the sphere using a complicated formula. But within a single city, the georgraphical grid can reasonably be assumed to be rectangular (not square!). So if you store the ratio between one degree of latitude and one degree of longitude for each city, then you can convert all coordinates to a square grid, compute difference from the entered location in <em>x</em> and <em>y</em> direction, square and add those. No need to take the square root, as ordering by squares works just as well.</p> <p>Something along these lines:</p> <pre class="lang-sql prettyprint-override"><code> SELECT location.name, location.lat, location.lon, … FROM locations, cities WHERE location.city = city.id AND city.name = ? AND location.tag = ? ORDER BY POW((location.lat - ?), 2) + POW((location.lon - ?)*city.geoaspect, 2) </code></pre> <p>where parameters are the city name, the selected tag and the entered latitude and longitude.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload