Comparing version 0.0.1 to 0.0.3
#!/usr/bin/env node | ||
var concat = require('concat-stream'), | ||
opener = require('opener'), | ||
tty = require('tty'), | ||
path = require('path'), | ||
fs = require('fs'), | ||
argv = require('minimist')(process.argv.slice(2)); | ||
opener = require('opener'), | ||
tty = require('tty'), | ||
path = require('path'), | ||
fs = require('fs'), | ||
wellknown = require('wellknown'), | ||
argv = require('minimist')(process.argv.slice(2)); | ||
@@ -13,58 +14,74 @@ ((argv._[0] && fs.createReadStream(argv._[0])) || process.stdin).pipe(concat(openData)); | ||
function sendData(json) { | ||
console.log(JSON.stringify(json)); | ||
console.log(JSON.stringify(json)); | ||
} | ||
function trim (str) { | ||
return str.replace(/^\s*/, '').replace(/\s*$/, ''); | ||
return str.replace(/^\s*/, '').replace(/\s*$/, ''); | ||
} | ||
function tryParsingAsFeatures(body) { | ||
var lines = body.toString().split('\n'); | ||
var failed = false; | ||
var features = []; | ||
for (var i=0; i < lines.length; i++) { | ||
var line = trim(lines[i]); | ||
if (line[line.length - 1] == ',') { | ||
line = line.slice(0, line.length - 1); | ||
} | ||
if (line[0] != '{') { | ||
// assume it's grep output with the filename included | ||
line = line.replace(/^[^:]+:/, ''); | ||
} | ||
if (line == '') { | ||
continue; | ||
} | ||
try { | ||
var json = JSON.parse(line); | ||
if (json['type'] != 'Feature') { | ||
console.error('Line ' + i + ' did not look like a feature ' + JSON.stringify(json)); | ||
failed = true; | ||
} else { | ||
features.push(json); | ||
} | ||
} catch(e) { | ||
console.error('Line ' + i + ' could not parse as json: ' + e); | ||
console.error(line); | ||
failed = true; | ||
} | ||
var lines = body.toString().split('\n'); | ||
var failed = false; | ||
var features = []; | ||
for (var i=0; i < lines.length; i++) { | ||
var line = trim(lines[i]); | ||
if (line[line.length - 1] == ',') { | ||
line = line.slice(0, line.length - 1); | ||
} | ||
if (line[0] != '{') { | ||
// assume it's grep output with the filename included | ||
line = line.replace(/^[^:]+:/, ''); | ||
} | ||
if (line == '') { | ||
continue; | ||
} | ||
var json = null; | ||
if (!failed) { | ||
sendData({ | ||
"type": "FeatureCollection", | ||
"features": features | ||
}) | ||
json = wellknown(line); | ||
if (json != null) { | ||
// work around a bug in wellknown parsing multipolygons with no loops | ||
if (json['type'] === 'MultiPolygon' && | ||
typeof(json['coordinates'][0][0][0]) == typeof(45.0)) { | ||
console.error('fixing coords on multipolygon') | ||
json['coordinates'] = [json['coordinates']]; | ||
} | ||
json = { 'type': 'Feature', 'geometry': json } | ||
} else { | ||
console.error('Valid GeoJSON file required as input.'); | ||
try { | ||
json = JSON.parse(line); | ||
if (json['type'] != 'Feature') { | ||
console.error('Line ' + i + ' did not look like a feature ' + JSON.stringify(json)); | ||
failed = true; | ||
} | ||
} catch(e) { | ||
console.error('Line ' + i + ' could not parse as json: ' + e); | ||
console.error(line); | ||
failed = true; | ||
} | ||
} | ||
if (json != null) { | ||
features.push(json); | ||
} | ||
} | ||
if (!failed) { | ||
sendData({ | ||
"type": "FeatureCollection", | ||
"features": features | ||
}) | ||
} else { | ||
console.error('Valid GeoJSON file required as input.'); | ||
} | ||
} | ||
function openData(body) { | ||
try { | ||
sendData(JSON.parse(body.toString())) | ||
} catch(e) { | ||
console.error('Trying to parse as lines of features.'); | ||
tryParsingAsFeatures(body.toString()); | ||
} | ||
try { | ||
sendData(JSON.parse(body.toString())) | ||
} catch(e) { | ||
console.error('Trying to parse as lines of features.'); | ||
tryParsingAsFeatures(body.toString()); | ||
} | ||
} |
{ | ||
"name": "geojsonify", | ||
"version": "0.0.1", | ||
"description": "Make lines of geojson features into a valid geojson collection", | ||
"version": "0.0.3", | ||
"description": "Make lines of geojson features or wkt into a valid geojson collection", | ||
"main": "index.js", | ||
@@ -10,3 +10,3 @@ "scripts": { | ||
"bin": { | ||
"geojsonio": "geojsonio.js" | ||
"geojsonify": "geojsonify.js" | ||
}, | ||
@@ -30,4 +30,5 @@ "repository": { | ||
"concat-stream": "~1.0.1", | ||
"minimist": "0.0.2" | ||
"minimist": "0.0.2", | ||
"wellknown": "~0.1.2" | ||
} | ||
} |
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
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
3870
75
4
+ Addedwellknown@~0.1.2
+ Addedcore-util-is@1.0.3(transitive)
+ Addedexpect.js@0.2.0(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedisarray@0.0.1(transitive)
+ Addedminimist@0.0.5(transitive)
+ Addedreadable-stream@1.1.14(transitive)
+ Addedsharkdown@0.0.1(transitive)
+ Addedsplit@0.2.10(transitive)
+ Addedstream-spigot@2.1.2(transitive)
+ Addedstring_decoder@0.10.31(transitive)
+ Addedthrough@2.3.8(transitive)
+ Addedwellknown@0.1.3(transitive)