Skip to content

Commit

Permalink
[File Upload] [Maps] Reduce precision of coordinates for geo imports (#…
Browse files Browse the repository at this point in the history
…135133)

* Reduce precision of coordinates for geo imports

* [CI] Auto-commit changed files from 'node scripts/eslint --no-cache --fix'

* Revert to jsts@1.6.2

The jsts library does not transpile modules since 2.0. So it is not currently possible to use the newer library.

* Fix yarn lockfile

* Fix test

Test runs on features, not feature collections. 😬

Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
nickpeihl and kibanamachine authored Jul 8, 2022
1 parent aee38f1 commit 6c08eac
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
import * as jsts from 'jsts';
import rewind from '@mapbox/geojson-rewind';

// The GeoJSON specification suggests limiting coordinate precision to six decimal places
// See https://datatracker.ietf.org/doc/html/rfc7946#section-11.2
// We can enforce rounding to six decimal places by setting the PrecisionModel scale
// scale = 10^n where n = maximum number of decimal places
const precisionModel = new jsts.geom.PrecisionModel(Math.pow(10, 6));
const geometryPrecisionReducer = new jsts.precision.GeometryPrecisionReducer(precisionModel);
geometryPrecisionReducer.setChangePrecisionModel(true);
const geoJSONReader = new jsts.io.GeoJSONReader();
const geoJSONWriter = new jsts.io.GeoJSONWriter();

Expand Down Expand Up @@ -36,6 +43,8 @@ export function cleanGeometry({ geometry }) {
if (!geometry) {
return null;
}
const geometryToWrite = geometry.isSimple() || geometry.isValid() ? geometry : geometry.buffer(0);

// GeometryPrecisionReducer will automatically clean invalid geometries
const geometryToWrite = geometryPrecisionReducer.reduce(geometry);
return geoJSONWriter.write(geometryToWrite);
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,44 @@ describe('geo_json_clean_and_validate', () => {
});
});

it('should reduce coordinate precision', () => {
const ludicrousPrecisionGeoJson = {
type: 'Feature',
properties: {},
geometry: {
type: 'Polygon',
coordinates: [
[
[108.28125, 61.77312286453146],
[72.0703125, 46.31658418182218],
[99.49218749999999, 22.917922936146045],
[133.2421875, 27.059125784374068],
[139.5703125, 52.908902047770255],
[108.28125, 61.77312286453146],
],
],
},
};

expect(geoJsonCleanAndValidate(ludicrousPrecisionGeoJson)).toEqual({
type: 'Feature',
properties: {},
geometry: {
type: 'Polygon',
coordinates: [
[
[108.28125, 61.773123],
[72.070313, 46.316584],
[99.492187, 22.917923],
[133.242188, 27.059126],
[139.570313, 52.908902],
[108.28125, 61.773123],
],
],
},
});
});

it('should reverse counter-clockwise winding order', () => {
const counterClockwiseGeoJson = {
type: 'Feature',
Expand Down

0 comments on commit 6c08eac

Please sign in to comment.