How to determine closest stopPoint for each line

Hi there,
I’m trying to use this data to show only the closest stop point for any specific line, to specific coordinates I provide, whether it’s a Bus, Tube, or other.

I’m querying the data using the ‘list of stopPoints within an area’ and using NaptanMetroStation, NaptanPublicBusCoachTram, and NaptanRailStation stop points. Should I be using a different query or stopPoint type for this info?

For NaptanPublicBusCoachTram stopPoints, I can see the CompassPoint & ‘Towards’ data of bus stops. But as compassPoints change stop to stop (depending on the angle of the road), and towards data is not incredibly consistent, I was hoping for something more concrete to determine the direction of a bus. Something like ‘inbound’/‘outbound’ or a unique ID defining the direction of travel. Is there anything like that that I’m missing?
This is all I see for each individual bus line, listed under a station:
“id”: “632”,
“name”: “632”,
“uri”: “/Line/632”,
“type”: “Line”,

I’m brand new to this data so appreciate any hints/suggestions anyone can give.
Best,
Gabriel

@gmisserlis

I have found it very useful to use Postgres for this kind of thing. It’s got a built-in system for doing earthdistance which is easy to find the closest thing using a table with fields fpNorthing and fpEasting

earth_distance(  ll_to_earth(\"fpNorthing\", \"fpEasting\"),  ll_to_earth($lat,$long)) as distance	

Conceptually (PHP/Laravel) it like this…

public function executeForLatLong($lat, $long)
{
    DB::select("create extension if not exists cube;");
    DB::select("create extension if not exists earthdistance;");

    $a = DB::select("SELECT  \"strNRECode\", \"fpNorthing\", \"fpEasting\" , earth_distance(  ll_to_earth(\"fpNorthing\", \"fpEasting\"),  ll_to_earth($lat,$long)) as distance	FROM public.\"tblAllRailLocations\"   where \"strNRECode\" IN ($strListOfStations)	order by distance   limit 1");
    $strNRECode = ($a[0]->strNRECode);
    return  $strNRECode;
}

Of course, you grab the input data from the https://tfl.gov.uk/tfl/syndication/feeds/journey-planner-timetables.zip

Thanks Brian! Appreciate it!

1 Like