Top Related Projects
π JavaScript library for mobile-friendly interactive maps πΊπ¦
Interactive, thoroughly customizable maps in the browser, powered by vector tiles and WebGL
A lightweight set of tools for working with ArcGIS services in Leaflet. :rocket:
OpenLayers
JavaScript library to transform coordinates from one coordinate system to another, including datum transformations
MapLibre GL JS - Interactive vector tile maps in the browser
Quick Overview
Turf.js is a powerful JavaScript library for geospatial analysis and manipulation. It provides a wide range of functions for working with GeoJSON data, including spatial operations, measurements, transformations, and data classification. Turf.js can be used in both browser and Node.js environments, making it versatile for various geospatial applications.
Pros
- Comprehensive set of geospatial functions
- Easy to use with a simple and consistent API
- Works in both browser and Node.js environments
- Actively maintained with regular updates and improvements
Cons
- Performance can be slower for large datasets compared to native GIS software
- Limited support for complex geospatial operations
- Lacks built-in visualization capabilities
- Some advanced features may require additional libraries or plugins
Code Examples
- Calculate the distance between two points:
import * as turf from '@turf/turf';
const point1 = turf.point([-74.0059, 40.7128]); // New York City
const point2 = turf.point([-118.2437, 34.0522]); // Los Angeles
const distance = turf.distance(point1, point2, {units: 'miles'});
console.log(`Distance: ${distance.toFixed(2)} miles`);
- Create a buffer around a point:
import * as turf from '@turf/turf';
const point = turf.point([-90.548630, 14.616599]); // Guatemala City
const buffer = turf.buffer(point, 10, {units: 'kilometers'});
console.log(JSON.stringify(buffer));
- Find the center of a polygon:
import * as turf from '@turf/turf';
const polygon = turf.polygon([[
[-5, 52], [-4, 56], [-2, 51], [-7, 54], [-5, 52]
]]);
const center = turf.center(polygon);
console.log(JSON.stringify(center));
Getting Started
To start using Turf.js in your project, follow these steps:
- Install Turf.js using npm:
npm install @turf/turf
- Import Turf.js in your JavaScript file:
import * as turf from '@turf/turf';
- Start using Turf.js functions in your code:
const point = turf.point([0, 0]);
const buffer = turf.buffer(point, 100, {units: 'kilometers'});
console.log(JSON.stringify(buffer));
Now you're ready to use Turf.js for geospatial analysis in your project!
Competitor Comparisons
π JavaScript library for mobile-friendly interactive maps πΊπ¦
Pros of Leaflet
- Comprehensive mapping library with built-in UI components and controls
- Extensive plugin ecosystem for additional functionality
- Optimized for mobile devices and touch interactions
Cons of Leaflet
- Focused primarily on web mapping, less suitable for complex geospatial analysis
- Larger file size and potentially higher resource usage for simple mapping tasks
- Steeper learning curve for beginners due to its extensive feature set
Code Comparison
Leaflet (creating a map):
var map = L.map('map').setView([51.505, -0.09], 13);
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png').addTo(map);
Turf (calculating distance between points):
var point1 = turf.point([-75.343, 39.984]);
var point2 = turf.point([-75.534, 39.123]);
var distance = turf.distance(point1, point2, {units: 'miles'});
Summary
Leaflet is a comprehensive web mapping library with a rich ecosystem, ideal for creating interactive maps with various UI components. It excels in mobile optimization but may be overkill for simple mapping tasks. Turf, on the other hand, focuses on geospatial analysis and is more lightweight, making it better suited for complex calculations without the overhead of a full mapping library.
Interactive, thoroughly customizable maps in the browser, powered by vector tiles and WebGL
Pros of mapbox-gl-js
- Comprehensive mapping library with built-in rendering capabilities
- Extensive documentation and community support
- Seamless integration with Mapbox services and data
Cons of mapbox-gl-js
- Larger file size and potentially higher resource usage
- Steeper learning curve for beginners
- Some features require a Mapbox account or API key
Code Comparison
mapbox-gl-js:
mapboxgl.accessToken = 'your-access-token';
const map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/streets-v11',
center: [-74.5, 40],
zoom: 9
});
turf:
const point1 = turf.point([-74.5, 40]);
const point2 = turf.point([-73.5, 41]);
const distance = turf.distance(point1, point2, {units: 'miles'});
console.log(distance);
Key Differences
- mapbox-gl-js focuses on map rendering and interaction, while turf specializes in geospatial analysis and calculations
- turf is more lightweight and can be used independently of any mapping library
- mapbox-gl-js provides a complete mapping solution, whereas turf is often used in conjunction with other mapping libraries
Use Cases
- Choose mapbox-gl-js for full-featured interactive maps with custom styling
- Opt for turf when performing complex geospatial operations without needing map visualization
A lightweight set of tools for working with ArcGIS services in Leaflet. :rocket:
Pros of esri-leaflet
- Seamless integration with Esri services and ArcGIS Online
- Access to rich Esri basemaps and data layers
- Built-in support for Esri authentication and security features
Cons of esri-leaflet
- Dependency on Esri services and potential licensing costs
- Limited to Esri-specific data formats and services
- Steeper learning curve for developers unfamiliar with Esri ecosystem
Code Comparison
esri-leaflet:
L.esri.basemapLayer('Streets').addTo(map);
L.esri.featureLayer({
url: 'https://services.arcgis.com/...'
}).addTo(map);
Turf:
var point = turf.point([-75.343, 39.984]);
var buffer = turf.buffer(point, 50, {units: 'miles'});
L.geoJSON(buffer).addTo(map);
Key Differences
- esri-leaflet focuses on integrating Esri services with Leaflet maps
- Turf is a general-purpose geospatial analysis library
- esri-leaflet is tied to Esri ecosystem, while Turf is more flexible
- Turf provides advanced geospatial operations, esri-leaflet simplifies Esri service consumption
OpenLayers
Pros of OpenLayers
- Comprehensive mapping library with extensive features for creating interactive maps
- Supports a wide range of data formats and map providers
- Large and active community with frequent updates and extensive documentation
Cons of OpenLayers
- Steeper learning curve due to its extensive feature set
- Larger file size, which may impact page load times
- More complex setup process compared to Turf
Code Comparison
OpenLayers:
import Map from 'ol/Map';
import View from 'ol/View';
import TileLayer from 'ol/layer/Tile';
import OSM from 'ol/source/OSM';
const map = new Map({
layers: [new TileLayer({ source: new OSM() })],
target: 'map',
view: new View({ center: [0, 0], zoom: 2 })
});
Turf:
import * as turf from '@turf/turf';
const point1 = turf.point([0, 0]);
const point2 = turf.point([1, 1]);
const distance = turf.distance(point1, point2);
OpenLayers is a full-featured mapping library, while Turf focuses on geospatial analysis. OpenLayers is better suited for creating interactive maps with various layers and controls, whereas Turf excels at performing spatial operations and calculations on geographic data. The choice between the two depends on the specific requirements of your project.
JavaScript library to transform coordinates from one coordinate system to another, including datum transformations
Pros of proj4js
- Specialized in coordinate transformations and projections
- Supports a wide range of coordinate reference systems
- Lightweight and focused on a specific task
Cons of proj4js
- Limited to coordinate transformations and projections
- Lacks advanced geospatial analysis capabilities
- Smaller community and fewer updates compared to Turf
Code Comparison
proj4js:
const proj4 = require('proj4');
const fromCrs = 'EPSG:4326';
const toCrs = 'EPSG:3857';
const point = [12.5, 41.9];
const transformedPoint = proj4(fromCrs, toCrs, point);
Turf:
const turf = require('@turf/turf');
const point = turf.point([12.5, 41.9]);
const buffer = turf.buffer(point, 100, {units: 'kilometers'});
const area = turf.area(buffer);
Summary
proj4js excels in coordinate transformations and projections, supporting a wide range of coordinate reference systems. It's lightweight and focused on this specific task. However, it lacks the broader geospatial analysis capabilities offered by Turf.
Turf, on the other hand, provides a comprehensive suite of geospatial analysis tools, including distance calculations, area measurements, and various geometric operations. It has a larger community and more frequent updates but may be overkill for projects that only require coordinate transformations.
Choose proj4js for projects focused on coordinate transformations and projections, and Turf for more complex geospatial analysis tasks.
MapLibre GL JS - Interactive vector tile maps in the browser
Pros of maplibre-gl-js
- Provides a full-featured mapping library with interactive, customizable maps
- Supports vector tiles and 3D terrain rendering
- Offers a wide range of built-in controls and UI elements
Cons of maplibre-gl-js
- Larger file size and potentially higher resource usage
- Steeper learning curve for basic geospatial operations
- Less focused on geospatial analysis and calculations
Code Comparison
maplibre-gl-js:
const map = new maplibregl.Map({
container: 'map',
style: 'https://demotiles.maplibre.org/style.json',
center: [-74.5, 40],
zoom: 9
});
Turf:
const point1 = turf.point([-74.5, 40]);
const point2 = turf.point([-73.5, 41]);
const distance = turf.distance(point1, point2, {units: 'kilometers'});
Summary
maplibre-gl-js is a comprehensive mapping library that excels in rendering interactive maps with various features and controls. It's ideal for projects requiring full-fledged map visualization and interaction. On the other hand, Turf focuses on geospatial analysis and calculations, making it more suitable for projects that need to perform spatial operations without necessarily rendering maps. The choice between the two depends on the specific requirements of your project, with maplibre-gl-js being better for map rendering and Turf for geospatial computations.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
A modular geospatial engine written in JavaScript
Turf is a JavaScript library for spatial analysis. It includes traditional spatial operations, helper functions for creating GeoJSON data, and data classification and statistics tools. Turf can be added to your website as a client-side plugin, or you can run Turf server-side with Node.js (see below).
Installation
In Node.js
# get all of turf
npm install @turf/turf
# or get individual packages
npm install @turf/helpers
npm install @turf/buffer
In browser
Download the minified file, and include it in a script tag. This will expose a global variable named turf
.
<script src="turf.min.js" charset="utf-8"></script>
You can also include it directly from a CDN:
<script src="https://cdn.jsdelivr.net/npm/@turf/turf@6/turf.min.js"></script>
TypeScript
TypeScript definitions are packaged with each module. No DefinitelyTyped packages required.
Other languages
Ports of Turf.js are available in:
- Java (Android, Java SE)
- Swift (iOS, macOS, tvOS, watchOS, Linux)
-
Turf for Swift is experimental and its public API is subject to change. Please use with care.
-
- Dart/Flutter (Dart Web, Dart Native; Flutter for iOS, Android, macOS, Windows, Linux, Web)
-
The Turf for Dart port is still in progress, the implementation status can be found in the README.
-
Data in Turf
Turf uses GeoJSON for all geographic data. Turf expects the data to be standard WGS84 longitude, latitude coordinates. Check out geojson.io for a tool to easily create this data.
NOTE: Turf expects data in (longitude, latitude) order per the GeoJSON standard.
Most Turf functions work with GeoJSON features. These are pieces of data that represent a collection of properties (ie: population, elevation, zipcode, etc.) along with a geometry. GeoJSON has several geometry types such as:
- Point
- LineString
- Polygon
Turf provides a few geometry functions of its own. These are nothing more than simple (and optional) wrappers that output plain old GeoJSON. For example, these two methods of creating a point are functionally equivalent:
// Note order: longitude, latitude.
var point1 = turf.point([-73.988214, 40.749128]);
var point2 = {
type: 'Feature',
geometry: {
type: 'Point',
// Note order: longitude, latitude.
coordinates: [-73.988214, 40.749128]
},
properties: {}
};
Browser support
Turf packages are compiled to target ES2017. However, the browser version of @turf/turf is transpiled to also include support for IE11. If you are using these packages and need to target IE11, please transpile the following packages as part of your build:
@turf/*
robust-predicates
rbush
tinyqueue
Contributors
This project exists thanks to all the people who contribute. If you are interested in helping, check out the Contributing Guide.
Backers
Thank you to all our backers! Γ°ΒΒΒ [Become a backer]
Sponsors
Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [Become a sponsor]
Top Related Projects
π JavaScript library for mobile-friendly interactive maps πΊπ¦
Interactive, thoroughly customizable maps in the browser, powered by vector tiles and WebGL
A lightweight set of tools for working with ArcGIS services in Leaflet. :rocket:
OpenLayers
JavaScript library to transform coordinates from one coordinate system to another, including datum transformations
MapLibre GL JS - Interactive vector tile maps in the browser
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot