Skip to content

Commit

Permalink
Fix spreadsheet utility
Browse files Browse the repository at this point in the history
  • Loading branch information
daemon1024 committed Aug 17, 2021
1 parent e255383 commit c95d14d
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 70 deletions.
4 changes: 3 additions & 1 deletion example/layers.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ var PLpeople = L.layerGroup.PLpeople();
var PurpleLayer = L.layerGroup.purpleLayer();
var ToxicRelease = L.layerGroup.toxicReleaseLayer();
var PFASTracker = L.layerGroup.pfasLayer();
var testSpreadsheetLayer = L.layerGroup.testSpreadsheetLayer();
var AQICNLayer = L.layerGroup.aqicnLayer();
var OSMLandfillMineQuarryLayer = L.layerGroup.osmLandfillMineQuarryLayer();

Expand Down Expand Up @@ -239,7 +240,7 @@ var overlayMaps = {
},
},
'pfasLayer': PFASTracker,

'testSpreadsheetLayer' : testSpreadsheetLayer,
'aqicnLayer': AQICNLayer,
'openaq': OpenAqLayer,
'luftdaten': LuftdatenLayer,
Expand All @@ -263,6 +264,7 @@ var allMapLayers = {
'skytruth': SkyTruth,
'fractracker': Fractracker,
'pfasLayer': PFASTracker,
'testSpreadsheetLayer' : testSpreadsheetLayer,
'toxicReleaseLayer': ToxicRelease,
'odorreport': OdorReport,
'mapknitter': MapKnitter,
Expand Down
128 changes: 59 additions & 69 deletions src/util/googleSpreadsheetLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,135 +11,126 @@ L.SpreadsheetLayer = L.LayerGroup.extend({
// sheet index: defaults to 0 (first sheet)
// },

initialize: function(options) {
initialize: function (options) {
options = options || {};
L.Util.setOptions(this, options);
this._layers = {};
this._columns = this.options.columns || [];
this._columns = [];
this.options.imageOptions = this.options.imageOptions || {};
this.options.sheetNum = this.options.sheetNum || 0;
this._parsedToOrig = {};
this._lat = this._cleanColumnName(this.options.lat);
this._lon = this._cleanColumnName(this.options.lon);
this._columns = this._cleanColumns(this._columns);
this._lat = this.options.lat;
this._lon = this.options.lon;
},

_cleanColumns: function(columns) {
for (var i = 0; i < columns.length; i++) { // the names of the columns are processed before given in JSON, so we must parse these column names too
var parsedColumnName = this._cleanColumnName(columns[i]);
this._parsedToOrig[parsedColumnName] = columns[i]; // Here we create an object with the parsed names as keys and original names as values;
columns[i] = parsedColumnName;
}
if (L.Util.indexOf(columns, this._lat) <= -1) { // parse lat and lon names the same way, then add them to columns if not there
columns.push(this._lat);
this._parsedToOrig[this._lat] = this.options.lat;
}
if (L.Util.indexOf(columns, this._lon) <= -1) {
columns.push(this._lon);
this._parsedToOrig[this._lon] = this.options.lon;
}
return columns;
},

_cleanColumnName: function(columnName) { // Tries to emulate google's conversion of column titles
return columnName.replace(/^[^a-zA-Z]+/g, '') // remove any non letters from the front till first letter
.replace(/\s+|[!@#$%^&*()]+/g, '') // remove most symbols
.toLowerCase();
},

onAdd: function(map) {
onAdd: function (map) {
this._map = map;
var self = this;
this._getURL().then(function() { // Wait for getURL to finish before requesting data. This way we can do it just once
self.requestData();
});
this._getURL();
self.requestData();
},

onRemove: function(map) {
onRemove: function (map) {
this.clearLayers();
map.spin(false);
this._layers = {};
},

_getURL: function() {
_getURL: function () {
var spreadsheetID = this._getSpreadsheetID(); // To find the URL we need, we first need to find the spreadsheetID
var self = this;
// Then we have to make another request in order to find the worksheet ID, which is changed by the sheet within the spreadsheet we want
var spreadsheetFeedURL = 'https://spreadsheets.google.com/feeds/worksheets/' + spreadsheetID + '/public/basic?alt=json';
// Here we return the getjson request so that the previous code may know when it has completed
return this._getWorksheetID(spreadsheetID, spreadsheetFeedURL);
var spreadsheetFeedURL =
"https://sheets.googleapis.com/v4/spreadsheets/" +
spreadsheetID +
"/values/Sheet1?key=AIzaSyASUPXHvLt2N9fKvI5CnRI6EjV3P39YsMc";
self.options.url = spreadsheetFeedURL;
return spreadsheetFeedURL;
},

_getSpreadsheetID: function() {
var sections = this.options.url.split('/'); // The spreadsheet ID generally comes after a section with only 1 character, usually a D.
_getSpreadsheetID: function () {
var sections = this.options.url.split("/"); // The spreadsheet ID generally comes after a section with only 1 character, usually a D.
var spreadsheetID;
var len = sections.length;
for (var i = 1; i < len; i++) {
if (sections[i - 1].length === 1) { // Here we check to see if the previous one was 1 character
if (sections[i - 1].length === 1) {
// Here we check to see if the previous one was 1 character
spreadsheetID = sections[i];
break;
}
}
return spreadsheetID;
},

_getWorksheetID: function(spreadsheetID, spreadsheetFeedURL) {
var self = this;
return $.getJSON(spreadsheetFeedURL, function(data) {
// The worksheetID we want is dependent on which sheet we are looking for
var tmpLink = data.feed.entry[self.options.sheetNum].id.$t;
var sections = tmpLink.split('/');
// It is always the last section of the URL
var sheetID = sections[sections.length - 1];
// Set the URL to the final one.
self.options.url = 'https://spreadsheets.google.com/feeds/list/' + spreadsheetID + '/' + sheetID + '/public/values?alt=json';
});
},
// _getWorksheetID: function (spreadsheetID, spreadsheetFeedURL) {
// var self = this;
// return $.getJSON(spreadsheetFeedURL, function (data) {
// // The worksheetID we want is dependent on which sheet we are looking for
// var tmpLink = data.feed.entry[self.options.sheetNum].id.$t;
// var sections = tmpLink.split("/");
// // It is always the last section of the URL
// var sheetID = sections[sections.length - 1];
// // Set the URL to the final one.
// self.options.url =
// "https://spreadsheets.google.com/feeds/list/" +
// spreadsheetID +
// "/" +
// sheetID +
// "/public/values?alt=json";
// });
// },

requestData: function() {
requestData: function () {
var self = this;
(function() {
(function () {
var $ = window.jQuery;
var ssURL = self.options.url || '';
var ssURL = self.options.url || "";
self._map.spin(true);
// start fetching data from the URL
$.getJSON(ssURL, function(data) {
self.parseData(data.feed.entry);
$.getJSON(ssURL, function (data) {
self.parseData(data.values);
self._map.spin(false);
});
})();
},

parseData: function(data) {
for (var i = 0; i < data.length; i++) {
parseData: function (data) {
//Zeroth element contains column names
this._columns = data[0];
for (var i = 1; i < data.length; i++) {
this.addMarker(data[i]);
}
},

addMarker: function(data) {
var urlSections = data.id.$t.split('/');
var key = urlSections[urlSections.length - 1];
addMarker: function (data) {
var key = data[0];
if (!this._layers[key]) {
var marker = this.getMarker(data);
this._layers[key] = marker;
this.addLayer(marker);
}
},

getMarker: function(data) {
getMarker: function (data) {
var info = {};
for (var i = 0; i < this._columns.length; i++) {
info[this._columns[i]] = data['gsx$' + this._columns[i]].$t || ''; // The JSON has gsx$ appended to the front of each columnname
info[this._columns[i]] = data[i] || ""; // Map column name to item index
}
// Get coordinates the coordinates; remember that _lat and _lon are the column names, not the actual values
var latlon = [parseInt(info[this._lat]), parseInt(info[this._lon])];
var generatePopup = this.options.generatePopup || function() { return; };
var generatePopup =
this.options.generatePopup ||
function () {
return;
};
// Generate an object using the original column names as keys
var origInfo = this._createOrigInfo(info);
return L.marker(latlon, this.options.imageOptions).bindPopup(generatePopup(origInfo));
return L.marker(latlon, this.options.imageOptions).bindPopup(
generatePopup(origInfo)
);
},

_createOrigInfo: function(info) {
_createOrigInfo: function (info) {
// The user will most likely give their generatePopup in terms of the column names typed in,
// not the parsed names. So this creates a new object that uses the original typed column
// names as the keys
Expand All @@ -150,9 +141,8 @@ L.SpreadsheetLayer = L.LayerGroup.extend({
}
return origInfo;
},

});

L.spreadsheetLayer = function(options) {
L.spreadsheetLayer = function (options) {
return new L.SpreadsheetLayer(options);
};

0 comments on commit c95d14d

Please sign in to comment.