-
Notifications
You must be signed in to change notification settings - Fork 0
/
multiLineStringGeometry.js
104 lines (93 loc) · 3.74 KB
/
multiLineStringGeometry.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
const { validCoordinate } = require('../coordinates/validCoordinate')
/**
* Verifies an object is a valid GeoJSON MultiLineString Geometry. This geometry requires a
* 'type' property that must equal "MultiLineString", and a 'coordinates' property that contains
* an array of linestring arrays (i.e. each linestring array containing at least two or more valid
* WGS-84 GeoJSON coordinates).
*
* The coordinates may be an empty array, but may not be an array of empty arrays.
*
* Foreign members are allowed with the exceptions thrown below.
*
* @memberof Core.Geometries
* @see https://github.com/M-Scott-Lassiter/jest-geojson/issues/12
* @param {object} geometryObject a GeoJSON MultiLineString Geometry object
* @returns {boolean} True if a valid GeoJSON MultiLineString Geometry. If invalid, it will throw an error.
* @throws {Error} Argument not an object
* @throws {Error} Must have a type property with value 'MultiLineString'
* @throws {Error} forbidden from having a property 'geometry', 'properties', or 'features'
* @example
* const multiLineString = {
"type": "MultiLineString",
"coordinates": [
[
[100.0, 0.0],
[101.0, 1.0]
],
[
[102.0, 2.0],
[103.0, 3.0]
]
]
}
const multiLineStringOneCoordinate = {
"type": "MultiLineString",
"coordinates": [
[
[100.0, 0.0]
]
]
}
const point = {
type: "Point",
coordinates: [100.0, 0.0]
}
console.log(multiLineStringGeometry(multiLineString)) // true
console.log(multiLineStringGeometry(point)) // throws error
console.log(multiLineStringGeometry(multiLineStringOneCoordinate)) // throws error
*/
function multiLineStringGeometry(geometryObject) {
if (
typeof geometryObject !== 'object' ||
Array.isArray(geometryObject) ||
geometryObject === null
) {
throw new Error('Argument must be a GeoJSON MultiLineString Geometry object.')
}
if (!('coordinates' in geometryObject)) {
throw new Error(`GeoJSON MultiLineString Geometry must contain a 'coordinates' property.`)
}
if (geometryObject.type !== 'MultiLineString') {
throw new Error(`Must have a type property with value 'MultiLineString'`)
}
if ('geometry' in geometryObject) {
throw new Error(
`GeoJSON MultiLineString Geometry objects are forbidden from having a property 'geometry'.`
)
}
if ('properties' in geometryObject) {
throw new Error(
`GeoJSON MultiLineString Geometry objects are forbidden from having a property 'properties'.`
)
}
if ('features' in geometryObject) {
throw new Error(
`GeoJSON MultiLineString Geometry objects are forbidden from having a property 'features'.`
)
}
// // // Geometry objects are allowed to have empty arrays as coordinates, however validCoordinate may not.
// // If coordinates is an empty array, we're done. Otherwise, check for coordinate validity.
// if (!Array.isArray(geometryObject.coordinates) && geometryObject.coordinates.length !== 1) {
// throw new Error('Coordinates property must be an array of valid GeoJSON coordinates')
// }
for (let i = 0; i < geometryObject.coordinates.length; i++) {
if (geometryObject.coordinates[i].length === 1) {
throw new Error('Coordinates array must contain two or more valid GeoJSON coordinates')
}
for (let j = 0; j < geometryObject.coordinates[i].length; j++) {
validCoordinate(geometryObject.coordinates[i][j])
}
}
return true
}
exports.multiLineStringGeometry = multiLineStringGeometry