Home > Software design >  Inconsistent results - Bing Maps API - Geocoding & Plotting using folium
Inconsistent results - Bing Maps API - Geocoding & Plotting using folium

Time:07-12

I am trying to get the co-ordinates and distance but getting inconsistencies from Bing Maps API. Is there something I'm doing wrong or do the responses really vary that much?

Issue: The co-ordinates that I get in the JSON response are different for the same starting address.

My input is an excel file with the starting and delivery addresses like so

enter image description here

df = df[['Starting Address', 'Delivery Address', 'Driving Distance (Miles)', 'Radial Distance (Miles)']]

df = df.reset_index()
for index, row in df.iterrows():
#Encoding Addresses
enc_start = urllib.parse.quote(df['Starting Address'][index], safe='')
print(enc_start)
enc_del = urllib.parse.quote(df['Delivery Address'][index], safe='')
print(enc_del)
#API Call
url = "http://dev.virtualearth.net/REST/V1/Routes/Driving?wp.0="   enc_start   "&wp.1="   enc_del   "&distanceUnit=mi"   "&optmz=distance"   "&key="   bingMapsKey
response = requests.get(url).json() #Setting API response (JSON)

try:
    s_lat = response["resourceSets"][0]["resources"][0]["bbox"][2] #Fetching Starting Address Latitude
    s_lon = response["resourceSets"][0]["resources"][0]["bbox"][3] #Fetching Starting Address Longitude
    d_lat = response["resourceSets"][0]["resources"][0]["bbox"][0] #Fetching Delivery Address Latitude
    d_lon = response["resourceSets"][0]["resources"][0]["bbox"][1] #Fetching Delivery Address Longitude

    coords_start = (s_lat, s_lon) #Coords of Starting Address
    coords_del = (d_lat, d_lon) #Coords of Delivery Address
    print("c_str ", coords_start)
    print("c_del ", coords_del)

    #Optimized Travel Distance
    travelDistance = response["resourceSets"][0]["resources"][0]["travelDistance"]
    print("travel dist ", travelDistance)
    df.loc[[index], 'Driving Distance (Miles)'] = travelDistance

    # folium.Marker(location=[d_lat, d_lon], popup = df["Delivery Address"][index]).add_to(mc) #Creating Output Map

except:
    IndexError

#Radial Distance
    radialDistance = haversine(coords_start, coords_del, unit=Unit.MILES)
    # # print(radialDistance)
    df.loc[[index], 'Radial Distance (Miles)'] = radialDistance

Output: enter image description here

CodePudding user response:

You are grabbing the bounding box coordinates rather than at actual starting location. The bounding box is just a recommendation for viewing the full route and would change if both the start and end are not identical between queries.

To get the actual start/end points;

s_lat = response["resourceSets"][0]["resources"][0]["routeLegs"][0]["actualStart"]["coordinates"][0]
s_lon = response["resourceSets"][0]["resources"][0]["routeLegs"][0]["actualStart"]["coordinates"][1]
d_lat = response["resourceSets"][0]["resources"][0]["routeLegs"][0]["actualEnd"]["coordinates"][0]
d_lon = response["resourceSets"][0]["resources"][0]["routeLegs"][0]["actualEnd"]["coordinates"][1]
  • Related