Author: Stewart Berry
1 November 2024
Efficient routing and logistics planning are essential in GIS workflows, whether it's optimizing delivery routes, scheduling services, or managing fuel consumption on long-distance trips. The robust GIS capabilities of Maptitude, combined with the power of Python and the GISDK Routing.Router class, offer a complete solution for calculating efficient routes, optimizing fuel costs, and ensuring overall travel efficiency. By integrating Python through the caliperpy package, GIS professionals can automate and streamline complex routing tasks, leveraging Python's capabilities for data manipulation and reporting. This guide demonstrates how to elevate your routing workflows using the Routing.Router class in Maptitude.
Maptitude is known for its advanced GIS capabilities, and the Routing.Router class enhances these by allowing you to:
Using Python in conjunction with Maptitude unlocks a vast ecosystem of libraries like Pandas and NumPy, while also automating route planning and report generation. This combination empowers GIS professionals to optimize logistics, reduce costs, and streamline operations.
Let’s walk through a simple example of how to use the Routing.Router class with Python via the caliperpy package. This example calculates a route between multiple locations, optimizes for travel time, and generates an Excel report:
import caliperpy # Connect to Maptitude dk = caliperpy.Maptitude.connect() # Define stops with coordinates and stop durations RoutingPoints = [ {"Coordinate": dk.Coord(-71259994, 42298892), "StopDuration": 30, "StopName": "Babson College"}, {"Coordinate": dk.Coord(-71250024, 42341178), "StopDuration": 30, "StopName": "Lasell College"}, {"Coordinate": dk.Coord(-71187232, 42298633), "StopDuration": 30, "StopName": "Mount Ida College"}, ] # Create router and set parameters router = dk.CreateGisdkObject("gis_ui", "Routing.Router") router.Minimize = "Time" router.IncludeRestStops = True router.TimeBetweenRests = 240 # 4 hours driving before a break router.RestStopDuration = 15 # 15-minute rest stops router.FuelPrice = 3.29 # Calculate the route path = router.Calculate(RoutingPoints, None) # Generate report if path is calculated if path: print(f"Path Time: {path.Time} minutes, Distance: {path.Distance} miles") router.ExportToExcel({"PathObj": path, "FileName": "route_report.xlsx"}) else: print("Error: Path calculation failed.")
The Routing.Router class offers powerful features for more complex scenarios, such as adding rest stops and accounting for fuel efficiency:
router.TimeBetweenRests = 300 # 5 hours router.RestStopDuration = 20 # 20 minutes per break
router.FuelConsumptionCity = 17 # MPG in city router.FuelConsumptionHighway = 23 # MPG on highway router.FuelPrice = 3.29 # per gallon
router.DistanceBasedCostCalculation = False # Prioritize fuel efficiency
Automating route planning for a large set of addresses can be done seamlessly using Python and Maptitude. Here’s how you can import addresses from an Excel file and generate optimized routes:
import sys import os import caliperpy import pandas as pd from tkinter import Tk from tkinter.filedialog import askopenfilename # Select Excel file with addresses def get_excel_file(): Tk().withdraw() return askopenfilename(title="Select Excel File", filetypes=[("Excel files", "*.xlsx *.xls")]) # Read addresses from Excel file def read_addresses_from_excel(file_path): df = pd.read_excel(file_path) address_col = df.columns[df.columns.str.contains('address', case=False)].tolist()[0] city_col = df.columns[df.columns.str.contains('city', case=False)].tolist()[0] zip_col = df.columns[df.columns.str.contains('zip', case=False)].tolist()[0] return df[[address_col, city_col, zip_col]].to_dict('records') # Main routing logic def main(): excel_file = get_excel_file() addresses = read_addresses_from_excel(excel_file) # Connect to Maptitude dk = caliperpy.Maptitude.connect() # Define stops from Excel addresses stop_list = [ {"address": addr['Address'], "city": addr['City'], "postal_code": addr['ZIP']} for addr in addresses ] # Calculate route with the stops router = dk.CreateGisdkObject("Routing.Router") path = router.Calculate(stop_list, None) # Generate Excel report if path: router.ExportToExcel({"PathObj": path, "FileName": "route_report.xlsx"})
The Routing.Router class can be applied in various industries for:
Combining the robust routing features of Maptitude with Python’s flexibility, GIS professionals can optimize routes, minimize costs, and generate detailed reports. Whether managing delivery fleets or planning service routes, Python’s integration with Maptitude takes your GIS workflows to the next level.
Ready to get started? Install the caliperpy package, write your first script, and unlock the full potential of Python-powered route optimization today!
Home | Products | Contact | Secure Store