Comparing version 1.0.3 to 3.0.0-canary.2f5f7167
var flip = require('./'); | ||
var Benchmark = require('benchmark'); | ||
var fs = require('fs'); | ||
var point = require('turf-point'); | ||
var linestring = require('turf-linestring'); | ||
var polygon = require('turf-polygon'); | ||
var featurecollection = require('turf-featurecollection'); | ||
var point = require('turf-helpers').point; | ||
var linestring = require('turf-helpers').lineString; | ||
var polygon = require('turf-helpers').polygon; | ||
var featurecollection = require('turf-helpers').featureCollection; | ||
@@ -9,0 +9,0 @@ var pt = point(1,0); |
75
index.js
@@ -0,9 +1,11 @@ | ||
var coordEach = require('turf-meta').coordEach; | ||
/** | ||
* Takes a {@link GeoJSON} object of any type and flips all of its coordinates | ||
* Takes input features and flips all of their coordinates | ||
* from `[x, y]` to `[y, x]`. | ||
* | ||
* @module turf/flip | ||
* @name flip | ||
* @category misc | ||
* @param {GeoJSON} input input GeoJSON object | ||
* @returns {GeoJSON} a GeoJSON object of the same type as `input` with flipped coordinates | ||
* @param {(Feature|FeatureCollection)} input input features | ||
* @returns {(Feature|FeatureCollection)} a feature or set of features of the same type as `input` with flipped coordinates | ||
* @example | ||
@@ -25,63 +27,12 @@ * var serbia = { | ||
*/ | ||
module.exports = flipAny; | ||
function flipAny(_) { | ||
module.exports = function flip(input) { | ||
// ensure that we don't modify features in-place and changes to the | ||
// output do not change the previous feature, including changes to nested | ||
// properties. | ||
var input = JSON.parse(JSON.stringify(_)); | ||
switch (input.type) { | ||
case 'FeatureCollection': | ||
for (var i = 0; i < input.features.length; i++) | ||
flipGeometry(input.features[i].geometry); | ||
return input; | ||
case 'Feature': | ||
flipGeometry(input.geometry); | ||
return input; | ||
default: | ||
flipGeometry(input); | ||
return input; | ||
} | ||
} | ||
input = JSON.parse(JSON.stringify(input)); | ||
function flipGeometry(geometry) { | ||
var coords = geometry.coordinates; | ||
switch(geometry.type) { | ||
case 'Point': | ||
flip0(coords); | ||
break; | ||
case 'LineString': | ||
case 'MultiPoint': | ||
flip1(coords); | ||
break; | ||
case 'Polygon': | ||
case 'MultiLineString': | ||
flip2(coords); | ||
break; | ||
case 'MultiPolygon': | ||
flip3(coords); | ||
break; | ||
case 'GeometryCollection': | ||
geometry.geometries.forEach(flipGeometry); | ||
break; | ||
} | ||
} | ||
function flip0(coord) { | ||
coord.reverse(); | ||
} | ||
function flip1(coords) { | ||
for(var i = 0; i < coords.length; i++) coords[i].reverse(); | ||
} | ||
function flip2(coords) { | ||
for(var i = 0; i < coords.length; i++) | ||
for(var j = 0; j < coords[i].length; j++) coords[i][j].reverse(); | ||
} | ||
function flip3(coords) { | ||
for(var i = 0; i < coords.length; i++) | ||
for(var j = 0; j < coords[i].length; j++) | ||
for(var k = 0; k < coords[i][j].length; k++) coords[i][j][k].reverse(); | ||
} | ||
coordEach(input, function (coord) { | ||
coord.reverse(); | ||
}); | ||
return input; | ||
}; |
{ | ||
"name": "turf-flip", | ||
"version": "1.0.3", | ||
"version": "3.0.0-canary.2f5f7167", | ||
"description": "turf flip module", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "tape test.js", | ||
"doc": "dox -r < index.js | doxme --readme > README.md" | ||
"test": "tape test.js" | ||
}, | ||
@@ -29,9 +28,7 @@ "repository": { | ||
"tape": "^3.5.0", | ||
"turf-featurecollection": "^1.0.0", | ||
"turf-linestring": "^1.0.1", | ||
"turf-point": "^2.0.0", | ||
"turf-polygon": "^1.0.2", | ||
"dox": "^0.6.1", | ||
"doxme": "^1.4.3" | ||
"turf-helpers": "^3.0.0-canary.2f5f7167" | ||
}, | ||
"dependencies": { | ||
"turf-meta": "^3.0.0-canary.2f5f7167" | ||
} | ||
} |
@@ -10,3 +10,3 @@ # turf-flip | ||
Takes a GeoJSON object of any type and flips all of its coordinates | ||
Takes input features and flips all of their coordinates | ||
from `[x, y]` to `[y, x]`. | ||
@@ -17,5 +17,5 @@ | ||
| parameter | type | description | | ||
| --------- | ------- | -------------------- | | ||
| `input` | GeoJSON | input GeoJSON object | | ||
| parameter | type | description | | ||
| --------- | -------------------------- | -------------- | | ||
| `input` | Feature\,FeatureCollection | input features | | ||
@@ -43,3 +43,3 @@ | ||
**Returns** `GeoJSON`, a GeoJSON object of the same type as `input` with flipped coordinates | ||
**Returns** `Feature,FeatureCollection`, a feature or set of features of the same type as `input` with flipped coordinates | ||
@@ -60,1 +60,2 @@ ## Installation | ||
24
test.js
var test = require('tape'); | ||
var flip = require('./'); | ||
var point = require('turf-point'); | ||
var linestring = require('turf-linestring'); | ||
var polygon = require('turf-polygon'); | ||
var featurecollection = require('turf-featurecollection'); | ||
var point = require('turf-helpers').point; | ||
var linestring = require('turf-helpers').lineString; | ||
var polygon = require('turf-helpers').polygon; | ||
var featurecollection = require('turf-helpers').featureCollection; | ||
test('flip', function(t) { | ||
test('flip', function (t) { | ||
// Point Geometry | ||
var pt = point([1,0]); | ||
var pt = point([1, 0]); | ||
var flippedPt = flip(pt.geometry); | ||
@@ -19,3 +19,3 @@ t.equal(flippedPt.coordinates[0], 0); | ||
// Point | ||
var pt2 = point([1,0]); | ||
var pt2 = point([1, 0]); | ||
var flippedPt2 = flip(pt2); | ||
@@ -28,3 +28,3 @@ | ||
// Line | ||
var line = linestring([[1,0], [1,0]]); | ||
var line = linestring([[1, 0], [1, 0]]); | ||
var flippedLine = flip(line); | ||
@@ -39,3 +39,3 @@ | ||
// Polygon | ||
var poly = polygon([[[1,0], [1,0], [1,2],[1,0]], [[.2,.2], [.3,.3],[.1,.2], [1,0],[.2,.2]]]); | ||
var poly = polygon([[[1, 0], [1, 0], [1, 2], [1, 0]], [[.2, .2], [.3, .3], [.1, .2], [1, 0], [.2, .2]]]); | ||
var flippedPoly = flip(poly); | ||
@@ -54,5 +54,5 @@ | ||
// FeatureCollection | ||
var pt1 = point([1,0]); | ||
var pt2 = point([1,0]); | ||
var fc = featurecollection([pt1, pt2]); | ||
var pt3 = point([1, 0]); | ||
var pt4 = point([1, 0]); | ||
var fc = featurecollection([pt3, pt4]); | ||
var flippedFC = flip(fc); | ||
@@ -59,0 +59,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
3
0
58
7006
1
7
119
1
1
+ Addedturf-meta@3.0.12(transitive)