@turf/isobands 7.0.0-alpha.1 → 7.0.0-alpha.110

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@turf/isobands",
3
- "version": "7.0.0-alpha.1",
3
+ "version": "7.0.0-alpha.110+1411d63a7",
4
4
  "description": "turf isobands module",
5
5
  "author": "Turf Authors",
6
6
  "contributors": [
@@ -28,57 +28,62 @@
28
28
  "topography",
29
29
  "filled"
30
30
  ],
31
- "main": "dist/js/index.js",
32
- "module": "dist/es/index.js",
31
+ "type": "commonjs",
32
+ "main": "dist/cjs/index.cjs",
33
+ "module": "dist/esm/index.mjs",
34
+ "types": "dist/cjs/index.d.ts",
33
35
  "exports": {
34
36
  "./package.json": "./package.json",
35
37
  ".": {
36
- "types": "./dist/js/index.d.ts",
37
- "import": "./dist/es/index.js",
38
- "require": "./dist/js/index.js"
38
+ "import": {
39
+ "types": "./dist/esm/index.d.mts",
40
+ "default": "./dist/esm/index.mjs"
41
+ },
42
+ "require": {
43
+ "types": "./dist/cjs/index.d.ts",
44
+ "default": "./dist/cjs/index.cjs"
45
+ }
39
46
  }
40
47
  },
41
- "types": "dist/js/index.d.ts",
42
48
  "sideEffects": false,
43
49
  "files": [
44
50
  "dist"
45
51
  ],
46
52
  "scripts": {
47
- "bench": "tsx bench.js",
48
- "build": "npm-run-all build:*",
49
- "build:es": "tsc --outDir dist/es --module esnext --declaration false && echo '{\"type\":\"module\"}' > dist/es/package.json",
50
- "build:js": "tsc",
51
- "docs": "tsx ../../scripts/generate-readmes",
52
- "test": "npm-run-all test:*",
53
- "test:tape": "tsx test.js"
53
+ "bench": "tsx bench.ts",
54
+ "build": "tsup --config ../../tsup.config.ts",
55
+ "docs": "tsx ../../scripts/generate-readmes.ts",
56
+ "test": "npm-run-all --npm-path npm test:*",
57
+ "test:tape": "tsx test.ts"
54
58
  },
55
59
  "devDependencies": {
56
- "@turf/envelope": "^7.0.0-alpha.1",
57
- "@turf/point-grid": "^7.0.0-alpha.1",
58
- "@turf/random": "^7.0.0-alpha.1",
59
- "@turf/rhumb-destination": "^7.0.0-alpha.1",
60
- "@turf/truncate": "^7.0.0-alpha.1",
61
- "benchmark": "*",
62
- "chroma-js": "*",
63
- "load-json-file": "*",
64
- "matrix-to-grid": "*",
65
- "npm-run-all": "*",
66
- "rollup": "*",
67
- "tape": "*",
68
- "tslint": "*",
69
- "tsx": "*",
70
- "typescript": "*",
71
- "write-json-file": "*"
60
+ "@turf/envelope": "^7.0.0-alpha.110+1411d63a7",
61
+ "@turf/point-grid": "^7.0.0-alpha.110+1411d63a7",
62
+ "@turf/random": "^7.0.0-alpha.110+1411d63a7",
63
+ "@turf/rhumb-destination": "^7.0.0-alpha.110+1411d63a7",
64
+ "@turf/truncate": "^7.0.0-alpha.110+1411d63a7",
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"
72
76
  },
