turf.interpolate
What is turf.interpolate?
turf.interpolate takes a FeatureCollection of Points with a numeric property and estimates that property across a regular grid using Inverse Distance Weighting (IDW). It returns a FeatureCollection of grid cells (Point or Polygon) whose properties[property] values have been interpolated from the inputs.
turf.interpolate(points, cellSize, options?) → FeatureCollection<Point | Polygon>Options include:
gridType—'square'(default),'point','hex','triangle'property— property name to read and write (default'elevation')units— distance units forcellSize(default'kilometers')weight— IDW distance-decay exponent (default1)bbox— grid extent, computed from points if omitted
When would you use turf.interpolate?
Use turf.interpolate for quick client-side heatmap or choropleth surfaces when you have scattered sensor readings — air-quality stations, temperature gauges, noise monitors, rainfall totals — and want to render a continuous surface without round-tripping to a geostatistics server.
IDW is fast and simple, but it is not a substitute for kriging or spline interpolation when you need uncertainty estimates or honour anisotropy. For visual dashboards and EDA, though, it is the default choice.
1const surface = turf.interpolate(stations, 1, {
2 gridType: 'hex',
3 property: 'pm25',
4 units: 'kilometers',
5 weight: 2
6});FAQs
How do I install Turf.js to use this function?
Install the scoped package npm install @turf/interpolate and import import { interpolate } from '@turf/interpolate'. The umbrella @turf/turf bundle exposes it as turf.interpolate.
What does the weight option do?
weight is the power parameter in IDW. Higher values (3, 4) make nearby points dominate more sharply — producing "bullseye" artefacts around samples. Lower values (1, 2) produce smoother fields. The default of 1 is fine for smooth data; raise to 2 for most real-world use.
Why is my surface blocky or patchy?
Too few input points, too coarse a cellSize, or a high weight exponent. Increase your station density, lower cellSize, and set weight to 2. If the input points cluster in one corner, the rest of the grid has little information to draw on.
When should I use a different interpolator?
For geostatistics, trend analysis, or anything requiring uncertainty, use kriging via a tool like turf-kriging, gstat, or PostGIS. turf.interpolate is a quick visual tool, not a statistically rigorous method.