What is @turf/truncate?
@turf/truncate is a module in the Turf.js library that is used to truncate the precision of coordinates in GeoJSON data. This can be useful for reducing the size of GeoJSON files or for ensuring consistent precision across datasets.
What are @turf/truncate's main functionalities?
Truncate Coordinates
This feature allows you to truncate the coordinates of a GeoJSON object to a specified precision. In this example, the coordinates of a point are truncated to 3 decimal places.
const truncate = require('@turf/truncate');
const point = { "type": "Feature", "geometry": { "type": "Point", "coordinates": [70.123456789, 40.987654321] } };
const truncated = truncate(point, { precision: 3 });
console.log(truncated);
Truncate with Custom Options
This feature allows you to truncate the coordinates of a GeoJSON object with custom options. In this example, the coordinates of a LineString are truncated to 2 decimal places, and the number of coordinates is also limited to 2.
const truncate = require('@turf/truncate');
const lineString = { "type": "Feature", "geometry": { "type": "LineString", "coordinates": [[70.123456789, 40.987654321], [71.123456789, 41.987654321]] } };
const truncated = truncate(lineString, { precision: 2, coordinates: 2 });
console.log(truncated);
Other packages similar to @turf/truncate
geojson-precision
The geojson-precision package allows you to set the precision of GeoJSON coordinates. It is similar to @turf/truncate in that it helps reduce the size of GeoJSON files by limiting the number of decimal places in coordinates. However, it is a standalone package and not part of a larger geospatial library like Turf.js.
simplify-geojson
The simplify-geojson package is used to simplify GeoJSON geometries by reducing the number of points in the geometries. While it does not specifically truncate coordinate precision, it serves a similar purpose of reducing the size and complexity of GeoJSON data. It is more focused on simplifying shapes rather than just truncating coordinates.
@turf/truncate
truncate
Takes a GeoJSON Feature or FeatureCollection and truncates the precision of the geometry.
Parameters
-
geojson
GeoJSON any GeoJSON Feature, FeatureCollection, Geometry or GeometryCollection.
-
options
Object Optional parameters (optional, default {}
)
options.precision
number coordinate decimal precision (optional, default 6
)options.coordinates
number maximum number of coordinates (primarly used to remove z coordinates) (optional, default 3
)options.mutate
boolean allows GeoJSON input to be mutated (significant performance increase if true) (optional, default false
)
Examples
var point = turf.point([
70.46923055566859,
58.11088890802906,
1508
]);
var options = {precision: 3, coordinates: 2};
var truncated = turf.truncate(point, options);
var addToMap = [truncated];
Returns GeoJSON layer with truncated geometry
This module is part of the Turfjs project, an open source module collection dedicated to geographic algorithms. It is maintained in the Turfjs/turf repository, where you can create PRs and issues.
Installation
Install this single module individually:
$ npm install @turf/truncate
Or install the all-encompassing @turf/turf module that includes all modules as functions:
$ npm install @turf/turf