grid-to-matrix
Advanced tools
Comparing version 1.1.0 to 1.2.0
@@ -818,2 +818,3 @@ (function (global, factory) { | ||
* @param {string} [property='elevation'] the property name in `points` from which z-values will be pulled | ||
* @param {boolean} [flip=false] returns the matrix upside-down | ||
* @returns {Array<Array<number>>} matrix of property values | ||
@@ -841,3 +842,3 @@ * @example | ||
*/ | ||
var index = function (grid, property) { | ||
var index = function (grid, property, flip) { | ||
// validation | ||
@@ -847,3 +848,3 @@ index$2.collectionOf(grid, 'Point', 'input must contain Points'); | ||
var pointsMatrix = sortPointsByLatLng(grid); | ||
var pointsMatrix = sortPointsByLatLng(grid, flip); | ||
@@ -877,5 +878,6 @@ var matrix = []; | ||
* @param {FeatureCollection<Point>} points GeoJSON Point features | ||
* @param {boolean} [flip=false] returns the matrix upside-down | ||
* @returns {Array<Array<Point>>} points by latitude and longitude | ||
*/ | ||
function sortPointsByLatLng(points) { | ||
function sortPointsByLatLng(points, flip) { | ||
var pointsByLatitude = {}; | ||
@@ -901,3 +903,7 @@ | ||
var pointMatrix = orderedRowsByLatitude.sort(function (a, b) { | ||
return getCoords(b[0])[1] - getCoords(a[0])[1]; | ||
if (flip) { | ||
return getCoords(a[0])[1] - getCoords(b[0])[1]; | ||
} else { | ||
return getCoords(b[0])[1] - getCoords(a[0])[1]; | ||
} | ||
}); | ||
@@ -904,0 +910,0 @@ return pointMatrix; |
@@ -1,1 +0,1 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e.gridToMatrix=t()}(this,function(){function e(e){if(!e)throw new Error("No obj passed");var r=t(e);if(r.length>1&&"number"==typeof r[0]&&"number"==typeof r[1])return r;throw new Error("Coordinate is not a valid Point")}function t(e){if(!e)throw new Error("No obj passed");var t;if(e.length?t=e:e.coordinates?t=e.coordinates:e.geometry&&e.geometry.coordinates&&(t=e.geometry.coordinates),t)return r(t),t;throw new Error("No valid coordinates")}function r(e){if(e.length>1&&"number"==typeof e[0]&&"number"==typeof e[1])return!0;if(Array.isArray(e[0])&&e[0].length)return r(e[0]);throw new Error("coordinates must only contain numbers")}function o(e,t,r){if(!t||!r)throw new Error("type and name required");if(!e||e.type!==t)throw new Error("Invalid input to "+r+": must be a "+t+", given "+e.type)}function n(e,t,r){if(!e)throw new Error("No feature passed");if(!r)throw new Error(".featureOf() requires a name");if(!e||"Feature"!==e.type||!e.geometry)throw new Error("Invalid input to "+r+", Feature with geometry required");if(!e.geometry||e.geometry.type!==t)throw new Error("Invalid input to "+r+": must be a "+t+", given "+e.geometry.type)}function i(e,t,r){if(!e)throw new Error("No featureCollection passed");if(!r)throw new Error(".collectionOf() requires a name");if(!e||"FeatureCollection"!==e.type)throw new Error("Invalid input to "+r+", FeatureCollection required");for(var o=0;o<e.features.length;o++){var n=e.features[o];if(!n||"Feature"!==n.type||!n.geometry)throw new Error("Invalid input to "+r+", Feature with geometry required");if(!n.geometry||n.geometry.type!==t)throw new Error("Invalid input to "+r+": must be a "+t+", given "+n.geometry.type)}}function u(e,t,r){var o,n,i,f,a,l,y,p,c,s,g=0,h=0,m="FeatureCollection"===e.type,d="Feature"===e.type,w=m?e.features.length:1;for(o=0;w>o;o++)for(c=m?e.features[o].geometry:d?e.geometry:e,s="GeometryCollection"===c.type,y=s?c.geometries.length:1,f=0;y>f;f++)if(l=s?c.geometries[f]:c,p=l.coordinates,g=!r||"Polygon"!==l.type&&"MultiPolygon"!==l.type?0:1,"Point"===l.type)t(p,h),h++;else if("LineString"===l.type||"MultiPoint"===l.type)for(n=0;n<p.length;n++)t(p[n],h),h++;else if("Polygon"===l.type||"MultiLineString"===l.type)for(n=0;n<p.length;n++)for(i=0;i<p[n].length-g;i++)t(p[n][i],h),h++;else if("MultiPolygon"===l.type)for(n=0;n<p.length;n++)for(i=0;i<p[n].length;i++)for(a=0;a<p[n][i].length-g;a++)t(p[n][i][a],h),h++;else{if("GeometryCollection"!==l.type)throw new Error("Unknown Geometry Type");for(n=0;n<l.geometries.length;n++)u(l.geometries[n],t,r)}}function f(e,t,r,o){var n=r;return u(e,function(e,o){n=0===o&&void 0===r?e:t(n,e,o)},o),n}function a(e,t){var r;switch(e.type){case"FeatureCollection":for(r=0;r<e.features.length;r++)t(e.features[r].properties,r);break;case"Feature":t(e.properties,0)}}function l(e,t,r){var o=r;return a(e,function(e,n){o=0===n&&void 0===r?e:t(o,e,n)}),o}function y(e,t){if("Feature"===e.type)t(e,0);else if("FeatureCollection"===e.type)for(var r=0;r<e.features.length;r++)t(e.features[r],r)}function p(e,t,r){var o=r;return y(e,function(e,n){o=0===n&&void 0===r?e:t(o,e,n)}),o}function c(e){var t=[];return u(e,function(e){t.push(e)}),t}function s(e,t){var r,o,n,i,u,f,a,l=0,y="FeatureCollection"===e.type,p="Feature"===e.type,c=y?e.features.length:1;for(r=0;c>r;r++)for(f=y?e.features[r].geometry:p?e.geometry:e,a="GeometryCollection"===f.type,u=a?f.geometries.length:1,n=0;u>n;n++)if(i=a?f.geometries[n]:f,"Point"===i.type||"LineString"===i.type||"MultiPoint"===i.type||"Polygon"===i.type||"MultiLineString"===i.type||"MultiPolygon"===i.type)t(i,l),l++;else{if("GeometryCollection"!==i.type)throw new Error("Unknown Geometry Type");for(o=0;o<i.geometries.length;o++)t(i.geometries[o],l),l++}}function g(e,t,r){var o=r;return s(e,function(e,n){o=0===n&&void 0===r?e:t(o,e,n)}),o}function h(e){var t={};v(e,function(e){var r=w(e)[1];t[r]||(t[r]=[]),t[r].push(e)});var r=Object.keys(t).map(function(e){var r=t[e],o=r.sort(function(e,t){return w(e)[0]-w(t)[0]});return o}),o=r.sort(function(e,t){return w(t[0])[1]-w(e[0])[1]});return o}var m={geojsonType:o,collectionOf:i,featureOf:n,getCoord:e,getCoords:t,containsNumber:r},d={coordEach:u,coordReduce:f,propEach:a,propReduce:l,featureEach:y,featureReduce:p,coordAll:c,geomEach:s,geomReduce:g},w=m.getCoords,v=d.featureEach,E=function(e,t){m.collectionOf(e,"Point","input must contain Points"),t=t||"elevation";for(var r=h(e),o=[],n=0;n<r.length;n++){for(var i=r[n],u=[],f=0;f<i.length;f++){var a=i[f];u.push(a.properties[t]?a.properties[t]:0)}o.push(u)}return o};return E}); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e.gridToMatrix=t()}(this,function(){function e(e){if(!e)throw new Error("No obj passed");var r=t(e);if(r.length>1&&"number"==typeof r[0]&&"number"==typeof r[1])return r;throw new Error("Coordinate is not a valid Point")}function t(e){if(!e)throw new Error("No obj passed");var t;if(e.length?t=e:e.coordinates?t=e.coordinates:e.geometry&&e.geometry.coordinates&&(t=e.geometry.coordinates),t)return r(t),t;throw new Error("No valid coordinates")}function r(e){if(e.length>1&&"number"==typeof e[0]&&"number"==typeof e[1])return!0;if(Array.isArray(e[0])&&e[0].length)return r(e[0]);throw new Error("coordinates must only contain numbers")}function o(e,t,r){if(!t||!r)throw new Error("type and name required");if(!e||e.type!==t)throw new Error("Invalid input to "+r+": must be a "+t+", given "+e.type)}function n(e,t,r){if(!e)throw new Error("No feature passed");if(!r)throw new Error(".featureOf() requires a name");if(!e||"Feature"!==e.type||!e.geometry)throw new Error("Invalid input to "+r+", Feature with geometry required");if(!e.geometry||e.geometry.type!==t)throw new Error("Invalid input to "+r+": must be a "+t+", given "+e.geometry.type)}function i(e,t,r){if(!e)throw new Error("No featureCollection passed");if(!r)throw new Error(".collectionOf() requires a name");if(!e||"FeatureCollection"!==e.type)throw new Error("Invalid input to "+r+", FeatureCollection required");for(var o=0;o<e.features.length;o++){var n=e.features[o];if(!n||"Feature"!==n.type||!n.geometry)throw new Error("Invalid input to "+r+", Feature with geometry required");if(!n.geometry||n.geometry.type!==t)throw new Error("Invalid input to "+r+": must be a "+t+", given "+n.geometry.type)}}function u(e,t,r){var o,n,i,f,a,l,y,p,c,s,g=0,h=0,m="FeatureCollection"===e.type,d="Feature"===e.type,w=m?e.features.length:1;for(o=0;w>o;o++)for(c=m?e.features[o].geometry:d?e.geometry:e,s="GeometryCollection"===c.type,y=s?c.geometries.length:1,f=0;y>f;f++)if(l=s?c.geometries[f]:c,p=l.coordinates,g=!r||"Polygon"!==l.type&&"MultiPolygon"!==l.type?0:1,"Point"===l.type)t(p,h),h++;else if("LineString"===l.type||"MultiPoint"===l.type)for(n=0;n<p.length;n++)t(p[n],h),h++;else if("Polygon"===l.type||"MultiLineString"===l.type)for(n=0;n<p.length;n++)for(i=0;i<p[n].length-g;i++)t(p[n][i],h),h++;else if("MultiPolygon"===l.type)for(n=0;n<p.length;n++)for(i=0;i<p[n].length;i++)for(a=0;a<p[n][i].length-g;a++)t(p[n][i][a],h),h++;else{if("GeometryCollection"!==l.type)throw new Error("Unknown Geometry Type");for(n=0;n<l.geometries.length;n++)u(l.geometries[n],t,r)}}function f(e,t,r,o){var n=r;return u(e,function(e,o){n=0===o&&void 0===r?e:t(n,e,o)},o),n}function a(e,t){var r;switch(e.type){case"FeatureCollection":for(r=0;r<e.features.length;r++)t(e.features[r].properties,r);break;case"Feature":t(e.properties,0)}}function l(e,t,r){var o=r;return a(e,function(e,n){o=0===n&&void 0===r?e:t(o,e,n)}),o}function y(e,t){if("Feature"===e.type)t(e,0);else if("FeatureCollection"===e.type)for(var r=0;r<e.features.length;r++)t(e.features[r],r)}function p(e,t,r){var o=r;return y(e,function(e,n){o=0===n&&void 0===r?e:t(o,e,n)}),o}function c(e){var t=[];return u(e,function(e){t.push(e)}),t}function s(e,t){var r,o,n,i,u,f,a,l=0,y="FeatureCollection"===e.type,p="Feature"===e.type,c=y?e.features.length:1;for(r=0;c>r;r++)for(f=y?e.features[r].geometry:p?e.geometry:e,a="GeometryCollection"===f.type,u=a?f.geometries.length:1,n=0;u>n;n++)if(i=a?f.geometries[n]:f,"Point"===i.type||"LineString"===i.type||"MultiPoint"===i.type||"Polygon"===i.type||"MultiLineString"===i.type||"MultiPolygon"===i.type)t(i,l),l++;else{if("GeometryCollection"!==i.type)throw new Error("Unknown Geometry Type");for(o=0;o<i.geometries.length;o++)t(i.geometries[o],l),l++}}function g(e,t,r){var o=r;return s(e,function(e,n){o=0===n&&void 0===r?e:t(o,e,n)}),o}function h(e,t){var r={};v(e,function(e){var t=w(e)[1];r[t]||(r[t]=[]),r[t].push(e)});var o=Object.keys(r).map(function(e){var t=r[e],o=t.sort(function(e,t){return w(e)[0]-w(t)[0]});return o}),n=o.sort(function(e,r){return t?w(e[0])[1]-w(r[0])[1]:w(r[0])[1]-w(e[0])[1]});return n}var m={geojsonType:o,collectionOf:i,featureOf:n,getCoord:e,getCoords:t,containsNumber:r},d={coordEach:u,coordReduce:f,propEach:a,propReduce:l,featureEach:y,featureReduce:p,coordAll:c,geomEach:s,geomReduce:g},w=m.getCoords,v=d.featureEach,E=function(e,t,r){m.collectionOf(e,"Point","input must contain Points"),t=t||"elevation";for(var o=h(e,r),n=[],i=0;i<o.length;i++){for(var u=o[i],f=[],a=0;a<u.length;a++){var l=u[a];f.push(l.properties[t]?l.properties[t]:0)}n.push(f)}return n};return E}); |
14
index.js
@@ -12,2 +12,3 @@ var invariant = require('@turf/invariant'); | ||
* @param {string} [property='elevation'] the property name in `points` from which z-values will be pulled | ||
* @param {boolean} [flip=false] returns the matrix upside-down | ||
* @returns {Array<Array<number>>} matrix of property values | ||
@@ -35,3 +36,3 @@ * @example | ||
*/ | ||
module.exports = function (grid, property) { | ||
module.exports = function (grid, property, flip) { | ||
// validation | ||
@@ -41,3 +42,3 @@ invariant.collectionOf(grid, 'Point', 'input must contain Points'); | ||
var pointsMatrix = sortPointsByLatLng(grid); | ||
var pointsMatrix = sortPointsByLatLng(grid, flip); | ||
@@ -71,5 +72,6 @@ var matrix = []; | ||
* @param {FeatureCollection<Point>} points GeoJSON Point features | ||
* @param {boolean} [flip=false] returns the matrix upside-down | ||
* @returns {Array<Array<Point>>} points by latitude and longitude | ||
*/ | ||
function sortPointsByLatLng(points) { | ||
function sortPointsByLatLng(points, flip) { | ||
var pointsByLatitude = {}; | ||
@@ -95,5 +97,9 @@ | ||
var pointMatrix = orderedRowsByLatitude.sort(function (a, b) { | ||
return getCoords(b[0])[1] - getCoords(a[0])[1]; | ||
if (flip) { | ||
return getCoords(a[0])[1] - getCoords(b[0])[1]; | ||
} else { | ||
return getCoords(b[0])[1] - getCoords(a[0])[1]; | ||
} | ||
}); | ||
return pointMatrix; | ||
} |
{ | ||
"name": "grid-to-matrix", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "Takes a GeoJSON pointGrid and returns a correspondent matrix array of 'property' values of the points", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -14,2 +14,4 @@ # grid-to-matrix | ||
- `flip` \[**[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)**] returns the matrix upside-down (optional, default `false`) | ||
**Returns** | ||
@@ -27,2 +29,8 @@ | ||
**browser (ES5)** | ||
```html | ||
<script src="https://unpkg.com/grid-to-matrix@1.1.0/dist/grid-to-matrix.min.js"></script> | ||
``` | ||
### Quickstart | ||
@@ -29,0 +37,0 @@ |
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
44041
953
61
0