Functions / Turf.js / turf.tag
Turf.jsJoins

turf.tag

What is turf.tag?

turf.tag performs a point-in-polygon spatial join. For every point inside one of the polygons, it copies a named property from the polygon onto the point under a new property name.

JavaScript
turf.tag(points, polygons, field, outField) → FeatureCollection<Point>

Parameters:

  • points — FeatureCollection of Points
  • polygons — FeatureCollection of Polygons or MultiPolygons
  • field — property name on the polygons to copy
  • outField — property name to write on each matching point

When would you use turf.tag?

Use turf.tag whenever you need each point to know which polygon it belongs to — stamping every delivery ping with its zone id, labelling incident reports with the precinct name, or assigning every tree in an inventory to its census block. After tagging, you can group or aggregate by the new property with ordinary JavaScript.

Points that fall outside all polygons appear in the output with the outField unset. If you only care about the points that match, follow up with pointsWithinPolygon or filter the result manually.

JavaScript
1const tagged = turf.tag(orders, zones, 'zoneId', 'zone');
2const byZone = tagged.features.reduce((acc, f) => {
3  const id = f.properties.zone;
4  if (id) (acc[id] = acc[id] || 0, acc[id] += 1);
5  return acc;
6}, {});

FAQs

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

Install npm install @turf/tag and import with import { tag } from '@turf/tag'. It is also available via the umbrella @turf/turf package.

What if a point lies inside two overlapping polygons?

The point takes the value from the last matching polygon iterated — which depends on the order in the input FeatureCollection. Pre-dissolve overlaps or choose a deterministic ordering before calling turf.tag.

Does the output include points that did not match any polygon?

Yes. Every input point is in the output; the outField is simply absent on non-matches. Filter for f.properties[outField] !== undefined if you need matches only.

When should I use turf.tag versus turf.pointsWithinPolygon?

Use turf.tag when you need to know which polygon each point belongs to. Use turf.pointsWithinPolygon when you only care whether the point is in any polygon at all.