I’m building a charity asset database for a university project, with around 1,000 to 1,500 records that each have latitude and longitude. What’s a practical way to return only assets within a chosen radius of a user’s location? I’m considering PostgreSQL, Node, and React.
What’s a sensible way to search charity records by distance?
Answers
Keep the filtering on the server, so the client asks for a location and radius and gets back only the matching assets. With PostgreSQL, PostGIS is a straightforward option: store each point as a geographic value and use a distance query such as ST_DWithin. It handles the radius in meters and keeps the query logic out of the frontend.
For a project this size, performance probably won’t be a problem either way. PostGIS is still a useful choice if you want a clear, standard geospatial solution.
Reply to this answer
You can keep it simpler if you don’t want to add a spatial extension. First query a latitude and longitude bounding box around the requested point, then calculate the actual distance for those candidates with a haversine calculation and discard anything outside the radius. The box is just a quick pre-filter, not the final distance check.
Be careful with longitude: the distance represented by one degree changes with latitude. A fixed latitude-and-longitude offset is only an approximation, so don’t treat it as an accurate radius test on its own.
Reply to this answer
At 1,500 rows, even a simple approach may be perfectly adequate. You could calculate distances for all records on the server and return the matches, then measure how it behaves before adding indexes or more complex spatial tooling.
I wouldn’t send the entire dataset to the browser just to filter it there. That adds unnecessary data transfer and makes the server do less of the work it should own.
Reply to this answer
PostgreSQL also has the earthdistance extension, which can calculate distances between coordinates in a query. That may be enough for a basic radius search if you’d rather not use PostGIS. Check the extension’s setup and indexing requirements for your PostgreSQL installation before relying on it.
Whichever option you choose, make the radius and coordinate units explicit in the API so the frontend and database aren’t accidentally mixing kilometers, miles, and meters.
Reply to this answer
Add your answer
Answers are reviewed before publication.
6B__Fandrel:
That makes sense. I’ll keep the location filtering on the backend and avoid overcomplicating the first version.