User:Proteins/mapfunctions.js

// // A library of mapping functions that may help in plotting geotag coordinates // // Unless specified otherwise, all angles are measured in radians, NOT degrees // // To use this script, add "importScript('User:Proteins/mapfunctions.js');" to your monobook.js subpage // under your user page, as you can see at User:Proteins/monobook.js

//********************************************* // Constructor function for the map coordinates //********************************************* function MapCoordinates(arg_x, arg_y) { this.x = arg_x; this.y = arg_y; } // closes constructor function MapCoordinates

//*********************************************************** // Compute the Mollweide angle by iteration (Newton's method) // The equation to be solved is: // 2 * gamma + sin(2 * gamma) = PI * sin(latitude) //*********************************************************** function wikimapMollweideAngle(arg_latitude) { var error_string = ""; var alert_string = "";

var zero_tolerance = 0.00001;

var gamma; var del_gamma; var derivative; var zero_function;

gamma = (0.25 * Math.PI) * arg_latitude; zero_function = 2.0 * gamma + Math.sin(2.0 * gamma) - Math.PI * Math.sin(arg_latitude); derivative = 2.0 + 2.0 * Math.cos(2.0 * gamma); if (derivative == 0.0) { error_string = "ERROR in wikimapMollweideAngle: The derivative is zero.\n"; window.alert(error_string); return; }	del_gamma = - zero_function / derivative; // Newton's method gamma += del_gamma;

while (Math.abs(del_gamma) > zero_tolerance) {		zero_function = 2.0 * gamma + Math.sin(2.0 * gamma) - Math.PI * Math.sin(arg_latitude); derivative = 2.0 + 2.0 * Math.cos(2.0 * gamma); if (derivative == 0.0) { error_string = "ERROR in wikimapMollweideAngle: The derivative is zero.\n"; window.alert(error_string); return; }		del_gamma = - zero_function / derivative; gamma += del_gamma; }

return gamma; } // closes function wikimapMollweideAngle

//********************* // Mollweide projection //********************* function wikimapMollweideProjection(arg_latitude, arg_longitude) { var alert_string = ""; var error_string = "";

var map_coordinates;

var map_x = 0.0; var map_y = 0.0; var Mollweide_angle = 0.0;

Mollweide_angle = wikimapMollweideAngle(arg_latitude);

map_x = 2.0 * (Math.SQRT2/Math.PI) * Math.cos(Mollweide_angle) * arg_longitude; map_y = Math.SQRT2 * Math.sin(Mollweide_angle);

map_coordinates = new MapCoordinates(map_x, map_y); return map_coordinates; } // closes function wikimapMollweideProjection

//************************************************** // The Main Function (mainly for diagnostic testing) //************************************************** function wikimapFunctions { var error_string = ""; var alert_string = "";

var local_pi = 3.14159265358; var convert_radians_to_degrees = (180.0/Math.PI); var convert_degrees_to_radians = (Math.PI/180.0);

var latitude_in_degrees = 45.0; var latitude_in_radians = (latitude_in_degrees * convert_degrees_to_radians);

var Mollweide_angle_in_radians = 0.0; var Mollweide_angle_in_degrees = 0.0;

latitude_in_degrees = window.prompt("Please enter a latitude in degrees:", "45"); latitude_in_radians = latitude_in_degrees * convert_degrees_to_radians;

Mollweide_angle_in_radians = wikimapMollweideAngle(latitude_in_radians); Mollweide_angle_in_degrees = Mollweide_angle_in_radians * convert_radians_to_degrees;

alert_string = "The Mollweide angle for " + latitude_in_degrees + " deg latitude is " + Mollweide_angle_in_degrees + " deg."; window.alert(alert_string);

} // closes function wikimapFunctions

$(function { mw.util.addPortletLink('p-navigation', 'javascript:wikimapFunctions', 'Map functions', 'ca-mapfunctions', 'A library of mapping functions', , ); });

//