Skip to content

Instantly share code, notes, and snippets.

@spinningcat
Created February 11, 2025 21:51
Show Gist options
  • Save spinningcat/4016cc9f9d023523474bb870854b32e4 to your computer and use it in GitHub Desktop.
Save spinningcat/4016cc9f9d023523474bb870854b32e4 to your computer and use it in GitHub Desktop.
import time
import pandas as pd
import requests
# Set headers for the OpenStreetMap API request
headers = {
"User-Agent": "MyPythonApp/1.0 ([email protected])"
}
# Read the CSV file
df = pd.read_csv('neighhbourdata.csv')
# Counter to keep track of the number of rows processed
counter = 0
# Iterate over the rows of the DataFrame
for index, row in df.iterrows():
print(counter)
if row["Read"] == True:
print("True line")
if row["Read"] == False:
print("False line")
if counter >= 1000 : # Stop after processing 3 rows
break
# Format the query for the OpenStreetMap API
query = f'{row["CityName"].replace(" ", "")}, {row["DistrictName"]}, {row["neighbourhood"]}'
url = f'https://nominatim.openstreetmap.org/search?q={query}&format=json'
print(url)
response = requests.get(url, headers=headers)
if response.status_code == 200:
data = response.json()
print(data)
if data: # Check if data is not empty
# Update the DataFrame with the latitude and longitude
df.at[index, 'Lat'] = data[0]["lat"] # Latitude
df.at[index, 'Long'] = data[0]["lon"] # Longitude
df.at[index, 'Read'] = True # Mark the row as read
df.at[index, 'URL'] = url
df.at[index, 'Emoty'] = False
print(f"Processed row {index}: {query} -> Lat: {data[0]['lat']}, Lon: {data[0]['lon']}")
if not data:
df.at[index, 'Lat'] = ""
df.at[index, 'Long'] = ""
df.at[index, 'Read'] = True # Mark the row as read
df.at[index, 'URL'] = url
df.at[index, 'Emoty'] = True
else:
print(f"Failed to fetch data for: {query}")
counter += 1
# Add a delay to avoid hitting API rate limits
time.sleep(5)
# Save the updated DataFrame to a new CSV file (or overwrite the existing one)
df.to_csv('neighhbourdata.csv', index=False)
# Print the updated DataFrame
print("\nUpdated DataFrame:")
print(df)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment