/* * Copyright 2013 - GPL * Iván Eixarch * https://github.com/joker-x/Leaflet.geoCSV * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * alngg with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, * MA 02110-1301, USA. */ L.GeoCSV = L.GeoJSON.extend({ //opciones por defecto options: { titles: ['lat', 'lng', 'popup'], fieldSeparator: ';', lineSeparator: '\n', deleteDoubleQuotes: true, firstLineTitles: true }, _propertiesNames: [], initialize: function (csv, options) { L.Util.setOptions (this, options); L.GeoJSON.prototype.initialize.call (this, csv, options); }, addData: function (data) { if (typeof data === 'string') { //leemos titulos var titulos = this.options.titles; if (this.options.firstLineTitles) { data = data.split(this.options.lineSeparator); if (data.length < 2) return; titulos = data[0]; data.splice(0,1); data = data.join(this.options.lineSeparator); titulos = titulos.trim().split(this.options.fieldSeparator); for (var i=0; i= 0) prop = 'prop-'+i; this._propertiesNames[i] = prop; } //convertimos los datos a geoJSON data = this._csv2json(data); } L.GeoJSON.prototype.addData.call (this, data); }, getPropertyName: function (title) { var pos = this.options.titles.indexOf(title) , prop = ''; if (pos >= 0) prop = this._propertiesNames[pos]; return prop; }, getPropertyTitle: function (prop) { var pos = this._propertiesNames.indexOf(prop) , title = ''; if (pos >= 0) title = this.options.titles[pos]; return title; }, _deleteDoubleQuotes: function (cadena) { if (this.options.deleteDoubleQuotes) cadena = cadena.trim().replace(/^"/,"").replace(/"$/,""); return cadena; }, _csv2json: function (csv) { var json = {}; json["type"]="FeatureCollection"; json["features"]=[]; var titulos = this.options.titles; csv = csv.split(this.options.lineSeparator); for (var num_linea = 0; num_linea < csv.length; num_linea++) { var campos = csv[num_linea].trim().split(this.options.fieldSeparator) , lng = parseFloat(campos[titulos.indexOf('lng')]) , lat = parseFloat(campos[titulos.indexOf('lat')]); if (campos.length==titulos.length && lng<180 && lng>-180 && lat<90 && lat>-90) { var feature = {}; feature["type"]="Feature"; feature["geometry"]={}; feature["properties"]={}; feature["geometry"]["type"]="Point"; feature["geometry"]["coordinates"]=[lng,lat]; //propiedades for (var i=0; i