Tesla Motors recently announced their new model 3, which is a less expensive version of their model S. The company received over a quarter of a million pre-orders. Since the launch, potential buyers have been evaluating the locations of charging stations before they purchase the model 3. You have been hired by Tesla Motors to create a Python script that would provide information about charging station location and distance for potential buyers.
Data provided by NREL (https://developer.nrel.gov/docs/transportation/alt-fuel-stations-v1/all/) on charging station location can be found on D2L in three different formats:
(1) tesla_locations.csv: contains the longitude and latitude of every Tesla charging stations in the US and this file is the simplest and easiest to work with because the data can be directly loaded into a numpy array using numpy.genfromtxt() with the delimiter set to “,” and skip_header set to 1,
(2) testa_cities.csv contains the name of the city where the charging station is located – the cities are listed in the same order as the longitude and latitude data. This data file is optional, and can be imported using numpy.genfromtxt, but you need to set dtype=’a50’ where ‘a’ tells numpy the data is a string and 50 is the character import limit.
(3) tesla_data.csv contains one column with the city name and the next two columns contain the latitude and longitude (i.e., this file combines the previous two). This data file is also optional, and can be imported using numpy.genfromtxt() but you need to set dtype=(‘a50’,’f8’,’f8’).
After you have imported your data, you should use matplotlib to generate a plot with a point for every charging station. Note that the first column of data is longitude, i.e., the y-values, and the second column is latitude, i.e., the x-values, so you need to plot: (second column, first column). The plot should ‘resemble’ the US (especially the coasts) if plotted correctly.
Finally, you need to write two functions:
(1) The first function should be passed a current location (longitude, latitude) and the array with all the locations. Then is should loop through all the locations and find the nearest charging station to the current location. It should return the row number for the nearest charging station and the distance in terms of miles (note: you may assume 60 miles per degree of latitude and longitude). There are multiple ways to write this function, but you might find the numpy.argmin() function helpful – returns the index (i.e., row number) of the smallest entry in a vector (e.g., a vector of distances).
(2) The second function should be passed a current location (longitude, latitude) and return either a list of row numbers or a list of cities that are within 60 miles of the current location (i.e., within a radius of 1.0 in terms of degrees of longitude and latitude). Hint: start by creating an empty list (myList = []) and then append() the row number or city name for every charging station within a radius of 1.0. It is easier to just append a row number, but 5 points of extra credit are available if you can return the list of city names.
All distances should be calculated using a Euclidian distance (d = √Δx2 + Δy2), and not that this is not exactly correct as the earth is a sphere and not a flat plane.
Test both functions by using one of the following ‘current locations’:
Bozeman [45.6770,-111.0429]
San Francisco [37.7749, -122.4194]
Hence, the first function would return the closest charging station to Bozeman (or SF) and the second function would return the charging stations within 60 miles of Bozeman (or SF).