What is @turf/point-on-feature?
@turf/point-on-feature is a module from the Turf.js library that allows you to find a point on a given GeoJSON feature. This can be useful for various geospatial analyses, such as finding the centroid of a polygon or a point on a line.
What are @turf/point-on-feature's main functionalities?
Find a point on a polygon
This feature allows you to find a point on a polygon. The code sample demonstrates how to create a polygon and then find a point on it using the @turf/point-on-feature module.
const turf = require('@turf/turf');
const polygon = turf.polygon([[
[-81, 41],
[-81, 47],
[-72, 47],
[-72, 41],
[-81, 41]
]]);
const pointOnPolygon = turf.pointOnFeature(polygon);
console.log(pointOnPolygon);
Find a point on a line
This feature allows you to find a point on a line. The code sample demonstrates how to create a line and then find a point on it using the @turf/point-on-feature module.
const turf = require('@turf/turf');
const line = turf.lineString([[-83, 30], [-84, 36], [-78, 41]]);
const pointOnLine = turf.pointOnFeature(line);
console.log(pointOnLine);
Other packages similar to @turf/point-on-feature
centroid
The 'centroid' package calculates the centroid of a polygon. While @turf/point-on-feature can find a point on any feature, 'centroid' is specifically focused on finding the centroid of polygons.
geojson-utils
The 'geojson-utils' package provides various utilities for working with GeoJSON data, including finding points on features. It offers a broader range of functionalities compared to @turf/point-on-feature, which is more specialized.
@turf/point-on-feature
pointOnFeature
Takes a Feature or FeatureCollection and returns a Point guaranteed to be on the surface of the feature.
- Given a Polygon, the point will be in the area of the polygon
- Given a LineString, the point will be along the string
- Given a Point, the point will the same as the input
Parameters
geojson
GeoJSON any Feature or FeatureCollection
Examples
var polygon = turf.polygon([[
[116, -36],
[131, -32],
[146, -43],
[155, -25],
[133, -9],
[111, -22],
[116, -36]
]]);
var pointOnPolygon = turf.pointOnFeature(polygon);
var addToMap = [polygon, pointOnPolygon];
Returns Feature<Point> a point on the surface of input
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 module individually:
$ npm install @turf/point-on-feature
Or install the Turf module that includes it as a function:
$ npm install @turf/turf
6.3.0
Fix issues importing Turf for react-native, webpack 5, and other bundlers
(PR https://github.com/Turfjs/turf/pull/2004 - Author r0b0t3d)
(PR https://github.com/Turfjs/turf/pull/2011 - Author mfedderly)
[@turf/turf
][turf] expose @turf/boolean-intersect
(PR https://github.com/Turfjs/turf/pull/2007 - Author rowanwins)