-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit adds a new utility function, read_csv_file, to the project. The function reads a CSV file and returns its contents as a list of dictionaries. This function will be used in the convert_address_to_coords function to read the bottle_caps.csv file.
- Loading branch information
1 parent
774f422
commit 659d6d7
Showing
2 changed files
with
30 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,41 @@ | ||
import requests | ||
import numpy as np | ||
from utils.csv_reader import read_csv_file | ||
|
||
def get_geocoordinates(address): | ||
""" | ||
Get the geographic coordinates of an address from OpenStreetMap's Nominatim service. | ||
""" | ||
Get the geographic coordinates of an address from OpenStreetMap's Nominatim service. | ||
Parameters: | ||
address (str): The address to geocode. | ||
Parameters: | ||
address (str): The address to geocode. | ||
Returns: | ||
tuple: The latitude and longitude of the address, or None if the geocoding request failed. | ||
""" | ||
response = requests.get( | ||
'https://nominatim.openstreetmap.org/search', | ||
params={'q': address, 'format': 'json'} | ||
) | ||
Returns: | ||
tuple: The latitude and longitude of the address, or None if the geocoding request failed. | ||
""" | ||
response = requests.get( | ||
'https://nominatim.openstreetmap.org/search', | ||
params={'q': address, 'format': 'json'} | ||
) | ||
|
||
data = response.json() | ||
data = response.json() | ||
|
||
if data: | ||
return float(data[0]['lon']), float(data[0]['lat']) | ||
if data: | ||
return float(data[0]['lon']), float(data[0]['lat']) | ||
|
||
return None | ||
return None | ||
|
||
def convert_address_to_coords(csvfile): | ||
# Read in bottle_caps.csv | ||
bottle_caps = np.genfromtxt(csvfile, delimiter=",", dtype=str, skip_header=1) | ||
# Read in bottle_caps.csv | ||
bottle_caps = read_csv_file(csvfile) | ||
|
||
bottle_cap_coords = [] | ||
|
||
# bottle_cap_coords = [(13.0944453, 54.2907393), (11.591350844509808, 53.37741685), (12.459549961499999, 50.52140085), (7.956500987110655, 50.9910225), (9.653526, 51.4176975), (11.553636633022338, 48.14608575), (11.9069408, 48.3068168), (6.654346047066047, 51.83582915), (10.8870087, 49.889238199999994), (10.227481824523348, 50.45433915), (11.606263141955619, 50.1081385), (7.1418535749854755, 51.26109235)] | ||
|
||
# Get the geocoordinates of each bottle cap | ||
for i in range(len(bottle_caps)): | ||
bottle_cap = get_geocoordinates(bottle_caps[i][2]) | ||
bottle_cap = get_geocoordinates(bottle_caps[i]['brewery_address']) | ||
print(bottle_caps[i]['brewery_address']) | ||
bottle_cap_coords.append(bottle_cap) | ||
|
||
return bottle_cap_coords |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import csv | ||
|
||
def read_csv_file(csvfile): | ||
# Open the csv file | ||
with open(csvfile, 'r') as file: | ||
# Create a csv reader | ||
reader = csv.DictReader(file, quotechar='"', delimiter=',', quoting=csv.QUOTE_ALL, skipinitialspace=True) | ||
# Convert the csv reader to a list and return it | ||
data = list(reader) | ||
return data |