All docschevron-rightHelpchevron-rightarrow-leftGetting Startedchevron-rightSpatial analysis

Spatial analysis

Spatial analysis includes a variety of techniques and processes used to understand the patterns and relationships of geographic features.

How spatial analysis works

Geospatial analysis is a large family of tasks like calculating area and distance and joining points to polygons that enable people to see patterns or relationships within their data. Spatial analysis is used in many industries: to find the nearest coffee shop, calculate travel time, show regional statistics for utility usage, or to analyze accessibility and equity issues like access to ballot drop boxes.

Examples of spatial analysis

The map below is a classic example of spatial analysis, created by physician John Snow during the 1854 cholera outbreak in London. Snow plotted cholera cases in the Soho area of London around Broad Street and noticed a cluster around the water pump. This led to improved sanitation facilities and the discovery that cholera infection was water-borne rather than airborne.

Source: John Snow - Published by C.F. Cheffins, Lith, Southhampton Buildings, London, England, 1854 in Snow, John. On the Mode of Communication of Cholera, 2nd Ed, John Churchill, New Burlington Street, London, England, 1855.

Spatial analysis is also a huge part of geographic information systems (GIS), where analysts use geospatial data to analyze and solve problems in logistics, retail, urban planning, epidemiology, geology, and many other fields and industries.

Using spatial analysis

You can use Mapbox tools like the Isochrone API or open source tools like Turf.js to do spatial analysis on Mapbox maps.

Mapbox Isochrone API

Isochrones reflect the total geographic area you can reach from a specific starting point, given an amount of time and a mode of transportation.

The Mapbox Isochrone API computes what areas are reachable and returns the reachable regions as contours of polygons or lines that you can display on a map.

Isochrone demo

The demo below uses the Isochrone API to generate color-shaded polygons that illustrate what areas are reachable within a 15, 30, 45, and 60-minute drive from Newark Liberty International Airport:

To learn more, try our Isochrone API playground or read our Isochrone API documentation.

Using isochrones as geofences

Isochrones can be useful for geofencing, the practice of defining virtual boundaries around geographic areas so you can trigger automated workflows when a user travels into or out of a geofenced zone.

By using isochrones as geofences, you can create workflows to:

  • Let passengers know when a rideshare driver is two minutes away
  • Pick a curbside pickup order when the customer is 20 minutes away
  • Send push notifications to truck drivers when they approach a warehouse
  • Direct food couriers to high-demand zones during peak delivery hours

Mapbox GL JS and Turf.js

Turf is an open source JavaScript library that allows you to do spatial operations in the browser. Turf helps you analyze, aggregate, and transform data to visualize it in new ways and answer advanced questions.

You can add Turf to your webpage in the <head> of your document alongside Mapbox GL JS. You can use the CDN (the URL displayed below), or you can download the Turf library and source it locally.

<script src='https://npmcdn.com/@turf/turf/turf.min.js'></script>

When you add Turf to your site, it exposes a global variable turf from which you can run any of the turf functions. For example, if you have a GeoJSON LineString feature named dc and you want to know its length in miles, you can use the length function.

The example below uses Mapbox GL JS and Turf to measure the distance along a line using turf.length. Click on the map to start drawing a line.

guide
Measure distances in Mapbox GL JS

Click points on a map to create lines that measure distanced using turf.length.

chevron-right

Maps SDK for Android and the Java SDK

The Mapbox Java SDK includes a library for using Turf algorithms in Java projects. Here's an example that uses the Java SDK's Turf library with the Maps SDK for Android to generate a circle with a radius expressed in physical distance units:

guide
Java SDK Turf library

Get started with the Java SDK's Turf library.

chevron-right

Maps SDK for iOS and Turf for Swift

Turf for Swift is a spatial analysis library written in Swift (ported from Turf.js) and can be used with the Mapbox Maps SDK for iOS. You can read more about the Turf for Swift library on GitHub.

Was this page helpful?