-
Notifications
You must be signed in to change notification settings - Fork 944
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New module @turf/interpolate
(proposal)
#832
Changes from 6 commits
9bbfdad
971d1df
3acd8cb
a11aebc
5755018
d76b61f
2db7a8f
bb89170
2659aea
2cd7100
0195c9f
35ab0d3
749a575
86fbc4c
1abe3dc
66de2d0
5556ee3
9b8031b
8883eb2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2017 TurfJS | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
this software and associated documentation files (the "Software"), to deal in | ||
the Software without restriction, including without limitation the rights to | ||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
the Software, and to permit persons to whom the Software is furnished to do so, | ||
subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# @turf/interpolate | ||
|
||
# interpolate | ||
|
||
Takes a set of points and estimates their 'property' values on a grid, using the [Inverse Distance Weighting (IDW)](https://en.wikipedia.org/wiki/Inverse_distance_weighting) method. | ||
|
||
**Parameters** | ||
|
||
- `points` **[FeatureCollection](http://geojson.org/geojson-spec.html#feature-collection-objects)<[Point](http://geojson.org/geojson-spec.html#point)>** input points with known value | ||
- `cellSize` **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** The distance across each grid point | ||
- `property` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** the field name containing the known value to interpolate on (optional, default `elevation`) | ||
- `units` **\[[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)]** used in calculating cellSize, can be degrees, radians, miles, or kilometers (optional, default `kilometers`) | ||
- `weight` **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** Exponent regulating the distance-decay weighting | ||
|
||
Returns a **[FeatureCollection](http://geojson.org/geojson-spec.html#feature-collection-objects)<[Point](http://geojson.org/geojson-spec.html#point)>** A grid of points with a `property` field with the interpolated value | ||
|
||
**Examples** | ||
|
||
```javascript | ||
var points = turf.random('points', 30, { | ||
bbox: [50, 30, 70, 50] | ||
}); | ||
// add a random property to each point | ||
turf.featureEach(points, function(point) { | ||
point.properties.solRad = Math.random() * 50; | ||
}); | ||
var grid = interpolate(points, 100, 'solRad', 'miles'); | ||
//addToMap | ||
var addToMap = grid | ||
``` | ||
|
||
<!-- This file is automatically generated. Please don't edit it directly: | ||
if you find an error, edit the source file (likely index.js), and re-run | ||
./scripts/generate-readmes in the turf project. --> | ||
|
||
--- | ||
|
||
This module is part of the [Turfjs project](http://turfjs.org/), an open source | ||
module collection dedicated to geographic algorithms. It is maintained in the | ||
[Turfjs/turf](https://github.com/Turfjs/turf) repository, where you can create | ||
PRs and issues. | ||
|
||
### Installation | ||
|
||
Install this module individually: | ||
|
||
```sh | ||
$ npm install @turf/interpolate | ||
``` | ||
|
||
Or install the Turf module that includes it as a function: | ||
|
||
```sh | ||
$ npm install @turf/turf | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
const Benchmark = require('benchmark'); | ||
const path = require('path'); | ||
const fs = require('fs'); | ||
const load = require('load-json-file'); | ||
const interpolate = require('./'); | ||
|
||
// Define Fixtures | ||
const directory = path.join(__dirname, 'test', 'in') + path.sep; | ||
const fixtures = fs.readdirSync(directory).map(filename => { | ||
return { | ||
name: path.parse(filename).name, | ||
geojson: load.sync(directory + filename) | ||
}; | ||
}); | ||
|
||
/** | ||
* Benchmark Results | ||
* | ||
* points-random: 18.246ms | ||
* points1: 12.013ms | ||
* points-random x 607 ops/sec ±2.56% (76 runs sampled) | ||
* points1 x 533 ops/sec ±2.13% (78 runs sampled) | ||
*/ | ||
const suite = new Benchmark.Suite('turf-interpolate'); | ||
for (const {name, geojson} of fixtures) { | ||
const {property, cellSize, units, weight} = geojson.properties; | ||
|
||
console.time(name); | ||
interpolate(geojson, cellSize, property, units, weight); | ||
console.timeEnd(name); | ||
suite.add(name, () => interpolate(geojson, cellSize, property, units, weight)); | ||
} | ||
|
||
suite | ||
.on('cycle', e => console.log(String(e.target))) | ||
.on('complete', () => {}) | ||
.run(); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/// <reference types="geojson" /> | ||
|
||
import {Points} from '@turf/helpers'; | ||
|
||
/** | ||
* http://turfjs.org/docs/#interpolate | ||
*/ | ||
declare function interpolate(points: Points, cellSize: number, property?: number, units?: string, weight?:number): Points; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Contradicts my previous comment, but import {Units} from '@turf/helpers' There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah! nice to know, I didn't noticed the types in |
||
declare namespace interpolate { } | ||
export = interpolate; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
var meta = require('@turf/meta'); | ||
var bbox = require('@turf/bbox'); | ||
var poinGrid = require('@turf/point-grid'); | ||
var distance = require('@turf/distance'); | ||
var invariant = require('@turf/invariant'); | ||
var featureEach = meta.featureEach; | ||
var collectionOf = invariant.collectionOf; | ||
|
||
/** | ||
* Takes a set of points and estimates their 'property' values on a grid using the Inverse Distance Weighting (IDW) method.](https://en.wikipedia.org/wiki/Inverse_distance_weighting. | ||
* | ||
* @name interpolate | ||
* @param {FeatureCollection<Point>} points with known value | ||
* @param {number} cellSize the distance across each grid point | ||
* @param {string} [property='elevation'] the property name in `points` from which z-values will be pulled | ||
* @param {string} [units=kilometers] used in calculating cellSize, can be degrees, radians, miles, or kilometers | ||
* @param {number} [weight=1] exponent regulating the distance-decay weighting | ||
* @returns {FeatureCollection<Point>} grid of points with 'property' | ||
* @example | ||
* var points = turf.random('points', 30, { | ||
* bbox: [50, 30, 70, 50] | ||
* }); | ||
* // add a random property to each point | ||
* turf.featureEach(points, function(point) { | ||
* point.properties.solRad = Math.random() * 50; | ||
* }); | ||
* var grid = interpolate(points, 100, 'solRad', 'miles'); | ||
* | ||
* //addToMap | ||
* var addToMap = grid | ||
*/ | ||
module.exports = function (points, cellSize, property, units, weight) { | ||
// validation | ||
if (!points) throw new Error('points is required'); | ||
collectionOf(points, 'Point', 'input must contain Points'); | ||
if (!cellSize) throw new Error('cellSize is required'); | ||
if (cellSize === undefined || cellSize === null) throw new Error('cellWidth is required'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm going to assume this is a duplicate of
if (!cellSize) throw new Error('cellSize is required');
if (cellSize === undefined || cellSize === null) throw new Error('cellWidth is required'); |
||
|
||
// default values | ||
property = property || 'elevation'; | ||
weight = weight || 1; | ||
|
||
// create the point grid | ||
var grid = poinGrid(bbox(points), cellSize, units, true); | ||
|
||
featureEach(grid, function (gridPoint) { | ||
var zw = 0; | ||
var sw = 0; | ||
// calculate the distance from each input point to the grid points | ||
featureEach(points, function (point) { | ||
var d = distance(gridPoint, point, units); | ||
var zValue = point.properties[property]; | ||
if (zValue === undefined) throw new Error('Specified property is missing'); | ||
if (d === 0) zw = zValue; | ||
var w = 1.0 / Math.pow(d, weight); | ||
sw += w; | ||
zw += w * zValue; | ||
}); | ||
// write interpolated value for each grid point | ||
gridPoint.properties[property] = zw / sw; | ||
}); | ||
|
||
return grid; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
{ | ||
"name": "@turf/interpolate", | ||
"version": "4.5.0", | ||
"description": "turf interpolate module", | ||
"main": "index.js", | ||
"types": "index.d.ts", | ||
"files": [ | ||
"index.js", | ||
"index.d.ts" | ||
], | ||
"scripts": { | ||
"test": "node test.js", | ||
"bench": "node bench.js" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/Turfjs/turf.git" | ||
}, | ||
"keywords": [ | ||
"turf", | ||
"idw", | ||
"interpolate" | ||
], | ||
"author": "Turf Authors", | ||
"contributors": [ | ||
"Stefano Borghi <@stebogit>" | ||
], | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/Turfjs/turf/issues" | ||
}, | ||
"homepage": "https://github.com/Turfjs/turf", | ||
"devDependencies": { | ||
"@turf/helpers": "^4.5.2", | ||
"benchmark": "^2.1.4", | ||
"load-json-file": "^2.0.0", | ||
"chromatism": "2.6.0", | ||
"tape": "^4.6.3", | ||
"write-json-file": "^2.0.0" | ||
}, | ||
"dependencies": { | ||
"@turf/point-grid": "^4.5.2", | ||
"@turf/distance": "^4.5.2", | ||
"@turf/invariant": "^4.5.2", | ||
"@turf/bbox": "^4.5.2", | ||
"@turf/meta": "^4.5.2" | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
const test = require('tape'); | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const load = require('load-json-file'); | ||
const write = require('write-json-file'); | ||
const round = require('@turf/helpers').round; | ||
const featureEach = require('@turf/meta').featureEach; | ||
const chromatism = require('chromatism'); | ||
const interpolate = require('./'); | ||
|
||
const directories = { | ||
in: path.join(__dirname, 'test', 'in') + path.sep, | ||
out: path.join(__dirname, 'test', 'out') + path.sep | ||
}; | ||
|
||
const fixtures = fs.readdirSync(directories.in).map(filename => { | ||
return { | ||
filename, | ||
name: path.parse(filename).name, | ||
geojson: load.sync(directories.in + filename) | ||
}; | ||
}); | ||
|
||
test('turf-interpolate', t => { | ||
for (const {filename, name, geojson} of fixtures) { | ||
const {property, cellSize, units, weight} = geojson.properties; | ||
|
||
const grid = interpolate(geojson, cellSize, property, units, weight); | ||
const result = colorize(grid, property); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added Node 7 & 8 were passing, but Node 6 was failing (0.0000001 of a difference). |
||
|
||
if (process.env.REGEN) write.sync(directories.out + filename, result); | ||
t.deepEquals(grid, load.sync(directories.out + filename), name); | ||
} | ||
t.end(); | ||
}); | ||
|
||
|
||
// style result | ||
function colorize(grid, property) { | ||
property = property || 'elevation' | ||
let max = -Infinity; | ||
let min = Infinity; | ||
featureEach(grid, function (point) { | ||
const value = point.properties[property]; | ||
if (value > max) max = value; | ||
if (value < min) min = value; | ||
}); | ||
const delta = (max - min) * 1.1; // extend range to enhance color shade | ||
|
||
featureEach(grid, function (point) { | ||
const value = point.properties[property]; | ||
const percent = round((value - min) / delta * 100); | ||
const color = chromatism.brightness(percent, '#002c56').hex; | ||
point.properties['marker-color'] = color; | ||
}); | ||
|
||
return grid; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since
@turf/helpers
is not required in the mainpackage.json
, it might be better to include the Full GeoJSON typescript definition instead of importing it (in case the dependencies wouldn't include@turf/helpers
).