turf-flip
Advanced tools
Comparing version 1.0.1 to 1.0.2
124
index.js
@@ -1,58 +0,86 @@ | ||
module.exports = function(fc) { | ||
if(fc.type === 'Feature'){ | ||
switch(fc.geometry.type){ | ||
/** | ||
* Takes a {@link GeoJSON} object of any type and flips all of its coordinates | ||
* from `[x, y]` to `[y, x]`. | ||
* | ||
* @module turf/flip | ||
* @category misc | ||
* @param {GeoJSON} input input GeoJSON object | ||
* @returns {GeoJSON} a GeoJSON object of the same type as `input` with flipped coordinates | ||
* @example | ||
* var serbia = { | ||
* "type": "Feature", | ||
* "properties": {}, | ||
* "geometry": { | ||
* "type": "Point", | ||
* "coordinates": [20.566406, 43.421008] | ||
* } | ||
* }; | ||
* | ||
* //=serbia | ||
* | ||
* var saudiArabia = turf.flip(serbia); | ||
* | ||
* //=saudiArabia | ||
*/ | ||
module.exports = flipAny; | ||
function flipAny(_) { | ||
// 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; | ||
} | ||
} | ||
function flipGeometry(geometry) { | ||
var coords = geometry.coordinates; | ||
switch(geometry.type) { | ||
case 'Point': | ||
fc.geometry.coordinates = flipCoordinate(fc.geometry.coordinates); | ||
return fc; | ||
flip0(coords); | ||
break; | ||
case 'LineString': | ||
fc.geometry.coordinates.forEach(function(coordinates, i){ | ||
coordinates = flipCoordinate(coordinates); | ||
fc.geometry.coordinates[i] = coordinates; | ||
}); | ||
return fc; | ||
case 'MultiPoint': | ||
flip1(coords); | ||
break; | ||
case 'Polygon': | ||
fc.geometry.coordinates.forEach(function(ring, i){ | ||
ring.forEach(function(coordinates, k){ | ||
coordinates = flipCoordinate(coordinates); | ||
fc.geometry.coordinates[i][k] = coordinates; | ||
}); | ||
}); | ||
return fc; | ||
case 'MultiLineString': | ||
flip2(coords); | ||
break; | ||
case 'MultiPolygon': | ||
flip3(coords); | ||
break; | ||
case 'GeometryCollection': | ||
geometry.geometries.forEach(flipGeometry); | ||
break; | ||
} | ||
} | ||
else if(fc.type === 'FeatureCollection'){ | ||
fc.features.forEach(function(feature){ | ||
switch(feature.geometry.type){ | ||
case 'Point': | ||
feature.geometry.coordinates = flipCoordinate(feature.geometry.coordinates); | ||
break; | ||
case 'LineString': | ||
feature.geometry.coordinates.forEach(function(coordinates, i){ | ||
coordinates = flipCoordinate(coordinates); | ||
feature.geometry.coordinates[i] = coordinates; | ||
}); | ||
break; | ||
case 'Polygon': | ||
feature.geometry.coordinates.forEach(function(ring, i){ | ||
ring.forEach(function(coordinates, k){ | ||
coordinates = flipCoordinate(coordinates); | ||
feature.geometry.coordinates[i][k] = coordinates; | ||
}); | ||
}); | ||
break; | ||
} | ||
}); | ||
return fc; | ||
} | ||
else { | ||
var err = new Error('Unknown geometry type'); | ||
return err; | ||
} | ||
} | ||
function flipCoordinate (coordinates) { | ||
return([coordinates[1], coordinates[0]]); | ||
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(); | ||
} |
{ | ||
"name": "turf-flip", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "turf flip module", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "tape test.js" | ||
"test": "tape test.js", | ||
"doc": "dox -r < index.js | doxme --readme > README.md" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/morganherlocker/turf-flip.git" | ||
"url": "https://github.com/Turfjs/turf-flip.git" | ||
}, | ||
@@ -22,13 +23,15 @@ "keywords": [ | ||
"bugs": { | ||
"url": "https://github.com/morganherlocker/turf-flip/issues" | ||
"url": "https://github.com/Turfjs/turf-flip/issues" | ||
}, | ||
"homepage": "https://github.com/morganherlocker/turf-flip", | ||
"homepage": "https://github.com/Turfjs/turf-flip", | ||
"devDependencies": { | ||
"benchmark": "^1.0.0", | ||
"tape": "^2.13.4", | ||
"turf-featurecollection": "^0.1.0", | ||
"turf-linestring": "^0.1.1", | ||
"turf-point": "^0.1.1", | ||
"turf-polygon": "^0.1.1" | ||
"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" | ||
} | ||
} |
@@ -1,36 +0,56 @@ | ||
turf-flip | ||
========= | ||
[![Build Status](https://travis-ci.org/Turfjs/turf-flip.svg)](https://travis-ci.org/Turfjs/turf-flip) | ||
# turf-flip | ||
Takes a point, linestring, polygon, or featurecollection, and flips all of its coordinates from [x, y] to [y, x]. | ||
[![build status](https://secure.travis-ci.org/Turfjs/turf-flip.png)](http://travis-ci.org/Turfjs/turf-flip) | ||
###Install | ||
turf flip module | ||
```sh | ||
npm install turf-flip | ||
``` | ||
###Parameters | ||
### `turf.flip(input)` | ||
|name|description| | ||
|---|---| | ||
|fc|a geojson feature or featurecollection| | ||
Takes a GeoJSON object of any type and flips all of its coordinates | ||
from `[x, y]` to `[y, x]`. | ||
###Usage | ||
### Parameters | ||
| parameter | type | description | | ||
| --------- | ------- | -------------------- | | ||
| `input` | GeoJSON | input GeoJSON object | | ||
### Example | ||
```js | ||
flip(fc) | ||
var serbia = { | ||
"type": "Feature", | ||
"properties": {}, | ||
"geometry": { | ||
"type": "Point", | ||
"coordinates": [20.566406, 43.421008] | ||
} | ||
}; | ||
//=serbia | ||
var saudiArabia = turf.flip(serbia); | ||
//=saudiArabia | ||
``` | ||
###Example | ||
```js | ||
var flip = require('turf-flip') | ||
var polygon = require('turf-polygon') | ||
**Returns** `GeoJSON`, a GeoJSON object of the same type as `input` with flipped coordinates | ||
var poly = polygon([[[1,0], [1,0], [1,2]], [[.2,.2], [.3,.3],[.1,.2]]]) | ||
## Installation | ||
var flipped = flip(poly) | ||
Requires [nodejs](http://nodejs.org/). | ||
console.log(flipped) | ||
``` | ||
```sh | ||
$ npm install turf-flip | ||
``` | ||
## Tests | ||
```sh | ||
$ npm test | ||
``` | ||
38
test.js
@@ -8,10 +8,19 @@ var test = require('tape'); | ||
test('flip', function(t){ | ||
test('flip', function(t) { | ||
// Point Geometry | ||
var pt = point([1,0]); | ||
var flippedPt = flip(pt.geometry); | ||
t.equal(flippedPt.coordinates[0], 0); | ||
t.equal(flippedPt.coordinates[1], 1); | ||
t.equal(pt.geometry.coordinates[0], 1, 'does not mutate original'); | ||
t.equal(pt.geometry.coordinates[1], 0, 'does not mutate original'); | ||
// Point | ||
var pt = point(1,0); | ||
var flippedPt = flip(pt); | ||
var pt2 = point([1,0]); | ||
var flippedPt2 = flip(pt2); | ||
t.ok(flippedPt, 'should flip a point coordinate'); | ||
t.equal(flippedPt.geometry.coordinates[0], 0); | ||
t.equal(flippedPt.geometry.coordinates[1], 1); | ||
t.ok(flippedPt2, 'should flip a point coordinate'); | ||
t.equal(flippedPt2.geometry.coordinates[0], 0); | ||
t.equal(flippedPt2.geometry.coordinates[1], 1); | ||
@@ -21,3 +30,3 @@ // Line | ||
var flippedLine = flip(line); | ||
t.ok(flippedLine, 'should flip the x and ys of a linestring'); | ||
@@ -30,3 +39,3 @@ t.equal(flippedLine.geometry.coordinates[0][0], 0); | ||
// Polygon | ||
var poly = polygon([[[1,0], [1,0], [1,2]], [[.2,.2], [.3,.3],[.1,.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); | ||
@@ -41,12 +50,11 @@ | ||
t.equal(flippedPoly.geometry.coordinates[0][2][1], 1); | ||
t.equal(flippedPoly.geometry.coordinates[1][2][0], .2); | ||
t.equal(flippedPoly.geometry.coordinates[1][2][1], .1); | ||
t.equal(flippedPoly.geometry.coordinates[1][2][0], 0.2); | ||
t.equal(flippedPoly.geometry.coordinates[1][2][1], 0.1); | ||
// FeatureCollection | ||
var pt1 = point(1,0); | ||
var pt2 = point(1,0); | ||
var pt1 = point([1,0]); | ||
var pt2 = point([1,0]); | ||
var fc = featurecollection([pt1, pt2]); | ||
var flippedFC = flip(fc); | ||
t.ok(flippedFC, 'should flip the x and ys of a featurecollection'); | ||
@@ -59,2 +67,2 @@ t.equal(flippedFC.features[0].geometry.coordinates[0], 0); | ||
t.end(); | ||
}); | ||
}); |
8735
164
57
8