Be notified if tracker is out of the specified trip or even specify expected trip time and notice if not trip progressing as expected

Can provide a Transilien trip for instance but note that it is not very precise, as described in Improve_websites_thanks_to_open_source/issues/367.

Having a list of locations with expected time seems to be appropriate. Then could precise a time threshold if have not passed a specific location at a given time.

What about round-trip? It may involve issues as go twice at a specific location.

import requests
import json
import haversine as hs
import datetime

headers = {
    'apikey': 'CENSORED'
}

def get(url):
    url = f'https://prim.iledefrance-mobilites.fr/marketplace/navitia/coverage/fr-idf/{url}'
    return requests.get(url, headers = headers).json()

def getStopAreaId(id_):
    return f'stop_area:IDFM:{id_}'

from_ = 'LONGITUDE;LATITUDE'
to = getStopAreaId(CENSORED)

url = f'journeys?from={from_}&to={to}'

# How to force having solution for this journey?
#url += '&datetime=20240213T085814'

data = get(url)
#print(json.dumps(data, indent = 4))
journey = data['journeys'][0]

def getSectionCoords(sectionEnd):
    return sectionEnd.get('stop_area', sectionEnd.get('stop_point', sectionEnd.get('address')))['coord']

locations = []

def parseDatetime(datetimeStr):
    return datetime.datetime.strptime(datetimeStr, '%Y%m%dT%H%M%S')

sections = journey['sections']
for sectionIndex, section in enumerate(sections):
    subSections = section['geojson']['coordinates']
    
    # Linear progression assumption
    subSections = [subSection[::-1] for subSection in subSections]
    totalDistance = sum([hs.haversine(subSection, nextSubSection) for subSection, nextSubSection in zip(subSections, subSections[1:])])
    
    departureTime = parseDatetime(section['departure_date_time'])
    arrivalTime = parseDatetime(section['arrival_date_time'])
    tripTime = arrivalTime - departureTime
    tripTimePerDistance = tripTime / totalDistance
    
    print('a', departureTime)
    locations += [{
        'coords': subSections[0],
        'time': str(departureTime)
    }]
    
    # TODO: Seem to possibly have decreasing time at the end, should investigate that.
    subSectionArrivalTime = departureTime
    for subSection, nextSubSection in zip(subSections, subSections[1:]):
        distance = hs.haversine(subSection, nextSubSection)
        subSectionArrivalTime += tripTimePerDistance * distance
        locations += [{
            'coords': nextSubSection,
            'time': str(subSectionArrivalTime)
        }]
        print('b', subSectionArrivalTime)

#print(json.dumps(locations, indent = 4))

Above algorithm does not precise tram sections for instance, while time can be approximated with something linear, intermediary locations are necessary. Can rely on section path or geojson, geojson looks more precise. Note that should have a within sub section linear progression assumption too for even more precision. More precisely should determine the theoretical location based on current time and verify that the tracker is not that far or similar, the other way around seems to easily introduce issues.

placeName = input('Place: ')

if placeName != '':
    url = f'places?q={placeName}'
    data = get(url)
    print(json.dumps(data['places'][0], indent = 4))
{
    "error": {
        "id": "no_solution",
        "message": "no solution found for this journey"
    },
    "feed_publishers": [],
    "links": [],
    "tickets": [],
    "disruptions": [],
    "terminus": [],
    "context": {
        "current_datetime": "20240214T010104",
        "timezone": "Europe/Paris"
    },
    "notes": [],
    "exceptions": []
}

Can investigate transilien.py on my phone.

Edited by Benjamin Loison