[BNM] Can this convoluted JavaScript function be simplified?
Alex
alex at fatdrop.co.uk
Sat Nov 8 17:49:01 GMT 2008
I should explain that really:
First, it works out which quarter of the globe the input is in.
Up to 90 degrees == quarter 0
Up to 180 == q1
Up to 270 == q2
Up to 360 == q3.
Then find the real coordinate based on the quarter and the modulus of
the input with 90 degrees. If it's negative it's 90 minus the modulus.
Then, and this is the bit I'm not sure about, if the Longitude is > 90
or < -90 negate the Latitude.
Alex =]
Alex wrote:
> Bit of a headbender that one!!
>
> I *think* this works. haven't tested much.. dunno if it's simpler or not:
>
> function findQuarter(coord){
> var qtrs = Math.floor(coord/90);
> qtrs -= (Math.floor(qtrs/4)*4);
> return qtrs;
> }
>
> function findCoord(i) {
> if( i > 0 )
> i = (i % 90) + (findQuarter(i)*90);
> else
> i = (90-(i % 90)) + (findQuarter(i)*90);
> if( i > 180 ) i = 360 - i;
> return i;
> }
>
> function getLatLong(lat, lng) {
> lat = findCoord(lat);
> lng = findCoord(lng);
> if( lng > 90 || lng < -90 ) lat = 0 - lat;
> return [lat, lng];
> }
>
>
>
> Premasagar Rose wrote:
>
>> Yes, that's it.
>> And I want them to be intelligently reassigned in this way:
>>
>> * Latitude beyond the poles continues around the other side:
>> o Adding an extra 10 degrees beyond 90 degrees (which is the
>> north pole) becomes 80 degrees latitude, with longitude
>> switching over to the other side, e.g. [100, 50] => [80, -50]
>> * Longitude gets wrapped around to the other side of the planet. E.g.:
>> o -190 becomes 170
>> o 190 becomes -170
>>
>> Hope that makes sense.
>>
>> Prem
>>
>>
>>
>>
>> -------- Original Message --------
>> Subject: Re: [BNM] Can this convoluted JavaScript function be simplified?
>> From: Alex <alex at fatdrop.co.uk>
>> To: Brighton New Media <bnmlist at brightonnewmedia.org>
>> Date: 08/11/2008 15:52
>>
>>
>>> So you just want to confine the Lat values to values between -90 and 90
>>> and the Long between -180 and 180?
>>>
>>>
>>>
>>> Premasagar Rose wrote:
>>>
>>>
>>>
>>>> A little challenge for the weekend, perhaps?
>>>>
>>>> I've written a JavaScript function that gives me the results that I
>>>> need, but in quite a convoluted way. I've spent all my brain cells now
>>>> and can't think how to further simplify it. It'll probably involve more
>>>> use of modulus (%) or something.
>>>>
>>>> The function takes two numbers, which represent latitude and longitude
>>>> coordinates. The idea is that it will "normalise" any invalid
>>>> coordinates (e.g. further north than the north pole, or further west
>>>> than Alaska), so that they become valid again. It's part of geo plugin
>>>> for jQuery that I'm working on.
>>>>
>>>> Latitude runs from -90 at the south pole, to 0 at the equator, to 90 at
>>>> the north pole.
>>>> Longitude runs from -180 west of Alaska, to 0 in the UK, to 180 east of
>>>> Fiji.
>>>> For consistency, I have -180 longitude always represented as 180 (the
>>>> two coordinates are actually the same).
>>>>
>>>> The results of a simplified version should be absolutely identical. Any
>>>> suggestions on improving it?:
>>>>
>>>> ===
>>>>
>>>> function normalizeLatLng(lat, lng){
>>>> lat = lat % 360;
>>>> if (lat > 90 && lat <= 180){
>>>> lat = 180 - lat;
>>>> lng = lng - 180;
>>>> }
>>>> else if (lat > 180 && lat <= 270){
>>>> lat = 180 - lat;
>>>> lng = lng - 180;
>>>> }
>>>> else if (lat > 270 && lat <= 360){
>>>> lat = lat - 360;
>>>> }
>>>> else if (lat < -90 && lat >= -180){
>>>> lat = 0 - (lat + 180);
>>>> lng = lng - 180;
>>>> }
>>>> else if (lat < -180 && lat >= -270){
>>>> lat = 180 - lat;
>>>> lng = lng - 180;
>>>> }
>>>> else if (lat < -270 && lat >= -360){
>>>> lat = lat + 360;
>>>> }
>>>>
>>>> if (lng <= -180 || lng > 180){
>>>> lng = (lng % 180) - (lng > 0 ? 180 : -180);
>>>> if (lng === -180){
>>>> lng = 180;
>>>> }
>>>> }
>>>> return [lat,lng];
>>>> }
>>>>
>>>> ===
>>>>
>>>> I look forward to any suggestions.
>>>>
>>>> Cheers,
>>>> Premasagar
>>>>
>>>>
>>>>
>>>>
>>>>
>>>
>>>
>>>
>>
>>
>
>
--
http://www.fatdrop.co.uk - digital music services
http://blog.fatdrop.co.uk - music industry blog
FATDROP LTD | Registered in England and Wales | Company No. 6199983
More information about the BNMlist
mailing list. Powered by Wessex Networks