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, and show regional statistics for utility usage. It's also a huge part of GIS, where many problems are solved with spatial analysis.

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.

map of 1854 cholera outbreak in London
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.

Today, spatial analysis is used in epidemiology, biology, statistics, economics, commerce and business, urban planning, geology, oil and gas, and in many other industries.

Using spatial analysis

There are open source tools you can use to perform spatial analysis on a Mapbox map.

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 or Mapbox.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.

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.

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?