Spatial analysis fundamentals
Quick answer
This tutorial introduces Turf.js as a pure computation library: geometry in, geometry or numbers out, usable in browsers and Node. It covers the turf.point, lineString, polygon and featureCollection constructors, the [lng, lat] convention, and the unit rules where distance defaults to kilometers while area is always square meters. You will be able to run your first measurement and sanity-check the result.
Key points
- Call turf.distance(a, b, { units: 'kilometers' }) instead of computing planar distance from coordinates.
- Close polygon rings by repeating the first coordinate at the end of every ring.
- Validate output ranges, since longitudes must stay between -180 and 180 and latitudes between -90 and 90.
Example
<script src="https://cdn.jsdelivr.net/npm/@turf/turf@7/turf.min.js"></script>
<script>
var a = turf.point([116.391263, 39.907359]); // 天安门
var b = turf.point([121.473701, 31.230416]); // 上海人民广场
var km = turf.distance(a, b, { units: 'kilometers' });
console.log(km); // 1067 左右,单位为公里
</script>var geom = { type: 'Point', coordinates: [116.391263, 39.907359] };
var feat = turf.feature(geom, { name: '天安门' });
console.log(feat.properties.name); // 天安门
Notes
- Passing [lat, lng] from Leaflet or map.getCenter() places Beijing in the Indian Ocean without any error.
- turf.area has no units option, so divide the square-meter result by 1e6 for square kilometers.
English edition written for Atlaslog. Code is taken from the original Chinese page and runs unchanged.
Atlaslog (2026). Spatial analysis fundamentals. https://www.jingweizhi.com/en/turf/tutorials/basics.html (accessed 2026-11-03).