Skip to content

Commit

Permalink
Merge pull request #5 from Chameleon-company/surajrnair
Browse files Browse the repository at this point in the history
surajrnair: Create a python function to calculate navigation from origin to destination
  • Loading branch information
jamesdaviesdeveloper authored Sep 27, 2024
2 parents 97319ab + 72a105c commit e82ec19
Showing 1 changed file with 152 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "6kBqmzvGhxZr",
"outputId": "2ad763dc-f8d0-4fa0-e363-3439f9e029f1"
},
"outputs": [
{
"output_type": "stream",
"name": "stderr",
"text": [
"WARNING:urllib3.connectionpool:Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ReadTimeoutError(\"HTTPSConnectionPool(host='nominatim.openstreetmap.org', port=443): Read timed out. (read timeout=1)\")': /search?q=Sydney%2C+Australia&format=json&limit=1\n"
]
},
{
"output_type": "stream",
"name": "stdout",
"text": [
"No charging stations found in the specified radius.\n"
]
}
],
"source": [
"import requests\n",
"import json\n",
"from geopy.geocoders import Nominatim\n",
"from geopy.distance import geodesic\n",
"import math\n",
"\n",
"def get_charging_stations(latitude, longitude, radius):\n",
" \"\"\"\n",
" Fetches charging stations within a given radius using the Chargefox API.\n",
"\n",
" Args:\n",
" latitude: Latitude of the center point.\n",
" longitude: Longitude of the center point.\n",
" radius: Radius in kilometers.\n",
"\n",
" Returns:\n",
" List of charging station coordinates, or an empty list if none are found.\n",
" \"\"\"\n",
" url = \"https://api.chargefox.com/v1/chargers/search\"\n",
" params = {\n",
" \"latitude\": latitude,\n",
" \"longitude\": longitude,\n",
" \"radius\": radius,\n",
" \"access_token\": \"YOUR_CHARGEFOX_API_KEY\" # Replace with your API key\n",
" }\n",
"\n",
" response = requests.get(url, params=params)\n",
" data = json.loads(response.text)\n",
"\n",
" if 'chargers' in data:\n",
" charging_stations = [(charger['location']['coordinates'][1], charger['location']['coordinates'][0]) for charger in data['chargers']]\n",
" return charging_stations\n",
" else:\n",
" return []\n",
"\n",
"def calculate_route(origin, destination, max_range, starting_range, buffer_range, charging_stations):\n",
" \"\"\"\n",
" Calculates the route with EV charging stops.\n",
"\n",
" Args:\n",
" origin: Tuple of (latitude, longitude) for the origin.\n",
" destination: Tuple of (latitude, longitude) for the destination.\n",
" max_range: Maximum distance the EV can travel on a full charge.\n",
" starting_range: Distance the EV can travel with the current charge.\n",
" buffer_range: Safety buffer to ensure the vehicle doesn't run out before reaching the next station.\n",
" charging_stations: List of charging station coordinates.\n",
"\n",
" Returns:\n",
" List of tuples of (latitude, longitude) representing the route with charging stops.\n",
" \"\"\"\n",
" if not charging_stations:\n",
" print(\"No charging stations available.\")\n",
" return None\n",
"\n",
" route = [origin]\n",
" current_location = origin\n",
" current_range = starting_range\n",
"\n",
" while geodesic(current_location, destination).km > current_range - buffer_range:\n",
" nearest_station = min(charging_stations, key=lambda station: geodesic(current_location, station).km)\n",
" distance_to_station = geodesic(current_location, nearest_station).km\n",
"\n",
" if distance_to_station > current_range - buffer_range:\n",
" print(f\"Cannot reach station at {nearest_station} from {current_location}.\")\n",
" return None\n",
"\n",
" route.append(nearest_station)\n",
" print(f\"Added charging stop at: {nearest_station}, remaining range reset to: {max_range} km\")\n",
" current_location = nearest_station\n",
" current_range = max_range\n",
"\n",
" route.append(destination)\n",
" return route\n",
"\n",
"# Example usage\n",
"geolocator = Nominatim(user_agent=\"my_app\")\n",
"origin_address = \"Melbourne, Australia\"\n",
"destination_address = \"Sydney, Australia\"\n",
"\n",
"origin = geolocator.geocode(origin_address)\n",
"destination = geolocator.geocode(destination_address)\n",
"\n",
"if origin and destination:\n",
" origin_coords = (origin.latitude, origin.longitude)\n",
" destination_coords = (destination.latitude, destination.longitude)\n",
"\n",
" max_range = 300 # km\n",
" starting_range = 200 # km\n",
" buffer_range = 20 # km\n",
"\n",
" # Get charging stations for origin and destination\n",
" origin_stations = get_charging_stations(origin.latitude, origin.longitude, 100)\n",
" destination_stations = get_charging_stations(destination.latitude, destination.longitude, 100)\n",
" all_stations = origin_stations + destination_stations\n",
"\n",
" if not all_stations:\n",
" print(\"No charging stations found in the specified radius.\")\n",
" else:\n",
" route = calculate_route(origin_coords, destination_coords, max_range, starting_range, buffer_range, all_stations)\n",
" if route:\n",
" print(\"Route with charging stops:\", route)\n",
" else:\n",
" print(\"Unable to find a feasible route with the given parameters.\")\n",
"else:\n",
" print(\"Error: Unable to geocode origin or destination address.\")\n"
]
}
]
}

0 comments on commit e82ec19

Please sign in to comment.