Turf.jsHelper

turf.featureCollection

What is turf.featureCollection?

turf.featureCollection is a constructor helper in Turf.js that wraps an array of GeoJSON Feature objects into a valid FeatureCollection. It performs no spatial work — it assembles a spec-compliant GeoJSON container that every other Turf function (and tools like Mapbox/MapLibre addSource) will accept.

JavaScript
turf.featureCollection(features, options?) → FeatureCollection

Options include:

  • bbox — a bounding box array ([minX, minY, maxX, maxY]) attached to the collection
  • id — identifier for the collection

When would you use turf.featureCollection?

Use turf.featureCollection whenever you are producing GeoJSON programmatically — converting a database query result, a CSV of coordinates, or the output of multiple Turf calls into a single source that you can hand to MapLibre GL's addSource or pass to another Turf operation like turf.bbox or turf.center.

It is also the idiomatic way to combine features you built with turf.point, turf.polygon, or turf.lineString into one object. If you only have one feature, there is no need to wrap it — most Turf functions accept a lone Feature directly.

JavaScript
1const a = turf.point([-122.42, 37.77], { name: 'SF' });
2const b = turf.point([-73.98, 40.76], { name: 'NYC' });
3const fc = turf.featureCollection([a, b]);

FAQs

How do I install Turf.js to use this function?

Install the scoped helper package for tree-shaking: npm install @turf/helpers, then import { featureCollection } from '@turf/helpers'. The full @turf/turf bundle also exposes it as turf.featureCollection.

Does turf.featureCollection validate each feature?

No. It only assembles the container. If one of the features has malformed coordinates, featureCollection will still return a FeatureCollection containing that invalid feature. Validate inputs beforehand (for example with turf.booleanValid) if you do not trust the source.

Can I mix geometry types in one FeatureCollection?

Yes — the GeoJSON spec allows heterogeneous geometry types inside one FeatureCollection. Map renderers that expect a single geometry type (like some layer styles) will ignore features of the wrong type, so you may prefer to split by type when styling.

When should I use turf.geometryCollection instead?

Use turf.geometryCollection when you need a single Feature whose geometry field contains multiple heterogeneous geometries (rare, and poorly supported in many renderers). Use turf.featureCollection for the common case of grouping multiple independent features with their own properties.