or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

advanced-analysis.mdboolean-operations.mdcoordinate-operations.mddata-generation.mddata-utilities.mdgeometric-operations.mdhelpers.mdindex.mdinvariant.mdline-processing.mdmeasurement.mdmeta.mdspatial-analysis.md

meta.mddocs/

0

# Geometry Iteration and Manipulation

1

2

Utilities for iterating over and manipulating GeoJSON geometries, coordinates, and properties using functional programming patterns. These functions provide efficient ways to traverse and transform geographic data structures.

3

4

## Capabilities

5

6

### Coordinate Iteration

7

8

Functions for iterating over coordinate arrays in GeoJSON objects.

9

10

```typescript { .api }

11

/**

12

* Iterate over coordinates in any GeoJSON object, similar to Array.forEach()

13

*/

14

function coordEach(

15

geojson: AllGeoJSON,

16

callback: (

17

currentCoord: number[],

18

coordIndex: number,

19

featureIndex: number,

20

multiFeatureIndex: number,

21

geometryIndex: number

22

) => void,

23

excludeWrapCoord?: boolean

24

): void;

25

26

/**

27

* Reduce coordinates in any GeoJSON object, similar to Array.reduce()

28

*/

29

function coordReduce<Reducer>(

30

geojson: AllGeoJSON,

31

callback: (

32

previousValue: Reducer,

33

currentCoord: number[],

34

coordIndex: number,

35

featureIndex: number,

36

multiFeatureIndex: number,

37

geometryIndex: number

38

) => Reducer,

39

initialValue?: Reducer

40

): Reducer;

41

42

/**

43

* Get all coordinates from any GeoJSON object.

44

*/

45

function coordAll(geojson: AllGeoJSON): number[][];

46

```

47

48

**Usage Examples:**

49

50

```typescript

51

import { coordEach, coordReduce, coordAll, polygon } from "@turf/turf";

52

53

const poly = polygon([[[0, 0], [0, 10], [10, 10], [10, 0], [0, 0]]]);

54

55

// Iterate over each coordinate

56

coordEach(poly, (coord, coordIndex) => {

57

console.log(`Coordinate ${coordIndex}: [${coord[0]}, ${coord[1]}]`);

58

});

59

60

// Calculate the sum of x coordinates

61

const sumX = coordReduce(poly, (sum, coord) => sum + coord[0], 0);

62

63

// Get all coordinates as an array

64

const allCoords = coordAll(poly);

65

// Returns: [[0, 0], [0, 10], [10, 10], [10, 0], [0, 0]]

66

```

67

68

### Feature Iteration

69

70

Functions for iterating over features in FeatureCollections.

71

72

```typescript { .api }

73

/**

74

* Iterate over features in any GeoJSON object, similar to Array.forEach.

75

*/

76

function featureEach<G, P>(

77

geojson: Feature<G, P> | FeatureCollection<G, P> | Feature<GeometryCollection, P>,

78

callback: (currentFeature: Feature<G, P>, featureIndex: number) => void

79

): void;

80

81

/**

82

* Reduce features in any GeoJSON object, similar to Array.reduce().

83

*/

84

function featureReduce<Reducer, G, P>(

85

geojson: Feature<G, P> | FeatureCollection<G, P> | Feature<GeometryCollection, P>,

86

callback: (

87

previousValue: Reducer,

88

currentFeature: Feature<G, P>,

89

featureIndex: number

90

) => Reducer,

91

initialValue?: Reducer

92

): Reducer;

93

```

94

95

**Usage Examples:**

96

97

```typescript

98

import { featureEach, featureReduce, featureCollection, point } from "@turf/turf";

99

100

const points = featureCollection([

101

point([0, 0], { name: "Point A" }),

102

point([1, 1], { name: "Point B" }),

103

point([2, 2], { name: "Point C" })

104

]);

105

106

// Iterate over each feature

107

featureEach(points, (feature, index) => {

108

console.log(`Feature ${index}: ${feature.properties.name}`);

109

});

110

111

// Count features with specific properties

112

const namedFeatures = featureReduce(points, (count, feature) => {

113

return feature.properties.name ? count + 1 : count;

114

}, 0);

115

```

116

117

### Property Iteration

118

119

Functions for iterating over feature properties.

120

121

