}

Generate Geo-Coordinates from Street Names CSV (Latitude & Longitude)

Level:
Fortgeschritten 👍
Preview result
Icon für einen externen Link
Im Webflow Designer öffnen und klonen
Icon für einen externen Link
Open and clone in Webflow Designer
Icon für einen externen Link

An AI like ChatGPT can do a lot of work for us that is actually boring and no one wants to do. Here on my channel, I always give you challenges and application examples that came up in my everyday work as a web designer and how I solved them with chat GPT or other AI tools.

In this example, I wanted to integrate a dealer list with over 300 addresses into an interactive map on a customer website. To do this, I needed the respective latitude and longitude coordinates, which I wanted to generate automatically from a CSV file. ChatGPT wrote me the Python code for this.

Step-by-step guide

  1. Open the CSV file with addresses in Numbers on the Mac.
  2. Make sure you use the following column descriptions: “Street,” “Zip Code,” “City,” “Country,” “Latitude,” and “Longitude.”
  3. Open the terminal (enter the Spotlight search on the Mac terminal)
  4. Type: “pip install geopy.” If the code doesn't work, you'll need to install Python first.
  5. Save the code below to a koordinaten.py text file:
  6. Store the file “koordinaten.py” and the “addresses.scv” in a “Script” folder on the desktop on the Mac
  7. In the terminal on the MacBook, enter the following: python3 /users/yourusername/desktop/script/coordinates.pyReplace “your username” with your actual username on the Mac.
  8. Sit back and relax until the ergebnisse.csv is complete. Depending on the number of entries, this may take time.

Code:


From geopy.geocoders import nominatim
Importing csv

geolocator = nominate (user_agent="my-app”)

with open ('adressen.csv', newline=”, encoding='utf-8') as csvfile:
reader = csv.DictReader (csvfile, delimiter='; ')
with open ('ergebnisse.csv', 'w', newline=”, encoding='utf-8') as a result file:
fieldnames = ['street', 'zip code ',' city ',' country ',' latitude ',' longitude ']
writer = csv.dictWriter (resultfile, fieldnames=fieldnames)
writer.writeheader ()
For row in reader:
address = f "{row ['street']}, {row ['zip code ']} {row [' city ']}, {row [' country ']}”
try:
location = geolocator.geocode (address)
writer.writerow ({
'street': row ['street'],
'zip code': row ['zip code '],
'place': row ['location'],
'country': row ['country'],
'latitude': location.latitude,
'length': location.longitude
})
Except:
writer.writerow ({
'street': row ['street'],
'zip code': row ['zip code '],
'place': row ['location'],
'country': row ['country'],
'latitude':”,
'length':”
})

I used the following Promt for Chat GPT:

I have a “adressen.csv” file, which contains a street name, zip code, city and country. I need the latitude and longitude coordinates for each of these addresses.

For this purpose, a new csv file “results” should be created, which lists street name, postal code, city, country, latitude and longitude.

If there is an error with an address, the latitude and longitude fields should simply be left empty.

It has to be a free solution that doesn't require me to sign up anywhere.

Can you help me with that?