Code coverage report for csv2geojson/index.js

Statements: 98.84% (85 / 86)      Branches: 93.33% (56 / 60)      Functions: 100% (10 / 10)      Lines: 100% (75 / 75)     

All files » csv2geojson/ » index.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 1861     31 45   1 128     1 16 16   16 64 64 56 56 72   56             16 14 51     2       1 11 11 10     1   26 15 15     26   26     26     26 5 5           25   25   24 23 67 67   23 1 1 1                 23   23 27     27         27 27 27 27   27 27   27   1         26 25 25     26                             23     1 1 1             1 3   1 1           1 1 1             1 3   1 1           1                      
var dsv = require('dsv'),
    sexagesimal = require('sexagesimal');
 
function isLat(f) { return !!f.match(/(Lat)(itude)?/gi); }
function isLon(f) { return !!f.match(/(L)(on|ng)(gitude)?/i); }
 
function keyCount(o) {
    return (typeof o == 'object') ? Object.keys(o).length : 0;
}
 
function autoDelimiter(x) {
    var delimiters = [',', ';', '\t', '|'];
    var results = [];
 
    delimiters.forEach(function(delimiter) {
        var res = dsv(delimiter).parse(x);
        if (res.length >= 1) {
            var count = keyCount(res[0]);
            for (var i = 0; i < res.length; i++) {
                Iif (keyCount(res[i]) !== count) return;
            }
            results.push({
                delimiter: delimiter,
                arity: Object.keys(res[0]).length,
            });
        }
    });
 
    if (results.length) {
        return results.sort(function(a, b) {
            return b.arity - a.arity;
        })[0].delimiter;
    } else {
        return null;
    }
}
 
function auto(x) {
    var delimiter = autoDelimiter(x);
    if (!delimiter) return null;
    return dsv(delimiter).parse(x);
}
 
function csv2geojson(x, options, callback) {
 
    if (!callback) {
        callback = options;
        options = {};
    }
 
    options.delimiter = options.delimiter || ',';
 
    var latfield = options.latfield || '',
        lonfield = options.lonfield || '';
 
    var features = [],
        featurecollection = { type: 'FeatureCollection', features: features };
 
    if (options.delimiter === 'auto' && typeof x == 'string') {
        options.delimiter = autoDelimiter(x);
        if (!options.delimiter) return callback({
            type: 'Error',
            message: 'Could not autodetect delimiter'
        });
    }
 
    var parsed = (typeof x == 'string') ? dsv(options.delimiter).parse(x) : x;
 
    if (!parsed.length) return callback(null, featurecollection);
 
    if (!latfield || !lonfield) {
        for (var f in parsed[0]) {
            if (!latfield && isLat(f)) latfield = f;
            if (!lonfield && isLon(f)) lonfield = f;
        }
        if (!latfield || !lonfield) {
            var fields = [];
            for (var k in parsed[0]) fields.push(k);
            return callback({
                type: 'Error',
                message: 'Latitude and longitude fields not present',
                data: parsed,
                fields: fields
            });
        }
    }
 
    var errors = [];
 
    for (var i = 0; i < parsed.length; i++) {
        Eif (parsed[i][lonfield] !== undefined &&
            parsed[i][lonfield] !== undefined) {
 
            var lonk = parsed[i][lonfield],
                latk = parsed[i][latfield],
                lonf, latf,
                a;
 
            a = sexagesimal(lonk, 'EW');
            Eif (a) lonk = a;
            a = sexagesimal(latk, 'NS');
            if (a) latk = a;
 
            lonf = parseFloat(lonk);
            latf = parseFloat(latk);
 
            if (isNaN(lonf) ||
                isNaN(latf)) {
                errors.push({
                    message: 'A row contained an invalid value for latitude or longitude',
                    row: parsed[i]
                });
            } else {
                if (!options.includeLatLon) {
                    delete parsed[i][lonfield];
                    delete parsed[i][latfield];
                }
 
                features.push({
                    type: 'Feature',
                    properties: parsed[i],
                    geometry: {
                        type: 'Point',
                        coordinates: [
                            parseFloat(lonf),
                            parseFloat(latf)
                        ]
                    }
                });
            }
        }
    }
 
    callback(errors.length ? errors: null, featurecollection);
}
 
function toLine(gj) {
    var features = gj.features;
    var line = {
        type: 'Feature',
        geometry: {
            type: 'LineString',
            coordinates: []
        }
    };
    for (var i = 0; i < features.length; i++) {
        line.geometry.coordinates.push(features[i].geometry.coordinates);
    }
    line.properties = features[0].properties;
    return {
        type: 'FeatureCollection',
        features: [line]
    };
}
 
function toPolygon(gj) {
    var features = gj.features;
    var poly = {
        type: 'Feature',
        geometry: {
            type: 'Polygon',
            coordinates: [[]]
        }
    };
    for (var i = 0; i < features.length; i++) {
        poly.geometry.coordinates[0].push(features[i].geometry.coordinates);
    }
    poly.properties = features[0].properties;
    return {
        type: 'FeatureCollection',
        features: [poly]
    };
}
 
module.exports = {
    isLon: isLon,
    isLat: isLat,
    csv: dsv.csv.parse,
    tsv: dsv.tsv.parse,
    dsv: dsv,
    auto: auto,
    csv2geojson: csv2geojson,
    toLine: toLine,
    toPolygon: toPolygon
};