```typescript { .api }

122

/**

123

* Iterate over properties in any GeoJSON object, similar to Array.forEach()

124

*/

125

function propEach<Props>(

126

geojson: Feature<any> | FeatureCollection<any> | Feature<GeometryCollection>,

127

callback: (currentProperties: Props, featureIndex: number) => void

128

): void;

129

130

/**

131

* Reduce properties in any GeoJSON object into a single value, similar to how Array.reduce works.

132

*/

133

function propReduce<Reducer, P>(

134

geojson: Feature<any, P> | FeatureCollection<any, P> | Geometry,

135

callback: (

136

previousValue: Reducer,

137

currentProperties: P,

138

featureIndex: number

139

) => Reducer,

140

initialValue?: Reducer

141

): Reducer;

142

```

143

144

**Usage Examples:**

145

146

```typescript

147

import { propEach, propReduce, featureCollection, point } from "@turf/turf";

148

149

const points = featureCollection([

150

point([0, 0], { population: 1000, name: "City A" }),

151

point([1, 1], { population: 2000, name: "City B" }),

152

point([2, 2], { population: 1500, name: "City C" })

153

]);

154

155

// Iterate over properties

156

propEach(points, (properties, index) => {

157

console.log(`${properties.name}: ${properties.population}`);

158

});

159

160

// Calculate total population

161

const totalPopulation = propReduce(points, (total, props) => {

162

return total + (props.population || 0);

163

}, 0);

164

```

165

166

### Geometry Iteration

167

168

Functions for iterating over geometries within GeoJSON objects.

169

170

```typescript { .api }

171

/**

172

* Iterate over each geometry in any GeoJSON object, similar to Array.forEach()

173

*/

174

function geomEach<G, P>(

175

geojson: Feature<G, P> | FeatureCollection<G, P> | G | GeometryCollection | Feature<GeometryCollection, P>,

176

callback: (

177

currentGeometry: G,

178

featureIndex: number,

179

featureProperties: P,

180

featureBBox: BBox,

181

featureId: Id

182

) => void

183

): void;

184

185

/**

186

* Reduce geometry in any GeoJSON object, similar to Array.reduce().

187

*/

188

function geomReduce<Reducer, G, P>(

189

geojson: Feature<G, P> | FeatureCollection<G, P> | G | GeometryCollection | Feature<GeometryCollection, P>,

190

callback: (

191

previousValue: Reducer,

192

currentGeometry: G,

193

featureIndex: number,

194

featureProperties: P,

195

featureBBox: BBox,

196

featureId: Id

197

) => Reducer,

198

initialValue?: Reducer

199

): Reducer;

200

```

201

202

### Flattened Feature Iteration

203

204

Functions for iterating over flattened features (Multi* geometries split into individual features).

205

206

```typescript { .api }

207

/**

208

* Iterate over flattened features in any GeoJSON object, similar to Array.forEach.

209

*/

210

function flattenEach<G, P>(

211

geojson: Feature<G, P> | FeatureCollection<G, P> | G | GeometryCollection | Feature<GeometryCollection, P>,

212

callback: (

213

currentFeature: Feature<G, P>,

214

featureIndex: number,

215

multiFeatureIndex: number

216

) => void

217

): void;

218

219

/**

220

* Reduce flattened features in any GeoJSON object, similar to Array.reduce().

221

*/

222

function flattenReduce<Reducer, G, P>(

223

geojson: Feature<G, P> | FeatureCollection<G, P> | G | GeometryCollection | Feature<GeometryCollection, P>,

224

callback: (

225

previousValue: Reducer,

226

currentFeature: Feature<G, P>,

227

featureIndex: number,

228

multiFeatureIndex: number

229

) => Reducer,

230

initialValue?: Reducer

231

): Reducer;

232

```

233

234

### Line and Segment Iteration

235

236

Functions for iterating over line segments and linear features.

237

238

