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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 | 1 1459 1 1822 3 1822 290 1532 52 1480 1 587 1 2138 291 1847 1105 8 742 4 1 308 308 308 288 4 284 1 243 8 235 125 110 20 1 2167 30 2167 21 2146 1629 1491 1452 39 6 39 51 24 138 111 517 8 509 1676 1 1636 24 20 20 12 4 8 4 4 4 1 1636 97 93 4 4 1 253 253 253 222 1 108 108 108 105 1 108 108 108 105 1 105 105 105 93 1 96 96 96 89 1 102 102 102 99 1 102 102 102 96 8 96 38 1 454 454 454 8 454 92 454 454 360 1459 1459 7 7 1452 1452 2530 1718 1452 1 | /** * @alias geojsonhint * @param {(string|object)} GeoJSON given as a string or as an object * @param {Object} options * @param {boolean} [options.noDuplicateMembers=true] forbid repeated * properties. This is only available for string input, becaused parsed * Objects cannot have duplicate properties. * @returns {Array<Object>} an array of errors */ function hint(gj, options) { var errors = []; function root(_) { if ((!options || options.noDuplicateMembers !== false) && _.__duplicateProperties__) { errors.push({ message: 'An object contained duplicate members, making parsing ambigous: ' + _.__duplicateProperties__.join(', '), line: _.__line__ }); } if (!_.type) { errors.push({ message: 'The type property is required and was not found', line: _.__line__ }); } else if (!types[_.type]) { errors.push({ message: 'The type ' + _.type + ' is unknown', line: _.__line__ }); } else { types[_.type](_); } } function everyIs(_, type) { // make a single exception because typeof null === 'object' return _.every(function(x) { return (x !== null) && (typeof x === type); }); } function requiredProperty(_, name, type) { if (typeof _[name] === 'undefined') { return errors.push({ message: '"' + name + '" property required', line: _.__line__ }); } else if (type === 'array') { if (!Array.isArray(_[name])) { return errors.push({ message: '"' + name + '" property should be an array, but is an ' + (typeof _[name]) + ' instead', line: _.__line__ }); } } else if (type && typeof _[name] !== type) { return errors.push({ message: '"' + name + '" property should be ' + (type) + ', but is an ' + (typeof _[name]) + ' instead', line: _.__line__ }); } } // http://geojson.org/geojson-spec.html#feature-collection-objects function FeatureCollection(featureCollection) { crs(featureCollection); bbox(featureCollection); if (!requiredProperty(featureCollection, 'features', 'array')) { if (!everyIs(featureCollection.features, 'object')) { return errors.push({ message: 'Every feature must be an object', line: featureCollection.__line__ }); } featureCollection.features.forEach(Feature); } } // http://geojson.org/geojson-spec.html#positions function position(_, line) { if (!Array.isArray(_)) { return errors.push({ message: 'position should be an array, is a ' + (typeof _) + ' instead', line: _.__line__ || line }); } else { if (_.length < 2) { return errors.push({ message: 'position must have 2 or more elements', line: _.__line__ || line }); } if (!everyIs(_, 'number')) { return errors.push({ message: 'each element in a position must be a number', line: _.__line__ || line }); } } } function positionArray(coords, type, depth, line) { if (line === undefined && coords.__line__ !== undefined) { line = coords.__line__; } if (depth === 0) { return position(coords, line); } else { if (depth === 1 && type) { if (type === 'LinearRing') { if (!Array.isArray(coords[coords.length - 1])) { return errors.push({ message: 'a number was found where a coordinate array should have been found: this needs to be nested more deeply', line: line }); } if (coords.length < 4) { errors.push({ message: 'a LinearRing of coordinates needs to have four or more positions', line: line }); } if (coords.length && (coords[coords.length - 1].length !== coords[0].length || !coords[coords.length - 1].every(function(position, index) { return coords[0][index] === position; }))) { errors.push({ message: 'the first and last positions in a LinearRing of coordinates must be the same', line: line }); } } else if (type === 'Line' && coords.length < 2) { errors.push({ message: 'a line needs to have two or more coordinates to be valid', line: line }); } } else if (!Array.isArray(coords)) { errors.push({ message: 'a number was found where a coordinate array should have been found: this needs to be nested more deeply', line: line }); } else { coords.forEach(function(c) { positionArray(c, type, depth - 1, c.__line__ || line); }); } } } function crs(_) { if (!_.crs) return; if (typeof _.crs === 'object') { var strErr = requiredProperty(_.crs, 'type', 'string'), propErr = requiredProperty(_.crs, 'properties', 'object'); if (!strErr && !propErr) { // http://geojson.org/geojson-spec.html#named-crs if (_.crs.type === 'name') { requiredProperty(_.crs.properties, 'name', 'string'); } else if (_.crs.type === 'link') { requiredProperty(_.crs.properties, 'href', 'string'); } else { errors.push({ message: 'The type of a crs must be either "name" or "link"', line: _.__line__ }); } } } else { errors.push({ message: 'the value of the crs property must be an object, not a ' + (typeof _.crs), line: _.__line__ }); } } function bbox(_) { if (!_.bbox) { return; } if (Array.isArray(_.bbox)) { if (!everyIs(_.bbox, 'number')) { return errors.push({ message: 'each element in a bbox property must be a number', line: _.bbox.__line__ }); } } else { errors.push({ message: 'bbox property must be an array of numbers, but is a ' + (typeof _.bbox), line: _.__line__ }); } } // http://geojson.org/geojson-spec.html#point function Point(point) { crs(point); bbox(point); if (!requiredProperty(point, 'coordinates', 'array')) { position(point.coordinates); } } // http://geojson.org/geojson-spec.html#polygon function Polygon(polygon) { crs(polygon); bbox(polygon); if (!requiredProperty(polygon, 'coordinates', 'array')) { positionArray(polygon.coordinates, 'LinearRing', 2); } } // http://geojson.org/geojson-spec.html#multipolygon function MultiPolygon(multiPolygon) { crs(multiPolygon); bbox(multiPolygon); if (!requiredProperty(multiPolygon, 'coordinates', 'array')) { positionArray(multiPolygon.coordinates, 'LinearRing', 3); } } // http://geojson.org/geojson-spec.html#linestring function LineString(lineString) { crs(lineString); bbox(lineString); if (!requiredProperty(lineString, 'coordinates', 'array')) { positionArray(lineString.coordinates, 'Line', 1); } } // http://geojson.org/geojson-spec.html#multilinestring function MultiLineString(multiLineString) { crs(multiLineString); bbox(multiLineString); if (!requiredProperty(multiLineString, 'coordinates', 'array')) { positionArray(multiLineString.coordinates, 'Line', 2); } } // http://geojson.org/geojson-spec.html#multipoint function MultiPoint(multiPoint) { crs(multiPoint); bbox(multiPoint); if (!requiredProperty(multiPoint, 'coordinates', 'array')) { positionArray(multiPoint.coordinates, '', 1); } } function GeometryCollection(geometryCollection) { crs(geometryCollection); bbox(geometryCollection); if (!requiredProperty(geometryCollection, 'geometries', 'array')) { if (!everyIs(geometryCollection.geometries, 'object')) { errors.push({ message: 'The geometries array in a GeometryCollection must contain only geometry objects', line: geometryCollection.__line__ }); } geometryCollection.geometries.forEach(function(geometry) { if (geometry) root(geometry); }); } } function Feature(feature) { crs(feature); bbox(feature); // https://github.com/geojson/draft-geojson/blob/master/middle.mkd#feature-object if (feature.id !== undefined && typeof feature.id !== 'string' && typeof feature.id !== 'number') { errors.push({ message: 'Feature "id" property must have a string or number value', line: feature.__line__ }); } if (feature.type !== 'Feature') { errors.push({ message: 'GeoJSON features must have a type=feature property', line: feature.__line__ }); } requiredProperty(feature, 'properties', 'object'); if (!requiredProperty(feature, 'geometry', 'object')) { // http://geojson.org/geojson-spec.html#feature-objects // tolerate null geometry if (feature.geometry) root(feature.geometry); } } var types = { Point: Point, Feature: Feature, MultiPoint: MultiPoint, LineString: LineString, MultiLineString: MultiLineString, FeatureCollection: FeatureCollection, GeometryCollection: GeometryCollection, Polygon: Polygon, MultiPolygon: MultiPolygon }; if (typeof gj !== 'object' || gj === null || gj === undefined) { errors.push({ message: 'The root of a GeoJSON object must be an object.', line: 0 }); return errors; } root(gj); errors.forEach(function(err) { if (err.hasOwnProperty('line') && err.line === undefined) { delete err.line; } }); return errors; } module.exports.hint = hint; |