// Locapoint Class libralies // // Content-type: text/javascript // AUTHOR: Naoki Ueda // Copyright reserved by Naoki Ueda and Locazing Inc. // // To WHO READ THIS /****************************************************************************** THANK YOU for comming to Locapoint Site and reach to this file :) This 'Locapoint Class' is made ONLY for web searvices of locapoint.com, and does NOT define or publication of 'Locapoint Class' at this time. What is included in Locapoint Specification is only Difinition of Locapoint format and rules. It does not define any class or programming rules. Honestly, I'm not skilled program writer, and I think this code is not sophisticated enough. I really appreciate someone to write better (and beautiful) code of locapoint utilities! I want make Locapoint like a "Linux of GIS world", so help or support from you is always welcome!! ABOUT:PATENT I have a patent right for Locapoint encoding/decoding logic, machine, and media that has Locapoint on it. However, I officially provide a free right to use, write programs & utilities, create freewares, free web services, or any non-commercial activities. If you think you are going to start commercial business using Locapoint, I want you to contact me. Unauthrized use of Locapoint for commercial use may be charged for violation of license when my applied patent become officially glanted. Best Refards and Thanks, Nao (Naoki Ueda) *******************************************************************************/ // this.range.maxlat){ this.range.maxlat = this.Locapoint[counter].latitude; } if(this.Locapoint[counter].longitude < this.range.minlon){ this.range.minlon = this.Locapoint[counter].longitude; } if(this.Locapoint[counter].longitude > this.range.maxlon){ this.range.maxlon = this.Locapoint[counter].longitude; } this.range.centerlat = (this.range.maxlat + this.range.minlat)/2; this.range.centerlon = (this.range.maxlon + this.range.minlon)/2; this.range.height = Math.abs(this.range.maxlat - this.range.minlat); this.range.width = Math.abs(this.range.maxlon - this.range.minlon); addlat += this.Locapoint[counter].latitude; addlon += this.Locapoint[counter].longitude; } if(!this.Locapoint[counter].isvalid){ this.isvalid = false; } if(this.precisionlevel < this.Locapoint[counter].precisionlevel){ this.precisionlevel = this.Locapoint[counter].precisionlevel; } } this.range.averagelat = addlat/(this.length); this.range.averagelon = addlon/(this.length); return; } //Methods new LocapointItem(""); //Dummy Object LocapointItem.prototype.toString = function(){ return(this.value); } LocapointItem.prototype.Abbrev = function(precisionlevel){ var counter; var b=""; //working buffer var c; //work object if(this.value==""){ return(this); //Error, null } //if((!this.Locapoint[0].isFullarea) && (!this.Locapoint[0].isValidBase)){ // return(this); //No way to Abbrev //} for(counter = 0; counter < this.length; counter++){ b=b+this.Locapoint[counter].Abbrev(precisionlevel); if(counter < this.length -1){ c = this.Locapoint[counter].Full(); this.Locapoint[counter+1].SetAbrevBase(c.areacode); if((this.type == "path") && (counter != this.length -1)){ b=b+":"; }else if((this.type =="area") && (counter != this.length -1)){ b=b+"_"; } } } LPIobj = new LocapointItem(b); return(LPIobj); } LocapointItem.prototype.Full = function(){ var counter; var b=""; //working buffer var c; //work object if(this.value==""){ return(this); //Error, null } //if((!this.Locapoint[0].isFullarea) && (!this.Locapoint[0].isValidBase)){ // return(this); //No way to Full //} for(counter = 0; counter < this.length; counter++){ b=b+this.Locapoint[counter].Full(); if(counter < this.length -1){ c = this.Locapoint[counter].Full(); this.Locapoint[counter+1].SetAbrevBase(c.areacode); if((this.type == "path") && (counter != this.length -1)){ b=b+":"; }else if((this.type =="area") && (counter != this.length -1)){ b=b+"_"; } } } LPIobj = new LocapointItem(b); return(LPIobj); } LocapointItem.prototype.AddOneLocapoint = function(Locapoint) { var addstr; var addLPobj; if(Locapoint.value){ //if LocapointObject addstr = Locapoint.value addLPobj = Locapoint; }else{ //just a Locapoint string addstr = Locapoint addLPobj = new Locapoint(Locapoint) } if(this.type == "point"){ if(this.value ==""){ this.value = addstr; this.length = this.length + 1; this.Locapoint.push(addLPobj); if(!this.Locapoint[this.length -1].isvalid){this.isvalid = false;} } }else if (this.type == "path"){ if(this.value ==""){ this.value = addstr; }else{ this.value = this.value + ":" + addstr; } this.length = this.length + 1; this.Locapoint.push(addLPobj); if(!this.Locapoint[this.length -1].isvalid){this.isvalid = false;} }else if (this.type == "area"){ if(this.value ==""){ this.value = addstr; }else{ this.value = this.value + "_" + addstr; } this.length = this.length + 1; this.Locapoint.push(new Locapoint(addLPobj)); if(!this.Locapoint[this.length -1].isvalid){this.isvalid = false;} } } LocapointItem.prototype.RemoveOneLocapoint = function(){ var b; //working buffer; if(this.type == "point"){ }else if (this.type == "path"){ b = ""; if(this.length >1){ for(counter = 0; counter < this.length -1; counter++){ b= b + this.Locapoint[counter].value; if(counter < this.length -2){b = b + ":";} } this.value = b; this.length = this.length - 1; this.Locapoint.pop(); } else if(this.length == 1){ delete(this); //Delete by oneself. Possible?? } }else if (this.type == "area"){ b = ""; if(this.length >1){ for(counter = 0; counter < this.length -1; counter++){ b= b + this.Locapoint[counter].value; if(counter < this.length -2){b = b + "_";} } this.value = b; this.length = this.length - 1; this.Locapoint.pop(); } else if(this.length == 1){ delete(this); //Delete by oneself. Possible?? } } } //******* LocapointCombo Class - contains multiple LocapointItem ***** //Constructor function LocapointCombo(a) { this.value = a; this.length = 1; this.LocapointItem = []; this.isvalid = true; this.totallocapoints=0; this.totalpointitem =0; this.totalpathitem=0; this.totalareaitem =0; this.precisionlevel = 0; //Range Calculation this.range = new Object(); var addlat =0; var addlon =0; if(this.value ==""){ //in case constracted with null string this.length = 0; this.isvalid = false; return; } var InsideComment = false; var b = ""; //working buffer for full string var counter; var Ch; for(counter = 0; counter <= a.length; counter++){ Ch = a.charAt(counter); if (! InsideComment) { if(Ch == '"'){ //beginning of comment InsideComment = true; b = b + Ch; }else if(Ch == ',' ){ //end of LocapointItem this.LocapointItem.push(new LocapointItem(b)); b = ""; }else if(Ch == ' '){ //end of LocapointCombo this.LocapointItem.push(new LocapointItem(b)); b = ""; break; }else { b = b + Ch; } }else{ //Inside Comment if((Ch == '"') && (a.substr(counter-1, 1) == '\\') ){ //Escaped Double quote b = b + Ch; }else if (Ch == '"'){ //end of comment b = b + Ch; InsideComment = false; }else { b = b + Ch; } } if(counter == a.length -1){ //end of LocapointCombo this.LocapointItem.push(new LocapointItem(b)); b = ""; break; } } this.length = this.LocapointItem.length; //Loop check for each LocapointItem for(counter = 0; counter < this.length; counter++){ if(counter == 0){ //first item this.range.minlat =this.LocapointItem[0].range.minlat; this.range.maxlat =this.LocapointItem[0].range.maxlat; this.range.minlon =this.LocapointItem[0].range.minlon; this.range.maxlon =this.LocapointItem[0].range.maxlon; this.range.centerlat =this.LocapointItem[0].range.centerlat; this.range.centerlon =this.LocapointItem[0].range.centerlon; this.range.averagelat =this.LocapointItem[0].range.averagelat; this.range.averagelon =this.LocapointItem[0].range.averagelon; this.range.height =this.LocapointItem[0].range.height; this.range.width =this.LocapointItem[0].range.width; addlat += this.LocapointItem[counter].range.averagelat * this.LocapointItem[counter].length; addlon += this.LocapointItem[counter].range.averagelon * this.LocapointItem[counter].length; this.totallocapoints += this.LocapointItem[counter].length; }else{ if(this.LocapointItem[counter].range.minlat < this.range.minlat){ this.range.minlat = this.LocapointItem[counter].range.minlat; } if(this.LocapointItem[counter].range.maxlate > this.range.maxlat){ this.range.maxlat = this.LocapointItem[counter].range.maxlat; } if(this.LocapointItem[counter].range.minlon < this.range.minlon){ this.range.minlon = this.LocapointItem[counter].range.minlon; } if(this.LocapointItem[counter].range.maxlon > this.range.maxlon){ this.range.maxlon = this.LocapointItem[counter].range.maxlon; } this.range.centerlat = (this.range.maxlat + this.range.minlat)/2; this.range.centerlon = (this.range.maxlon + this.range.minlon)/2; this.range.height = Math.abs(this.range.maxlat - this.range.minlat); this.range.width = Math.abs(this.range.maxlon - this.range.minlon); addlat += this.LocapointItem[counter].range.averagelat * this.LocapointItem[counter].length; addlon += this.LocapointItem[counter].range.averagelon * this.LocapointItem[counter].length; this.totallocapoints += this.LocapointItem[counter].length; } if(!this.LocapointItem[counter].isvalid){ this.isvalid = false; } if(this.precisionlevel < this.LocapointItem[counter].precisionlevel){ this.precisionlevel = this.LocapointItem[counter].precisionlevel; } if(this.LocapointItem[counter].type == "point"){ this.totalpointitem +=1; }else if (this.LocapointItem[counter].type == "path"){ this.totalpathitem +=1; }else if (this.LocapointItem[counter].type == "area"){ this.totalareaitem +=1; } } this.range.averagelat = addlat/(this.totallocapoints); this.range.averagelon = addlon/(this.totallocapoints); return; } //Methods new LocapointCombo(""); //Dummy Object LocapointCombo.prototype.toString = function(){ return(this.value); } LocapointCombo.prototype.Abbrev = function(precisionlevel){ var LPIcounter; var b=""; //working buffer var c; //working object if(this.value==""){ return(this); //Error, null } if(! this.LocapointItem[0].Locapoint[0].isFullarea){ return(this); //Error, no base area to start } for(LPIcounter = 0; LPIcounter < this.length; LPIcounter++){ b = b + this.LocapointItem[LPIcounter].Abbrev(precisionlevel); if(LPIcounter < this.length -1){ lastLP = this.LocapointItem[LPIcounter].length -1; c = this.LocapointItem[LPIcounter].Locapoint[lastLP].Full(); this.LocapointItem[LPIcounter+1].Locapoint[0].SetAbrevBase(c.areacode); b = b + ","; } } LPCobj = new LocapointCombo(b); return(LPCobj); } LocapointCombo.prototype.Full = function(){ var LPIcounter; var b=""; //working buffer var c; //working object if(this.value==""){ return(this); //Error, null } if(! this.LocapointItem[0].Locapoint[0].isFullarea){ return(this); //Error, no base area to start } for(LPIcounter = 0; LPIcounter < this.length; LPIcounter++){ b = b + this.LocapointItem[LPIcounter].Full(); if(LPIcounter < this.length -1){ lastLP = this.LocapointItem[LPIcounter].length -1; c = this.LocapointItem[LPIcounter].Locapoint[lastLP].Full(); this.LocapointItem[LPIcounter+1].Locapoint[0].SetAbrevBase(c.areacode); b = b + ","; } } LPCobj = new LocapointCombo(b); return(LPCobj); } LocapointCombo.prototype.AddOneLocapointItem = function(LocapointItem){ if(LocapointItem.value){ //if LocapointItemObject addstr = LocapointItem.value addLPIobj = LocapointItem; }else{ //just a Locapoint Item string addstr = LocapointItem addLPIobj = new LocapointItem(LocapointItem) } if(this.value ==""){ this.value = addstr; }else{ this.value = this.value + "," + addstr; } this.length = this.length + 1; this.LocapointItem.push(addLPIobj); if(!this.LocapointItem[this.length -1].isvalid){this.isvalid = false;} } LocapointCombo.prototype.RemoveOneLocapointItem = function(LocapointItem_str){ var b = ""; //working buffer; if(this.length >1){ for(counter = 0; counter < this.length -1; counter++){ b= b + this.LocapointItem[counter].value; if(counter < this.length -2){b = b + ",";} } this.value = b; this.length = this.length - 1; this.LocapointItem.pop(); } else if(this.length == 1){ delete(this); //Delete by oneself. Possible?? } } //******* Utility functions **Not in Class************************************* function latlon2locapoint(lat, lon) { if(!(-90 <= lat-0 && lat-0 <= 90 && -180 <= lon-0 && lon-0 <= 180)){ return("Error:Invalid Input"); } if (lat==90){ lat=89.9999999999-0;} // if (lon==180){lon=179.9999999990-0;} var LPLat = Math.round((lat -0 +90 -0)*45697600/180); var LPLon = Math.round((lon -0 +180 -0)*45697600/360); //Encode return("" + String.fromCharCode(Math.floor(LPLat / 1757600) % 26 +65-0) + String.fromCharCode(Math.floor(LPLat / 67600) % 26 +65-0) + Math.floor(LPLat /6760) % 10 + "." + String.fromCharCode(Math.floor(LPLon / 1757600) % 26 +65-0) + String.fromCharCode(Math.floor(LPLon / 67600) % 26 +65-0) + Math.floor(LPLon / 6760) % 10 + "." + String.fromCharCode(Math.floor(LPLat / 260) % 26 +65-0) + String.fromCharCode(Math.floor(LPLat / 10) % 26 +65-0) + LPLat % 10 + "." + String.fromCharCode(Math.floor(LPLon / 260) % 26 +65-0) + String.fromCharCode(Math.floor(LPLon / 10) % 26 +65-0) + LPLon % 10); } function DMS_to_D(d, m, s){ if (d >= 0){ return(d -0 + m/60 -0 + s/3600 -0); }else{ return(d - m/60 - s/3600 -0); } } function LPGridLonLat(SWlocapoint, NElocapoint, level, maxlimit) { //return array which contains a set of latitude/longitude for Locapoint Grid //level 1 (A) to level 6 (AA0AA0) //Constants var UnitStep = Array(1757600, 67600, 6760, 260, 10, 1); var BaseChar = Array(65, 65, 48, 65, 65, 48); var Base = Array(26, 26, 10, 26, 26, 10); //Variables, Objects var LPgrid = new Object(); LPgrid.level = level; LPgrid.valid = true; LPgrid.lat = Array(); LPgrid.lon = Array(); LPgrid.latband = UnitStep[level-1]/45697600*180-90; LPgrid.lonband = UnitStep[level-1]/45697600*360-180; LPgrid.latmin = 0; LPgrid.lonmin = 0; LPgrid.latmax = 0; LPgrid.lonmax = 0; var latistepbase = 0; var laticode=SWlocapoint.laticode.substr(0,3)+SWlocapoint.laticode.substr(4,3); for(n_level = 0; n_level maxlimit){ LPgrid.valid = false; return(LPgrid); } } var longistepbase = 0; var longicode=SWlocapoint.longicode.substr(0,3)+SWlocapoint.longicode.substr(4,3); for(n_level = 0; n_level maxlimit){ LPgrid.valid = false; return(LPgrid); } } return(LPgrid); } //]]>