UNPKG

4.55 kBMarkdownView Raw
1[![Build Status](https://secure.travis-ci.org/mapbox/geojsonhint.svg?branch=master)](http://travis-ci.org/mapbox/geojsonhint) [![Coverage Status](https://coveralls.io/repos/mapbox/geojsonhint/badge.svg)](https://coveralls.io/r/mapbox/geojsonhint)
2
3# geojsonhint: complete, fast, standards-based validation for geojson
4
5A [lint](http://bit.ly/12jjJyW) tool for the [GeoJSON](http://www.geojson.org/)
6standard. geojsonhint is written _to the standard_, with no missing or additional
7opinions about structure.
8
9Thanks to `jsonlint-lines`, GeoJSON that is also not valid [JSON](http://json.org/)
10can return informative, line-oriented parsing errors.
11
12## Specification
13
14The basis of this tool is the published [GeoJSON](http://www.geojson.org/) 1.0 specification.
15In the few cases where [draft-geojson](https://github.com/geojson/draft-geojson/blob/master/middle.mkd),
16the ietf-candidate version of GeoJSON, is more precise (for instance, [the id property](https://github.com/mapbox/geojsonhint/issues/24)), the validator follows the draft spec as well.
17
18## API
19
20`errors = geojsonhint.hint(string or object, options)`
21
22Lint a file, given as a string or object. This call detects all aberrations from
23the GeoJSON standards and returns them as an array of errors. An example of the output:
24
25```json
26[{
27 "message": "\"features\" property should be an array, but is an object instead",
28 "line": 1
29}]
30```
31
32The options argument is optional. It has these options:
33
34`noDuplicateMembers`.
35
36By default, geojsonhint will treat repeated properties as an error: you can
37set noDuplicateMembers to false to allow them. For instance:
38
39```js
40geojsonhint.hint('{"type":"invalid","type":"Feature","properties":{},"geometry":null}', {
41 noDuplicateMembers: false
42});
43```
44
45The repeated `type` property in this input will be ignored with the option,
46and flagged without it.
47
48
49`precisionWarning`.
50
51GeoJSON [now recommends six decimal places of accuracy](https://tools.ietf.org/html/rfc7946#section-11.2)
52for coordinates (Section 11.2). This option adds a warning message when coordinates
53contain over 6 decimal places of accuracy, up to 10 coordinates before the warning
54message is truncated for performance.
55
56```js
57geojsonhint.hint('{ "type": "Point", "coordinates": [100.0000000001, 5.0000000001] }', {
58 precisionWarning: false
59});
60```
61
62With this option enabled, geojsonhint will produce these warnings:
63
64```js
65[{
66 line: 1,
67 level: 'message',
68 message: 'precision of coordinates should be reduced'
69}, {
70 line: 1,
71 level: 'message',
72 message: 'precision of coordinates should be reduced'
73}]
74```
75
76Without this option, this input will pass without errors.
77
78## Line Numbers
79
80Note that the GeoJSON can be given as a **string or as an object**. Here's how
81to choose which input to use:
82
83* `string` inputs receive **line numbers for each error**. These make errors
84 easier to track down if the GeoJSON is hand-written.
85* `object` inputs don't have line numbers but are evaluated faster, by up to 10x.
86 GeoJSONHint is _very fast already_ so unless you have identified it as a
87 bottleneck in your application, don't [prematurely optimize](http://c2.com/cgi/wiki?PrematureOptimization) based
88 on this fact.
89
90If you're really trying to save space and don't care about JSON validity errors -
91only GeoJSON errors - you can `require('geojsonhint/lib/object')` to get a version
92of this library that bypasses jsonlint-lines and provides only the object
93interface.
94
95## use it
96
97**npm** (node.js, browserify, webpack, etc)
98
99 npm install --save geojsonhint
100
101**CDN / script tag**
102
103Hit this URL to resolve to the latest pinned version.
104
105 https://unpkg.com/geojsonhint@latest/geojsonhint.js
106
107## As a command-line utility
108
109Install:
110
111 npm install -g geojsonhint
112
113```
114➟ geojsonhint
115Usage: geojsonhint FILE.geojson
116
117Options:
118 --json output json-formatted data for hints
119```
120
121```
122➟ geojsonhint test.geojson
123line 9, each element in a position must be a number
124```
125
126## Development
127
128* Tests: `npm test`
129* Building the browser version: `npm run build`
130
131## See Also
132
133* [geojsonhint online](https://www.mapbox.com/geojsonhint/)
134* [grunt-geojsonhint](https://github.com/jieter/grunt-geojsonhint) does it as a Grunt task
135* [geojsonlint.com](http://geojsonlint.com/) does this server-side
136* [GeoJSON-Validation](https://github.com/craveprogramminginc/GeoJSON-Validation) is another node module for this.
137* [geojson-assert](https://github.com/calvinmetcalf/geojson-assert) does it in assertion tests
138* [geojsonlint](https://github.com/ropenscilabs/geojsonlint) does it in an R package