turf.randomPolygon
What is turf.randomPolygon?
turf.randomPolygon produces a FeatureCollection<Polygon> of randomly shaped, star-like polygons. Each polygon is a single closed ring built from a configurable number of vertices radiating from a random centre.
turf.randomPolygon(count?, options?) → FeatureCollection<Polygon>Options include:
bbox—[minX, minY, maxX, maxY](default[-180, -90, 180, 90])num_vertices— vertices per polygon (default 10)max_radial_length— maximum degrees from polygon centre to a vertex (default 10)
When would you use turf.randomPolygon?
Use turf.randomPolygon to build fixtures for spatial-join tests (turf.pointsWithinPolygon, turf.tag), synthetic catchment areas for clustering demos, or as throwaway shapes when prototyping a styling idea in MapLibre or Mapbox.
The default max_radial_length: 10 produces polygons ten degrees wide — far too large at city scale. Dial it down (for example to 0.01) for realistic neighborhood-sized outputs.
1const zones = turf.randomPolygon(20, {
2 bbox: [-122.6, 37.6, -122.3, 37.9],
3 num_vertices: 8,
4 max_radial_length: 0.02
5});FAQs
How do I install Turf.js to use this function?
Install with npm install @turf/random and import as import { randomPolygon } from '@turf/random', or reach for turf.randomPolygon via the @turf/turf bundle.
Can the polygons self-intersect?
The generator builds polygons by sorting vertices radially around a centre, so results are not self-intersecting in the generic case. Very long radii combined with small counts can still produce degenerate shapes — validate with turf.booleanValid if it matters.
Why are my polygons spanning half the globe?
The default max_radial_length is 10 degrees. Set a smaller value such as 0.01 for city-scale shapes. The option is in degrees regardless of any units setting.
How do I avoid overlapping polygons?
randomPolygon does not deduplicate or space its outputs. For non-overlapping layouts, use a grid helper like turf.hexGrid or turf.squareGrid and then jitter the cells.