Search code examples
postgresqlpostgis

PostGIS checking if a circle contains a certain point


I have Fighters and Arenas. Each of the objects has its own geolocation represented by a pair of values: (lat, long), each Fighter has also searchDistance, that's, a search radius. I need to output arenas available to each fighter, that's to say, those arenas that are located within the fighter's search distance.

The PostGIS documentation looks somewhat cryptic to me, too many terms that I don't understand. Seems like I need to go with ST_DWithin but I don't get how exactly and what values should be passed this function.

The function is supposed to be like this:

public async isArenaAvailableToFighter(fighterId: number, arenaId: number): Promise<boolean> {
    // 1. Retrieve (long, lat) for the arena and (long, lat, searchDistance) for the fighter;
    // 2. Using PostGIS check if the arena is within the fighters search radius;
    //    return TRUE if it is and return FALSE if it is not;
    return;
}

My problem is with the second step.


Solution

  • That should be as simple as

    WHERE ST_DWithin(player, arena, radius)
    

    If you want to use geometrical functions, it would be much smarter to store the location as a point than as a pair of coordinates.