Sort station names after countries and save them as csv

Question:

How can I sort a list of geographical coordinates into different lists based on the country and save each list as a separate CSV file?

Koforidua_ANUC,6.109N,0.302W
Kuching,1.491N,110.349E
Kuopio,62.892N,27.634E
Kuwait_University,29.325N,47.971E

Edit: The comments from tripleee were helpful


Is it possible to add regional mountain ranges to my plot, such as the Atlas Mountains or the Hoggar Mountains in Nigeria, without using Geopandas or similar libraries? Unfortunately, these features are not readily available in the Geopandas database. Since I don’t have access to maps APIs or software like QGIS or GDAL, I’m limited to using freely available databases.

Asked By: S.Kociok

||

Answers:

The following lines solved my problem:

    from geopy.geocoders import Nominatim 
geolocator = Nominatim(user_agent="Geolocation")
    
    def get_country(station, lat, lon):
        location = geolocator.reverse(f"{lat}, {lon}", exactly_one=True)
        return location.raw['address'].get('country', 'unknown')
    
    stations = [
        ['Ouagadougou', 12.424, -1.487],
        ['Ouarzazate', 30.928, -6.913],
        ['Oujda', 34.653, -1.898],
        ['Oukaimeden', 31.209, 7.864],
        ['Pitres', 36.936, 3.326],
        ['Quarzazate', 30.939, 6.909] ]
    
    for station in stations:
        name, lat, lon = station
        country = get_country(name, lat, lon)
        print(f"Station: {name}, Latitude: {lat}, Longitude: {lon}, Country: {country}")

After that you can sort it after countries and save them into csv.

Answered By: S.Kociok
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.