Leaflet.GeoCSV

Leaflet plugin for loading a CSV file as geoJSON layer

Download as .zip Download as .tar.gz View on GitHub

Leaflet GeoCSV

English: Leaflet plugin for loading a CSV file as geoJSON layer.

Castellano: Plugin para Leaflet que permite cargar un archivo CSV como capa geoJSON.

News

Why GeoCSV?

Download

Options

GeoCSV inherits the configuration options and methods of its parent object, the GeoJSON layer, and further defines the following of its own:

Methods

Use

  1. Include the plugin JavaScript file after leaflet.js: <script src="leaflet.geocsv.js"></script>

  2. Create a GeoCSV layer by instantiating the class or calling L.geoCsv(). Where csvFileContents is the content of a CSV file and options is an object literal as described in the previous section: var csvLayer = L.geoCsv(csvFileContents, options);

An example, using jQuery to read a CSV file, and adding a GeoCSV layer to a map:

(function() {
  'use strict';

  var map = L.map('mapContainer');

  $.get('data.csv', function(csvContents) {
    var geoLayer = L.geoCsv(csvContents, {firstLineTitles: true, fieldSeparator: ','});
    map.addLayer(geoLayer);
  });
});

Examples

Complete examples can be found within the examples subdirectory of the repository:

¿Por qué GeoCSV?

Descarga

Opciones

Leaflet GeoCSV hereda de GeoJSON, por lo que pueden usarse todas las opciones y métodos de la superclase. Además define las siguientes opciones propias:

Métodos

Uso

  1. Incluimos el plugin en nuestra página, detrás de leaflet.js:
<script src="leaflet.geocsv.js"></script>
  1. Creamos la capa GeoCSV bien instanciando la clase o utilizando el alias L.geoCsv:
var mi_geocsv = L.geoCsv (csv_string, opciones);

Las opciones son las que hemos visto en el apartado anterior. El primer parámetro es un string con el contenido del fichero CSV. Si a la hora de instanciarlo no tenemos disponibles los datos, csv_string puede valer null y cargar los datos más adelante usando el método addData. Ejemplo de carga asíncrona usando jQuery:

//...
var mi_geocsv = L.geoCsv (null, {firstLineTitles: true, fieldSeparator: ','});
//...
$.ajax ({
  type:'GET',
  dataType:'text',
  url:'datos.csv',
  error: function() {
    alert('No se pudieron cargar los datos');
  },
  success: function(csv) {
    mi_geocsv.addData(csv);
    mapa.addLayer(mi_geocsv);
  }
});

Ejemplos

En el subdirectorio example se encuentran ejemplos completos del uso del plugin: