Skip to content

Commit

Permalink
New @turf/isolines based on MarchingSquares.js (#781)
Browse files Browse the repository at this point in the history
* replaced conrec with marchingsquare

* fixed script and tests

* updated bench

* updated readme

* updated package.json

* updated index.d.ts

* introduced suggested changes

* added zProperty to index.d.ts

* minor doc change

* Updates to Isolines
- Divide properties={} to two params
- Simplify Catch error logic (index.js)
- Update Typescript definition
- Add types.ts to test Typescript definition
- Add single task to benchmark
- Update Yarn.lock
- Simplify bench & test logic
- Save fixtures out to GeoJSON
CC: @stebogit

* perIsoline overlaps properties from toAllIsolines

* Update Readme
  • Loading branch information
stebogit authored and DenisCarriere committed Jun 11, 2017
1 parent 537cd4b commit 321d0df
Show file tree
Hide file tree
Showing 17 changed files with 5,721 additions and 637 deletions.
19 changes: 11 additions & 8 deletions packages/turf-isolines/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,30 @@ value breaks and generates [isolines](http://en.wikipedia.org/wiki/Isoline).
**Parameters**

- `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 `[]`)

**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, {
bbox: [0, 30, 20, 50]
});
for (var i = 0; i < points.features.length; i++) {
points.features[i].properties.z = Math.random() * 10;
}
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)&lt;[LineString](http://geojson.org/geojson-spec.html#linestring)>** isolines
Returns **[FeatureCollection](http://geojson.org/geojson-spec.html#feature-collection-objects)&lt;[MultiLineString](http://geojson.org/geojson-spec.html#multilinestring)>** a FeatureCollection of [MultiLineString](http://geojson.org/geojson-spec.html#multilinestring) features representing isolines

<!-- 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
Expand Down
66 changes: 52 additions & 14 deletions packages/turf-isolines/bench.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,56 @@
var isolines = require('./');
var Benchmark = require('benchmark');
var fs = require('fs');
const fs = require('fs');
const path = require('path');
const load = require('load-json-file');
const Benchmark = require('benchmark');
const matrixToGrid = require('matrix-to-grid');
const isolines = require('./');

var points = JSON.parse(fs.readFileSync(__dirname+'/test/Points.geojson'));
// Define Fixtures
const directory = path.join(__dirname, 'test', 'in') + path.sep;
const fixtures = fs.readdirSync(directory).map(filename => {
return {
filename,
name: path.parse(filename).name,
jsondata: load.sync(directory + filename)
};
});

var suite = new Benchmark.Suite('turf-isolines');
/**
* Benchmark Results
*
* bigMatrix: 21.457ms
* matrix1: 0.741ms
* matrix2: 0.547ms
* pointGrid: 1.373ms
* bigMatrix x 168 ops/sec ±2.79% (72 runs sampled)
* matrix1 x 17,145 ops/sec ±4.73% (68 runs sampled)
* matrix2 x 11,004 ops/sec ±2.56% (79 runs sampled)
* pointGrid x 12,109 ops/sec ±2.01% (77 runs sampled)
*/
const suite = new Benchmark.Suite('turf-isolines');
for (const {name, jsondata, filename} of fixtures) {
const {
breaks,
zProperty,
propertiesPerIsoline,
propertiesToAllIsolines,
matrix,
cellSize,
units,
origin} = jsondata.properties || jsondata;

// allow GeoJSON FeatureCollection or Matrix
let points;
if (filename.includes('geojson')) points = jsondata;
else points = matrixToGrid(matrix, origin, cellSize, {zProperty, units});

console.time(name);
isolines(points, breaks, zProperty, propertiesToAllIsolines, propertiesPerIsoline);
console.timeEnd(name);
suite.add(name, () => isolines(points, breaks, zProperty, propertiesToAllIsolines, propertiesPerIsoline));
}
suite
.add('turf-isolines',function () {
isolines(points, 'elevation', 15, [25, 45, 55, 65, 85, 95, 105, 120, 180]);
})
.on('cycle', function (event) {
console.log(String(event.target));
})
.on('complete', function () {

})
.on('cycle', e => console.log(String(e.target)))
.on('complete', () => {})
.run();

Loading

0 comments on commit 321d0df

Please sign in to comment.