0

I know my question is very vague but I'm still learning. For context, we have 3 dataframe:

users: UID, originlongitude, originlatitude, destinationlongitude, destinationlatitude

Bus: BusID, Name

BusStops: BusID, BusStopID, longtitude, latitude

I would like to find the busses each user can take base on the criteria that the origin and destination of a user is at least 0.5km away from any bus stop of a bus.

I have coded the following in Python but I am very unsure if this is the right approach in terms of efficiency (run time) of the code, as well as correctness (whether it works).

import geopy.distance as geo

User['Busses'] = []

for userindex, userrow in User.iterrows():
    routes = []
    olat = userrow['OriginLatitute']
    olong = userrow['OriginLongitute']
    ocoor = (olat, olong)
    dlat = userrow['DestinationLatitude']
    dlong = userrow['DestinationLongitude']
    dcoor = (dlat, dlong)

    for routeindex, routerow in Bus.iterrows():
        flag = [False, False]
        Routeid = routerow['BusID']
        Routestops = RouteStop[RouteStop['BusID'] == Routeid]
    
        for rsindex, rsrow in BusStops.iterrows():
            rlat = rsrow['Latitude']
            rlong = rsrow['Longitude']
            rcoor = (rlat, rlong)
            origindist = geo.distance(ocoor, rcoor).km
            destdist = geo.distance(dcoor, rcoor).km
            if origindist < 0.5:
                flag[0] = True
            if destdist < 0.5:
                flag[1] = True
            if False not in flag:
                routes.append(rsrow['BusID'])
                break
    User.set_values(userindex, 'Busses', routes)

From how i see it, the runtime seems to be almost n^3 which might not be ideal. Is there a better solution to this problem? Would appreciate any help whether is it improving the runtime or correcting the code.

Samuel
  • 225
  • 3
  • 11
  • Please show runnable code. That will help to understand your problem –  Aug 08 '21 at 07:23
  • To test whether it works I think you just have to make some test data to feed through, where you know what the answer should be. For efficiency, maybe apply off-the-shelf tools like `pandas.DataFrame.join`. – TMBailey Aug 08 '21 at 09:01
  • without a fuller question / sample data cannot really answer. It looks like it will be aligned with this answer I have given before https://stackoverflow.com/questions/63300859/python-location-show-distance-from-closest-other-location/63330746#63330746 – Rob Raymond Aug 08 '21 at 13:05

0 Answers0