0

I am trying to calculate a distance between two locations, using their coordinates. However I don't know how I can access the coordinate values, since they are in a dictionary.

I am very new to coding, and didn't understand any of the code I found regarding this problem, since it's too advanced for me. I don't really know where to start. My main function creates the dictionary: (Edit)

def main():
    filename = input("Enter the filename:\n")
    file= open(filename, 'r')
    rows= file.readlines()
    d = {}
    list = []
    for x in rows:

        list.append(x)
    #print(list)
    for elem in list:

        row = elem.split(";")

        d[row[3]] = {row[0], row[1]} #these are the indexes that the name and latitude & longitude have in the file

{'Location1': {'40.155444793742276', '28.950292890004903'}, 'Location2': ... }

The dictionary is like this, so the key is the name and then the coordinates are the values. Here is the function, which contains barely anything so far:

def calculate_distance(dictionary, location1, location2):

    distance_x = dictionary[location1] - dictionary[location2] 
    # Here I don't know how I can get the values from the dictionary, 
    # since there are two values, longitude and latitude...

    distance_y = ...
    distance = ... # Here I will use the pythagorean theorem

    return distance

Basically I just need to know how to work with the dictionary, since I don't know how I can get the values out so I can use them to calculate the distance. --> How to search a key from a dictionary and get the values to my use. Thank you for answering my stupid question. :)

Cara
  • 3
  • 3
  • [This one](https://stackoverflow.com/questions/19412462/getting-distance-between-two-points-based-on-latitude-longitude) might be helpful. – zipa Nov 06 '19 at 15:33
  • Could you give us a) the part of the main function where the dict is created and b) the value of type(dictionary[location1]) please? – FiddleStix Nov 06 '19 at 15:45
  • euclidean distance on latitude / longitude coordinates is not a good idea: for example points `(0, 90)` and `(180, 90)` are exactly at the same position on the sphere but a euclidean distance returns `180`. Use the haversine formula instead. – Thibault D. Nov 06 '19 at 15:58

2 Answers2

1

Well you are starting out, its normal that this makes it more difficult for you.

So lets see, you have a function that outputs a dictionary where the keys are locations and the values are coordinate pairs. First lets talk about the data types that you use.

location_map={'Location1': {'40.155444793742276', '28.950292890004903'}, 'Location2': ... }

I think there is an issue with your values, it seems that they are sets of strings. This has 2 main advantages for your goal.
First, set objects do not support indexing, this means that you cannot access location_map['Location1'][0] to get the first coordinate. Trying this would give you a TypeError. Instead, by using tuples when creating your map would allow you to index. You can do this by defining the coordinates as tuple([longitude,latitude]) instead of {longitude,latitude}.
Second, it seems that your coordinates are strings, in order to perform arithmetic operations with your data you need a numeric type such as integers or in your case floats. If you are reading longitude and latitude values as strings you can convert them by using float(longitude) and float(latitude).

0

There are multiple ways to do it, few are listed below:

# option 1
for i, v in data.items(): # to get key and value from dict.
    for k in v: # get each element of value (its a set)
        print (k)

# option 2
for i, v in data.items(): # to get key and value from dict.
    value_data = [k for k in list(v)] # convert set to list and put it in a list
    print (i, value_data[0], value_data[1]) # use values from here

I would suggest you to go through the python documentations to get more in-depth knowledge.

Akash Swain
  • 520
  • 3
  • 13