What is @turf/mask?
@turf/mask is a module in the Turf.js library that allows you to create a mask from a polygon. This mask can be used to clip or hide parts of other geometries that fall outside the polygon. It is particularly useful in geospatial analysis for focusing on specific areas of interest.
What are @turf/mask's main functionalities?
Create a Mask
This feature allows you to create a mask from a given polygon. The mask can then be used to clip other geometries, effectively hiding parts of them that fall outside the polygon.
const turf = require('@turf/turf');
const polygon = turf.polygon([[
[-5, 52],
[-4, 52],
[-4, 53],
[-5, 53],
[-5, 52]
]]);
const mask = turf.mask(polygon);
console.log(JSON.stringify(mask));
Clip Geometry with Mask
This feature demonstrates how to use the mask to clip a line geometry. The resulting geometry will only include the parts of the line that fall within the mask polygon.
const turf = require('@turf/turf');
const polygon = turf.polygon([[
[-5, 52],
[-4, 52],
[-4, 53],
[-5, 53],
[-5, 52]
]]);
const mask = turf.mask(polygon);
const line = turf.lineString([[-6, 51], [-3, 54]]);
const clipped = turf.mask(line, mask);
console.log(JSON.stringify(clipped));
Other packages similar to @turf/mask
turf-clip
The turf-clip package provides functionalities to clip geometries using polygons. It is similar to @turf/mask but focuses more on clipping rather than masking.
jsts
The jsts (JavaScript Topology Suite) library offers a wide range of spatial operations, including clipping and masking. It is more comprehensive than @turf/mask but also more complex to use.
geojson-merge
The geojson-merge package allows you to merge multiple GeoJSON objects into one. While it doesn't offer masking directly, it can be used in conjunction with other tools to achieve similar results.
@turf/mask
mask
Takes polygons or multipolygons and an optional mask, and returns an exterior
ring polygon with holes.
Parameters
Examples
const polygon = turf.polygon([[[112, -21], [116, -36], [146, -39], [153, -24], [133, -10], [112, -21]]]);
const mask = turf.polygon([[[90, -55], [170, -55], [170, 10], [90, 10], [90, -55]]]);
const masked = turf.mask(polygon, mask);
const addToMap = [masked]
Returns Feature<Polygon> Masked Polygon (exterior ring with holes)
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/mask
Or install the all-encompassing @turf/turf module that includes all modules as functions:
$ npm install @turf/turf