armgilles / vcub_keeper

Analyse de l'activité des stations Vcub dans la métropole de Bordeaux afin de détecter en amont les stations hors service
https://vcubwatcher.herokuapp.com/
MIT License
2 stars 0 forks source link

Changement dans l'API de Bordeaux #80

Open armgilles opened 1 month ago

armgilles commented 1 month ago

Code depuis data.py :

ef get_data_from_api_bdx_by_station(station_id, start_date, stop_date):
    """
    Permet d'obtenir les données d'activité d'une station via une API d'open data Bordeaux

    Parameters
    ----------
    station_id : Int or List
        Numéro de la station de Vcub
    start_date : str
        Date de début de la Time Serie
    stop_date : str
        Date de fin de la Time Serie

    Returns
    -------
    Time serie in Json format

    Examples
    --------
    station_json = get_data_from_api_bdx_by_station(station_id=19, 
                                                    start_date='2020-10-14',
                                                    stop_date='2020-10-17')
    """

    # Si plusieurs station_id ([124,  15,  60,])
    if isinstance(station_id, (list, np.ndarray)):
        station_id = ','.join(map(str, station_id))

        url = \
            'https://data.bordeaux-metropole.fr/geojson/aggregate/ci_vcub_p?key=' + KEY_API_BDX + '&rangeStart=' + str(start_date) +\
                '&filter={"ident":{"$in":[' + str(station_id) +']}}&rangeEnd=' + str(stop_date) +\
                '&rangeStep=5min&attributes={"nom": "mode", "etat": "mode", "nbplaces": "max", "nbvelos": "max"}'
    # Si une seul station_id
    else:
        url = \
            'https://data.bordeaux-metropole.fr/geojson/aggregate/ci_vcub_p?key=' + KEY_API_BDX + '&rangeStart=' + str(start_date) +\
                '&filter={"ident":' + str(station_id) +'}&rangeEnd=' + str(stop_date) +\
                '&rangeStep=5min&attributes={"nom": "mode", "etat": "mode", "nbplaces": "max", "nbvelos": "max"}'

    response = requests.get(url)
    return response.json()

Pour des dates anciennes, ça marche correctement :

station_id=106
start_date='2021-12-29'
stop_date='2022-01-07'

station_json = get_data_from_api_bdx_by_station(station_id=station_id, 
                                                start_date=start_date,
                                                stop_date=stop_date)

station_df = transform_json_api_bdx_station_data_to_df(station_json)

# Check unique station number
assert station_df['station_id'].unique() == 106

# Check la longeur du DataFrame
assert len(station_df) == 1296

assert (station_df.columns ==  ['station_id', 'date', 'available_stands', 'available_bikes', 'status',
                                'transactions_in', 'transactions_out', 'transactions_all']).all()

image

Mais pour une date plus récente, on ne retrouve pas l'id de la bonne station :

station_id=106
start_date='2023-12-29'
stop_date='2024-01-07'

station_json = get_data_from_api_bdx_by_station(station_id=station_id, 
                                                start_date=start_date,
                                                stop_date=stop_date)

station_df = transform_json_api_bdx_station_data_to_df(station_json)
station_df

image

Il semble que GID et IDENT ont été modifié dans l'api : https://opendata.bordeaux-metropole.fr/explore/dataset/ci_vcub_p/table/

image

Problème lié avec ident dans l'url de query et de comment associé station_id.

armgilles commented 1 month ago

Link to https://github.com/armgilles/vcub_keeper/issues/50 ?