geojson-precision
Advanced tools
Comparing version
10
index.js
(function() { | ||
function parse(t, precision) { | ||
function parse(t, coordinatePrecision, extrasPrecision) { | ||
function point(p) { | ||
return p.map(function(e) { | ||
return 1 * e.toFixed(precision); | ||
return p.map(function(e, index) { | ||
if (index < 2) { | ||
return 1 * e.toFixed(coordinatePrecision); | ||
} else { | ||
return 1 * e.toFixed(extrasPrecision); | ||
} | ||
}); | ||
@@ -9,0 +13,0 @@ } |
{ | ||
"name": "geojson-precision", | ||
"version": "0.4.0", | ||
"version": "1.0.0", | ||
"description": "Remove meaningless precision from GeoJSON", | ||
"main": "index.js", | ||
"dependencies": { | ||
"commander": "^2.4.0" | ||
"commander": "2.19.0" | ||
}, | ||
"devDependencies": { | ||
"assert": "^1.1.2", | ||
"geojsonhint": "^0.3.4", | ||
"should": "^4.0.4" | ||
"assert": "1.4.1", | ||
"@mapbox/geojsonhint": "2.1.0", | ||
"mocha": "5.2.0", | ||
"should": "13.2.3" | ||
}, | ||
@@ -17,2 +18,5 @@ "scripts": { | ||
}, | ||
"engines": { | ||
"node": ">=6.4.0" | ||
}, | ||
"bin": { | ||
@@ -31,3 +35,3 @@ "geojson-precision": "./bin/geojson-precision" | ||
"author": "John J Czaplewski <jczaplew@gmail.com>", | ||
"license": "CC0", | ||
"license": "MIT", | ||
"bugs": { | ||
@@ -34,0 +38,0 @@ "url": "https://github.com/jczaplew/geojson-precision/issues" |
@@ -14,15 +14,15 @@ # geojson-precision | ||
###.parse(*geojson*, *precision*) | ||
### .parse(*geojson*, *precision*) | ||
````geojson```` is a valid GeoJSON object, and can be of type ````Point````, ````LineString````, ````Polygon````, ````MultiPoint````, ````MultiPolygon````, ````MultiLineString````, ````GeometryCollection````, ````Feature````, or ````FeatureCollection````. If you are unsure whether or not your GeoJSON object is valid, you can run it through a linter such as [geojsonhint](https://github.com/mapbox/geojsonhint). | ||
````precision```` is a positive integer. If your specified ````precision```` value is greater than the precision of the input geometry, the output precision will be the same as the input. For example, if your input coordinates are ````[10.0, 20.0]````, and you specify a ````precision```` of ````5````, the output will be the same as the input. | ||
````precision```` is a positive integer. If your specified ````precision```` value is greater than the precision of the input geometry, the output precision will be the same as the input. For example, if your input coordinates are ````[10.0, 20.0]````, and you specify a ````precision```` of ````5````, the output will be the same as the input. | ||
##### Example use: | ||
```` | ||
var gp = require("geojson-precision"); | ||
````javascript | ||
const gp = require("geojson-precision"); | ||
var trimmed = gp.parse({ | ||
let trimmed = gp.parse({ | ||
"type": "Point", | ||
@@ -39,3 +39,3 @@ "coordinates": [ | ||
```` | ||
````javascript | ||
{ | ||
@@ -51,8 +51,8 @@ "type": "Point", | ||
````.parse()```` can also be used directly, for example: | ||
```` | ||
var gp = require("geojson-precision"); | ||
var trimmed = gp({ ... }, 3); | ||
````javascript | ||
const gp = require("geojson-precision"); | ||
let trimmed = gp({ ... }, 3); | ||
```` | ||
@@ -62,8 +62,11 @@ | ||
## CLI | ||
Geojson-precision can also be used via the command line. Especially easy to use if installed globally (using ````-g````). | ||
Geojson-precision can also be used via the command line when installed globally (using ````-g````). | ||
###Parameters | ||
### Parameters | ||
###### precision (-p) | ||
A positive integer specifying coordinate precision | ||
###### extras precision (-e) | ||
A positive integer specifying extra coordinate precision for things like the z value when the coordinate is [longitude, latitude, elevation]. | ||
###### input | ||
@@ -86,3 +89,3 @@ An input GeoJSON file | ||
- [wellknown](https://github.com/mapbox/wellknown/pull/18) | ||
- [wellknown](https://github.com/mapbox/wellknown/pull/18) | ||
- [LilJSON](https://github.com/migurski/LilJSON) | ||
@@ -92,2 +95,2 @@ | ||
## License | ||
CC0 | ||
MIT |
@@ -21,2 +21,11 @@ exports.featurePoint = { | ||
exports.pointz = { | ||
"type": "Point", | ||
"coordinates": [ | ||
18.984375, | ||
57.32652122521709, | ||
123.4567890 | ||
] | ||
}; | ||
exports.featureLinestring = { | ||
@@ -95,4 +104,4 @@ "type": "Feature", | ||
[ | ||
-10.546875, | ||
44.59046718130883 | ||
2.4609375, | ||
35.17380831799959 | ||
], | ||
@@ -104,4 +113,4 @@ [ | ||
[ | ||
2.4609375, | ||
35.17380831799959 | ||
-10.546875, | ||
44.59046718130883 | ||
], | ||
@@ -116,15 +125,10 @@ [ | ||
exports.holyPolygon = { "type": "Polygon", | ||
"coordinates": [ | ||
[ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ], | ||
[ [100.2, 0.2], [100.8, 0.2], [100.8, 0.8], [100.2, 0.8], [100.2, 0.2] ] | ||
] | ||
}; | ||
exports.holyPolygon = {"type":"Polygon","coordinates":[[[100,0],[101,0],[101,1],[100,1],[100,0]],[[100.2,0.2],[100.2,0.8],[100.8,0.8],[100.8,0.2],[100.2,0.2]]]} | ||
exports.multipoint = { "type": "MultiPoint", | ||
"coordinates": [ | ||
"coordinates": [ | ||
[ | ||
-89.6484375, | ||
43.32517767999296 | ||
], | ||
], | ||
[ | ||
@@ -137,13 +141,7 @@ -49.5703125, | ||
56.559482483762245 | ||
] | ||
] | ||
] | ||
}; | ||
exports.multipoly = { "type": "MultiPolygon", | ||
"coordinates": [ | ||
[[[102.0, 2.0], [103.0, 2.0], [103.0, 3.0], [102.0, 3.0], [102.0, 2.0]]], | ||
[[[100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0]], | ||
[[100.2, 0.2], [100.8, 0.2], [100.8, 0.8], [100.2, 0.8], [100.2, 0.2]]] | ||
] | ||
}; | ||
exports.multipoly = {"type":"MultiPolygon","coordinates":[[[[102,2],[103,2],[103,3],[102,3],[102,2]]],[[[100,0],[101,0],[101,1],[100,1],[100,0]],[[100.2,0.2],[100.2,0.8],[100.8,0.8],[100.8,0.2],[100.2,0.2]]]]}; | ||
@@ -211,4 +209,4 @@ exports.multilinestring = { "type": "MultiLineString", | ||
[ | ||
-31.289062500000004, | ||
34.30714385628804 | ||
-7.03125, | ||
10.833305983642491 | ||
], | ||
@@ -220,4 +218,4 @@ [ | ||
[ | ||
-7.03125, | ||
10.833305983642491 | ||
-31.289062500000004, | ||
34.30714385628804 | ||
], | ||
@@ -316,2 +314,1 @@ [ | ||
exports.baddy_object = {"aKey": "aValue"}; | ||
112
test/test.js
@@ -1,10 +0,10 @@ | ||
var assert = require("assert"), | ||
should = require("should"), | ||
geojsonhint = require("geojsonhint"), | ||
gp = require("../index.js"), | ||
tg = require("./test_geometry.js"); | ||
const assert = require("assert") | ||
const should = require("should") | ||
const geojsonhint = require("@mapbox/geojsonhint") | ||
const gp = require("../index.js") | ||
const tg = require("./test_geometry.js") | ||
function test(feature, precision, cb) { | ||
var parsed = gp.parse(feature, precision), | ||
errors = geojsonhint.hint(JSON.stringify(parsed)); | ||
let parsed = gp.parse(feature, precision) | ||
let errors = geojsonhint.hint(JSON.stringify(parsed)); | ||
if (errors.length) { | ||
@@ -17,5 +17,5 @@ cb(errors); | ||
describe("point", function() { | ||
it("should return valid GeoJSON with the specified precision", function(done) { | ||
test(tg.point, 3, function(errors) { | ||
describe("point", () => { | ||
it("should return valid GeoJSON with the specified precision", (done) => { | ||
test(tg.point, 3, (errors) => { | ||
if (errors) { | ||
@@ -30,5 +30,17 @@ throw new Error(JSON.stringify(errors)); | ||
describe("feature point", function() { | ||
it("should return valid GeoJSON with the specified precision", function(done) { | ||
test(tg.featurePoint, 3, function(errors) { | ||
describe("pointz", () => { | ||
it("should return valid GeoJSON with the specified Z precision", (done) => { | ||
let zPrecision = 2; | ||
let parsed = gp(tg.pointz, 3, zPrecision); | ||
if (parsed.coordinates[2].toString() !== tg.pointz.coordinates[2].toFixed(zPrecision)) { | ||
throw new Error("z coordinate precisions don't match"); | ||
} else { | ||
done(); | ||
} | ||
}); | ||
}); | ||
describe("feature point", () => { | ||
it("should return valid GeoJSON with the specified precision", (done) => { | ||
test(tg.featurePoint, 3, (errors) => { | ||
if (errors) { | ||
@@ -43,5 +55,5 @@ throw new Error(JSON.stringify(errors)); | ||
describe("feature linestring", function() { | ||
it("should return valid GeoJSON with the specified precision", function(done) { | ||
test(tg.featureLinestring, 3, function(errors) { | ||
describe("feature linestring", () => { | ||
it("should return valid GeoJSON with the specified precision", (done) => { | ||
test(tg.featureLinestring, 3, (errors) => { | ||
if (errors) { | ||
@@ -56,5 +68,5 @@ throw new Error(JSON.stringify(errors)); | ||
describe("linestring", function() { | ||
it("should return valid GeoJSON with the specified precision", function(done) { | ||
test(tg.linestring, 3, function(errors) { | ||
describe("linestring", () => { | ||
it("should return valid GeoJSON with the specified precision", (done) => { | ||
test(tg.linestring, 3, (errors) => { | ||
if (errors) { | ||
@@ -69,5 +81,5 @@ throw new Error(JSON.stringify(errors)); | ||
describe("multipoint", function() { | ||
it("should return valid GeoJSON with the specified precision", function(done) { | ||
test(tg.multipoint, 3, function(errors) { | ||
describe("multipoint", () => { | ||
it("should return valid GeoJSON with the specified precision", (done) => { | ||
test(tg.multipoint, 3, (errors) => { | ||
if (errors) { | ||
@@ -82,5 +94,5 @@ throw new Error(JSON.stringify(errors)); | ||
describe("polygon", function() { | ||
it("should return valid GeoJSON with the specified precision", function(done) { | ||
test(tg.polygon, 3, function(errors) { | ||
describe("polygon", () => { | ||
it("should return valid GeoJSON with the specified precision", (done) => { | ||
test(tg.polygon, 3, (errors) => { | ||
if (errors) { | ||
@@ -95,5 +107,5 @@ throw new Error(JSON.stringify(errors)); | ||
describe("holy polygon", function() { | ||
it("should return valid GeoJSON with the specified precision", function(done) { | ||
test(tg.holyPolygon, 3, function(errors) { | ||
describe("holy polygon", () => { | ||
it("should return valid GeoJSON with the specified precision", (done) => { | ||
test(tg.holyPolygon, 3, (errors) => { | ||
if (errors) { | ||
@@ -108,5 +120,5 @@ throw new Error(JSON.stringify(errors)); | ||
describe("multipolygon", function() { | ||
it("should return valid GeoJSON with the specified precision", function(done) { | ||
test(tg.multipoly, 3, function(errors) { | ||
describe("multipolygon", () => { | ||
it("should return valid GeoJSON with the specified precision", (done) => { | ||
test(tg.multipoly, 3, (errors) => { | ||
if (errors) { | ||
@@ -121,5 +133,5 @@ throw new Error(JSON.stringify(errors)); | ||
describe("multi linestring", function() { | ||
it("should return valid GeoJSON with the specified precision", function(done) { | ||
test(tg.multilinestring, 3, function(errors) { | ||
describe("multi linestring", () => { | ||
it("should return valid GeoJSON with the specified precision", (done) => { | ||
test(tg.multilinestring, 3, (errors) => { | ||
if (errors) { | ||
@@ -134,5 +146,5 @@ throw new Error(JSON.stringify(errors)); | ||
describe("feature collection", function() { | ||
it("should return valid GeoJSON with the specified precision", function(done) { | ||
test(tg.featureCollection, 3, function(errors) { | ||
describe("feature collection", () => { | ||
it("should return valid GeoJSON with the specified precision", (done) => { | ||
test(tg.featureCollection, 3, (errors) => { | ||
if (errors) { | ||
@@ -147,5 +159,5 @@ throw new Error(JSON.stringify(errors)); | ||
describe("geometry collection", function() { | ||
it("should return valid GeoJSON with the specified precision", function(done) { | ||
test(tg.geometryCollection, 3, function(errors) { | ||
describe("geometry collection", () => { | ||
it("should return valid GeoJSON with the specified precision", (done) => { | ||
test(tg.geometryCollection, 3, (errors) => { | ||
if (errors) { | ||
@@ -160,4 +172,4 @@ throw new Error(JSON.stringify(errors)); | ||
describe("null value", function() { | ||
it("should return the same null value", function(done) { | ||
describe("null value", () => { | ||
it("should return the same null value", (done) => { | ||
var parsed = gp(tg.baddy_null, 4); | ||
@@ -173,4 +185,4 @@ | ||
describe("undefined value", function() { | ||
it("should return the same thing value", function(done) { | ||
describe("undefined value", () => { | ||
it("should return the same thing value", (done) => { | ||
var parsed = gp(tg.baddy_undefined, 5); | ||
@@ -186,4 +198,4 @@ | ||
describe("empty array", function() { | ||
it("should return the same thing value", function(done) { | ||
describe("empty array", () => { | ||
it("should return the same thing value", (done) => { | ||
var parsed = gp(tg.empty, 5); | ||
@@ -199,4 +211,4 @@ | ||
describe("bad object", function() { | ||
it("should return the same thing value", function(done) { | ||
describe("bad object", () => { | ||
it("should return the same thing value", (done) => { | ||
var parsed = gp(tg.baddy_object, 5); | ||
@@ -212,4 +224,4 @@ | ||
describe("null Feature geometry", function() { | ||
it("should return the same thing value", function(done) { | ||
describe("null Feature geometry", () => { | ||
it("should return the same thing value", (done) => { | ||
var parsed = gp(tg.baddy_nogeom, 5); | ||
@@ -216,0 +228,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Misc. License Issues
License(Experimental) A package's licensing information has fine-grained problems.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
Unidentified License
License(Experimental) Something that seems like a license was found, but its contents could not be matched with a known license.
Found 1 instance in 1 package
0
-100%0
-100%822
1.48%0
-100%91
3.41%25208
-99.62%4
33.33%8
-11.11%+ Added
- Removed
Updated