Skip to content

Commit

Permalink
Fixed interpolate with clone
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisCarriere committed Sep 29, 2017
1 parent 36458fb commit 6e97e30
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 20 deletions.
13 changes: 8 additions & 5 deletions packages/turf-interpolate/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import distance from '@turf/distance';
import centroid from '@turf/centroid';
import squareGrid from '@turf/square-grid';
import triangleGrid from '@turf/triangle-grid';
import clone from '@turf/clone';
import { featureCollection } from '@turf/helpers';
import { featureEach } from '@turf/meta';
import { collectionOf } from '@turf/invariant';

Expand Down Expand Up @@ -49,7 +51,7 @@ export default function (points, cellSize, gridType, property, units, weight) {
switch (gridType) {
case 'point':
case 'points':
grid = poinGrid(box, cellSize, units, true);
grid = poinGrid(box, cellSize, {units: units, bboxIsMask: true});
break;
case 'square':
case 'squares':
Expand All @@ -66,7 +68,7 @@ export default function (points, cellSize, gridType, property, units, weight) {
default:
throw new Error('invalid gridType');
}

var results = [];
featureEach(grid, function (gridFeature) {
var zw = 0;
var sw = 0;
Expand All @@ -85,8 +87,9 @@ export default function (points, cellSize, gridType, property, units, weight) {
zw += w * zValue;
});
// write interpolated value for each grid point
gridFeature.properties[property] = zw / sw;
var newFeature = clone(gridFeature);
newFeature.properties[property] = zw / sw;
results.push(newFeature);
});

return grid;
return featureCollection(results);
}
9 changes: 5 additions & 4 deletions packages/turf-interpolate/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,20 @@
},
"homepage": "https://github.com/Turfjs/turf",
"devDependencies": {
"@std/esm": "*",
"@turf/truncate": "*",
"benchmark": "*",
"chromatism": "*",
"load-json-file": "*",
"tape": "*",
"write-json-file": "*",
"rollup": "*",
"@std/esm": "*",
"uglify-js": "*"
"tape": "*",
"uglify-js": "*",
"write-json-file": "*"
},
"dependencies": {
"@turf/bbox": "^4.7.3",
"@turf/centroid": "^4.7.3",
"@turf/clone": "^4.7.3",
"@turf/distance": "^4.7.3",
"@turf/helpers": "5.0.0-alpha",
"@turf/hex-grid": "^4.7.3",
Expand Down
23 changes: 12 additions & 11 deletions packages/turf-interpolate/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,33 @@ const directories = {
out: path.join(__dirname, 'test', 'out') + path.sep
};

const fixtures = fs.readdirSync(directories.in).map(filename => {
var fixtures = fs.readdirSync(directories.in).map(filename => {
return {
filename,
name: path.parse(filename).name,
geojson: load.sync(directories.in + filename)
};
});
// fixtures = fixtures.filter(fixture => fixture.name === 'points-random')

test('turf-interpolate', t => {
for (const {filename, name, geojson} of fixtures) {
let {property, cellSize, gridType, units, weight} = geojson.properties;
property = property || 'elevation';

// Truncate coordinates & elevation (property) to 6 precision
let grid = truncate(interpolate(geojson, cellSize, gridType, property, units, weight));
propEach(grid, props => { props[property] = round(props[property]); });
const result = colorize(grid, property);
let result = truncate(interpolate(geojson, cellSize, gridType, property, units, weight));
propEach(result, (properties, featureIndex) => {
properties[property] = round(properties[property]);
});
result = colorize(result, property, name);

if (process.env.REGEN) write.sync(directories.out + filename, result);
t.deepEquals(result, load.sync(directories.out + filename), name);
}
t.end();
});



test('turf-interpolate -- throws errors', t => {
const cellSize = 1;
const property = 'elevation';
Expand Down Expand Up @@ -75,19 +76,19 @@ test('turf-interpolate -- zValue from 3rd coordinate', t => {
});

// style result
function colorize(grid, property) {
function colorize(grid, property, name) {
property = property || 'elevation';
let max = -Infinity;
let min = Infinity;
featureEach(grid, function (feature) {
const value = feature.properties[property];
propEach(grid, properties => {
const value = properties[property];
if (value > max) max = value;
if (value < min) min = value;
});
const delta = (max - min);
if (delta === 0) throw new Error('data must provide a z elevation difference');
if (delta === 0) throw new Error(name + ' delta is invalid');

featureEach(grid, function (feature) {
featureEach(grid, feature => {
const value = feature.properties[property];
const percent = round((value - min - delta / 2) / delta * 100);
// darker corresponds to higher values
Expand Down

0 comments on commit 6e97e30

Please sign in to comment.