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

invariant.mddocs/

0

# Input Validation and Data Extraction

1

2

Utilities for validating GeoJSON inputs and extracting coordinates and geometries safely with type checking. These functions ensure data integrity and provide safe access to GeoJSON components.

3

4

## Capabilities

5

6

### Coordinate Extraction

7

8

Safely extract coordinates from various GeoJSON input types.

9

10

```typescript { .api }

11

/**

12

* Extracts coordinate from Point Feature, Geometry or array.

13

* Unwraps a coordinate from a Point Feature, Geometry or a single coordinate.

14

*/

15

function getCoord(coord: Feature<Point> | Point | number[]): number[];

16

17

/**

18

* Extracts coordinates from Feature, Geometry or array.

19

* Unwraps coordinates from a Feature, Geometry Object or an Array.

20

*/

21

function getCoords<G>(coords: any[] | Feature<G> | G): any[];

22

```

23

24

**Usage Examples:**

25

26

```typescript

27

import { getCoord, getCoords, point, polygon } from "@turf/turf";

28

29

// Extract coordinates from various formats

30

const pt = point([102, 0.5]);

31

const coord1 = getCoord(pt); // [102, 0.5]

32

const coord2 = getCoord([102, 0.5]); // [102, 0.5]

33

const coord3 = getCoord(pt.geometry); // [102, 0.5]

34

35

// Extract coordinates from polygon

36

const poly = polygon([[[100, 0], [101, 0], [101, 1], [100, 1], [100, 0]]]);

37

const coords = getCoords(poly); // [[[100, 0], [101, 0], [101, 1], [100, 1], [100, 0]]]

38

39

console.log('Point coordinates:', coord1);

40

console.log('Polygon coordinates:', coords);

41

```

42

43

### Geometry Extraction

44

45

Extract geometries from Features safely.

46

47

```typescript { .api }

48

/**

49

* Gets Geometry from Feature or Geometry Object.

50

* Get Geometry Object from Feature.

51

*/

52

function getGeom<G>(geojson: Feature<G> | G): G;

53

54

/**

55

* Gets GeoJSON object's type.

56

* Get GeoJSON object's type, prioritizing Geometry type over Feature type.

57

*/

58

function getType(geojson: Feature<any> | FeatureCollection<any> | Geometry, name?: string): string;

59

```

60

61

**Usage Examples:**

62

63

```typescript

64

import { getGeom, getType, point, polygon, featureCollection } from "@turf/turf";

65

66

const pt = point([102, 0.5]);

67

const poly = polygon([[[100, 0], [101, 0], [101, 1], [100, 1], [100, 0]]]);

68

const collection = featureCollection([pt, poly]);

69

70

// Extract geometries

71

const pointGeom = getGeom(pt); // Point geometry

72

const polyGeom = getGeom(poly); // Polygon geometry

73

74

// Get types

75

const pointType = getType(pt); // "Point"

76

const polyType = getType(poly); // "Polygon"

77

const collectionType = getType(collection); // "FeatureCollection"

78

79

console.log('Point geometry:', pointGeom);

80

console.log('Types:', pointType, polyType, collectionType);

81

```

82

83

### Data Validation

84

85

Validate GeoJSON data and enforce type expectations.

86

87

```typescript { .api }

88

/**

89

* Checks if coordinates contains numbers.

90

* Checks if coordinates contains a number.

91

*/

92

function containsNumber(coordinates: any[]): boolean;

93

94

/**

95

* Enforces GeoJSON type expectations.

96

* Enforce expectations about types of GeoJSON objects for Turf.

97

*/

98

function geojsonType(value: any, type: string, name: string): void;

99

100

/**

101

* Enforces Feature geometry type expectations.

102

* Enforce expectations about types of Feature inputs for Turf.

103

* Internally this uses geojsonType to judge geometry types

104

*/

105

function featureOf(feature: Feature<any>, type: string, name: string): void;

106

107

/**

108

* Enforces FeatureCollection geometry type expectations.

109

* Enforce expectations about types of FeatureCollection inputs for Turf.

110

* Internally this uses geojsonType to judge geometry types.

111

*/

112

function collectionOf(featureCollection: FeatureCollection<any>, type: string, name: string): void;

113

```

114

115

**Usage Examples:**

116

117

```typescript

118

import {

119

containsNumber,

120

geojsonType,

121

featureOf,

122

collectionOf,

123

point,

124

polygon,

125

featureCollection

126

} from "@turf/turf";

127

128

// Validate coordinate arrays

129

const validCoords = [102, 0.5];

130

const invalidCoords = ["102", "0.5"];

131

132

console.log('Valid coords contain numbers:', containsNumber(validCoords)); // true

133

console.log('Invalid coords contain numbers:', containsNumber(invalidCoords)); // false

134

135

// Type enforcement (these will throw errors if types don't match)

136

const pt = point([102, 0.5]);

137

const poly = polygon([[[100, 0], [101, 0], [101, 1], [100, 1], [100, 0]]]);

138

const collection = featureCollection([pt]);

139

140

try {

141

// These will pass

142

geojsonType(pt, 'Feature', 'myFunction');

143

featureOf(pt, 'Point', 'myFunction');

144

collectionOf(collection, 'Point', 'myFunction');

145

146

// This would throw an error

147

// featureOf(pt, 'Polygon', 'myFunction'); // Error: Invalid input to myFunction: must be a Polygon, given Point

148

} catch (error) {

149

console.error(error.message);

150

}

151

```

152

153

## Error Handling

154

155

The invariant functions will throw descriptive errors when validation fails:

156

157

- `geojsonType()` throws if the GeoJSON object is not of the expected type

158

- `featureOf()` throws if the Feature doesn't contain the expected geometry type

159

- `collectionOf()` throws if the FeatureCollection doesn't contain features of the expected geometry type

160

- `getCoord()` throws if the input cannot be converted to a coordinate array

161

- `getCoords()` throws if the input cannot be converted to coordinate arrays

162

163

**Error Example:**

164

165

```typescript

166

import { featureOf, point, polygon } from "@turf/turf";

167

168

const pt = point([102, 0.5]);

169

170

try {

171

featureOf(pt, 'Polygon', 'myFunction');

172

} catch (error) {

173

console.error(error.message);

174

// "Invalid input to myFunction: must be a Polygon, given Point"

175

}

176

```

177

178

These validation functions are designed to be used internally by other Turf functions to ensure type safety and provide clear error messages when incorrect data types are passed.