@turf/isolines 7.0.0-alpha.2 → 7.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -9
- package/dist/cjs/index.cjs +151 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/cjs/index.d.cts +38 -0
- package/dist/{js → esm}/index.d.ts +4 -2
- package/dist/esm/index.js +151 -0
- package/dist/esm/index.js.map +1 -0
- package/package.json +42 -36
- package/dist/es/index.js +0 -119
- package/dist/es/lib/grid-to-matrix.js +0 -101
- package/dist/es/lib/marchingsquares-isocontours.js +0 -385
- package/dist/es/package.json +0 -1
- package/dist/js/index.js +0 -122
- package/dist/js/lib/grid-to-matrix.d.ts +0 -37
- package/dist/js/lib/grid-to-matrix.js +0 -104
- package/dist/js/lib/marchingsquares-isocontours.d.ts +0 -1
- package/dist/js/lib/marchingsquares-isocontours.js +0 -388
package/README.md
CHANGED
|
@@ -61,26 +61,21 @@ Returns **[FeatureCollection][4]<[MultiLineString][10]>** a FeatureCollection of
|
|
|
61
61
|
|
|
62
62
|
[11]: https://tools.ietf.org/html/rfc7946#section-3.1.5
|
|
63
63
|
|
|
64
|
-
<!-- This file is automatically generated. Please don't edit it directly
|
|
65
|
-
if you find an error, edit the source file (likely index.js), and re-run
|
|
66
|
-
./scripts/generate-readmes in the turf project. -->
|
|
64
|
+
<!-- This file is automatically generated. Please don't edit it directly. If you find an error, edit the source file of the module in question (likely index.js or index.ts), and re-run "yarn docs" from the root of the turf project. -->
|
|
67
65
|
|
|
68
66
|
---
|
|
69
67
|
|
|
70
|
-
This module is part of the [Turfjs project](
|
|
71
|
-
module collection dedicated to geographic algorithms. It is maintained in the
|
|
72
|
-
[Turfjs/turf](https://github.com/Turfjs/turf) repository, where you can create
|
|
73
|
-
PRs and issues.
|
|
68
|
+
This module is part of the [Turfjs project](https://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.
|
|
74
69
|
|
|
75
70
|
### Installation
|
|
76
71
|
|
|
77
|
-
Install this module individually:
|
|
72
|
+
Install this single module individually:
|
|
78
73
|
|
|
79
74
|
```sh
|
|
80
75
|
$ npm install @turf/isolines
|
|
81
76
|
```
|
|
82
77
|
|
|
83
|
-
Or install the
|
|
78
|
+
Or install the all-encompassing @turf/turf module that includes all modules as functions:
|
|
84
79
|
|
|
85
80
|
```sh
|
|
86
81
|
$ npm install @turf/turf
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
3
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
4
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
5
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
6
|
+
var __spreadValues = (a, b) => {
|
|
7
|
+
for (var prop in b || (b = {}))
|
|
8
|
+
if (__hasOwnProp.call(b, prop))
|
|
9
|
+
__defNormalProp(a, prop, b[prop]);
|
|
10
|
+
if (__getOwnPropSymbols)
|
|
11
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
12
|
+
if (__propIsEnum.call(b, prop))
|
|
13
|
+
__defNormalProp(a, prop, b[prop]);
|
|
14
|
+
}
|
|
15
|
+
return a;
|
|
16
|
+
};
|
|
17
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
18
|
+
|
|
19
|
+
// index.ts
|
|
20
|
+
var _bbox = require('@turf/bbox');
|
|
21
|
+
var _meta = require('@turf/meta');
|
|
22
|
+
var _invariant = require('@turf/invariant');
|
|
23
|
+
var _helpers = require('@turf/helpers');
|
|
24
|
+
var _marchingsquares = require('marchingsquares');
|
|
25
|
+
|
|
26
|
+
// lib/grid-to-matrix.js
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
function gridToMatrix(grid, options) {
|
|
31
|
+
options = options || {};
|
|
32
|
+
if (!_helpers.isObject.call(void 0, options))
|
|
33
|
+
throw new Error("options is invalid");
|
|
34
|
+
var zProperty = options.zProperty || "elevation";
|
|
35
|
+
var flip = options.flip;
|
|
36
|
+
var flags = options.flags;
|
|
37
|
+
_invariant.collectionOf.call(void 0, grid, "Point", "input must contain Points");
|
|
38
|
+
var pointsMatrix = sortPointsByLatLng(grid, flip);
|
|
39
|
+
var matrix = [];
|
|
40
|
+
for (var r = 0; r < pointsMatrix.length; r++) {
|
|
41
|
+
var pointRow = pointsMatrix[r];
|
|
42
|
+
var row = [];
|
|
43
|
+
for (var c = 0; c < pointRow.length; c++) {
|
|
44
|
+
var point = pointRow[c];
|
|
45
|
+
if (point.properties[zProperty])
|
|
46
|
+
row.push(point.properties[zProperty]);
|
|
47
|
+
else
|
|
48
|
+
row.push(0);
|
|
49
|
+
if (flags === true)
|
|
50
|
+
point.properties.matrixPosition = [r, c];
|
|
51
|
+
}
|
|
52
|
+
matrix.push(row);
|
|
53
|
+
}
|
|
54
|
+
return matrix;
|
|
55
|
+
}
|
|
56
|
+
__name(gridToMatrix, "gridToMatrix");
|
|
57
|
+
function sortPointsByLatLng(points, flip) {
|
|
58
|
+
var pointsByLatitude = {};
|
|
59
|
+
_meta.featureEach.call(void 0, points, function(point) {
|
|
60
|
+
var lat = _invariant.getCoords.call(void 0, point)[1];
|
|
61
|
+
if (!pointsByLatitude[lat])
|
|
62
|
+
pointsByLatitude[lat] = [];
|
|
63
|
+
pointsByLatitude[lat].push(point);
|
|
64
|
+
});
|
|
65
|
+
var orderedRowsByLatitude = Object.keys(pointsByLatitude).map(function(lat) {
|
|
66
|
+
var row = pointsByLatitude[lat];
|
|
67
|
+
var rowOrderedByLongitude = row.sort(function(a, b) {
|
|
68
|
+
return _invariant.getCoords.call(void 0, a)[0] - _invariant.getCoords.call(void 0, b)[0];
|
|
69
|
+
});
|
|
70
|
+
return rowOrderedByLongitude;
|
|
71
|
+
});
|
|
72
|
+
var pointMatrix = orderedRowsByLatitude.sort(function(a, b) {
|
|
73
|
+
if (flip)
|
|
74
|
+
return _invariant.getCoords.call(void 0, a[0])[1] - _invariant.getCoords.call(void 0, b[0])[1];
|
|
75
|
+
else
|
|
76
|
+
return _invariant.getCoords.call(void 0, b[0])[1] - _invariant.getCoords.call(void 0, a[0])[1];
|
|
77
|
+
});
|
|
78
|
+
return pointMatrix;
|
|
79
|
+
}
|
|
80
|
+
__name(sortPointsByLatLng, "sortPointsByLatLng");
|
|
81
|
+
|
|
82
|
+
// index.ts
|
|
83
|
+
function isolines(pointGrid, breaks, options) {
|
|
84
|
+
options = options || {};
|
|
85
|
+
if (!_helpers.isObject.call(void 0, options))
|
|
86
|
+
throw new Error("options is invalid");
|
|
87
|
+
const zProperty = options.zProperty || "elevation";
|
|
88
|
+
const commonProperties = options.commonProperties || {};
|
|
89
|
+
const breaksProperties = options.breaksProperties || [];
|
|
90
|
+
_invariant.collectionOf.call(void 0, pointGrid, "Point", "Input must contain Points");
|
|
91
|
+
if (!breaks)
|
|
92
|
+
throw new Error("breaks is required");
|
|
93
|
+
if (!Array.isArray(breaks))
|
|
94
|
+
throw new Error("breaks must be an Array");
|
|
95
|
+
if (!_helpers.isObject.call(void 0, commonProperties))
|
|
96
|
+
throw new Error("commonProperties must be an Object");
|
|
97
|
+
if (!Array.isArray(breaksProperties))
|
|
98
|
+
throw new Error("breaksProperties must be an Array");
|
|
99
|
+
const matrix = gridToMatrix(pointGrid, { zProperty, flip: true });
|
|
100
|
+
const createdIsoLines = createIsoLines(
|
|
101
|
+
matrix,
|
|
102
|
+
breaks,
|
|
103
|
+
zProperty,
|
|
104
|
+
commonProperties,
|
|
105
|
+
breaksProperties
|
|
106
|
+
);
|
|
107
|
+
const scaledIsolines = rescaleIsolines(createdIsoLines, matrix, pointGrid);
|
|
108
|
+
return _helpers.featureCollection.call(void 0, scaledIsolines);
|
|
109
|
+
}
|
|
110
|
+
__name(isolines, "isolines");
|
|
111
|
+
function createIsoLines(matrix, breaks, zProperty, commonProperties, breaksProperties) {
|
|
112
|
+
const results = [];
|
|
113
|
+
for (let i = 0; i < breaks.length; i++) {
|
|
114
|
+
const threshold = +breaks[i];
|
|
115
|
+
const properties = __spreadValues(__spreadValues({}, commonProperties), breaksProperties[i]);
|
|
116
|
+
properties[zProperty] = threshold;
|
|
117
|
+
const isoline = _helpers.multiLineString.call(void 0,
|
|
118
|
+
_marchingsquares.isoContours.call(void 0, matrix, threshold, { linearRing: false, noFrame: true }),
|
|
119
|
+
properties
|
|
120
|
+
);
|
|
121
|
+
results.push(isoline);
|
|
122
|
+
}
|
|
123
|
+
return results;
|
|
124
|
+
}
|
|
125
|
+
__name(createIsoLines, "createIsoLines");
|
|
126
|
+
function rescaleIsolines(createdIsoLines, matrix, points) {
|
|
127
|
+
const gridBbox = _bbox.bbox.call(void 0, points);
|
|
128
|
+
const originalWidth = gridBbox[2] - gridBbox[0];
|
|
129
|
+
const originalHeigth = gridBbox[3] - gridBbox[1];
|
|
130
|
+
const x0 = gridBbox[0];
|
|
131
|
+
const y0 = gridBbox[1];
|
|
132
|
+
const matrixWidth = matrix[0].length - 1;
|
|
133
|
+
const matrixHeight = matrix.length - 1;
|
|
134
|
+
const scaleX = originalWidth / matrixWidth;
|
|
135
|
+
const scaleY = originalHeigth / matrixHeight;
|
|
136
|
+
const resize = /* @__PURE__ */ __name((point) => {
|
|
137
|
+
point[0] = point[0] * scaleX + x0;
|
|
138
|
+
point[1] = point[1] * scaleY + y0;
|
|
139
|
+
}, "resize");
|
|
140
|
+
createdIsoLines.forEach((isoline) => {
|
|
141
|
+
_meta.coordEach.call(void 0, isoline, resize);
|
|
142
|
+
});
|
|
143
|
+
return createdIsoLines;
|
|
144
|
+
}
|
|
145
|
+
__name(rescaleIsolines, "rescaleIsolines");
|
|
146
|
+
var turf_isolines_default = isolines;
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
exports.default = turf_isolines_default; exports.isolines = isolines;
|
|
151
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../index.ts","../../lib/grid-to-matrix.js"],"names":["collectionOf","isObject"],"mappings":";;;;;;;;;;;;;;;;;;;AAAA,SAAS,YAAY;AACrB,SAAS,iBAAiB;AAC1B,SAAS,gBAAAA,qBAAoB;AAC7B,SAAS,iBAAiB,mBAAmB,YAAAC,iBAAgB;AAE7D,SAAS,mBAAmB;;;ACL5B,SAAS,WAAW,oBAAoB;AACxC,SAAS,mBAAmB;AAC5B,SAAS,gBAAgB;AAkCzB,SAAS,aAAa,MAAM,SAAS;AAEnC,YAAU,WAAW,CAAC;AACtB,MAAI,CAAC,SAAS,OAAO;AAAG,UAAM,IAAI,MAAM,oBAAoB;AAC5D,MAAI,YAAY,QAAQ,aAAa;AACrC,MAAI,OAAO,QAAQ;AACnB,MAAI,QAAQ,QAAQ;AAGpB,eAAa,MAAM,SAAS,2BAA2B;AAEvD,MAAI,eAAe,mBAAmB,MAAM,IAAI;AAEhD,MAAI,SAAS,CAAC;AAGd,WAAS,IAAI,GAAG,IAAI,aAAa,QAAQ,KAAK;AAC5C,QAAI,WAAW,aAAa,CAAC;AAC7B,QAAI,MAAM,CAAC;AACX,aAAS,IAAI,GAAG,IAAI,SAAS,QAAQ,KAAK;AACxC,UAAI,QAAQ,SAAS,CAAC;AAEtB,UAAI,MAAM,WAAW,SAAS;AAAG,YAAI,KAAK,MAAM,WAAW,SAAS,CAAC;AAAA;AAChE,YAAI,KAAK,CAAC;AAEf,UAAI,UAAU;AAAM,cAAM,WAAW,iBAAiB,CAAC,GAAG,CAAC;AAAA,IAC7D;AACA,WAAO,KAAK,GAAG;AAAA,EACjB;AAEA,SAAO;AACT;AA/BS;AAyCT,SAAS,mBAAmB,QAAQ,MAAM;AACxC,MAAI,mBAAmB,CAAC;AAGxB,cAAY,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C,cAAU,SAAS,MAAM;AAAA,EAC3B,CAAC;AACD,SAAO;AACT;AAhCS;AAmCT,IAAO,wBAAQ","sourcesContent":["import { bbox } from \"@turf/bbox\";\nimport { coordEach } from \"@turf/meta\";\nimport { collectionOf } from \"@turf/invariant\";\nimport { multiLineString, featureCollection, isObject } from \"@turf/helpers\";\n// @ts-expect-error Legacy JS library with no types defined\nimport { isoContours } from \"marchingsquares\";\nimport { gridToMatrix } from \"./lib/grid-to-matrix.js\";\nimport {\n FeatureCollection,\n Point,\n MultiLineString,\n Feature,\n GeoJsonProperties,\n} from \"geojson\";\n\n/**\n * Takes a grid {@link FeatureCollection} of {@link Point} features with z-values and an array of\n * value breaks and generates [isolines](https://en.wikipedia.org/wiki/Contour_line).\n *\n * @name isolines\n * @param {FeatureCollection<Point>} pointGrid input points\n * @param {Array<number>} breaks values of `zProperty` where to draw isolines\n * @param {Object} [options={}] Optional parameters\n * @param {string} [options.zProperty='elevation'] the property name in `points` from which z-values will be pulled\n * @param {Object} [options.commonProperties={}] GeoJSON properties passed to ALL isolines\n * @param {Array<Object>} [options.breaksProperties=[]] GeoJSON properties passed, in order, to the correspondent isoline;\n * the breaks array will define the order in which the isolines are created\n * @returns {FeatureCollection<MultiLineString>} a FeatureCollection of {@link MultiLineString} features representing isolines\n * @example\n * // create a grid of points with random z-values in their properties\n * var extent = [0, 30, 20, 50];\n * var cellWidth = 100;\n * var pointGrid = turf.pointGrid(extent, cellWidth, {units: 'miles'});\n *\n * for (var i = 0; i < pointGrid.features.length; i++) {\n * pointGrid.features[i].properties.temperature = Math.random() * 10;\n * }\n * var breaks = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];\n *\n * var lines = turf.isolines(pointGrid, breaks, {zProperty: 'temperature'});\n *\n * //addToMap\n * var addToMap = [lines];\n */\nfunction isolines(\n pointGrid: FeatureCollection<Point>,\n breaks: number[],\n options?: {\n zProperty?: string;\n commonProperties?: GeoJsonProperties;\n breaksProperties?: GeoJsonProperties[];\n }\n) {\n // Optional parameters\n options = options || {};\n if (!isObject(options)) throw new Error(\"options is invalid\");\n const zProperty = options.zProperty || \"elevation\";\n const commonProperties = options.commonProperties || {};\n const breaksProperties = options.breaksProperties || [];\n\n // Input validation\n collectionOf(pointGrid, \"Point\", \"Input must contain Points\");\n if (!breaks) throw new Error(\"breaks is required\");\n if (!Array.isArray(breaks)) throw new Error(\"breaks must be an Array\");\n if (!isObject(commonProperties))\n throw new Error(\"commonProperties must be an Object\");\n if (!Array.isArray(breaksProperties))\n throw new Error(\"breaksProperties must be an Array\");\n\n // Isoline methods\n const matrix = gridToMatrix(pointGrid, { zProperty: zProperty, flip: true });\n const createdIsoLines = createIsoLines(\n matrix,\n breaks,\n zProperty,\n commonProperties,\n breaksProperties\n );\n const scaledIsolines = rescaleIsolines(createdIsoLines, matrix, pointGrid);\n\n return featureCollection(scaledIsolines);\n}\n\n/**\n * Creates the isolines lines (featuresCollection of MultiLineString features) from the 2D data grid\n *\n * Marchingsquares process the grid data as a 3D representation of a function on a 2D plane, therefore it\n * assumes the points (x-y coordinates) are one 'unit' distance. The result of the isolines function needs to be\n * rescaled, with turfjs, to the original area and proportions on the map\n *\n * @private\n * @param {Array<Array<number>>} matrix Grid Data\n * @param {Array<number>} breaks BreakProps\n * @param {string} zProperty name of the z-values property\n * @param {Object} [commonProperties={}] GeoJSON properties passed to ALL isolines\n * @param {Object} [breaksProperties=[]] GeoJSON properties passed to the correspondent isoline\n * @returns {Array<MultiLineString>} isolines\n */\nfunction createIsoLines(\n matrix: number[][],\n breaks: number[],\n zProperty: string,\n commonProperties: GeoJsonProperties,\n breaksProperties: GeoJsonProperties[]\n): Feature<MultiLineString>[] {\n const results = [];\n for (let i = 0; i < breaks.length; i++) {\n const threshold = +breaks[i]; // make sure it's a number\n\n const properties = { ...commonProperties, ...breaksProperties[i] };\n properties[zProperty] = threshold;\n // Pass options to marchingsquares lib to reproduce historical turf\n // behaviour.\n const isoline = multiLineString(\n isoContours(matrix, threshold, { linearRing: false, noFrame: true }),\n properties\n );\n\n results.push(isoline);\n }\n return results;\n}\n\n/**\n * Translates and scales isolines\n *\n * @private\n * @param {Array<MultiLineString>} createdIsoLines to be rescaled\n * @param {Array<Array<number>>} matrix Grid Data\n * @param {Object} points Points by Latitude\n * @returns {Array<MultiLineString>} isolines\n */\nfunction rescaleIsolines(\n createdIsoLines: Feature<MultiLineString>[],\n matrix: number[][],\n points: FeatureCollection<Point>\n) {\n // get dimensions (on the map) of the original grid\n const gridBbox = bbox(points); // [ minX, minY, maxX, maxY ]\n const originalWidth = gridBbox[2] - gridBbox[0];\n const originalHeigth = gridBbox[3] - gridBbox[1];\n\n // get origin, which is the first point of the last row on the rectangular data on the map\n const x0 = gridBbox[0];\n const y0 = gridBbox[1];\n\n // get number of cells per side\n const matrixWidth = matrix[0].length - 1;\n const matrixHeight = matrix.length - 1;\n\n // calculate the scaling factor between matrix and rectangular grid on the map\n const scaleX = originalWidth / matrixWidth;\n const scaleY = originalHeigth / matrixHeight;\n\n const resize = (point: number[]) => {\n point[0] = point[0] * scaleX + x0;\n point[1] = point[1] * scaleY + y0;\n };\n\n // resize and shift each point/line of the createdIsoLines\n createdIsoLines.forEach((isoline) => {\n coordEach(isoline, resize);\n });\n return createdIsoLines;\n}\n\nexport { isolines };\nexport default isolines;\n","import { getCoords, collectionOf } from \"@turf/invariant\";\nimport { featureEach } from \"@turf/meta\";\nimport { isObject } from \"@turf/helpers\";\n\n/**\n * Takes a {@link Point} grid and returns a correspondent matrix {Array<Array<number>>}\n * of the 'property' values\n *\n * @name gridToMatrix\n * @param {FeatureCollection<Point>} grid of points\n * @param {Object} [options={}] Optional parameters\n * @param {string} [options.zProperty='elevation'] the property name in `points` from which z-values will be pulled\n * @param {boolean} [options.flip=false] returns the matrix upside-down\n * @param {boolean} [options.flags=false] flags, adding a `matrixPosition` array field ([row, column]) to its properties,\n * the grid points with coordinates on the matrix\n * @returns {Array<Array<number>>} matrix of property values\n * @example\n * var extent = [-70.823364, -33.553984, -70.473175, -33.302986];\n * var cellSize = 3;\n * var grid = turf.pointGrid(extent, cellSize);\n * // add a random property to each point between 0 and 60\n * for (var i = 0; i < grid.features.length; i++) {\n * grid.features[i].properties.elevation = (Math.random() * 60);\n * }\n * gridToMatrix(grid);\n * //= [\n * [ 1, 13, 10, 9, 10, 13, 18],\n * [34, 8, 5, 4, 5, 8, 13],\n * [10, 5, 2, 1, 2, 5, 4],\n * [ 0, 4, 56, 19, 1, 4, 9],\n * [10, 5, 2, 1, 2, 5, 10],\n * [57, 8, 5, 4, 5, 0, 57],\n * [ 3, 13, 10, 9, 5, 13, 18],\n * [18, 13, 10, 9, 78, 13, 18]\n * ]\n */\nfunction gridToMatrix(grid, options) {\n // Optional parameters\n options = options || {};\n if (!isObject(options)) throw new Error(\"options is invalid\");\n var zProperty = options.zProperty || \"elevation\";\n var flip = options.flip;\n var flags = options.flags;\n\n // validation\n collectionOf(grid, \"Point\", \"input must contain Points\");\n\n var pointsMatrix = sortPointsByLatLng(grid, flip);\n\n var matrix = [];\n // create property matrix from sorted points\n // looping order matters here\n for (var r = 0; r < pointsMatrix.length; r++) {\n var pointRow = pointsMatrix[r];\n var row = [];\n for (var c = 0; c < pointRow.length; c++) {\n var point = pointRow[c];\n // Check if zProperty exist\n if (point.properties[zProperty]) row.push(point.properties[zProperty]);\n else row.push(0);\n // add flags\n if (flags === true) point.properties.matrixPosition = [r, c];\n }\n matrix.push(row);\n }\n\n return matrix;\n}\n\n/**\n * Sorts points by latitude and longitude, creating a 2-dimensional array of points\n *\n * @private\n * @param {FeatureCollection<Point>} points GeoJSON Point features\n * @param {boolean} [flip=false] returns the matrix upside-down\n * @returns {Array<Array<Point>>} points ordered by latitude and longitude\n */\nfunction sortPointsByLatLng(points, flip) {\n var pointsByLatitude = {};\n\n // divide points by rows with the same latitude\n featureEach(points, function (point) {\n var lat = getCoords(point)[1];\n if (!pointsByLatitude[lat]) pointsByLatitude[lat] = [];\n pointsByLatitude[lat].push(point);\n });\n\n // sort points (with the same latitude) by longitude\n var orderedRowsByLatitude = Object.keys(pointsByLatitude).map(function (lat) {\n var row = pointsByLatitude[lat];\n var rowOrderedByLongitude = row.sort(function (a, b) {\n return getCoords(a)[0] - getCoords(b)[0];\n });\n return rowOrderedByLongitude;\n });\n\n // sort rows (of points with the same latitude) by latitude\n var pointMatrix = orderedRowsByLatitude.sort(function (a, b) {\n if (flip) return getCoords(a[0])[1] - getCoords(b[0])[1];\n else return getCoords(b[0])[1] - getCoords(a[0])[1];\n });\n\n return pointMatrix;\n}\n\nexport { gridToMatrix };\nexport default gridToMatrix;\n"]}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { FeatureCollection, Point, GeoJsonProperties, MultiLineString } from 'geojson';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Takes a grid {@link FeatureCollection} of {@link Point} features with z-values and an array of
|
|
5
|
+
* value breaks and generates [isolines](https://en.wikipedia.org/wiki/Contour_line).
|
|
6
|
+
*
|
|
7
|
+
* @name isolines
|
|
8
|
+
* @param {FeatureCollection<Point>} pointGrid input points
|
|
9
|
+
* @param {Array<number>} breaks values of `zProperty` where to draw isolines
|
|
10
|
+
* @param {Object} [options={}] Optional parameters
|
|
11
|
+
* @param {string} [options.zProperty='elevation'] the property name in `points` from which z-values will be pulled
|
|
12
|
+
* @param {Object} [options.commonProperties={}] GeoJSON properties passed to ALL isolines
|
|
13
|
+
* @param {Array<Object>} [options.breaksProperties=[]] GeoJSON properties passed, in order, to the correspondent isoline;
|
|
14
|
+
* the breaks array will define the order in which the isolines are created
|
|
15
|
+
* @returns {FeatureCollection<MultiLineString>} a FeatureCollection of {@link MultiLineString} features representing isolines
|
|
16
|
+
* @example
|
|
17
|
+
* // create a grid of points with random z-values in their properties
|
|
18
|
+
* var extent = [0, 30, 20, 50];
|
|
19
|
+
* var cellWidth = 100;
|
|
20
|
+
* var pointGrid = turf.pointGrid(extent, cellWidth, {units: 'miles'});
|
|
21
|
+
*
|
|
22
|
+
* for (var i = 0; i < pointGrid.features.length; i++) {
|
|
23
|
+
* pointGrid.features[i].properties.temperature = Math.random() * 10;
|
|
24
|
+
* }
|
|
25
|
+
* var breaks = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
|
|
26
|
+
*
|
|
27
|
+
* var lines = turf.isolines(pointGrid, breaks, {zProperty: 'temperature'});
|
|
28
|
+
*
|
|
29
|
+
* //addToMap
|
|
30
|
+
* var addToMap = [lines];
|
|
31
|
+
*/
|
|
32
|
+
declare function isolines(pointGrid: FeatureCollection<Point>, breaks: number[], options?: {
|
|
33
|
+
zProperty?: string;
|
|
34
|
+
commonProperties?: GeoJsonProperties;
|
|
35
|
+
breaksProperties?: GeoJsonProperties[];
|
|
36
|
+
}): FeatureCollection<MultiLineString, GeoJsonProperties>;
|
|
37
|
+
|
|
38
|
+
export { isolines as default, isolines };
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { FeatureCollection, Point,
|
|
1
|
+
import { FeatureCollection, Point, GeoJsonProperties, MultiLineString } from 'geojson';
|
|
2
|
+
|
|
2
3
|
/**
|
|
3
4
|
* Takes a grid {@link FeatureCollection} of {@link Point} features with z-values and an array of
|
|
4
5
|
* value breaks and generates [isolines](https://en.wikipedia.org/wiki/Contour_line).
|
|
@@ -33,4 +34,5 @@ declare function isolines(pointGrid: FeatureCollection<Point>, breaks: number[],
|
|
|
33
34
|
commonProperties?: GeoJsonProperties;
|
|
34
35
|
breaksProperties?: GeoJsonProperties[];
|
|
35
36
|
}): FeatureCollection<MultiLineString, GeoJsonProperties>;
|
|
36
|
-
|
|
37
|
+
|
|
38
|
+
export { isolines as default, isolines };
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
3
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
4
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
5
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
6
|
+
var __spreadValues = (a, b) => {
|
|
7
|
+
for (var prop in b || (b = {}))
|
|
8
|
+
if (__hasOwnProp.call(b, prop))
|
|
9
|
+
__defNormalProp(a, prop, b[prop]);
|
|
10
|
+
if (__getOwnPropSymbols)
|
|
11
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
12
|
+
if (__propIsEnum.call(b, prop))
|
|
13
|
+
__defNormalProp(a, prop, b[prop]);
|
|
14
|
+
}
|
|
15
|
+
return a;
|
|
16
|
+
};
|
|
17
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
18
|
+
|
|
19
|
+
// index.ts
|
|
20
|
+
import { bbox } from "@turf/bbox";
|
|
21
|
+
import { coordEach } from "@turf/meta";
|
|
22
|
+
import { collectionOf as collectionOf2 } from "@turf/invariant";
|
|
23
|
+
import { multiLineString, featureCollection, isObject as isObject2 } from "@turf/helpers";
|
|
24
|
+
import { isoContours } from "marchingsquares";
|
|
25
|
+
|
|
26
|
+
// lib/grid-to-matrix.js
|
|
27
|
+
import { getCoords, collectionOf } from "@turf/invariant";
|
|
28
|
+
import { featureEach } from "@turf/meta";
|
|
29
|
+
import { isObject } from "@turf/helpers";
|
|
30
|
+
function gridToMatrix(grid, options) {
|
|
31
|
+
options = options || {};
|
|
32
|
+
if (!isObject(options))
|
|
33
|
+
throw new Error("options is invalid");
|
|
34
|
+
var zProperty = options.zProperty || "elevation";
|
|
35
|
+
var flip = options.flip;
|
|
36
|
+
var flags = options.flags;
|
|
37
|
+
collectionOf(grid, "Point", "input must contain Points");
|
|
38
|
+
var pointsMatrix = sortPointsByLatLng(grid, flip);
|
|
39
|
+
var matrix = [];
|
|
40
|
+
for (var r = 0; r < pointsMatrix.length; r++) {
|
|
41
|
+
var pointRow = pointsMatrix[r];
|
|
42
|
+
var row = [];
|
|
43
|
+
for (var c = 0; c < pointRow.length; c++) {
|
|
44
|
+
var point = pointRow[c];
|
|
45
|
+
if (point.properties[zProperty])
|
|
46
|
+
row.push(point.properties[zProperty]);
|
|
47
|
+
else
|
|
48
|
+
row.push(0);
|
|
49
|
+
if (flags === true)
|
|
50
|
+
point.properties.matrixPosition = [r, c];
|
|
51
|
+
}
|
|
52
|
+
matrix.push(row);
|
|
53
|
+
}
|
|
54
|
+
return matrix;
|
|
55
|
+
}
|
|
56
|
+
__name(gridToMatrix, "gridToMatrix");
|
|
57
|
+
function sortPointsByLatLng(points, flip) {
|
|
58
|
+
var pointsByLatitude = {};
|
|
59
|
+
featureEach(points, function(point) {
|
|
60
|
+
var lat = getCoords(point)[1];
|
|
61
|
+
if (!pointsByLatitude[lat])
|
|
62
|
+
pointsByLatitude[lat] = [];
|
|
63
|
+
pointsByLatitude[lat].push(point);
|
|
64
|
+
});
|
|
65
|
+
var orderedRowsByLatitude = Object.keys(pointsByLatitude).map(function(lat) {
|
|
66
|
+
var row = pointsByLatitude[lat];
|
|
67
|
+
var rowOrderedByLongitude = row.sort(function(a, b) {
|
|
68
|
+
return getCoords(a)[0] - getCoords(b)[0];
|
|
69
|
+
});
|
|
70
|
+
return rowOrderedByLongitude;
|
|
71
|
+
});
|
|
72
|
+
var pointMatrix = orderedRowsByLatitude.sort(function(a, b) {
|
|
73
|
+
if (flip)
|
|
74
|
+
return getCoords(a[0])[1] - getCoords(b[0])[1];
|
|
75
|
+
else
|
|
76
|
+
return getCoords(b[0])[1] - getCoords(a[0])[1];
|
|
77
|
+
});
|
|
78
|
+
return pointMatrix;
|
|
79
|
+
}
|
|
80
|
+
__name(sortPointsByLatLng, "sortPointsByLatLng");
|
|
81
|
+
|
|
82
|
+
// index.ts
|
|
83
|
+
function isolines(pointGrid, breaks, options) {
|
|
84
|
+
options = options || {};
|
|
85
|
+
if (!isObject2(options))
|
|
86
|
+
throw new Error("options is invalid");
|
|
87
|
+
const zProperty = options.zProperty || "elevation";
|
|
88
|
+
const commonProperties = options.commonProperties || {};
|
|
89
|
+
const breaksProperties = options.breaksProperties || [];
|
|
90
|
+
collectionOf2(pointGrid, "Point", "Input must contain Points");
|
|
91
|
+
if (!breaks)
|
|
92
|
+
throw new Error("breaks is required");
|
|
93
|
+
if (!Array.isArray(breaks))
|
|
94
|
+
throw new Error("breaks must be an Array");
|
|
95
|
+
if (!isObject2(commonProperties))
|
|
96
|
+
throw new Error("commonProperties must be an Object");
|
|
97
|
+
if (!Array.isArray(breaksProperties))
|
|
98
|
+
throw new Error("breaksProperties must be an Array");
|
|
99
|
+
const matrix = gridToMatrix(pointGrid, { zProperty, flip: true });
|
|
100
|
+
const createdIsoLines = createIsoLines(
|
|
101
|
+
matrix,
|
|
102
|
+
breaks,
|
|
103
|
+
zProperty,
|
|
104
|
+
commonProperties,
|
|
105
|
+
breaksProperties
|
|
106
|
+
);
|
|
107
|
+
const scaledIsolines = rescaleIsolines(createdIsoLines, matrix, pointGrid);
|
|
108
|
+
return featureCollection(scaledIsolines);
|
|
109
|
+
}
|
|
110
|
+
__name(isolines, "isolines");
|
|
111
|
+
function createIsoLines(matrix, breaks, zProperty, commonProperties, breaksProperties) {
|
|
112
|
+
const results = [];
|
|
113
|
+
for (let i = 0; i < breaks.length; i++) {
|
|
114
|
+
const threshold = +breaks[i];
|
|
115
|
+
const properties = __spreadValues(__spreadValues({}, commonProperties), breaksProperties[i]);
|
|
116
|
+
properties[zProperty] = threshold;
|
|
117
|
+
const isoline = multiLineString(
|
|
118
|
+
isoContours(matrix, threshold, { linearRing: false, noFrame: true }),
|
|
119
|
+
properties
|
|
120
|
+
);
|
|
121
|
+
results.push(isoline);
|
|
122
|
+
}
|
|
123
|
+
return results;
|
|
124
|
+
}
|
|
125
|
+
__name(createIsoLines, "createIsoLines");
|
|
126
|
+
function rescaleIsolines(createdIsoLines, matrix, points) {
|
|
127
|
+
const gridBbox = bbox(points);
|
|
128
|
+
const originalWidth = gridBbox[2] - gridBbox[0];
|
|
129
|
+
const originalHeigth = gridBbox[3] - gridBbox[1];
|
|
130
|
+
const x0 = gridBbox[0];
|
|
131
|
+
const y0 = gridBbox[1];
|
|
132
|
+
const matrixWidth = matrix[0].length - 1;
|
|
133
|
+
const matrixHeight = matrix.length - 1;
|
|
134
|
+
const scaleX = originalWidth / matrixWidth;
|
|
135
|
+
const scaleY = originalHeigth / matrixHeight;
|
|
136
|
+
const resize = /* @__PURE__ */ __name((point) => {
|
|
137
|
+
point[0] = point[0] * scaleX + x0;
|
|
138
|
+
point[1] = point[1] * scaleY + y0;
|
|
139
|
+
}, "resize");
|
|
140
|
+
createdIsoLines.forEach((isoline) => {
|
|
141
|
+
coordEach(isoline, resize);
|
|
142
|
+
});
|
|
143
|
+
return createdIsoLines;
|
|
144
|
+
}
|
|
145
|
+
__name(rescaleIsolines, "rescaleIsolines");
|
|
146
|
+
var turf_isolines_default = isolines;
|
|
147
|
+
export {
|
|
148
|
+
turf_isolines_default as default,
|
|
149
|
+
isolines
|
|
150
|
+
};
|
|
151
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../index.ts","../../lib/grid-to-matrix.js"],"sourcesContent":["import { bbox } from \"@turf/bbox\";\nimport { coordEach } from \"@turf/meta\";\nimport { collectionOf } from \"@turf/invariant\";\nimport { multiLineString, featureCollection, isObject } from \"@turf/helpers\";\n// @ts-expect-error Legacy JS library with no types defined\nimport { isoContours } from \"marchingsquares\";\nimport { gridToMatrix } from \"./lib/grid-to-matrix.js\";\nimport {\n FeatureCollection,\n Point,\n MultiLineString,\n Feature,\n GeoJsonProperties,\n} from \"geojson\";\n\n/**\n * Takes a grid {@link FeatureCollection} of {@link Point} features with z-values and an array of\n * value breaks and generates [isolines](https://en.wikipedia.org/wiki/Contour_line).\n *\n * @name isolines\n * @param {FeatureCollection<Point>} pointGrid input points\n * @param {Array<number>} breaks values of `zProperty` where to draw isolines\n * @param {Object} [options={}] Optional parameters\n * @param {string} [options.zProperty='elevation'] the property name in `points` from which z-values will be pulled\n * @param {Object} [options.commonProperties={}] GeoJSON properties passed to ALL isolines\n * @param {Array<Object>} [options.breaksProperties=[]] GeoJSON properties passed, in order, to the correspondent isoline;\n * the breaks array will define the order in which the isolines are created\n * @returns {FeatureCollection<MultiLineString>} a FeatureCollection of {@link MultiLineString} features representing isolines\n * @example\n * // create a grid of points with random z-values in their properties\n * var extent = [0, 30, 20, 50];\n * var cellWidth = 100;\n * var pointGrid = turf.pointGrid(extent, cellWidth, {units: 'miles'});\n *\n * for (var i = 0; i < pointGrid.features.length; i++) {\n * pointGrid.features[i].properties.temperature = Math.random() * 10;\n * }\n * var breaks = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];\n *\n * var lines = turf.isolines(pointGrid, breaks, {zProperty: 'temperature'});\n *\n * //addToMap\n * var addToMap = [lines];\n */\nfunction isolines(\n pointGrid: FeatureCollection<Point>,\n breaks: number[],\n options?: {\n zProperty?: string;\n commonProperties?: GeoJsonProperties;\n breaksProperties?: GeoJsonProperties[];\n }\n) {\n // Optional parameters\n options = options || {};\n if (!isObject(options)) throw new Error(\"options is invalid\");\n const zProperty = options.zProperty || \"elevation\";\n const commonProperties = options.commonProperties || {};\n const breaksProperties = options.breaksProperties || [];\n\n // Input validation\n collectionOf(pointGrid, \"Point\", \"Input must contain Points\");\n if (!breaks) throw new Error(\"breaks is required\");\n if (!Array.isArray(breaks)) throw new Error(\"breaks must be an Array\");\n if (!isObject(commonProperties))\n throw new Error(\"commonProperties must be an Object\");\n if (!Array.isArray(breaksProperties))\n throw new Error(\"breaksProperties must be an Array\");\n\n // Isoline methods\n const matrix = gridToMatrix(pointGrid, { zProperty: zProperty, flip: true });\n const createdIsoLines = createIsoLines(\n matrix,\n breaks,\n zProperty,\n commonProperties,\n breaksProperties\n );\n const scaledIsolines = rescaleIsolines(createdIsoLines, matrix, pointGrid);\n\n return featureCollection(scaledIsolines);\n}\n\n/**\n * Creates the isolines lines (featuresCollection of MultiLineString features) from the 2D data grid\n *\n * Marchingsquares process the grid data as a 3D representation of a function on a 2D plane, therefore it\n * assumes the points (x-y coordinates) are one 'unit' distance. The result of the isolines function needs to be\n * rescaled, with turfjs, to the original area and proportions on the map\n *\n * @private\n * @param {Array<Array<number>>} matrix Grid Data\n * @param {Array<number>} breaks BreakProps\n * @param {string} zProperty name of the z-values property\n * @param {Object} [commonProperties={}] GeoJSON properties passed to ALL isolines\n * @param {Object} [breaksProperties=[]] GeoJSON properties passed to the correspondent isoline\n * @returns {Array<MultiLineString>} isolines\n */\nfunction createIsoLines(\n matrix: number[][],\n breaks: number[],\n zProperty: string,\n commonProperties: GeoJsonProperties,\n breaksProperties: GeoJsonProperties[]\n): Feature<MultiLineString>[] {\n const results = [];\n for (let i = 0; i < breaks.length; i++) {\n const threshold = +breaks[i]; // make sure it's a number\n\n const properties = { ...commonProperties, ...breaksProperties[i] };\n properties[zProperty] = threshold;\n // Pass options to marchingsquares lib to reproduce historical turf\n // behaviour.\n const isoline = multiLineString(\n isoContours(matrix, threshold, { linearRing: false, noFrame: true }),\n properties\n );\n\n results.push(isoline);\n }\n return results;\n}\n\n/**\n * Translates and scales isolines\n *\n * @private\n * @param {Array<MultiLineString>} createdIsoLines to be rescaled\n * @param {Array<Array<number>>} matrix Grid Data\n * @param {Object} points Points by Latitude\n * @returns {Array<MultiLineString>} isolines\n */\nfunction rescaleIsolines(\n createdIsoLines: Feature<MultiLineString>[],\n matrix: number[][],\n points: FeatureCollection<Point>\n) {\n // get dimensions (on the map) of the original grid\n const gridBbox = bbox(points); // [ minX, minY, maxX, maxY ]\n const originalWidth = gridBbox[2] - gridBbox[0];\n const originalHeigth = gridBbox[3] - gridBbox[1];\n\n // get origin, which is the first point of the last row on the rectangular data on the map\n const x0 = gridBbox[0];\n const y0 = gridBbox[1];\n\n // get number of cells per side\n const matrixWidth = matrix[0].length - 1;\n const matrixHeight = matrix.length - 1;\n\n // calculate the scaling factor between matrix and rectangular grid on the map\n const scaleX = originalWidth / matrixWidth;\n const scaleY = originalHeigth / matrixHeight;\n\n const resize = (point: number[]) => {\n point[0] = point[0] * scaleX + x0;\n point[1] = point[1] * scaleY + y0;\n };\n\n // resize and shift each point/line of the createdIsoLines\n createdIsoLines.forEach((isoline) => {\n coordEach(isoline, resize);\n });\n return createdIsoLines;\n}\n\nexport { isolines };\nexport default isolines;\n","import { getCoords, collectionOf } from \"@turf/invariant\";\nimport { featureEach } from \"@turf/meta\";\nimport { isObject } from \"@turf/helpers\";\n\n/**\n * Takes a {@link Point} grid and returns a correspondent matrix {Array<Array<number>>}\n * of the 'property' values\n *\n * @name gridToMatrix\n * @param {FeatureCollection<Point>} grid of points\n * @param {Object} [options={}] Optional parameters\n * @param {string} [options.zProperty='elevation'] the property name in `points` from which z-values will be pulled\n * @param {boolean} [options.flip=false] returns the matrix upside-down\n * @param {boolean} [options.flags=false] flags, adding a `matrixPosition` array field ([row, column]) to its properties,\n * the grid points with coordinates on the matrix\n * @returns {Array<Array<number>>} matrix of property values\n * @example\n * var extent = [-70.823364, -33.553984, -70.473175, -33.302986];\n * var cellSize = 3;\n * var grid = turf.pointGrid(extent, cellSize);\n * // add a random property to each point between 0 and 60\n * for (var i = 0; i < grid.features.length; i++) {\n * grid.features[i].properties.elevation = (Math.random() * 60);\n * }\n * gridToMatrix(grid);\n * //= [\n * [ 1, 13, 10, 9, 10, 13, 18],\n * [34, 8, 5, 4, 5, 8, 13],\n * [10, 5, 2, 1, 2, 5, 4],\n * [ 0, 4, 56, 19, 1, 4, 9],\n * [10, 5, 2, 1, 2, 5, 10],\n * [57, 8, 5, 4, 5, 0, 57],\n * [ 3, 13, 10, 9, 5, 13, 18],\n * [18, 13, 10, 9, 78, 13, 18]\n * ]\n */\nfunction gridToMatrix(grid, options) {\n // Optional parameters\n options = options || {};\n if (!isObject(options)) throw new Error(\"options is invalid\");\n var zProperty = options.zProperty || \"elevation\";\n var flip = options.flip;\n var flags = options.flags;\n\n // validation\n collectionOf(grid, \"Point\", \"input must contain Points\");\n\n var pointsMatrix = sortPointsByLatLng(grid, flip);\n\n var matrix = [];\n // create property matrix from sorted points\n // looping order matters here\n for (var r = 0; r < pointsMatrix.length; r++) {\n var pointRow = pointsMatrix[r];\n var row = [];\n for (var c = 0; c < pointRow.length; c++) {\n var point = pointRow[c];\n // Check if zProperty exist\n if (point.properties[zProperty]) row.push(point.properties[zProperty]);\n else row.push(0);\n // add flags\n if (flags === true) point.properties.matrixPosition = [r, c];\n }\n matrix.push(row);\n }\n\n return matrix;\n}\n\n/**\n * Sorts points by latitude and longitude, creating a 2-dimensional array of points\n *\n * @private\n * @param {FeatureCollection<Point>} points GeoJSON Point features\n * @param {boolean} [flip=false] returns the matrix upside-down\n * @returns {Array<Array<Point>>} points ordered by latitude and longitude\n */\nfunction sortPointsByLatLng(points, flip) {\n var pointsByLatitude = {};\n\n // divide points by rows with the same latitude\n featureEach(points, function (point) {\n var lat = getCoords(point)[1];\n if (!pointsByLatitude[lat]) pointsByLatitude[lat] = [];\n pointsByLatitude[lat].push(point);\n });\n\n // sort points (with the same latitude) by longitude\n var orderedRowsByLatitude = Object.keys(pointsByLatitude).map(function (lat) {\n var row = pointsByLatitude[lat];\n var rowOrderedByLongitude = row.sort(function (a, b) {\n return getCoords(a)[0] - getCoords(b)[0];\n });\n return rowOrderedByLongitude;\n });\n\n // sort rows (of points with the same latitude) by latitude\n var pointMatrix = orderedRowsByLatitude.sort(function (a, b) {\n if (flip) return getCoords(a[0])[1] - getCoords(b[0])[1];\n else return getCoords(b[0])[1] - getCoords(a[0])[1];\n });\n\n return pointMatrix;\n}\n\nexport { gridToMatrix };\nexport default gridToMatrix;\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAAA,SAAS,YAAY;AACrB,SAAS,iBAAiB;AAC1B,SAAS,gBAAAA,qBAAoB;AAC7B,SAAS,iBAAiB,mBAAmB,YAAAC,iBAAgB;AAE7D,SAAS,mBAAmB;;;ACL5B,SAAS,WAAW,oBAAoB;AACxC,SAAS,mBAAmB;AAC5B,SAAS,gBAA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AAC,OAAO,CAAC;AAE3B,UAAM,aAAa,kCAAK,mBAAqB,iBAAiB,CAAC;AAC/D,eAAW,SAAS,IAAI;AAGxB,UAAM,UAAU;AAAA,MACd,YAAY,QAAQ,WAAW,EAAE,YAAY,OAAO,SAAS,KAAK,CAAC;AAAA,MACnE;AAAA,IACF;AAEA,YAAQ,KAAK,OAAO;AAAA,EACtB;AACA,SAAO;AACT;AAvBS;AAkCT,SAAS,gBACP,iBACA,QACA,QACA;AAEA,QAAM,WAAW,KAAK,MAAM;AAC5B,QAAM,gBAAgB,SAAS,CAAC,IAAI,SAAS,CAAC;AAC9C,QAAM,iBAAiB,SAAS,CAAC,IAAI,SAAS,CAAC;AAG/C,QAAM,KAAK,SAAS,CAAC;AACrB,QAAM,KAAK,SAAS,CAAC;AAGrB,QAAM,cAAc,OAAO,CAAC,EAAE,SAAS;AACvC,QAAM,eAAe,OAAO,SAAS;AAGrC,QAAM,SAAS,gBAAgB;AAC/B,QAAM,SAAS,iBAAiB;AAEhC,QAAM,SAAS,wBAAC,UAAoB;AAClC,UAAM,CAAC,IAAI,MAAM,CAAC,IAAI,SAAS;AAC/B,UAAM,CAAC,IAAI,MAAM,CAAC,IAAI,SAAS;AAAA,EACjC,GAHe;AAMf,kBAAgB,QAAQ,CAAC,YAAY;AACnC,cAAU,SAAS,MAAM;AAAA,EAC3B,CAAC;AACD,SAAO;AACT;AAhCS;AAmCT,IAAO,wBAAQ;","names":["collectionOf","isObject","isObject","collectionOf"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@turf/isolines",
|
|
3
|
-
"version": "7.0.0
|
|
3
|
+
"version": "7.0.0",
|
|
4
4
|
"description": "turf isolines module",
|
|
5
5
|
"author": "Turf Authors",
|
|
6
6
|
"contributors": [
|
|
@@ -27,54 +27,60 @@
|
|
|
27
27
|
"elevation",
|
|
28
28
|
"topography"
|
|
29
29
|
],
|
|
30
|
-
"
|
|
31
|
-
"
|
|
30
|
+
"type": "module",
|
|
31
|
+
"main": "dist/cjs/index.cjs",
|
|
32
|
+
"module": "dist/esm/index.js",
|
|
33
|
+
"types": "dist/esm/index.d.ts",
|
|
32
34
|
"exports": {
|
|
33
35
|
"./package.json": "./package.json",
|
|
34
36
|
".": {
|
|
35
|
-
"
|
|
36
|
-
|
|
37
|
-
|
|
37
|
+
"import": {
|
|
38
|
+
"types": "./dist/esm/index.d.ts",
|
|
39
|
+
"default": "./dist/esm/index.js"
|
|
40
|
+
},
|
|
41
|
+
"require": {
|
|
42
|
+
"types": "./dist/cjs/index.d.cts",
|
|
43
|
+
"default": "./dist/cjs/index.cjs"
|
|
44
|
+
}
|
|
38
45
|
}
|
|
39
46
|
},
|
|
40
|
-
"types": "dist/js/index.d.ts",
|
|
41
47
|
"sideEffects": false,
|
|
42
48
|
"files": [
|
|
43
49
|
"dist"
|
|
44
50
|
],
|
|
45
51
|
"scripts": {
|
|
46
|
-
"bench": "tsx bench.
|
|
47
|
-
"build": "
|
|
48
|
-
"
|
|
49
|
-
"
|
|
50
|
-
"
|
|
51
|
-
"test": "
|
|
52
|
-
"test:tape": "tsx test.js",
|
|
53
|
-
"test:types": "tsc --esModuleInterop --noEmit --strict types.ts"
|
|
52
|
+
"bench": "tsx bench.ts",
|
|
53
|
+
"build": "tsup --config ../../tsup.config.ts",
|
|
54
|
+
"docs": "tsx ../../scripts/generate-readmes.ts",
|
|
55
|
+
"test": "npm-run-all --npm-path npm test:*",
|
|
56
|
+
"test:tape": "tsx test.ts",
|
|
57
|
+
"test:types": "tsc --esModuleInterop --module node16 --moduleResolution node16 --noEmit --strict types.ts"
|
|
54
58
|
},
|
|
55
59
|
"devDependencies": {
|
|
56
|
-
"@turf/envelope": "^7.0.0
|
|
57
|
-
"@turf/point-grid": "^7.0.0
|
|
58
|
-
"@turf/random": "^7.0.0
|
|
59
|
-
"@turf/rhumb-destination": "^7.0.0
|
|
60
|
-
"@turf/truncate": "^7.0.0
|
|
61
|
-
"benchmark": "
|
|
62
|
-
"
|
|
63
|
-
"
|
|
64
|
-
"
|
|
65
|
-
"
|
|
66
|
-
"
|
|
67
|
-
"
|
|
68
|
-
"
|
|
69
|
-
"
|
|
70
|
-
"
|
|
60
|
+
"@turf/envelope": "^7.0.0",
|
|
61
|
+
"@turf/point-grid": "^7.0.0",
|
|
62
|
+
"@turf/random": "^7.0.0",
|
|
63
|
+
"@turf/rhumb-destination": "^7.0.0",
|
|
64
|
+
"@turf/truncate": "^7.0.0",
|
|
65
|
+
"@types/benchmark": "^2.1.5",
|
|
66
|
+
"@types/tape": "^4.2.32",
|
|
67
|
+
"benchmark": "^2.1.4",
|
|
68
|
+
"load-json-file": "^7.0.1",
|
|
69
|
+
"matrix-to-grid": "^4.0.0",
|
|
70
|
+
"npm-run-all": "^4.1.5",
|
|
71
|
+
"tape": "^5.7.2",
|
|
72
|
+
"tsup": "^8.0.1",
|
|
73
|
+
"tsx": "^4.6.2",
|
|
74
|
+
"typescript": "^5.2.2",
|
|
75
|
+
"write-json-file": "^5.0.0"
|
|
71
76
|
},
|
|
72
77
|
"dependencies": {
|
|
73
|
-
"@turf/bbox": "^7.0.0
|
|
74
|
-
"@turf/helpers": "^7.0.0
|
|
75
|
-
"@turf/invariant": "^7.0.0
|
|
76
|
-
"@turf/meta": "^7.0.0
|
|
77
|
-
"
|
|
78
|
+
"@turf/bbox": "^7.0.0",
|
|
79
|
+
"@turf/helpers": "^7.0.0",
|
|
80
|
+
"@turf/invariant": "^7.0.0",
|
|
81
|
+
"@turf/meta": "^7.0.0",
|
|
82
|
+
"marchingsquares": "^1.3.3",
|
|
83
|
+
"tslib": "^2.6.2"
|
|
78
84
|
},
|
|
79
|
-
"gitHead": "
|
|
85
|
+
"gitHead": "3d3a7917025fbabe191dbddbc89754b86f9c7739"
|
|
80
86
|
}
|