Merge pull request #98 from amercader/master
Make easier to plot GeoJSON geometries, eg from CSV files
This commit is contained in:
@@ -141,6 +141,13 @@
|
|||||||
<input type="text" name="separator" value="," class="spam1"/>
|
<input type="text" name="separator" value="," class="spam1"/>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
<div class="control-group">
|
||||||
|
<label class="control-label">Text delimiter</label>
|
||||||
|
<div class="controls">
|
||||||
|
<input type="text" name="delimiter" value='"' />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
<div class="control-group">
|
<div class="control-group">
|
||||||
<label class="control-label">Encoding</label>
|
<label class="control-label">Encoding</label>
|
||||||
<div class="controls">
|
<div class="controls">
|
||||||
|
|||||||
@@ -128,6 +128,7 @@ var ExplorerApp = Backbone.View.extend({
|
|||||||
var file = $file.files[0];
|
var file = $file.files[0];
|
||||||
var options = {
|
var options = {
|
||||||
separator : $form.find('input[name="separator"]').val(),
|
separator : $form.find('input[name="separator"]').val(),
|
||||||
|
delimiter : $form.find('input[name="delimiter"]').val(),
|
||||||
encoding : $form.find('input[name="encoding"]').val()
|
encoding : $form.find('input[name="encoding"]').val()
|
||||||
};
|
};
|
||||||
recline.Backend.loadFromCSVFile(file, function(dataset) {
|
recline.Backend.loadFromCSVFile(file, function(dataset) {
|
||||||
|
|||||||
@@ -58,7 +58,9 @@ this.recline.Backend = this.recline.Backend || {};
|
|||||||
var options = options || {};
|
var options = options || {};
|
||||||
var trm = options.trim;
|
var trm = options.trim;
|
||||||
var separator = options.separator || ',';
|
var separator = options.separator || ',';
|
||||||
|
var delimiter = options.delimiter || '"';
|
||||||
|
|
||||||
|
|
||||||
var cur = '', // The character we are currently processing.
|
var cur = '', // The character we are currently processing.
|
||||||
inQuote = false,
|
inQuote = false,
|
||||||
fieldQuoted = false,
|
fieldQuoted = false,
|
||||||
@@ -105,8 +107,8 @@ this.recline.Backend = this.recline.Backend || {};
|
|||||||
field = '';
|
field = '';
|
||||||
fieldQuoted = false;
|
fieldQuoted = false;
|
||||||
} else {
|
} else {
|
||||||
// If it's not a ", add it to the field buffer
|
// If it's not a delimiter, add it to the field buffer
|
||||||
if (cur !== '"') {
|
if (cur !== delimiter) {
|
||||||
field += cur;
|
field += cur;
|
||||||
} else {
|
} else {
|
||||||
if (!inQuote) {
|
if (!inQuote) {
|
||||||
@@ -114,9 +116,9 @@ this.recline.Backend = this.recline.Backend || {};
|
|||||||
inQuote = true;
|
inQuote = true;
|
||||||
fieldQuoted = true;
|
fieldQuoted = true;
|
||||||
} else {
|
} else {
|
||||||
// Next char is ", this is an escaped "
|
// Next char is delimiter, this is an escaped delimiter
|
||||||
if (s.charAt(i + 1) === '"') {
|
if (s.charAt(i + 1) === delimiter) {
|
||||||
field += '"';
|
field += delimiter;
|
||||||
// Skip the next char
|
// Skip the next char
|
||||||
i += 1;
|
i += 1;
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ this.recline.View = this.recline.View || {};
|
|||||||
//
|
//
|
||||||
// <pre>
|
// <pre>
|
||||||
// {
|
// {
|
||||||
// // geomField if specified will be used in preference to lat/lon
|
// // geomField if specified will be used in preference to lat/lon
|
||||||
// geomField: {id of field containing geometry in the dataset}
|
// geomField: {id of field containing geometry in the dataset}
|
||||||
// lonField: {id of field containing longitude in the dataset}
|
// lonField: {id of field containing longitude in the dataset}
|
||||||
// latField: {id of field containing latitude in the dataset}
|
// latField: {id of field containing latitude in the dataset}
|
||||||
@@ -344,8 +344,14 @@ my.Map = Backbone.View.extend({
|
|||||||
_getGeometryFromDocument: function(doc){
|
_getGeometryFromDocument: function(doc){
|
||||||
if (this.geomReady){
|
if (this.geomReady){
|
||||||
if (this.state.get('geomField')){
|
if (this.state.get('geomField')){
|
||||||
// We assume that the contents of the field are a valid GeoJSON object
|
var value = doc.get(this.state.get('geomField'));
|
||||||
return doc.attributes[this.state.get('geomField')];
|
if (typeof(value) === 'string'){
|
||||||
|
// We have a GeoJSON string representation
|
||||||
|
return $.parseJSON(value);
|
||||||
|
} else {
|
||||||
|
// We assume that the contents of the field are a valid GeoJSON object
|
||||||
|
return value;
|
||||||
|
}
|
||||||
} else if (this.state.get('lonField') && this.state.get('latField')){
|
} else if (this.state.get('lonField') && this.state.get('latField')){
|
||||||
// We'll create a GeoJSON like point object from the two lat/lon fields
|
// We'll create a GeoJSON like point object from the two lat/lon fields
|
||||||
var lon = doc.get(this.state.get('lonField'));
|
var lon = doc.get(this.state.get('lonField'));
|
||||||
@@ -353,10 +359,7 @@ my.Map = Backbone.View.extend({
|
|||||||
if (lon && lat) {
|
if (lon && lat) {
|
||||||
return {
|
return {
|
||||||
type: 'Point',
|
type: 'Point',
|
||||||
coordinates: [
|
coordinates: [lon,lat]
|
||||||
doc.attributes[this.state.get('lonField')],
|
|
||||||
doc.attributes[this.state.get('latField')]
|
|
||||||
]
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -440,7 +443,12 @@ my.Map = Backbone.View.extend({
|
|||||||
this.features.getBounds = function(){
|
this.features.getBounds = function(){
|
||||||
var bounds = new L.LatLngBounds();
|
var bounds = new L.LatLngBounds();
|
||||||
this._iterateLayers(function (layer) {
|
this._iterateLayers(function (layer) {
|
||||||
bounds.extend(layer instanceof L.Marker ? layer.getLatLng() : layer.getBounds());
|
if (layer instanceof L.Marker){
|
||||||
|
bounds.extend(layer.getLatLng());
|
||||||
|
} else {
|
||||||
|
bounds.extend(layer.getBounds().getNorthEast());
|
||||||
|
bounds.extend(layer.getBounds().getSouthWest());
|
||||||
|
}
|
||||||
}, this);
|
}, this);
|
||||||
return (typeof bounds.getNorthEast() !== 'undefined') ? bounds : null;
|
return (typeof bounds.getNorthEast() !== 'undefined') ? bounds : null;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
(function ($) {
|
(function ($) {
|
||||||
module("Backend Local CSV");
|
module("Backend Local CSV");
|
||||||
|
|
||||||
test("parseCSV", function() {
|
test("parseCSV", function() {
|
||||||
var csv = '"Jones, Jay",10\n' +
|
var csv = '"Jones, Jay",10\n' +
|
||||||
'"Xyz ""ABC"" O\'Brien",11:35\n' +
|
'"Xyz ""ABC"" O\'Brien",11:35\n' +
|
||||||
'"Other, AN",12:35\n';
|
'"Other, AN",12:35\n';
|
||||||
@@ -29,7 +29,7 @@ test("parseCSV", function() {
|
|||||||
equal(dataset.currentDocuments.length, 3);
|
equal(dataset.currentDocuments.length, 3);
|
||||||
});
|
});
|
||||||
|
|
||||||
test("parseCSVsemicolon", function() {
|
test("parseCSVsemicolon", function() {
|
||||||
var csv = '"Jones; Jay";10\n' +
|
var csv = '"Jones; Jay";10\n' +
|
||||||
'"Xyz ""ABC"" O\'Brien";11:35\n' +
|
'"Xyz ""ABC"" O\'Brien";11:35\n' +
|
||||||
'"Other; AN";12:35\n';
|
'"Other; AN";12:35\n';
|
||||||
@@ -44,4 +44,20 @@ test("parseCSVsemicolon", function() {
|
|||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("parseCSVdelimiter", function() {
|
||||||
|
var csv = "'Jones, Jay',10\n" +
|
||||||
|
"'Xyz \"ABC\" O''Brien',11:35\n" +
|
||||||
|
"'Other; AN',12:35\n";
|
||||||
|
|
||||||
|
var array = recline.Backend.parseCSV(csv, {delimiter:"'"});
|
||||||
|
var exp = [
|
||||||
|
["Jones, Jay", 10],
|
||||||
|
["Xyz \"ABC\" O'Brien", "11:35" ],
|
||||||
|
["Other; AN", "12:35" ]
|
||||||
|
];
|
||||||
|
deepEqual(exp, array);
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
})(this.jQuery);
|
})(this.jQuery);
|
||||||
|
|||||||
Reference in New Issue
Block a user