Snap to Roads Ownership Rebates in Australia and New Zealand
TomTom Developer Relations·Jan 20, 2023

Snap to Roads Ownership Rebates in Australia and New Zealand

TomTom Developer Relations
Jan 20, 2023 · 8 min read

New Zealand and Australia reimburse businesses for vehicles used offroad for business purposes. TomTom’s Snap to Roads API can be incorporated into a Python application that tracks the distance and fuel spent off public roads for businesses looking to claim this rebate.

In New Zealand and Australia, a government program reimburses businesses for fuel-operated equipment, such as vehicles that are used on private roads for business purposes. To claim this rebate, businesses must track the fuel spent while operating off public roads. TomTom APIs are perfect for this task.

In this piece, we’ll explore the capabilities of the Snap to Roads API in retrieving data for the distance covered by vehicles on private and public roads — and developing a Python application that does just that. Our application will feed GPS data to the Snap to Roads API, examine the returned output, extract the distances, and input the returned data into a CSV file.


To follow along with this piece, you will need the following:

  • An understanding of the Python programming language

  • Access to a Google Colab environment, Jupyter Notebook, or another Notebook-based environment or Python IDE.

  • A TomTom developer account to get an API key for our API calls

Developing the Program

In a new code cell, paste the following snippet to import the required modules.

1import urllib.parse as urlparse
2import pandas as pd
3import requests as rq
4from tabulate import tabulate

