forked from joker-x/Leaflet.geoCSV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
leaflet.geocsv-src.js
123 lines (111 loc) · 4.05 KB
/
leaflet.geocsv-src.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
/*
* Copyright 2013 - GPL
* Iván Eixarch <[email protected]>
* 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
* along 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'],
latitudeTitle: 'lat',
longitudeTitle: 'lng',
fieldSeparator: ';',
lineSeparator: '\n',
deleteDoubleQuotes: true,
firstLineTitles: false
},
_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<titulos.length; i++) {
titulos[i] = this._deleteDoubleQuotes(titulos[i]);
}
this.options.titles = titulos;
}
//generamos _propertiesNames
for (var i=0; i<titulos.length; i++) {
var prop = titulos[i].toLowerCase().replace(/[^\w ]+/g,'').replace(/ +/g,'_');
if (prop == '' || prop == '_' || this._propertiesNames.indexOf(prop) >= 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(this.options.longitudeTitle)])
, lat = parseFloat(campos[titulos.indexOf(this.options.latitudeTitle)]);
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<titulos.length; i++) {
if (titulos[i] != this.options.latitudeTitle && titulos[i] != this.options.longitudeTitle) {
feature["properties"][this._propertiesNames[i]] = this._deleteDoubleQuotes(campos[i]);
}
}
json["features"].push(feature);
}
}
return json;
}
});
L.geoCsv = function (csv_string, options) {
return new L.GeoCSV (csv_string, options);
};