0
# Data Generation and Grids
1
2
Functions for generating geometric data including grids, random features, and structured spatial patterns. These functions create new GeoJSON data for analysis, testing, and spatial modeling.
3
4
## Capabilities
5
6
### Grid Generation
7
8
Create regular grid patterns for spatial analysis and data sampling.
9
10
```typescript { .api }
11
/**
12
* Creates a PointGrid as a FeatureCollection of Point features.
13
*/
14
function pointGrid(
15
bbox: BBox,
16
cellSide: number,
17
options?: { units?: Units; mask?: Feature<Polygon | MultiPolygon>; properties?: GeoJsonProperties }
18
): FeatureCollection<Point>;
19
20
/**
21
* Creates a square grid as a FeatureCollection of Polygon features.
22
*/
23
function squareGrid(
24
bbox: BBox,
25
cellSide: number,
26
options?: { units?: Units; mask?: Feature<Polygon | MultiPolygon>; properties?: GeoJsonProperties }
27
): FeatureCollection<Polygon>;
28
29
/**
30
* Creates a hexagonal grid as a FeatureCollection of Polygon features.
31
*/
32
function hexGrid(
33
bbox: BBox,
34
cellSide: number,
35
options?: { units?: Units; properties?: GeoJsonProperties; mask?: Feature<Polygon | MultiPolygon>; triangles?: boolean }
36
): FeatureCollection<Polygon>;
37
38
/**
39
* Creates a triangular grid as a FeatureCollection of Polygon features.
40
*/
41
function triangleGrid(
42
bbox: BBox,
43
cellSide: number,
44
options?: { units?: Units; properties?: GeoJsonProperties; mask?: Feature<Polygon | MultiPolygon> }
45
): FeatureCollection<Polygon>;
46
47
/**
48
* Creates a rectangular grid as a FeatureCollection of Polygon features.
49
*/
50
function rectangleGrid(
51
bbox: BBox,
52
cellWidth: number,
53
cellHeight: number,
54
options?: { units?: Units; properties?: GeoJsonProperties; mask?: Feature<Polygon | MultiPolygon> }
55
): FeatureCollection<Polygon>;
56
```
57
58
**Usage Examples:**
59
60
```typescript
61
import { pointGrid, squareGrid, hexGrid, triangleGrid, polygon } from "@turf/turf";
62
63
// Define study area
64
const bbox = [-95, 30, -85, 40]; // [west, south, east, north]
65
66
// Create different grid types
67
const points = pointGrid(bbox, 50, { units: 'miles' });
68
const squares = squareGrid(bbox, 50, { units: 'miles' });
69
const hexagons = hexGrid(bbox, 50, { units: 'miles' });
70
const triangles = triangleGrid(bbox, 50, { units: 'miles' });
71
72
// Create masked grid (only cells within polygon)
73
const mask = polygon([[[-90, 35], [-90, 38], [-88, 38], [-88, 35], [-90, 35]]]);
74
const maskedGrid = squareGrid(bbox, 25, {
75
units: 'miles',
76
mask: mask,
77
properties: { type: 'grid_cell' }
78
});
79
80
console.log(`Point grid: ${points.features.length} points`);
81
console.log(`Square grid: ${squares.features.length} squares`);
82
console.log(`Masked grid: ${maskedGrid.features.length} squares`);
83
```
84
85
### Random Data Generation
86
87
Generate random geographic features for testing and simulation.
88
89
```typescript { .api }
90
/**
91
* Returns a random position within a bounding box.
92
*/
93
function randomPosition(bbox?: BBox): Position;
94
95
/**
96
* Returns a FeatureCollection of random Point features.
97
*/
98
function randomPoint(
99
count?: number,
100
options?: { bbox?: BBox }
101
): FeatureCollection<Point>;
102
103
/**
104
* Returns a FeatureCollection of random Polygon features.
105
*/
106
function randomPolygon(
107
count?: number,
108
options?: { bbox?: BBox; num_vertices?: number; max_radial_length?: number }
109
): FeatureCollection<Polygon>;
110
111
/**
112
* Returns a FeatureCollection of random LineString features.
113
*/
114
function randomLineString(
115
count?: number,
116
options?: { bbox?: BBox; num_vertices?: number; max_length?: number; max_rotation?: number }
117
): FeatureCollection<LineString>;
118
```
119
120
**Usage Examples:**
121
122
```typescript
123
import { randomPoint, randomPolygon, randomLineString, randomPosition } from "@turf/turf";
124
125
// Generate random features within bounds
126
const bbox = [-180, -90, 180, 90];
127
128
// Random points
129
const randomPoints = randomPoint(100, { bbox });
130
131
// Random polygons with custom parameters
132
const randomPolygons = randomPolygon(20, {
133
bbox,
134
num_vertices: 6,
135
max_radial_length: 10
136
});
137
138
// Random lines
139
const randomLines = randomLineString(15, {
140
bbox,
141
num_vertices: 4,
142
max_length: 100,
143
max_rotation: Math.PI
144
});
145
146
// Single random position
147
const randomPos = randomPosition(bbox);
148
149
console.log(`Generated ${randomPoints.features.length} random points`);
150
console.log(`Generated ${randomPolygons.features.length} random polygons`);
151
console.log(`Random position: [${randomPos[0]}, ${randomPos[1]}]`);
152
```
153
154
### Geometric Shape Generation
155
156
Create specific geometric shapes and patterns.
157
158
```typescript { .api }
159
/**
160
* Takes a Point and calculates the great circle route to a second Point.
161
*/
162
function greatCircle(
163
start: Coord,
164
end: Coord,
165
options?: { offset?: number; npoints?: number; properties?: GeoJsonProperties }
166
): FeatureCollection<LineString>;
167
168
/**
169
* Creates a line arc between two points.
170
*/
171
function lineArc(
172
center: Coord,
173
radius: number,
174
bearing1: number,
175
bearing2: number,
176
options?: { steps?: number; units?: Units }
177
): Feature<LineString>;
178
179
/**
180
* Takes a center point and returns a square polygon.
181
*/
182
function square(
183
center: Coord,
184
sideLength: number,
185
options?: { units?: Units; properties?: GeoJsonProperties }
186
): Feature<Polygon>;
187
```
188
189
**Usage Examples:**
190
191
```typescript
192
import { greatCircle, lineArc, square, point } from "@turf/turf";
193
194
const start = point([-122, 48]);
195
const end = point([24, 60]);
196
const center = point([-75, 40]);
197
198
// Create great circle route
199
const route = greatCircle(start, end, { npoints: 100 });
200
201
// Create arc
202
const arc = lineArc(center, 50, 0, 90, {
203
units: 'kilometers',
204
steps: 32
205
});
206
207
// Create square
208
const squareShape = square(center, 100, {
209
units: 'kilometers',
210
properties: { type: 'square' }
211
});
212
213
console.log('Great circle route:', route);
214
console.log('Arc:', arc);
215
console.log('Square:', squareShape);
216
```
217
218
### Bezier Curves and Splines
219
220
Create smooth curved lines from control points.
221
222
```typescript { .api }
223
/**
224
* Takes a line and returns a curved version by applying a Bezier spline algorithm.
225
*/
226
function bezierSpline<P>(
227
line: Feature<LineString, P>,
228
options?: { resolution?: number; sharpness?: number }
229
): Feature<LineString, P>;
230
```
231
232
**Usage Examples:**
233
234
```typescript
235
import { bezierSpline, lineString } from "@turf/turf";
236
237
// Create angular line
238
const line = lineString([
239
[-76.091308, 18.427501],
240
[-76.695556, 18.729501],
241
[-76.552734, 19.40443],
242
[-74.61914, 19.134789],
243
[-73.652343, 20.07657],
244
[-73.157958, 20.210656]
245
]);
246
247
// Create smooth spline
248
const spline = bezierSpline(line, {
249
resolution: 10000,
250
sharpness: 0.85
251
});
252
253
console.log('Original line points:', line.geometry.coordinates.length);
254
console.log('Spline points:', spline.geometry.coordinates.length);
255
```