73
77
  "dependencies": {
74
- "@turf/area": "^7.0.0-alpha.1",
75
- "@turf/bbox": "^7.0.0-alpha.1",
76
- "@turf/boolean-point-in-polygon": "^7.0.0-alpha.1",
77
- "@turf/explode": "^7.0.0-alpha.1",
78
- "@turf/helpers": "^7.0.0-alpha.1",
79
- "@turf/invariant": "^7.0.0-alpha.1",
80
- "@turf/meta": "^7.0.0-alpha.1",
81
- "tslib": "^2.3.0"
78
+ "@turf/area": "^7.0.0-alpha.110+1411d63a7",
79
+ "@turf/bbox": "^7.0.0-alpha.110+1411d63a7",
80
+ "@turf/boolean-point-in-polygon": "^7.0.0-alpha.110+1411d63a7",
81
+ "@turf/explode": "^7.0.0-alpha.110+1411d63a7",
82
+ "@turf/helpers": "^7.0.0-alpha.110+1411d63a7",
83
+ "@turf/invariant": "^7.0.0-alpha.110+1411d63a7",
84
+ "@turf/meta": "^7.0.0-alpha.110+1411d63a7",
85
+ "marchingsquares": "^1.3.3",
86
+ "tslib": "^2.6.2"
82
87
  },
83
- "gitHead": "cf7a0c507b017ca066acffd0ce23bda5b393fb5a"
88
+ "gitHead": "1411d63a74c275c9216fe48e9d3cb2d48a359068"
84
89
  }
