Socket
Socket
Sign inDemoInstall

turf-flip

Package Overview
Dependencies
Maintainers
9
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

turf-flip - npm Package Compare versions

Comparing version 1.0.3 to 3.0.0-canary.2f5f7167

8

bench.js
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);

@@ -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

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc