Delivery route calculation with Amazon Location Service

Yuri Fenyuk
3 min readJun 18, 2021

--

At the beginning of June’21 AWS announced General Availability of Location Service which was presented as beta half of the year before. The majority of us use maps many times daily and the corresponding service is in AWS clouds now. Sure thing, maps are a visual category and a bigger chunk of Location Service is about using visualizations on front-end, but this article addresses the practical problem of finding an optimal way to visits delivery set of addresses (pandemic world! delivery as important as never before). Thus, it has only back-end code which uses route calculators feature to find the answer.

My task will be to decide on tomorrow's delivery plan. My route starts and ends in the depot with the address and I have delivery addresses list. My business along with deliveries is located in Berlin, just for fun.

Complete Node.js sample project is here.

All those addresses are in js-file:

Almost everything is in index.js.

At the top of the file, I reference aws-sdk, unit with 2 helping functions and addresses for delivery. Also update AWS region and create Location Service instance:

Next, the wrapping around AWS API’s CalculateRoute function is declared:

My function takes geo longitude&latitude of depot and deliveries as parameters. Please make sure you have Route calculator on your AWS account as this will be kind of execution container to do the calculation. I have sent depot as starting and ending point.

Since I plan tomorrow day, I send as departure time tomorrow’s 08:00 (which is the first of my helping function). Service takes this time into account by using usual at that time traffic statistics (sic!).

Calculation returns a lot of route details, not only including distance and time, but also details of every turn you need to take in the scope of reachingevery the waypoint. I need, mainly, summary node only.

Next dozen lines is another wrapping function around AWS API’s SearchPlaceIndexForText:

In fact, it receives postal address (“Gartenstraße 8, 12529 Schönefeld, Germany”) as input and returns coordinates (“[ 13.520739969574265, 52.397159995696114 ]”) as output. Again, make sure you have location index with the corresponding name in clouds.

Finally, the central part of my tutorial is down below:

#2..#12: Do geogoding (to geo-coordinates) of all postal addresses I have. As AWS Location Service needs geo-coordinates but I want to see postal addresses, I keep backtrace in waypointToAddressMap (going to use it on #35 later on).

#13: create permutations (al possible combination) of my delivery addresses by using my 2nd helper function. I want to have a simple tackle of old Travelling salesman problem, but now using traffic statistic and real city roads specific. So, my plan is to send all combinations to AWS Location Service and make it calculate the route for each (as of now, its WaypointPositions parameter is always ordered list). Actually, that is what I do in #15..#26 by awaiting all calculations and get only the distance, duration, and delivery sequence for each of the permutations.

#28..#34 find the delivery combination which should take minimum time to complete.

#35..#36 using coordinates mapping I find the postal address of the optimal delivery route and print it.

Finally, the console output:

Given my test sample with depot and 4 delivery points, I made 4 factorial (24) delivery combinations which, given the fact I leave my depot at 08:00 tomorrow and return back after the job is done, have given me the distance in the range between 63 and 94 kilometers and time between 80 and 128 minutes.

The quickest route is printed in geo-coordinates and in postal addresses!

Next possible step would be to add the chosen route visualization on front-end with a map which is also not too difficult in AWS Location Service.

Per AWS, they have very competitive (even cheaper than other maps providers) prices and as of today allow to chose between Esri and HERE Technologies as map data providers already, promising new ones in the future.

--

--

Yuri Fenyuk
Yuri Fenyuk

Written by Yuri Fenyuk

Experienced software developer

No responses yet