Comparing version 4.4.0 to 4.5.1
@@ -8,6 +8,6 @@ /// <reference types="geojson" /> | ||
/** | ||
* http://turfjs.org/docs/ | ||
* http://turfjs.org/docs/#idw | ||
*/ | ||
declare function idw(controlPoints: Points, valueField: string, b: number, cellWidth: number, units?: Units): Polygons; | ||
declare function idw(controlPoints: Points, valueField: string, weight: number, cellWidth: number, units?: Units): Polygons; | ||
declare namespace idw { } | ||
export = idw; |
59
index.js
@@ -0,5 +1,5 @@ | ||
var bbox = require('@turf/bbox'); | ||
var distance = require('@turf/distance'); | ||
var centroid = require('@turf/centroid'); | ||
var squareGrid = require('@turf/square-grid'); | ||
var centroid = require('@turf/centroid'); | ||
var bbox = require('@turf/bbox'); | ||
@@ -16,9 +16,14 @@ /** | ||
* @param {string} valueField GeoJSON field containing the known value to interpolate on | ||
* @param {number} b Exponent regulating the distance-decay weighting | ||
* @param {number} weight Exponent regulating the distance-decay weighting | ||
* @param {number} cellWidth The distance across each cell | ||
* @param {string} [units=kilometers] used in calculating cellSize, can be degrees, radians, miles, or kilometers | ||
* @returns {FeatureCollection<Polygon>} grid A grid of polygons with a property field "IDW" | ||
* @returns {FeatureCollection<Polygon>} grid A grid of polygons with a property field named as `valueField` | ||
*/ | ||
module.exports = function (controlPoints, valueField, b, cellWidth, units) { | ||
// check if field containing data exists.. | ||
module.exports = function (controlPoints, valueField, weight, cellWidth, units) { | ||
// validation | ||
if (!valueField) throw new Error('valueField is required'); | ||
if (weight === undefined || weight === null) throw new Error('weight is required'); | ||
if (cellWidth === undefined || cellWidth === null) throw new Error('cellWidth is required'); | ||
// check if field containing data exists. | ||
var filtered = controlPoints.features.filter(function (feature) { | ||
@@ -28,27 +33,25 @@ return feature.properties && | ||
}); | ||
if (filtered.length !== 0) { | ||
// create a sample square grid | ||
// compared to a point grid helps visualizing the output (like a raster..) | ||
var samplingGrid = squareGrid(bbox(controlPoints), cellWidth, units); | ||
var N = samplingGrid.features.length; | ||
for (var i = 0; i < N; i++) { | ||
var zw = 0; | ||
var sw = 0; | ||
// calculate the distance from each control point to cell's centroid | ||
for (var j = 0; j < controlPoints.features.length; j++) { | ||
var d = distance(centroid(samplingGrid.features[i]), controlPoints.features[j], units); | ||
if (d === 0) { | ||
zw = controlPoints.features[j].properties[valueField]; | ||
} | ||
var w = 1.0 / Math.pow(d, b); | ||
sw += w; | ||
zw += w * controlPoints.features[j].properties[valueField]; | ||
if (filtered.length === 0) throw new Error('Specified Data Field is Missing'); | ||
// create a sample square grid | ||
// compared to a point grid helps visualizing the output (like a raster..) | ||
var samplingGrid = squareGrid(bbox(controlPoints), cellWidth, units); | ||
var N = samplingGrid.features.length; | ||
for (var i = 0; i < N; i++) { | ||
var zw = 0; | ||
var sw = 0; | ||
// calculate the distance from each control point to cell's centroid | ||
for (var j = 0; j < controlPoints.features.length; j++) { | ||
var d = distance(centroid(samplingGrid.features[i]), controlPoints.features[j], units); | ||
if (d === 0) { | ||
zw = controlPoints.features[j].properties[valueField]; | ||
} | ||
// write IDW value for each grid cell | ||
samplingGrid.features[i].properties.z = zw / sw; | ||
var w = 1.0 / Math.pow(d, weight); | ||
sw += w; | ||
zw += w * controlPoints.features[j].properties[valueField]; | ||
} | ||
return samplingGrid; | ||
} else { | ||
console.log('Specified Data Field is Missing'); | ||
// write IDW value for each grid cell | ||
samplingGrid.features[i].properties[valueField] = zw / sw; | ||
} | ||
return samplingGrid; | ||
}; |
{ | ||
"name": "@turf/idw", | ||
"version": "4.4.0", | ||
"version": "4.5.1", | ||
"description": "turf idw module", | ||
@@ -20,2 +20,6 @@ "main": "index.js", | ||
"author": "Turf Authors", | ||
"contributors": [ | ||
"Stefano Borghi <@stebogit>", | ||
"Denis Carriere <@DenisCarriere>" | ||
], | ||
"license": "MIT", | ||
@@ -27,2 +31,3 @@ "bugs": { | ||
"devDependencies": { | ||
"@turf/helpers": "^4.5.1", | ||
"benchmark": "^2.1.4", | ||
@@ -34,7 +39,7 @@ "load-json-file": "^2.0.0", | ||
"dependencies": { | ||
"@turf/bbox": "^4.4.0", | ||
"@turf/centroid": "^4.4.0", | ||
"@turf/distance": "^4.4.0", | ||
"@turf/square-grid": "^4.4.0" | ||
"@turf/bbox": "^4.5.1", | ||
"@turf/centroid": "^4.5.1", | ||
"@turf/distance": "^4.5.1", | ||
"@turf/square-grid": "^4.5.1" | ||
} | ||
} |
@@ -14,7 +14,7 @@ # @turf/idw | ||
- `valueField` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** GeoJSON field containing the known value to interpolate on | ||
- `b` **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** Exponent regulating the distance-decay weighting | ||
- `weight` **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** Exponent regulating the distance-decay weighting | ||
- `cellWidth` **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** The distance across each cell | ||
- `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`) | ||
Returns **[FeatureCollection](http://geojson.org/geojson-spec.html#feature-collection-objects)<[Polygon](http://geojson.org/geojson-spec.html#polygon)>** grid A grid of polygons with a property field "IDW" | ||
Returns **[FeatureCollection](http://geojson.org/geojson-spec.html#feature-collection-objects)<[Polygon](http://geojson.org/geojson-spec.html#polygon)>** grid A grid of polygons with a property field named as `valueField` | ||
@@ -21,0 +21,0 @@ <!-- This file is automatically generated. Please don't edit it directly: |
7363
62
5
Updated@turf/bbox@^4.5.1
Updated@turf/centroid@^4.5.1
Updated@turf/distance@^4.5.1
Updated@turf/square-grid@^4.5.1