package/dist/es/index.js DELETED
@@ -1,229 +0,0 @@
1
- import bbox from "@turf/bbox";
2
- import area from "@turf/area";
3
- import booleanPointInPolygon from "@turf/boolean-point-in-polygon";
4
- import explode from "@turf/explode";
5
- import { collectionOf } from "@turf/invariant";
6
- import { polygon, multiPolygon, featureCollection, isObject, } from "@turf/helpers";
7
- import gridToMatrix from "./lib/grid-to-matrix.js";
8
- import isoBands from "./lib/marchingsquares-isobands.js";
9
- /**
10
- * Takes a square or rectangular grid {@link FeatureCollection} of {@link Point} features with z-values and an array of
11
- * value breaks and generates filled contour isobands.
12
- *
13
- * @name isobands
14
- * @param {FeatureCollection<Point>} pointGrid input points - must be square or rectangular
15
- * @param {Array<number>} breaks where to draw contours
16
- * @param {Object} [options={}] options on output
17
- * @param {string} [options.zProperty='elevation'] the property name in `points` from which z-values will be pulled
18
- * @param {Object} [options.commonProperties={}] GeoJSON properties passed to ALL isobands
19
- * @param {Array<Object>} [options.breaksProperties=[]] GeoJSON properties passed, in order, to the correspondent isoband (order defined by breaks)
20
- * @returns {FeatureCollection<MultiPolygon>} a FeatureCollection of {@link MultiPolygon} features representing isobands
21
- */
22
- function isobands(pointGrid, breaks, options) {
23
- // Optional parameters
24
- options = options || {};
25
- if (!isObject(options))
26
- throw new Error("options is invalid");
27
- const zProperty = options.zProperty || "elevation";
28
- const commonProperties = options.commonProperties || {};
29
- const breaksProperties = options.breaksProperties || [];
30
- // Validation
31
- collectionOf(pointGrid, "Point", "Input must contain Points");
32
- if (!breaks)
33
- throw new Error("breaks is required");
34
- if (!Array.isArray(breaks))
35
- throw new Error("breaks is not an Array");
36
- if (!isObject(commonProperties))
37
- throw new Error("commonProperties is not an Object");
38
- if (!Array.isArray(breaksProperties))
39
- throw new Error("breaksProperties is not an Array");
40
- // Isoband methods
41
- const matrix = gridToMatrix(pointGrid, { zProperty: zProperty, flip: true });
42
- let contours = createContourLines(matrix, breaks, zProperty);
43
- contours = rescaleContours(contours, matrix, pointGrid);
44
- const multipolygons = contours.map((contour, index) => {
45
- if (breaksProperties[index] && !isObject(breaksProperties[index])) {
46
- throw new Error("Each mappedProperty is required to be an Object");
47
- }
48
- // collect all properties
49
- const contourProperties = Object.assign(Object.assign({}, commonProperties), breaksProperties[index]);
50
- contourProperties[zProperty] = contour[zProperty];
51
- const multiP = multiPolygon(contour.groupedRings, contourProperties);
52
- return multiP;
53
- });
54
- return featureCollection(multipolygons);
55
- }
56
- /**
57
- * Creates the contours lines (featuresCollection of polygon features) from the 2D data grid
58
- *
59
- * Marchingsquares process the grid data as a 3D representation of a function on a 2D plane, therefore it
60
- * assumes the points (x-y coordinates) are one 'unit' distance. The result of the IsoBands function needs to be
61
- * rescaled, with turfjs, to the original area and proportions on the map
62
- *
63
- * @private
64
- * @param {Array<Array<number>>} matrix Grid Data
65
- * @param {Array<number>} breaks Breaks
66
- * @param {string} [property='elevation'] Property
67
- * @returns {Array<any>} contours
68
- */
69
- function createContourLines(matrix, breaks, property) {
70
- const contours = [];
71
- for (let i = 1; i < breaks.length; i++) {
72
- const lowerBand = +breaks[i - 1]; // make sure the breaks value is a number
73
- const upperBand = +breaks[i];
74
- const isobandsCoords = isoBands(matrix, lowerBand, upperBand - lowerBand);
75
- // as per GeoJson rules for creating a Polygon, make sure the first element
76
- // in the array of LinearRings represents the exterior ring (i.e. biggest area),
77
- // and any subsequent elements represent interior rings (i.e. smaller area);
78
- // this avoids rendering issues of the MultiPolygons on the map
79
- const nestedRings = orderByArea(isobandsCoords);
80
- const groupedRings = groupNestedRings(nestedRings);
81
- contours.push({
82
- groupedRings: groupedRings,
83
- [property]: lowerBand + "-" + upperBand,
84
- });
85
- }
86
- return contours;
87
- }
88
- /**
89
- * Transform isobands of 2D grid to polygons for the map
90
- *
91
- * @private
92
- * @param {Array<any>} contours Contours
93
- * @param {Array<Array<number>>} matrix Grid Data
94
- * @param {Object} points Points by Latitude
95
- * @returns {Array<any>} contours
96
- */
97
- function rescaleContours(contours, matrix, points) {
98
- // get dimensions (on the map) of the original grid
99
- const gridBbox = bbox(points); // [ minX, minY, maxX, maxY ]
100
- const originalWidth = gridBbox[2] - gridBbox[0];
101
- const originalHeigth = gridBbox[3] - gridBbox[1];
102
- // get origin, which is the first point of the last row on the rectangular data on the map
103
- const x0 = gridBbox[0];
104
- const y0 = gridBbox[1];
105
- // get number of cells per side
106
- const matrixWidth = matrix[0].length - 1;
107
- const matrixHeight = matrix.length - 1;
108
- // calculate the scaling factor between matrix and rectangular grid on the map
109
- const scaleX = originalWidth / matrixWidth;
110
- const scaleY = originalHeigth / matrixHeight;
111
- const resize = (point) => {
112
- point[0] = point[0] * scaleX + x0;
113
- point[1] = point[1] * scaleY + y0;
114
- };
115
- // resize and shift each point/line of the isobands
116
- contours.forEach(function (contour) {
117
- contour.groupedRings.forEach(function (lineRingSet) {
118
- lineRingSet.forEach(function (lineRing) {
119
- lineRing.forEach(resize);
120
- });
121
- });
122
- });
123
- return contours;
124
- }
125
- /* utility functions */
126
- /**
127
- * Returns an array of coordinates (of LinearRings) in descending order by area
128
- *
129
- * @private
130
- * @param {Array<LineString>} ringsCoords array of closed LineString
131
- * @returns {Array} array of the input LineString ordered by area
132
- */
133
- function orderByArea(ringsCoords) {
134
- const ringsWithArea = [];
135
- const areas = [];
136
- ringsCoords.forEach(function (coords) {
137
- // const poly = polygon([points]);
138
- const ringArea = area(polygon([coords]));
139
- // create an array of areas value
140
- areas.push(ringArea);
141
- // associate each lineRing with its area
142
- ringsWithArea.push({ ring: coords, area: ringArea });
143
- });
144
- areas.sort(function (a, b) {
145
- // bigger --> smaller
146
- return b - a;
147
- });
148
- // create a new array of linearRings coordinates ordered by their area
149
- const orderedByArea = [];
150
- areas.forEach(function (area) {
151
- for (let lr = 0; lr < ringsWithArea.length; lr++) {
152
- if (ringsWithArea[lr].area === area) {
153
- orderedByArea.push(ringsWithArea[lr].ring);
154
- ringsWithArea.splice(lr, 1);
155
- break;
156
- }
157
- }
158
- });
159
- return orderedByArea;
160
- }
161
- /**
162
- * Returns an array of arrays of coordinates, each representing
163
- * a set of (coordinates of) nested LinearRings,
164
- * i.e. the first ring contains all the others
165
- *
166
- * @private
167
- * @param {Array} orderedLinearRings array of coordinates (of LinearRings) in descending order by area
168
- * @returns {Array<Array>} Array of coordinates of nested LinearRings
169
- */
170
- function groupNestedRings(orderedLinearRings) {
171
- // create a list of the (coordinates of) LinearRings
172
- const lrList = orderedLinearRings.map((lr) => {
173
- return { lrCoordinates: lr, grouped: false };
174
- });
175
- const groupedLinearRingsCoords = [];
176
- while (!allGrouped(lrList)) {
177
- for (let i = 0; i < lrList.length; i++) {
178
- if (!lrList[i].grouped) {
179
- // create new group starting with the larger not already grouped ring
180
- const group = [];
181
- group.push(lrList[i].lrCoordinates);
182
- lrList[i].grouped = true;
183
- const outerMostPoly = polygon([lrList[i].lrCoordinates]);
184
- // group all the rings contained by the outermost ring
185
- for (let j = i + 1; j < lrList.length; j++) {
186
- if (!lrList[j].grouped) {
187
- const lrPoly = polygon([lrList[j].lrCoordinates]);
188
- if (isInside(lrPoly, outerMostPoly)) {
189
- group.push(lrList[j].lrCoordinates);
190
- lrList[j].grouped = true;
191
- }
192
- }
193
- }
194
- // insert the new group
195
- groupedLinearRingsCoords.push(group);
196
- }
197
- }
198
- }
199
- return groupedLinearRingsCoords;
200
- }
201
- /**
202
- * @private
203
- * @param {Polygon} testPolygon polygon of interest
204
- * @param {Polygon} targetPolygon polygon you want to compare with
205
- * @returns {boolean} true if test-Polygon is inside target-Polygon
206
- */
207
- function isInside(testPolygon, targetPolygon) {
208
- const points = explode(testPolygon);
209
- for (let i = 0; i < points.features.length; i++) {
210
- if (!booleanPointInPolygon(points.features[i], targetPolygon)) {
211
- return false;
212
- }
213
- }
214
- return true;
215
- }
216
- /**
217
- * @private
218
- * @param {Array<Object>} list list of objects which might contain the 'group' attribute
219
- * @returns {boolean} true if all the objects in the list are marked as grouped
220
- */
221
- function allGrouped(list) {
222
- for (let i = 0; i < list.length; i++) {
223
- if (list[i].grouped === false) {
224
- return false;
225
- }
226
- }
227
- return true;
228
- }
229
- export default isobands;
@@ -1,101 +0,0 @@
1
- import { getCoords, collectionOf } from "@turf/invariant";
2
- import { featureEach } from "@turf/meta";
3
- import { isObject } from "@turf/helpers";
4
- /**
5
- * Takes a {@link Point} grid and returns a correspondent matrix {Array<Array<number>>}
6
- * of the 'property' values
7
- *
8
- * @name gridToMatrix
9
- * @param {FeatureCollection<Point>} grid of points
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 {boolean} [options.flip=false] returns the matrix upside-down
13
- * @param {boolean} [options.flags=false] flags, adding a `matrixPosition` array field ([row, column]) to its properties,
14
- * the grid points with coordinates on the matrix
15
- * @returns {Array<Array<number>>} matrix of property values
16
- * @example
17
- * var extent = [-70.823364, -33.553984, -70.473175, -33.302986];
18
- * var cellSize = 3;
19
- * var grid = turf.pointGrid(extent, cellSize);
20
- * // add a random property to each point between 0 and 60
21
- * for (var i = 0; i < grid.features.length; i++) {
22
- * grid.features[i].properties.elevation = (Math.random() * 60);
23
- * }
24
- * gridToMatrix(grid);
25
- * //= [
26
- * [ 1, 13, 10, 9, 10, 13, 18],
27
- * [34, 8, 5, 4, 5, 8, 13],
28
- * [10, 5, 2, 1, 2, 5, 4],
29
- * [ 0, 4, 56, 19, 1, 4, 9],
30
- * [10, 5, 2, 1, 2, 5, 10],
31
- * [57, 8, 5, 4, 5, 0, 57],
32
- * [ 3, 13, 10, 9, 5, 13, 18],
33
- * [18, 13, 10, 9, 78, 13, 18]
34
- * ]
35
- */
36
- export default function gridToMatrix(grid, options) {
37
- // Optional parameters
38
- options = options || {};
39
- if (!isObject(options))
40
- throw new Error("options is invalid");
41
- var zProperty = options.zProperty || "elevation";
42
- var flip = options.flip;
43
- var flags = options.flags;
44
- // validation
45
- collectionOf(grid, "Point", "input must contain Points");
46
- var pointsMatrix = sortPointsByLatLng(grid, flip);
47
- var matrix = [];
48
- // create property matrix from sorted points
49
- // looping order matters here
50
- for (var r = 0; r < pointsMatrix.length; r++) {
51
- var pointRow = pointsMatrix[r];
52
- var row = [];
53
- for (var c = 0; c < pointRow.length; c++) {
54
- var point = pointRow[c];
55
- // Check if zProperty exist
56
- if (point.properties[zProperty])
57
- row.push(point.properties[zProperty]);
58
- else
59
- row.push(0);
60
- // add flags
61
- if (flags === true)
62
- point.properties.matrixPosition = [r, c];
63
- }
64
- matrix.push(row);
65
- }
66
- return matrix;
67
- }
68
- /**
69
- * Sorts points by latitude and longitude, creating a 2-dimensional array of points
70
- *
71
- * @private
72
- * @param {FeatureCollection<Point>} points GeoJSON Point features
73
- * @param {boolean} [flip=false] returns the matrix upside-down
74
- * @returns {Array<Array<Point>>} points ordered by latitude and longitude
75
- */
76
- function sortPointsByLatLng(points, flip) {
77
- var pointsByLatitude = {};
78
- // divide points by rows with the same latitude
79
- featureEach(points, function (point) {
80
- var lat = getCoords(point)[1];
81
- if (!pointsByLatitude[lat])
82
- pointsByLatitude[lat] = [];
83
- pointsByLatitude[lat].push(point);
84
- });
85
- // sort points (with the same latitude) by longitude
86
- var orderedRowsByLatitude = Object.keys(pointsByLatitude).map(function (lat) {
87
- var row = pointsByLatitude[lat];
88
- var rowOrderedByLongitude = row.sort(function (a, b) {
89
- return getCoords(a)[0] - getCoords(b)[0];
90
- });
91
- return rowOrderedByLongitude;
92
- });
93
- // sort rows (of points with the same latitude) by latitude
94
- var pointMatrix = orderedRowsByLatitude.sort(function (a, b) {
95
- if (flip)
96
- return getCoords(a[0])[1] - getCoords(b[0])[1];
97
- else
98
- return getCoords(b[0])[1] - getCoords(a[0])[1];
99
- });
100
- return pointMatrix;
101
- }