jquery - How to convert javascript array into geojson data? -
i want upload txt, xls , csv files leaflet. firstly trying txt files. js reads txt js array. want convert js array geojson. confused here. need clue how kind way should follow. appreciate time , thank kind replies.
$(function () { document.getelementbyid('file').onchange = function () { debugger; var file = this.files[0]; var reader = new filereader(); reader.onload = function (progressevent) { // entire file console.log(this.result); // lines var lines = this.result.split('\n'); var list = []; (var line = 0; line < lines.length; line++) { list.push(lines[line]); } }; reader.readastext(file); }; });
according mapping @ given github link guess may show below given case general approach better if use github geojson library or if adventurous may try extend following code...
var datastr = "name: 'location a', category: 'store', street: 'market', lat: 39.984, lng: -75.343, name: 'location b', category: 'house', street: 'broad', lat: 39.284, lng: -75.833, name: 'location c', category: 'office', street: 'south', lat: 39.123, lng: -74.534", data = datastr.split(/\s*,\s*(?=name)/g) .map(function(d){ return d.split(/\s*,\s*/) .reduce(function(r,s){ var [k,v] = s.replace(/\s*'|'\s*$/g,"") .split(":"); return k === "name" || k === "category" ? (r.properties[k] = v, r) : k === "lat" || k === "lng" ? (r.geometry.coordinates.unshift(+v), r) : r; }, {type : "feature", geometry : {type : "point", coordinates: [] }, properties: {name : "", category: "" } }); }), geojson = { type : "featurecollection", features: data }; console.log(geojson);
Comments
Post a Comment