urllib.parse will be used for encoding and decoding URLs to make them fit for use with the API. [pandas]( and tabulate will be used to easily display the data returned without needing many loops. Also, pandas has a built-in method for converting data frames into a CSV format. As the name suggests, requests will be used to make HTTP requests.

In another cell, create a variable for storing your API key:

__API_KEY__ = "" #Enter your API key here

The API documentation states that there is an optional parameter called fields, which specifies what the response should return. To be sure we have captured everything we want, we must set the fields parameter to make the API return everything in its response. Note that we have assigned it to a variable called fields. The last line removes the empty spaces, making the fields parameter ready for consumption by the Snap to Roads API.

This is the fields object we will be using:

1fields = "{
2  projectedPoints{
3    type,
4    geometry{
5      type,
6      Coordinates
7    },
8    properties{
9      routeIndex,
10      SnapResult
11     }
12  },
13  route{
14    type,
15    geometry{
16      type,
17      Coordinates
18    },
19    properties{
20      id,
21      linearReference,
22      speedLimits{
23        value,
24        unit,
25        Type
26      },
27      address{
28        roadName,
29        roadNumbers,
30        municipality,
31        countryName,
32        countryCode,
33        CountrySubdivision
34      },
35      traveledDistance{
36        value,
37        Unit
38      },
39      privateRoad,
40      partOfTunnel,
41      urbanArea,
42      elementType,
43      frc,
44      formOfWay,
45      roadUse,
46      laneInfo{
47        NumberOfLanes
48      },
49      heightInfo{
50        height,
51        chainage,
52        Unit
53      },
54      trafficSigns{
55        signType,
56        chainage,
57        Unit
58      },
59      TrafficLight
60    }
61  },
62  distances{
63    total,
64    ferry,
65    road,
66    privateRoad,
67    publicRoad,
68    offRoad,
69    Unit
70  }
73fields = fields.replace(" ", "")   

We now need to URL encode the field using the following code:

1processed_fields = urlparse.quote(fields)

Next, we need to add random coordinates from a route in New Zealand. More specifically, the route starts from Verissimo Drive, then follows Auckland Airport Pack & Ride, Nixon Road, George Bolt Memorial Drive, and finally, Tom Pearce Drive, stopping at Harriman Signs Shop.

Route in New Zealand

In a real-world situation, these coordinates would come automatically from a tool like a GPS-enabled app. Here’s the list of coordinates in longitude-latitude format.

1coordinate_list = ['174.789024,-36.988238', '174.788684,-36.989756', '174.788243,-36.990205', '174.788326,-36.990326', '174.788570,-36.990669', 
2                   '174.788751,-36.990944', '174.789341,-36.991844', '174.789742,-36.992466', '174.790107,-36.993006', '174.790122,-36.993085', 
3                   '174.790886,-36.993205', '174.791565,-36.993344', '174.792321,-36.993631', '174.793032,-36.993897', '174.793657,-36.994120', 
4                   '174.794266,-36.994328', '174.795366,-36.994549', '174.796619,-36.994746', '174.797885,-36.994971', '174.799229,-36.995434', 
5                   '174.799852,-36.996107', '174.800091,-36.996842', '174.800275,-36.998212', '174.800203,-36.998767', '174.799956,-36.998953', 
6                   '174.800069,-36.998707', '174.800149,-36.997668', '174.800010,-36.996728', '174.799690,-36.995976', '174.798969,-36.995380', 
7                   '174.798102,-36.995089', '174.796528,-36.994798', '174.795047,-36.994558', '174.793784,-36.994245', '174.792529,-36.993768', 
8                   '174.790874,-36.993269', '174.788680,-36.992958', '174.787039,-36.992714', '174.786566,-36.992682', '174.786341,-36.993357', 
9                   '174.786078,-36.994524', '174.786094,-36.995133', '174.786234,-36.995820', '174.786907,-36.997213', '174.787580,-36.998592', 
10                   '174.788578,-37.000330', '174.789111,-37.001356', '174.789511,-37.001598', '174.790109,-37.001467', '174.791160,-37.001169', 
11                   '174.793296,-37.000653']

Next, we separate them with semicolons using the code snippet below, for example:

3coordinates_to_string = ";".join([x for x in coordinate_list])

We use list comprehension to avoid many lines of code.

Next, we URL-encode the coordinates:

1processed_coordinates = urlparse.quote(coordinates_to_string)

The next step is setting the base URL while passing in the fields and the coordinates. This URL can be found on the API Explorer page after trying out the Snap to Roads API.

1base_url = ""+processed_coordinates+"&vehicleType=PassengerCar&fields="+processed_fields+"&key="+__API_KEY__

We then initiate a request to the API and obtain the route information. We will need the length of the route object to extract data from the output.

1json_response = rq.get(base_url).json()
2routes_obj = json_response['route']
3obj_length = len(routes_obj)

Now let’s take a quick look at the output.

The Output of the API

After executing a request, the output can be downloaded from the API Explorer page by clicking Download. We downloaded and opened this particular route using a JSON tool.

At a glance, we find three objects:

  • route contains the details of the path returned by the API.

  • projectedPoints are the points on the road network taken from the source coordinate projections as passed in the original input sequence.

  • distances contain the summary of the distances covered by the route.

We can see that the total distance is 4869 meters. The distance for private roads is 4830 meters, and the distance for public roads is 39 meters.

distances for roads

Each segment of the route object contains several other objects. For our purposes, we’re interested in traveledDistance and privateRoad, both under the properties object. These represent the distance covered by each route segment and whether the road is a private or a public one.

If the privateRoad property is true, then the road is private, and if it is false, the road is public. In the next steps, we programmatically access these values and display them.

route objects

Accessing the Values Programmatically

Let’s take a moment to check the output. Two lists for storing the private and public road distances are needed:

1private_road_list = []
2public_road_list = []

To avoid repetition and a long line of code, a function for appending the extracted values to the lists will come in handy:

1def list_appender(list_param, idx):
2  list_param.append(routes_obj[idx]['properties']['traveledDistance']['value'])

The line below uses for loops to check whether the value in a segment belongs to a private or public road and append it to a list.

1for i in range(obj_length): list_appender(private_road_list, i) if routes_obj[i]['properties']['privateRoad'] == True else list_appender(public_road_list, i)

We can validate if the values are correct using Python’s sum method:


Per the screenshots shared above, these will return 4830 and 39, respectively.

print sum

For the off-road distance, it is only included in the distances object and not the routes object. So we access it in this manner.

1offroad_distance = json_response['distances']['offRoad']

To display the data, we need three dictionaries for the sum of the three road type distances:

1sum_dict = {
2  "Private Roads": [sum(private_road_list)],
3  "Public Roads": [sum(public_road_list)],
4  "Off roads": [offroad_distance]
7private_df = pd.DataFrame(private_road_list)
8public_df = pd.DataFrame(public_road_list)
9off_road_df = pd.DataFrame([offroad_distance])

Also, we need a helper function for displaying the data frames in tabular form using the tabulate module:

1def display_dataframe(arg,header_vals):
2  print(tabulate(arg, headers=header_vals, tablefmt='psql'))

In individual cells, run the following code snippets to see the displayed tables:

4display_dataframe(distance_df,["Private roads",'Public roads'])

The last one displays this:

displayed tables

The final steps are accessing the file system, creating a CSV file called private-roads.csv in writable form, and adding the private road distance to the CSV file. We do that using the following code:

2def create_csv():
3  file_path = open('private-roads.csv', 'w')
4  file_path.close()
5  private_df.to_csv('private-roads.csv')

Run the method in a new cell:


The file will be created in the current working directory:

current directory

On opening it, we can see the data:

data tables within directory


This short tutorial demonstrates how you can use TomTom’s Snap to Roads API to obtain road usage information, which helps calculate tax rebates in New Zealand and Australia.

We started by preparing the needed data for the API. Next, we invoked the API, visually examined the response, and extracted the information programmatically. Finally, we displayed the data in tabular form and added it to a CSV file.

We hope you gained some valuable insights and a kick-start on using the API. Now try the API for yourself!

Get the developer newsletter.
No marketing fuff. Tech content only.

* Required field. By submitting your contact details to TomTom, you agree that we can contact you about marketing offers, newsletters, or to invite you to webinars and events. We could further personalize the content that you receive via cookies. You can unsubscribe at any time by the link included in our emails. Review our privacy policy.