From cbadc7859bbc9b82269ce516d09c0952a2f638e9 Mon Sep 17 00:00:00 2001 From: contra Date: Tue, 16 Apr 2019 13:26:19 -0400 Subject: [PATCH] 3.7.2 - make simplify actually work --- dist/util/geo/simplify.js | 2 +- package.json | 2 +- src/util/geo/simplify.js | 6 ++-- test/geo/simplify.js | 61 ++++++++++++++++++++------------------- 4 files changed, 37 insertions(+), 34 deletions(-) diff --git a/dist/util/geo/simplify.js b/dist/util/geo/simplify.js index 16b34d5..350dd39 100644 --- a/dist/util/geo/simplify.js +++ b/dist/util/geo/simplify.js @@ -24,7 +24,7 @@ exports.default = (geometry, { tolerance = 0.00001 } = {}) => { if (type === 'LineString' && coordinates.length === 2 && (0, _lodash.isEqual)(coordinates[0], coordinates[1])) { throw new Error('Invalid LineString! Only two coordinates that are identical.'); } - const res = (0, _turf.simplify)(geometry, { tolerance }); + const res = (0, _turf.simplify)((0, _turf.cleanCoords)((0, _turf.truncate)(geometry, { precision: 6, coordinates: 3 })), { tolerance }); return Object.assign({ type }, rest, { diff --git a/package.json b/package.json index 853a526..fce8573 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "vandelay-util", - "version": "3.7.1", + "version": "3.7.2", "description": "Grab bag of utilities for vandelay transform functions", "main": "dist/index.js", "keywords": [], diff --git a/src/util/geo/simplify.js b/src/util/geo/simplify.js index 6824987..0bea237 100644 --- a/src/util/geo/simplify.js +++ b/src/util/geo/simplify.js @@ -1,4 +1,4 @@ -import { simplify } from '@turf/turf' +import { simplify, truncate, cleanCoords } from '@turf/turf' import { isEqual } from 'lodash' // 1 = 69 miles @@ -16,7 +16,9 @@ export default (geometry, { tolerance=0.00001 }={}) => { if (type === 'LineString' && coordinates.length === 2 && isEqual(coordinates[0], coordinates[1])) { throw new Error('Invalid LineString! Only two coordinates that are identical.') } - const res = simplify(geometry, { tolerance }) + const res = simplify( + cleanCoords(truncate(geometry, { precision: 6, coordinates: 3 })) + , { tolerance }) return { type, ...rest, diff --git a/test/geo/simplify.js b/test/geo/simplify.js index 874484c..0fb8922 100644 --- a/test/geo/simplify.js +++ b/test/geo/simplify.js @@ -85,38 +85,39 @@ describe('geo#simplify', () => { } const expected = { type: 'LineString', - coordinates: [ [ -79.808996, 36.070662 ], - [ -79.80891, 36.070629999999994 ], - [ -79.809, 36.070499999999996 ], - [ -79.80912000000001, 36.07018 ], - [ -79.80922000000001, 36.07003 ], - [ -79.80940000000001, 36.06989 ], - [ -79.81016000000001, 36.07001 ], - [ -79.81026000000001, 36.06989 ], + coordinates: [ + [ -79.808996, 36.070662 ], + [ -79.80891, 36.07063 ], + [ -79.809, 36.0705 ], + [ -79.80912, 36.07018 ], + [ -79.80922, 36.07003 ], + [ -79.8094, 36.06989 ], + [ -79.81016, 36.07001 ], + [ -79.81026, 36.06989 ], [ -79.8104, 36.06962 ], [ -79.81048, 36.06933 ], - [ -79.81061, 36.069019999999995 ], - [ -79.81062, 36.068889999999996 ], - [ -79.81076, 36.06858999999999 ], - [ -79.81078000000001, 36.06844999999999 ], - [ -79.811, 36.068369999999994 ], - [ -79.81137000000001, 36.068389999999994 ], - [ -79.81151000000001, 36.06842999999999 ], - [ -79.81168000000001, 36.06844999999999 ], - [ -79.81184, 36.06844999999999 ], - [ -79.81201, 36.06841999999999 ], - [ -79.8124, 36.068409999999986 ], - [ -79.81239, 36.06853999999999 ], - [ -79.81258, 36.067519999999995 ], - [ -79.81258, 36.067389999999996 ], - [ -79.81264000000002, 36.06692999999999 ], - [ -79.81267000000003, 36.066419999999994 ], - [ -79.81266000000002, 36.06565999999999 ], - [ -79.81269000000002, 36.065519999999985 ], - [ -79.81307000000002, 36.06543999999999 ], - [ -79.81357000000003, 36.065299999999986 ], - [ -79.81380000000003, 36.06527999999999 ], - [ -79.81423000000002, 36.06533999999999 ] ] + [ -79.81061, 36.06902 ], + [ -79.81062, 36.06889 ], + [ -79.81076, 36.06859 ], + [ -79.81078, 36.06845 ], + [ -79.811, 36.06837 ], + [ -79.81137, 36.06839 ], + [ -79.81151, 36.06843 ], + [ -79.81168, 36.06845 ], + [ -79.81184, 36.06845 ], + [ -79.81201, 36.06842 ], + [ -79.8124, 36.06841 ], + [ -79.81239, 36.06854 ], + [ -79.81258, 36.06752 ], + [ -79.81258, 36.06739 ], + [ -79.81264, 36.06693 ], + [ -79.81267, 36.06642 ], + [ -79.81266, 36.06566 ], + [ -79.81269, 36.06552 ], + [ -79.81307, 36.06544 ], + [ -79.81357, 36.0653 ], + [ -79.8138, 36.06528 ], + [ -79.81423, 36.06534 ] ] } util.geo.simplify(src).should.eql(expected) util.geo.simplify(expected).should.eql(expected)