0

I found several functions from Python Libraries that allow you to reverse geocode. The problem I have is with initializing the function and specifying the parameters, I don't know what a user-agent or what an API is. The functions I would want to use are from reverse_geocode, and geopy_geocoders.

I have a list of tuples where each tuple is a coordinate representing a latitude and longitude: ((##,##),(##,##),...,(##,##)). All I want to do is put this list through a function that outputs a list corresponding to each coordinates associated State, Country.

Khan 99
  • 1
  • 1

1 Answers1

0

Could you do something like this? (assuming you want to use nominatim)

def get_state_country(coordinates): 
    """
    Function takes in a list of coordinates and returns a list of associated State, Country

    Args:
        coordinates (list): list of tuples representing lat and long coordinates

    Returns:
        list: list of tuples representing State, Country
    """
    state_country = []

    for coordinate in coordinates:
        url = "https://nominatim.openstreetmap.org/reverse?format=json&lat={}&lon={}".format(coordinate[0], coordinate[1])
        response = requests.get(url)
        response_json = json.loads(response.text)
        state_country.append((response_json['address']['state'], response_json['address']['country']))

    return state_country
PCDSandwichMan
  • 1,964
  • 1
  • 12
  • 23
  • So I tried using this, it should work, but I'm confused what response = requests.get(url) means? The error I'm getting is "name 'requests is not defined'. – Khan 99 Aug 21 '22 at 18:04
  • That is just a python HTTP library. You might want to take a couple of steps back before building your app. It might be beneficial to run through a python crash course on YouTube or similar before trying to put this together. – PCDSandwichMan Aug 21 '22 at 18:08
  • @Bohdan Also listed a nice resource as a comment to your question that might help. – PCDSandwichMan Aug 21 '22 at 18:09
  • Where can I find the resource? – Khan 99 Aug 21 '22 at 18:21
  • [here](https://stackoverflow.com/questions/69409255/how-to-get-city-state-and-country-from-a-list-of-latitude-and-longitude-coordi) – PCDSandwichMan Aug 21 '22 at 18:22