# EV Compatibility Score - Level 3 Chargers

The proximity of level 3 chargers to a vehicle owner's anchor location is a key piece of information for potential electric vehicle owners. In the event an EV owner needs to ensure access to a quick charge (generally 0 to 80% of battery in 40 minutes or less, where a level 2 charger might take 4 to 10 hours to charge from 0 to 80% of battery).

In order to give drivers specific information as to level 3 chargers in their area, we can calculate isochrones

The following code can make use of the MapBox api (see <https://docs.mapbox.com/api/navigation/isochrone/>) to find isochrones of desired sizes in `isochrone_times`. Note that MapBox requires the time to be 60 minutes or less, and the `contour` field should be checked, as the output is not necessarily in the same order as the input).&#x20;

For example, `isochrone_times = [30,15,5]`would find the isochrones that are 30, 15, and 5 minutes from the coordinates latitude = `anchor_lat` and longitude = `anchor_lng`. These isochrones ... Note that Valhalla has a similar capability (see <https://valhalla.github.io/valhalla/api/isochrone/api-reference/>).&#x20;

Similarly, `ev_chargers_dc` is obtained from <https://afdc.energy.gov/fuels/electricity_locations.html#/find/nearest?fuel=ELEC> where `ev_chargers = ev_chargers.loc[ev_chargers['ev_dc_fast_num'] >= 1]` (i.e. using only chargers that have a DC fast charger).

{% code overflow="wrap" lineNumbers="true" %}

```python
def isochrone_and_chargers_within(anchor_lng, anchor_lat, isochrone_times, ev_chargers_dc, api_key):
    
    isochrone_times_str = ','.join([str(i) for i in isochrone_times])
    url = \
    f'''https://api.mapbox.com/isochrone/v1/mapbox/driving/{anchor_lng},{anchor_lat}?contours_minutes={isochrone_times_str}&contours_colors=6706ce,04e813,4286f4&polygons=true&access_token={api_key}'''
    
    response = requests.get(url)

    geo_lst = []
    for i in range(len(response.json()['features'])):
        geo_lst.append(response.json()['features'][i]['geometry'])

    gdf_lst = []
    for i in range(len(geo_lst)):
        lngs = [i[0] for i in geo_lst[i]['coordinates'][0]]
        lats = [i[1] for i in geo_lst[i]['coordinates'][0]]
        #gdf_lst.append(gpd.GeoDataFrame(geometry = gpd.GeoSeries.from_xy(x = lngs, y = lats)))
        polygon_geom = Polygon(zip(lngs, lats))
        polygon = gpd.GeoDataFrame(index=[0], crs='epsg:4326', geometry=[polygon_geom])      
        gdf_lst.append(polygon)

    num_fast_charging_iso = []
    for gdf_iso in gdf_lst:
        num_fast_charging_iso.append(ev_chargers_dc.sjoin(gdf_iso).shape[0])

    return num_fast_charging_iso
```

{% endcode %}

Using this code for about 700 internal combustion engine vehicles, it can be seen that about 23% of these drivers have a level 3 charger within 5 minutes of their primary anchor location, 45% have one more than 5 minutes but less than 10 minutes away, and all but about 14% have one within at most 15 minutes.

<figure><img src="https://lh3.googleusercontent.com/lYDyPcvTr4k49woEwiJSDk-U-UvGUqx5DA7kJ12k6C2ajKZq3LZRzIEzB_hncNfJHnHB7rlGZ3bfw7dlldMXwi0_mttqj8SGgC5LdEsVf_pzg4QzTcg7i_XeXb10EGJQIaCDauoNpGFieExGCFbxK-g" alt="" width="563"><figcaption></figcaption></figure>

It may also be helpful to score long distance drives for how many EV chargers are along the route. There are certainly many ways to determine this information, but one way to find all EV chargers are within one mile of a known route using `geopandas` is below. Note that an api key may be obtained for free from nrel.gov from <https://developer.nrel.gov/> (and information on the `nearby-route` endpoint can be found here: <https://developer.nrel.gov/docs/transportation/alt-fuel-stations-v1/nearby-route/>.

{% code overflow="wrap" lineNumbers="true" %}

```
ls = LineString(gdf['geometry'].reset_index(drop = True))
route_ls = gpd.GeoDataFrame(index = [0], geometry = [ls], crs='epsg:4326')
url = f'https://developer.nrel.gov/api/alt-fuel-stations/v1/nearby-route.geojson?api_key={api_key}&distance=1&route={ls.wkt}'
response = requests.get(url, json = dct)

near_ev_chargers_properties = []
near_ev_chargers_geometries = []
for i in range(len(response.json()['features'])):
    near_ev_chargers_properties.append(response.json()['features'][i]['properties'])
    near_ev_chargers_geometries.append(response.json()['features'][i]['geometry'])
near_ev_chargers_gdf = gpd.GeoDataFrame(pd.merge(pd.DataFrame(near_ev_chargers_properties),pd.DataFrame(near_ev_chargers_geometries), \
         left_index = True, right_index = True))
```

{% endcode %}

&#x20;

<figure><img src="https://lh3.googleusercontent.com/0CVUQVJNOwW1CXK-enMUR7yLTMJ66sPhhrhfG4Y7XdTXKdKCuSsVqKIeBqZiwRjpH0tBArPGhZCXOG3ooEYqgQjeHNcJuAeams_SqAkFWY7qN3kavqQjVfAb2QF6TKks9OBUGv7MBxNK4qEhkyD29_k" alt=""><figcaption></figcaption></figure>
