On Jun 10, 1:37 pm, JavaJive <[email protected]> wrote: > There are actually two problems in one here: > > ii) This one does cope with Irish and CI NGRs > Just discovered that the version of GridRefToEN that I originally posted gives an erroneous result without error if one feeds it an odd length NGR. Here's a replacement routine that flags that as invalid by returning null:
function GridRefToEN( aGridRef, irish )
{
var result = null;
var errMsg = "Invalid British Isles Grid Reference";
try
{
with( Math )
{
if( floor(aGridRef.length/2) ==
aGridRef.length/2 )
{
var east = 0;
var north = 0;
var nDig = min( 2,
aGridRef.length );
var pow10 = 100000;
for( var i = 0; i < nDig; i++ )
{
if( (aGridRef.charAt(i) >= "A")
&& (aGridRef.charAt(i) <= "Z")
&& (floor(aGridRef.length/2) == aGridRef.length/2) )
{
if( aGridRef.charAt(i)
!= "I" )
{
var square
= aGridRef.substr(i, 1).charCodeAt( 0 ) -
"A".charCodeAt( 0 );
if( square > 7 )
square
= square - 1;
var eSq =
floor(square % 5);
var nSq
= 4 - floor(square / 5);
if( (i == 0) &&
!irish )
{
eSq =
(eSq - 2)*5;
nSq =
(nSq - 1)*5;
}
east +=
eSq*pow10;
north +=
nSq*pow10;
}
else
irish = (i ==
0);
}
else
throw errMsg;
}
nDig = max( 0, (aGridRef.length -
2)/2 );
pow10 = pow( 10, 5 - nDig );
east += nDig ? parseInt(
aGridRef.substr(2,nDig), 10 )*pow10 : 0;
north += nDig ? parseInt(
aGridRef.substr(2+nDig,nDig), 10 )
*pow10 : 0;
result = new OpenLayers.LonLat( east,
north );
}
else
throw errMsg;
}
}
catch(e){}
return result;
}
>
> function GridRefToEN( aGridRef, irish )
> {
> var result = null;
> try
> {
> with( Math )
> {
> var east = 0;
> var north = 0;
> var nDig = min( 2, aGridRef.length );
> var pow10 = 100000;
> for( var i = 0; i < nDig; i++ )
> {
> if( (aGridRef.charAt(i) >= "A") &&
> (aGridRef.charAt(i) <= "Z") )
> {
> if( aGridRef.charAt(i) != "I"
> )
> {
> var square =
> aGridRef.substr(i, 1).charCodeAt( 0 ) -
> "A".charCodeAt( 0 );
> if( square > 7 )
> square =
> square - 1;
> var eSq =
> floor(square % 5);
> var nSq = 4 -
> floor(square / 5);
> if( (i == 0) &&
> !irish )
> {
> eSq = (eSq -
> 2)*5;
> nSq = (nSq -
> 1)*5;
> }
> east += eSq*pow10;
> north += nSq*pow10;
> }
> else
> irish = (i == 0);
> }
> else
> throw "Invalid British Isles
> Grid Reference";
> }
> nDig = max( 0, (aGridRef.length - 2)/2 );
> pow10 = pow( 10, 5 - nDig );
> east += nDig ? parseInt(
> aGridRef.substr(2,nDig), 10 )*pow10 : 0;
> north += nDig ? parseInt(
> aGridRef.substr(2+nDig,nDig), 10 )
> *pow10 : 0;
> result = { "x":east, "y": north };
> }
> }
> catch(e){}
> return result;
> }
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Google Maps API" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/Google-Maps-API?hl=en
-~----------~----~----~----~------~----~------~--~---
