Skip to content

Instantly share code, notes, and snippets.

@erwiese
Last active January 20, 2020 12:16
Show Gist options
  • Save erwiese/39d7914c92218ad33a9c4da604815d65 to your computer and use it in GitHub Desktop.
Save erwiese/39d7914c92218ad33a9c4da604815d65 to your computer and use it in GitHub Desktop.
javascript/jquery recipes
// Convert array to object
var rnxSearch = formDataArray.reduce(function (o, val) { o[val.name] = val.value; return o; }, {});
// Round float
return Number(myfloat).toFixed(2);
/**************************************************
Forms
***************************************************/
// Check a radio
$("#xradio").prop('checked', true);
// Get field values
var startD = $('#formOptions').find('input[name="startdate"]').val();
var period = $('#formOptions').find('input[name="fileperiod"]:checked').val(); // checkbox
// Reset all form fields
<form id="formOptions">
<button type="reset">Reset</button>
</form>
...
$('#formOptions').trigger("reset");
// Get form values when submitting
<form id="formOptions">
<button type="submit">Draw chart</button>
</form>
...
$("#formOptions").on("submit", function (event) {
event.preventDefault();
//var formData = $(this).serializeArray();
var formInputStr = $(this).serialize();
...
}
/**************************************************
Dates
***************************************************/
// add one day
myDate.setDate(myDate.getDate() + 1);
// copy date
var date1 = new Date(); // new Date object containing the current date and time
var copiedDate = new Date(date1.getTime());
if (myDate.getTime() < Date.now()) { }
/**************************************************
Carto functions
***************************************************/
const rho = 180 / Math.PI,
// WGS-84
const ae = 6378137.000,
const be = 6356752.3142,
const ef = (Math.pow(6378137.000, 2) - Math.pow(6356752.3142, 2)) / Math.pow(6378137.000, 2),
const es = (Math.pow(6378137.000, 2) - Math.pow(6356752.3142, 2)) / Math.pow(6356752.3142, 2),
/*
* xyzEll - convert cartesian coordinates to geographic coordinates
*
* xyzEll(x,y,z,[typ])
* ------------------------------------------------------------------------
* Arguments Description Default
* ------------------------------------------------------------------------
* x Geocentric cartesian X coordinate
* Y Geocentric cartesian Y coordinate
* Z Geocentric cartesian Z coordinate
* typ Radians ('r') or degree ('d') d
*/
function xyzEll(x, y, z, typ) {
typ = typeof typ !== 'undefined' ? typ : 'd';
// Special locations (geocenter, pole)
if (x == 0 && y == 0 && z == 0) { return [0, 0, 0] };
if (x == 0 && y == 0 && z > 0) {
return typ === 'r' ? [90 / this.rho, 0, z - this.be] : [90, 0, z - this.be];
}
if (x == 0 && y == 0 && z < 0) {
return typ === 'r' ? [-90 / this.rho, 0, -z - this.be] : [-90, 0, -z - this.be];
}
// Auxiliary quantities
const p = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
const t = Math.atan2(z * this.ae, p * this.be);
// Latitude and longitude
const lam = Math.atan2(y, x);
const phi = Math.atan2(z + this.es * this.be * Math.pow(Math.sin(t), 3), p - this.ef * this.ae * Math.pow(Math.cos(t), 3));
if (isNaN(lam) || isNaN(phi)) {
console.error('lam or phi is not a number');
return [0, 0, 0];
}
// Auxiliary quantity
const n = Math.pow(this.ae, 2) / Math.sqrt(Math.pow(this.ae, 2) * Math.pow(Math.cos(phi), 2) + Math.pow(this.be, 2) * Math.pow(Math.sin(phi), 2));
// Height
const h = p / Math.cos(phi) - n;
return typ === 'r' ? [phi, lam, h] : [phi * this.rho, lam * this.rho, h];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment