Skip to content

Instantly share code, notes, and snippets.

@unmultimedio
Last active August 29, 2015 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save unmultimedio/8e58d4bd071bc64032cc to your computer and use it in GitHub Desktop.
Save unmultimedio/8e58d4bd071bc64032cc to your computer and use it in GitHub Desktop.
Google Apps Script code to convert a geotrace set of answers into static maps (images) with the traces using google maps api.
// onOpen executes everytime a user opens the document
function onOpen() {
// Get User Interface
var ui = SpreadsheetApp.getUi();
// Se agregan las dos opciones del menú
ui.createMenu('ODK Utilities')
.addItem('Convert geotrace to Map', 'geoTraceToMap')
.addToUi();
}
function geoTraceToMap(){
// Display a dialog box with a title, message, input field, and "Yes" and "No" buttons. The
// user can also close the dialog by clicking the close button in its title bar.
var ui = SpreadsheetApp.getUi();
var responseColumnSelected = ui.prompt('Column name', 'What is the name of the column with the "geotrace" data?', ui.ButtonSet.OK_CANCEL);
// In the case user hits OK
if (responseColumnSelected.getSelectedButton() == ui.Button.OK) {
// Get value from the textbox
var columnName = responseColumnSelected.getResponseText();
var sheet = SpreadsheetApp.getActiveSheet();
try{
// Findout the column Number
// Function found in: https://gist.github.com/russenreaktor/5520691
var columnIndex = getColumnNrByName(sheet, columnName);
// Get the letter of that column
var columnID = String.fromCharCode(65+columnIndex);
var alertMsg = "The script will read column '" + columnID + "' and stop when it finds any blank cell.";
var alertScriptStop = ui.alert(alertMsg);
// Call the function that makes the magic
convertColumnToShape(columnIndex);
} catch(err){
// If name of the column provided doesn't exist
var alertColumnNotFound = ui.alert(err);
}
}
}
// Receives the index of the column to convert
function convertColumnToShape(columnIndex){
// Gets the sheet
var sheet = SpreadsheetApp.getActiveSheet();
sheet.insertColumns(1);
sheet.getRange(1, 1).setValue("URL Mapa");
var columnPolyline = columnIndex+2;
// Until last row, skipping the header
for(var i=2; i<=sheet.getLastRow(); i++){
var perimeter = sheet.getRange(i, columnPolyline);
// Make sure isn't empty
if(!perimeter.isBlank()){
var data = perimeter.getValue();
// Delete all the possible double spaces
while(data.indexOf(" ")!=-1)data.replace(" "," ");
// Replace all the separations to remove spaces after semicolon
data = data.split("; ").join(";");
// Get coordinates in array
var coordinates = data.split(";");
// Prepare the path to push some points
var array_of_points = [];
// For all the coordinates
for(var p=0; p<coordinates.length; p++){
// get in array [lat, lng, alt, acc]
var coordinate_axis = coordinates[p].split(" ");
// verify at least it has lat and lng
if(coordinate_axis.length>=2){
// Push lat and lng
array_of_points.push(coordinate_axis[0]);
array_of_points.push(coordinate_axis[1]);
}
}
// Creates a map and adds a path
var polyline = Maps.encodePolyline(array_of_points);
var map = Maps.newStaticMap();
map.addPath(polyline);
// Set in the column we created previously
sheet.getRange(i, 1).setValue(map.getMapUrl());
}else{
// Found a blank cell, exit
return 0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment