@turf/isolines
Advanced tools
Comparing version 4.4.0 to 4.5.1
@@ -1,11 +0,14 @@ | ||
/// <reference types="geojson" /> | ||
import {Points, MultiLineStrings} from '@turf/helpers' | ||
type Points = GeoJSON.FeatureCollection<GeoJSON.Point>; | ||
type LineStrings = GeoJSON.FeatureCollection<GeoJSON.LineString>; | ||
/** | ||
* http://turfjs.org/docs/#isolines | ||
*/ | ||
declare function isolines(points: Points, z: string, resolution: number, breaks: Array<number>): LineStrings; | ||
declare function isolines( | ||
points: Points, | ||
breaks: number[], | ||
zProperty?:string, | ||
propertiesToAllIsolines?: Object, | ||
propertiesPerIsoline?: Object[]): MultiLineStrings; | ||
declare namespace isolines { } | ||
export = isolines; |
188
index.js
@@ -1,15 +0,11 @@ | ||
//https://github.com/jasondavies/conrec.js | ||
//http://stackoverflow.com/questions/263305/drawing-a-topographical-map | ||
var tin = require('@turf/tin'); | ||
var bbox = require('@turf/bbox'); | ||
var grid = require('@turf/point-grid'); | ||
var inside = require('@turf/inside'); | ||
var square = require('@turf/square'); | ||
var meta = require('@turf/meta'); | ||
var helpers = require('@turf/helpers'); | ||
var distance = require('@turf/distance'); | ||
var planepoint = require('@turf/planepoint'); | ||
var featurecollection = helpers.featureCollection; | ||
var linestring = helpers.lineString; | ||
var point = helpers.point; | ||
var Conrec = require('./conrec'); | ||
var invariant = require('@turf/invariant'); | ||
var gridToMatrix = require('grid-to-matrix'); | ||
var marchingsquares = require('marchingsquares'); | ||
var multiLineString = helpers.multiLineString; | ||
var coordEach = meta.coordEach; | ||
var collectionOf = invariant.collectionOf; | ||
var featureCollection = helpers.featureCollection; | ||
@@ -22,9 +18,10 @@ /** | ||
* @param {FeatureCollection<Point>} points input points | ||
* @param {string} z the property name in `points` from which z-values will be pulled | ||
* @param {number} resolution resolution of the underlying grid | ||
* @param {Array<number>} breaks where to draw contours | ||
* @returns {FeatureCollection<LineString>} isolines | ||
* @param {Array<number>} breaks values of `zProperty` where to draw isolines | ||
* @param {string} [zProperty='elevation'] the property name in `points` from which z-values will be pulled | ||
* @param {Object} [propertiesToAllIsolines={}] GeoJSON properties passed to ALL isolines | ||
* @param {Array<Object>} [propertiesPerIsoline=[]] GeoJSON properties passed, in order, to the correspondent | ||
* isoline; the breaks array will define the order in which the isolines are created | ||
* @returns {FeatureCollection<MultiLineString>} a FeatureCollection of {@link MultiLineString} features representing isolines | ||
* @example | ||
* // create random points with random | ||
* // z-values in their properties | ||
* // create random points with random z-values in their properties | ||
* var points = turf.random('point', 100, { | ||
@@ -37,66 +34,111 @@ * bbox: [0, 30, 20, 50] | ||
* var breaks = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; | ||
* var isolined = turf.isolines(points, 'z', 15, breaks); | ||
* //=isolined | ||
* var isolines = turf.isolines(points, breaks, 'temperature'); | ||
* | ||
* //addToMap | ||
* var addToMap = [isolines]; | ||
*/ | ||
module.exports = function (points, z, resolution, breaks) { | ||
var tinResult = tin(points, z); | ||
var bboxBBox = bbox(points); | ||
var squareBBox = square(bboxBBox); | ||
var sizeCellGrid = distance(point([squareBBox[0], squareBBox[1]]), point([squareBBox[2], squareBBox[1]]), 'kilometers') / resolution; | ||
var gridResult = grid(squareBBox, sizeCellGrid, 'kilometers'); | ||
var data = []; | ||
module.exports = function (points, breaks, zProperty, propertiesToAllIsolines, propertiesPerIsoline) { | ||
// Default Params | ||
zProperty = zProperty || 'elevation'; | ||
propertiesToAllIsolines = propertiesToAllIsolines || {}; | ||
propertiesPerIsoline = propertiesPerIsoline || []; | ||
for (var i = 0; i < gridResult.features.length; i++) { | ||
var pt = gridResult.features[i]; | ||
for (var j = 0; j < tinResult.features.length; j++) { | ||
var triangle = tinResult.features[j]; | ||
if (inside(pt, triangle)) { | ||
pt.properties = {}; | ||
pt.properties[z] = planepoint(pt, triangle); | ||
} | ||
} | ||
} | ||
// Input validation | ||
collectionOf(points, 'Point', 'Input must contain Points'); | ||
if (!breaks) throw new Error('breaks is required'); | ||
if (!Array.isArray(breaks)) throw new Error('breaks must be an Array'); | ||
if (!isObject(propertiesToAllIsolines)) throw new Error('propertiesToAllIsolines must be an Object'); | ||
if (!Array.isArray(propertiesPerIsoline)) throw new Error('propertiesPerIsoline must be an Array'); | ||
if (typeof zProperty !== 'string') throw new Error('zProperty must be a string'); | ||
var depth = Math.sqrt(gridResult.features.length); | ||
for (var x = 0; x < depth; x++) { | ||
var xGroup = gridResult.features.slice(x * depth, (x + 1) * depth); | ||
var xFlat = []; | ||
// Isolined methods | ||
var matrix = gridToMatrix(points, zProperty, true); | ||
var isolines = createIsoLines(matrix, breaks, zProperty, propertiesToAllIsolines, propertiesPerIsoline); | ||
var scaledIsolines = rescaleIsolines(isolines, matrix, points); | ||
for (var g = 0; g < xGroup.length; g++) { | ||
if (xGroup[g].properties) { | ||
xFlat.push(xGroup[g].properties[z]); | ||
} else { | ||
xFlat.push(0); | ||
} | ||
} | ||
data.push(xFlat); | ||
return featureCollection(scaledIsolines); | ||
}; | ||
/** | ||
* Creates the isolines lines (featuresCollection of MultiLineString features) from the 2D data grid | ||
* | ||
* Marchingsquares process the grid data as a 3D representation of a function on a 2D plane, therefore it | ||
* assumes the points (x-y coordinates) are one 'unit' distance. The result of the isolines function needs to be | ||
* rescaled, with turfjs, to the original area and proportions on the map | ||
* | ||
* @private | ||
* @param {Array<Array<number>>} matrix Grid Data | ||
* @param {Array<number>} breaks Breaks | ||
* @param {string} zProperty name of the z-values property | ||
* @param {Object} [propertiesToAllIsolines={}] GeoJSON properties passed to ALL isolines | ||
* @param {Object} [propertiesPerIsoline=[]] GeoJSON properties passed to the correspondent isoline | ||
* @returns {Array<MultiLineString>} isolines | ||
*/ | ||
function createIsoLines(matrix, breaks, zProperty, propertiesToAllIsolines, propertiesPerIsoline) { | ||
var isolines = []; | ||
for (var i = 1; i < breaks.length; i++) { | ||
var threshold = +breaks[i]; // make sure it's a number | ||
var properties = Object.assign( | ||
{}, | ||
propertiesToAllIsolines, | ||
propertiesPerIsoline[i] | ||
); | ||
properties[zProperty] = threshold; | ||
var isoline = multiLineString(marchingsquares.isoContours(matrix, threshold), properties); | ||
isolines.push(isoline); | ||
} | ||
var interval = (squareBBox[2] - squareBBox[0]) / depth; | ||
var xCoordinates = []; | ||
var yCoordinates = []; | ||
for (var d = 0; d < depth; d++) { | ||
xCoordinates.push(d * interval + squareBBox[0]); | ||
yCoordinates.push(d * interval + squareBBox[1]); | ||
} | ||
return isolines; | ||
} | ||
var c = new Conrec(); | ||
c.contour(data, 0, resolution, 0, resolution, xCoordinates, yCoordinates, breaks.length, breaks); | ||
var contourList = c.contourList(); | ||
/** | ||
* Translates and scales isolines | ||
* | ||
* @private | ||
* @param {Array<MultiLineString>} isolines to be rescaled | ||
* @param {Array<Array<number>>} matrix Grid Data | ||
* @param {Object} points Points by Latitude | ||
* @returns {Array<MultiLineString>} isolines | ||
*/ | ||
function rescaleIsolines(isolines, matrix, points) { | ||
var fc = featurecollection([]); | ||
contourList.forEach(function (c) { | ||
if (c.length > 2) { | ||
var polyCoordinates = []; | ||
c.forEach(function (coord) { | ||
polyCoordinates.push([coord.x, coord.y]); | ||
}); | ||
var poly = linestring(polyCoordinates); | ||
poly.properties = {}; | ||
poly.properties[z] = c.level; | ||
// get dimensions (on the map) of the original grid | ||
var gridBbox = bbox(points); // [ minX, minY, maxX, maxY ] | ||
var originalWidth = gridBbox[2] - gridBbox[0]; | ||
var originalHeigth = gridBbox[3] - gridBbox[1]; | ||
fc.features.push(poly); | ||
} | ||
// get origin, which is the first point of the last row on the rectangular data on the map | ||
var x0 = gridBbox[0]; | ||
var y0 = gridBbox[1]; | ||
// get number of cells per side | ||
var matrixWidth = matrix[0].length - 1; | ||
var matrixHeight = matrix.length - 1; | ||
// calculate the scaling factor between matrix and rectangular grid on the map | ||
var scaleX = originalWidth / matrixWidth; | ||
var scaleY = originalHeigth / matrixHeight; | ||
var resize = function (point) { | ||
point[0] = point[0] * scaleX + x0; | ||
point[1] = point[1] * scaleY + y0; | ||
}; | ||
// resize and shift each point/line of the isolines | ||
isolines.forEach(function (isoline) { | ||
coordEach(isoline, resize); | ||
}); | ||
return isolines; | ||
} | ||
return fc; | ||
}; | ||
/** | ||
* Checks input type | ||
* | ||
* @private | ||
* @param {*} input to be checked | ||
* @returns {boolean} true if the input is an Object | ||
*/ | ||
function isObject(input) { | ||
return (!!input) && (input.constructor === Object); | ||
} |
{ | ||
"name": "@turf/isolines", | ||
"version": "4.4.0", | ||
"version": "4.5.1", | ||
"description": "turf isolines module", | ||
@@ -9,4 +9,3 @@ "main": "index.js", | ||
"index.js", | ||
"index.d.ts", | ||
"conrec.js" | ||
"index.d.ts" | ||
], | ||
@@ -30,2 +29,5 @@ "scripts": { | ||
"author": "Turf Authors", | ||
"contributors": [ | ||
"Stefano Borghi <@stebogit>" | ||
], | ||
"license": "MIT", | ||
@@ -37,15 +39,19 @@ "bugs": { | ||
"devDependencies": { | ||
"@turf/envelope": "^4.5.1", | ||
"@turf/point-grid": "^3.14.0", | ||
"@turf/random": "^3.13.0", | ||
"benchmark": "^2.1.4", | ||
"tape": "^4.6.3" | ||
"load-json-file": "^2.0.0", | ||
"matrix-to-grid": "3.0.0", | ||
"tape": "^4.6.3", | ||
"write-json-file": "^2.0.0" | ||
}, | ||
"dependencies": { | ||
"@turf/bbox": "^4.4.0", | ||
"@turf/distance": "^4.4.0", | ||
"@turf/helpers": "^4.4.0", | ||
"@turf/inside": "^4.4.0", | ||
"@turf/planepoint": "^4.4.0", | ||
"@turf/point-grid": "^4.4.0", | ||
"@turf/square": "^4.4.0", | ||
"@turf/tin": "^4.4.0" | ||
"@turf/bbox": "^4.5.1", | ||
"@turf/helpers": "^4.5.1", | ||
"@turf/invariant": "^4.5.1", | ||
"@turf/meta": "^4.5.1", | ||
"grid-to-matrix": "^1.2.0", | ||
"marchingsquares": "^1.2.0" | ||
} | ||
} |
@@ -11,5 +11,7 @@ # @turf/isolines | ||
- `points` **[FeatureCollection](http://geojson.org/geojson-spec.html#feature-collection-objects)<[Point](http://geojson.org/geojson-spec.html#point)>** input points | ||
- `z` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** the property name in `points` from which z-values will be pulled | ||
- `resolution` **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** resolution of the underlying grid | ||
- `breaks` **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)>** where to draw contours | ||
- `breaks` **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)>** values of `zProperty` where to draw isolines | ||
- `zProperty` **\[[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)]** the property name in `points` from which z-values will be pulled (optional, default `'elevation'`) | ||
- `propertiesToAllIsolines` **\[[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)]** GeoJSON properties passed to ALL isolines (optional, default `{}`) | ||
- `propertiesPerIsoline` **\[[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)>]** GeoJSON properties passed, in order, to the correspondent | ||
isoline; the breaks array will define the order in which the isolines are created (optional, default `[]`) | ||
@@ -19,4 +21,3 @@ **Examples** | ||
```javascript | ||
// create random points with random | ||
// z-values in their properties | ||
// create random points with random z-values in their properties | ||
var points = turf.random('point', 100, { | ||
@@ -29,7 +30,9 @@ bbox: [0, 30, 20, 50] | ||
var breaks = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; | ||
var isolined = turf.isolines(points, 'z', 15, breaks); | ||
//=isolined | ||
var isolines = turf.isolines(points, breaks, 'temperature'); | ||
//addToMap | ||
var addToMap = [isolines]; | ||
``` | ||
Returns **[FeatureCollection](http://geojson.org/geojson-spec.html#feature-collection-objects)<[LineString](http://geojson.org/geojson-spec.html#linestring)>** isolines | ||
Returns **[FeatureCollection](http://geojson.org/geojson-spec.html#feature-collection-objects)<[MultiLineString](http://geojson.org/geojson-spec.html#multilinestring)>** a FeatureCollection of [MultiLineString](http://geojson.org/geojson-spec.html#multilinestring) features representing isolines | ||
@@ -36,0 +39,0 @@ <!-- This file is automatically generated. Please don't edit it directly: |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
6
60
11094
8
5
139
1
+ Added@turf/invariant@^4.5.1
+ Added@turf/meta@^4.5.1
+ Addedgrid-to-matrix@^1.2.0
+ Addedmarchingsquares@^1.2.0
+ Added@turf/helpers@5.1.5(transitive)
+ Added@turf/invariant@5.2.0(transitive)
+ Added@turf/meta@5.2.0(transitive)
+ Addedgrid-to-matrix@1.4.0(transitive)
+ Addedmarchingsquares@1.3.3(transitive)
- Removed@turf/distance@^4.4.0
- Removed@turf/inside@^4.4.0
- Removed@turf/planepoint@^4.4.0
- Removed@turf/point-grid@^4.4.0
- Removed@turf/square@^4.4.0
- Removed@turf/tin@^4.4.0
- Removed@turf/distance@4.7.3(transitive)
- Removed@turf/inside@4.7.3(transitive)
- Removed@turf/planepoint@4.7.3(transitive)
- Removed@turf/point-grid@4.7.3(transitive)
- Removed@turf/square@4.7.3(transitive)
- Removed@turf/tin@4.7.3(transitive)
Updated@turf/bbox@^4.5.1
Updated@turf/helpers@^4.5.1