├── .npmignore ├── test ├── test.sh ├── test.js ├── output.geojson └── data.geojson ├── .gitignore ├── README.md ├── package.json ├── bin └── coordtransform ├── LICENSE └── index.js /.npmignore: -------------------------------------------------------------------------------- 1 | test -------------------------------------------------------------------------------- /test/test.sh: -------------------------------------------------------------------------------- 1 | ../bin/coordtransform -t wgs84togcj02 data.geojson output.geojson -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | var transformGeoJSON = require('../index.js'); 2 | var anygeojson = { type: 'Point', coordinates: [118, 34] }; 3 | var result = transformGeoJSON(anygeojson, 'wgs84togcj02'); 4 | console.log(anygeojson); 5 | console.log(result); 6 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | 6 | # Runtime data 7 | pids 8 | *.pid 9 | *.seed 10 | 11 | # Directory for instrumented libs generated by jscoverage/JSCover 12 | lib-cov 13 | 14 | # Coverage directory used by tools like istanbul 15 | coverage 16 | 17 | # nyc test coverage 18 | .nyc_output 19 | 20 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 21 | .grunt 22 | 23 | # node-waf configuration 24 | .lock-wscript 25 | 26 | # Compiled binary addons (http://nodejs.org/api/addons.html) 27 | build/Release 28 | 29 | # Dependency directories 30 | node_modules 31 | jspm_packages 32 | 33 | # Optional npm cache directory 34 | .npm 35 | 36 | # Optional REPL history 37 | .node_repl_history 38 | -------------------------------------------------------------------------------- /test/output.geojson: -------------------------------------------------------------------------------- 1 | {"type":"FeatureCollection","features":[{"type":"Feature","properties":{},"geometry":{"type":"Polygon","coordinates":[[[107.846493,35.137064],[107.846747,38.031191],[113.078151,38.03159],[113.07781,35.13745],[107.846493,35.137064]]]}},{"type":"Feature","properties":{},"geometry":{"type":"Polygon","coordinates":[[[110.08826,32.322243],[110.087972,28.648938],[115.493516,28.918649],[116.591931,32.507488],[113.209405,34.051271],[110.08826,32.322243]]]}},{"type":"Feature","properties":{},"geometry":{"type":"LineString","coordinates":[[93.121602,34.123869],[114.83548,42.034917]]}},{"type":"Feature","properties":{},"geometry":{"type":"Point","coordinates":[115.845353,35.029214]}},{"type":"Feature","properties":{},"geometry":{"type":"Point","coordinates":[105.472242,27.874617]}}]} -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # coordtransform-cli 2 | A CLI version for coordtransform module. 3 | 4 | ### CLI Usage 5 | 6 | `npm install -g coordtransform-cli` 7 | 8 | ``` 9 | Usage: coordtransform -t typename input output 10 | 11 | Options: 12 | -t, --type transform type. 13 | [string] [required] [choices: "bd09togcj02", "gcj02tobd09", "wgs84togcj02", "gcj02towgs84"] 14 | -h, --help Show help [boolean] 15 | 16 | Examples: 17 | coordtransform -t bd09togcj02 input.geojson output.geojson 18 | ``` 19 | 20 | ### Node or browser Usage 21 | `npm install coordtransform-cli` 22 | 23 | ``` 24 | var transformGeoJSON = require('coordtransform-cli'); 25 | var anygeojson = { type: 'Point', coordinates: [118, 34] }; 26 | var result = transformGeoJSON(anygeojson, 'wgs84togcj02'); 27 | console.log(result); 28 | ``` -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "coordtransform-cli", 3 | "version": "0.0.13", 4 | "description": "A CLI version for coordtransform module.", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "node test/test.js" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/wandergis/coordtransform-cli.git" 12 | }, 13 | "bin": { 14 | "coordtransform": "bin/coordtransform" 15 | }, 16 | "keywords": [ 17 | "coordtransform", 18 | "geojson", 19 | "cli", 20 | "bd09", 21 | "wgs84", 22 | "gcj02" 23 | ], 24 | "author": "wandergis <763393390@qq.com> (https://wandergis.com)", 25 | "license": "MIT", 26 | "bugs": { 27 | "url": "https://github.com/wandergis/coordtransform-cli/issues" 28 | }, 29 | "homepage": "https://github.com/wandergis/coordtransform-cli#readme", 30 | "dependencies": { 31 | "coordtransform": "^2.0.2", 32 | "yargs": "^5.0.0" 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /bin/coordtransform: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | var yargs = require('yargs'); 4 | var fs = require('fs'); 5 | var transformGeoJSON = require('../index.js'); 6 | argv = yargs.option('t', { 7 | alias: 'type', 8 | demand: true, 9 | describe: "transform type.", 10 | type: 'string' 11 | }) 12 | .choices('t', ['bd09togcj02', 'gcj02tobd09', 'wgs84togcj02', 'gcj02towgs84']) 13 | .usage('Usage: coordtransform -t typename input output') 14 | .example('coordtransform -t bd09togcj02', 'input.geojson output.geojson') 15 | .help('h') 16 | .alias('h', 'help') 17 | .locale('en') 18 | .argv; 19 | var type = argv.t; 20 | var input = argv._[0]; 21 | var output = argv._[1] || './output.geojson'; 22 | if (type && input) { 23 | var originGeoJSON = JSON.parse(fs.readFileSync(input)); 24 | var result = transformGeoJSON(originGeoJSON, type); 25 | fs.writeFileSync(output, JSON.stringify(result)); 26 | console.log('Transform finished!'); 27 | } else { 28 | console.error('Input not specified') 29 | } 30 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 记忆的残骸 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /test/data.geojson: -------------------------------------------------------------------------------- 1 | { 2 | "type": "FeatureCollection", 3 | "features": [ 4 | { 5 | "type": "Feature", 6 | "properties": {}, 7 | "geometry": { 8 | "type": "Polygon", 9 | "coordinates": [ 10 | [ 11 | [ 12 | 107.841796875, 13 | 35.137879119634185 14 | ], 15 | [ 16 | 107.841796875, 17 | 38.03078569382294 18 | ], 19 | [ 20 | 113.0712890625, 21 | 38.03078569382294 22 | ], 23 | [ 24 | 113.0712890625, 25 | 35.137879119634185 26 | ], 27 | [ 28 | 107.841796875, 29 | 35.137879119634185 30 | ] 31 | ] 32 | ] 33 | } 34 | }, 35 | { 36 | "type": "Feature", 37 | "properties": {}, 38 | "geometry": { 39 | "type": "Polygon", 40 | "coordinates": [ 41 | [ 42 | [ 43 | 110.0830078125, 44 | 32.32427558887655 45 | ], 46 | [ 47 | 110.0830078125, 48 | 28.65203063036226 49 | ], 50 | [ 51 | 115.48828125000001, 52 | 28.92163128242129 53 | ], 54 | [ 55 | 116.5869140625, 56 | 32.509761735919426 57 | ], 58 | [ 59 | 113.203125, 60 | 34.05265942137599 61 | ], 62 | [ 63 | 110.0830078125, 64 | 32.32427558887655 65 | ] 66 | ] 67 | ] 68 | } 69 | }, 70 | { 71 | "type": "Feature", 72 | "properties": {}, 73 | "geometry": { 74 | "type": "LineString", 75 | "coordinates": [ 76 | [ 77 | 93.1201171875, 78 | 34.125447565116126 79 | ], 80 | [ 81 | 114.82910156249999, 82 | 42.032974332441405 83 | ] 84 | ] 85 | } 86 | }, 87 | { 88 | "type": "Feature", 89 | "properties": {}, 90 | "geometry": { 91 | "type": "Point", 92 | "coordinates": [ 93 | 115.83984375, 94 | 35.02999636902566 95 | ] 96 | } 97 | }, 98 | { 99 | "type": "Feature", 100 | "properties": {}, 101 | "geometry": { 102 | "type": "Point", 103 | "coordinates": [ 104 | 105.46875, 105 | 27.877928333679495 106 | ] 107 | } 108 | } 109 | ] 110 | } -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var coordtransform = require('coordtransform'); 2 | 3 | // from turf-meta https://github.com/Turfjs/turf/tree/master/packages/turf-meta 4 | 5 | /** 6 | * Iterate over coordinates in any GeoJSON object, similar to 7 | * Array.forEach. 8 | * 9 | * @param {Object} layer any GeoJSON object 10 | * @param {Function} callback a method that takes (value) 11 | * @param {boolean=} excludeWrapCoord whether or not to include 12 | * the final coordinate of LinearRings that wraps the ring in its iteration. 13 | */ 14 | function coordEach(layer, callback, excludeWrapCoord) { 15 | var i, j, k, g, l, geometry, stopG, coords, 16 | geometryMaybeCollection, 17 | wrapShrink = 0, 18 | isGeometryCollection, 19 | isFeatureCollection = layer.type === 'FeatureCollection', 20 | isFeature = layer.type === 'Feature', 21 | stop = isFeatureCollection ? layer.features.length : 1; 22 | for (i = 0; i < stop; i++) { 23 | geometryMaybeCollection = (isFeatureCollection ? layer.features[i].geometry : 24 | (isFeature ? layer.geometry : layer)); 25 | isGeometryCollection = geometryMaybeCollection.type === 'GeometryCollection'; 26 | stopG = isGeometryCollection ? geometryMaybeCollection.geometries.length : 1; 27 | 28 | for (g = 0; g < stopG; g++) { 29 | geometry = isGeometryCollection ? 30 | geometryMaybeCollection.geometries[g] : geometryMaybeCollection; 31 | coords = geometry.coordinates; 32 | 33 | wrapShrink = (excludeWrapCoord && 34 | (geometry.type === 'Polygon' || geometry.type === 'MultiPolygon')) ? 35 | 1 : 0; 36 | if (geometry.type === 'Point') { 37 | callback(coords); 38 | } else if (geometry.type === 'LineString' || geometry.type === 'MultiPoint') { 39 | for (j = 0; j < coords.length; j++) callback(coords[j]); 40 | } else if (geometry.type === 'Polygon' || geometry.type === 'MultiLineString') { 41 | for (j = 0; j < coords.length; j++) 42 | for (k = 0; k < coords[j].length - wrapShrink; k++) 43 | callback(coords[j][k]); 44 | } else if (geometry.type === 'MultiPolygon') { 45 | for (j = 0; j < coords.length; j++) 46 | for (k = 0; k < coords[j].length; k++) 47 | for (l = 0; l < coords[j][k].length - wrapShrink; l++) 48 | callback(coords[j][k][l]); 49 | } else if (geometry.type === 'GeometryCollection') { 50 | for (j = 0; j < geometry.geometries.length; j++) 51 | coordEach(geometry.geometries[j], callback, excludeWrapCoord); 52 | } else { 53 | throw new Error('Unknown Geometry Type'); 54 | } 55 | } 56 | } 57 | } 58 | /** 59 | * [transformGeoJSON description] 批量对geojson进行坐标转换 60 | * @param {[type]} originGeoJSON [description] 原始的geojson对象 61 | * @param {[type]} type [description] 转换类型 62 | * @return {[type]} [description] 返回转换后的geojson 63 | */ 64 | function transformGeoJSON(originGeoJSON, type) { 65 | var types = ['bd09togcj02', 'gcj02tobd09', 'wgs84togcj02', 'gcj02towgs84']; 66 | var originGeoJSON_clone = JSON.parse(JSON.stringify(originGeoJSON)); 67 | if (types.indexOf(type) !== -1) { 68 | coordEach(originGeoJSON_clone, function(coord) { 69 | var after_coord = coordtransform[type](coord[0], coord[1]); 70 | coord[0] = +after_coord[0].toFixed(6); 71 | coord[1] = +after_coord[1].toFixed(6); 72 | }); 73 | return originGeoJSON_clone; 74 | } else { 75 | throw Error("type shoule be one of ['bd09togcj02', 'gcj02tobd09', 'wgs84togcj02', 'gcj02towgs84']"); 76 | } 77 | } 78 | 79 | module.exports = transformGeoJSON; 80 | --------------------------------------------------------------------------------