Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@mapbox/geojsonhint

mapbox92.6kISC3.3.0

validate and sanity-check geojson files

geojson, hint

readme

Build Status Coverage Status

geojsonhint: complete, fast, standards-based validation for geojson

Important: development of geojsonhint is currently paused. Until development restarts, please refrain from adding non-critical issues or PRs.

A lint tool for the GeoJSON standard. geojsonhint is written to the standard, with no missing or additional opinions about structure.

Thanks to jsonlint-lines, GeoJSON that is also not valid JSON can return informative, line-oriented parsing errors.

Specification

The basis of this tool is the published GeoJSON specification.

API

errors = geojsonhint.hint(string or object, options)

Lint a file, given as a string or object. This call detects all aberrations from the GeoJSON standards and returns them as an array of errors. An example of the output:

[{
  "message": "\"features\" property should be an array, but is an object instead",
  "line": 1
}]

The options argument is optional. It has these options:

noDuplicateMembers.

By default, geojsonhint will treat repeated properties as an error: you can set noDuplicateMembers to false to allow them. For instance:

geojsonhint.hint('{"type":"invalid","type":"Feature","properties":{},"geometry":null}', {
    noDuplicateMembers: false
});

The repeated type property in this input will be ignored with the option, and flagged without it.

precisionWarning.

GeoJSON now recommends six decimal places of accuracy for coordinates (Section 11.2). This option adds a warning message when coordinates contain over 6 decimal places of accuracy, up to 10 coordinates before the warning message is truncated for performance.

geojsonhint.hint('{ "type": "Point", "coordinates": [100.0000000001, 5.0000000001] }', {
    precisionWarning: false
});

With this option enabled, geojsonhint will produce these warnings:

[{
  line: 1,
  level: 'message',
  message: 'precision of coordinates should be reduced'
}, {
  line: 1,
  level: 'message',
  message: 'precision of coordinates should be reduced'
}]

Without this option, this input will pass without errors.

ignoreRightHandRule.

GeoJSON specification defined that linear rings must follow right-hand rule, but also says that for backward compatibility reasons parsers should not rejects polygons wiht incorrect winding order. For that kind of situations geojsonhint has an option ignoreRightHandRule which is false by default. Setting this option to true will cause geojsonhint to skip right-hand rule validation.

geojsonhint.hint(geojsonWithIncorrectWindingOrder, {
  ignoreRightHandRule: true
});
`

with this option enabled, geojsonhint will not validate winding order.

Line Numbers

Note that the GeoJSON can be given as a string or as an object. Here's how to choose which input to use:

  • string inputs receive line numbers for each error. These make errors easier to track down if the GeoJSON is hand-written.
  • object inputs don't have line numbers but are evaluated faster, by up to 10x. GeoJSONHint is very fast already so unless you have identified it as a bottleneck in your application, don't prematurely optimize based on this fact.

If you're really trying to save space and don't care about JSON validity errors - only GeoJSON errors - you can require('geojsonhint/lib/object') to get a version of this library that bypasses jsonlint-lines and provides only the object interface.

use it

npm (node.js, browserify, webpack, etc)

npm install --save @mapbox/geojsonhint

CDN / script tag

Hit this URL to resolve to the latest pinned version.

https://unpkg.com/@mapbox/geojsonhint@latest/geojsonhint.js

As a command-line utility

Install:

npm install -g @mapbox/geojsonhint
➟ geojsonhint
Usage: geojsonhint FILE.geojson

Options:
  --json  output json-formatted data for hints
➟ geojsonhint test.geojson
line 9, each element in a position must be a number

Development

  • Tests: npm test
  • Building the browser version: npm run build

See Also

changelog

3.3.0

  • Update dependencies

3.2.0

  • Remove crs validation

3.1.0

  • Bump minimist version to 1.28, remeove unused underscore dependency

3.1.0

  • Adds options to disable right-hand rule validation #87

3.0.2

  • Bump underscore to 1.12.1

3.0.1

  • Bump minimist to >v1.2.5
  • Fixed the rhr check (forked to @rurban)

3.0.0

  • Bump tested node versions to 8 & 10

2.2.0

  • Use constructor object test to stay compatible with IE11

2.1.0

  • Exit 1 in cli tool when it hits an error
  • Update deps to latest versions

2.0.0

This is the first stable release of geojsonhint that supports the IETF GeoJSON standard

  • Fixes winding order reversal from beta releases.

2.0.0-beta2

  • Fix missing bin directory from beta1 release.

2.0.0-beta1

  • geojsonhint.js has been removed from this repository: it's a built file, useful for people who want to include geojson as a script tag. That file will still be available on unpkg, which is documented in the readme. This change ensures that contributors don't accidentally mess with the geojsonhint.js built file when they should be editing the source files in lib: now the repository contains only source files.
  • vfile and vfile-reporter are upgraded. output is slightly different in formatting but functionally the same.

2.0.0-beta

  • 2.0.0 will be released once the IETF Draft graduates to a specification
  • geojsonhint now validates according to the IETF specification of GeoJSON, which includes useful improvements in clarity.
  • we now use vfile for fancier message formatting in output
  • geojsonhint includes both warnings and errors now, so it can warn about things that are not technically wrong but can be improved, and enforce recommendations of the specification.

1.2.1

  • Fixes a case where coordinate arrays that aren't nested deeply would trigger an uncaught exception
  • Improves test coverage

1.2.0

  • Introduces a new option, noDuplicateMembers, and a stricter default behavior: repeated properties, which are ambiguous in JSON, are now forbidden by default with geojsonhint.

1.1.0

  • Adds purely object-based api, accessible via require('geojsonhint/object'). This is useful for performance-intensive browser libraries.
  • Boosts code coverage testing to 100%

1.0.0

  • Declares the public API
  • This adds compatibility with objects as well as strings, and adds a benchmark to confirm that it's faster.
  • Adds a .npmignore so that npm install geojson is significantly more efficient - excludes 496kb of testing fixtures
  • Adds JSDoc comment to the source

0.3.4

  • Tolerates id properties as numbers as well as strings, to match the actual specification.

0.3.3

  • Enforces the type of the Feature.id property

0.3.2

  • Detects & reports incorrectly nested LinearRing arrays

0.3.0

  • Now uses tap for tests
  • Modernized binary supports streams

0.2.0

  • JSON parse errors are now parsed and output as objects rather than raw errors with strings.
  • Stricter checking of LinearRing and Line coordinate length.