```typescript { .api }

239

/**

240

* Iterate over 2-vertex line segment in any GeoJSON object, similar to Array.forEach()

241

* (Multi)Point geometries do not contain segments therefore they are ignored during this operation.

242

*/

243

function segmentEach<P>(

244

geojson: AllGeoJSON,

245

callback: (

246

currentSegment?: Feature<LineString, P>,

247

featureIndex?: number,

248

multiFeatureIndex?: number,

249

segmentIndex?: number,

250

geometryIndex?: number

251

) => void

252

): void;

253

254

/**

255

* Reduce 2-vertex line segment in any GeoJSON object, similar to Array.reduce()

256

* (Multi)Point geometries do not contain segments therefore they are ignored during this operation.

257

*/

258

function segmentReduce<Reducer, P>(

259

geojson: FeatureCollection<Lines, P> | Feature<Lines, P> | Lines | Feature<GeometryCollection, P> | GeometryCollection,

260

callback: (

261

previousValue?: Reducer,

262

currentSegment?: Feature<LineString, P>,

263

featureIndex?: number,

264

multiFeatureIndex?: number,

265

segmentIndex?: number,

266

geometryIndex?: number

267

) => Reducer,

268

initialValue?: Reducer

269

): Reducer;

270

271

/**

272

* Iterate over line or ring coordinates in LineString, Polygon, MultiLineString, MultiPolygon Features or Geometries, similar to Array.forEach.

273

*/

274

function lineEach<P>(

275

geojson: FeatureCollection<Lines, P> | Feature<Lines, P> | Lines | Feature<GeometryCollection, P> | GeometryCollection,

276

callback: (

277

currentLine: Feature<LineString, P>,

278

featureIndex?: number,

279

multiFeatureIndex?: number,

280

geometryIndex?: number

281

) => void

282

): void;

283

284

/**

285

* Reduce features in any GeoJSON object, similar to Array.reduce().

286

*/

287

function lineReduce<Reducer, P>(

288

geojson: FeatureCollection<Lines, P> | Feature<Lines, P> | Lines | Feature<GeometryCollection, P> | GeometryCollection,

289

callback: (

290

previousValue?: Reducer,

291

currentLine?: Feature<LineString, P>,

292

featureIndex?: number,

293

multiFeatureIndex?: number,

294

geometryIndex?: number

295

) => Reducer,

296

initialValue?: Reducer

297

): Reducer;

298

```

299

300

**Usage Examples:**

301

302

```typescript

303

import { segmentEach, lineEach, polygon, lineString } from "@turf/turf";

304

305

const line = lineString([[0, 0], [1, 1], [2, 2], [3, 3]]);

306

307

// Iterate over each segment

308

segmentEach(line, (segment, featureIndex, multiFeatureIndex, segmentIndex) => {

309

console.log(`Segment ${segmentIndex}:`, segment?.geometry.coordinates);

310

});

311

312

// Iterate over each line (useful for polygons with multiple rings)

313

const poly = polygon([

314

[[0, 0], [0, 10], [10, 10], [10, 0], [0, 0]], // exterior ring

315

[[2, 2], [2, 8], [8, 8], [8, 2], [2, 2]] // hole

316

]);

317

318

lineEach(poly, (line, featureIndex, multiFeatureIndex, geometryIndex) => {

319

console.log(`Ring ${geometryIndex}:`, line.geometry.coordinates);

320

});

321

```

322

323

### Geometry Finder Functions

324

325

Functions for finding specific geometric elements by index.

326

327

```typescript { .api }

328

/**

329

* Finds a particular 2-vertex LineString Segment from a GeoJSON using @turf/meta indexes.

330

* Negative indexes are permitted. Point & MultiPoint will always return null.

331

*/

332

function findSegment<G, P>(

333

geojson: Feature<G, P> | FeatureCollection<G, P> | G,

334

options?: {

335

featureIndex?: number;

336

multiFeatureIndex?: number;

337

geometryIndex?: number;

338

segmentIndex?: number;

339

properties?: P;

340

bbox?: BBox;

341

id?: Id;

342

}

343

): Feature<LineString, P>;

344

345

/**

346

* Finds a particular Point from a GeoJSON using @turf/meta indexes.

347

* Negative indexes are permitted.

348

*/

349

function findPoint<G, P>(

350

geojson: Feature<G, P> | FeatureCollection<G, P> | G,

351

options?: {

352

featureIndex?: number;

353

multiFeatureIndex?: number;

354

geometryIndex?: number;

355

coordIndex?: number;

356

properties?: P;

357

bbox?: BBox;

358

id?: Id;

359

}

360

): Feature<Point, P>;

361

```

362

363

**Usage Examples:**

364

365

```typescript

366

import { findSegment, findPoint, polygon } from "@turf/turf";

367

368

const poly = polygon([[[0, 0], [0, 10], [10, 10], [10, 0], [0, 0]]]);

369

370

// Find specific segment

371

const secondSegment = findSegment(poly, { segmentIndex: 1 });

372

// Returns: LineString from [0, 10] to [10, 10]

373

374

// Find specific point

375

const thirdPoint = findPoint(poly, { coordIndex: 2 });

376

// Returns: Point at [10, 10]

377

378

// Use negative indices

379

const lastPoint = findPoint(poly, { coordIndex: -1 });

380

// Returns: Point at [0, 0] (last coordinate)

381

```