Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

geojsonify

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

geojsonify - npm Package Compare versions

Comparing version 0.0.1 to 0.0.3

113

geojsonify.js
#!/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"
}
}
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