Archive for October, 2007

mySQL & PI() to the Nth Decimal

Saturday, October 20th, 2007

Well, it’s not the most common function on the block - infact, I’m one of a handful of people who I’ve ever seen use it, but the help that this little blerb will give, will definately not be limited to simply PI(). It’s all about the decimal places. By default mySQL will return PI() as 7 decimal points.

Well, this is fine in general principle, but what about getting really accurate when your dealing with Geo Location’s? Like, finding the distance between two points on the globe? It’s important to get as accurate as you can be when determining these distances! Afterall, the difference between 1km away, and 1.15km away can mean the difference between arriving at a friends and arriving at their neighbors!

Okay, so how do we do it? Well, as I said, PI() returns 7 decimal points, but its easy to change that! You see 7, but mySQL does see more internally because it uses the full double-precision value. Okay, so what does that mean to you? Well, normally it wouldn’t matter, but for some reason I ran across problems with it when working with re-jigging select queries.

Case in point:

SELECT *,(ACOS(COS((’-33.91451667′ * (PI()/180))) * COS((location_lat * (PI()/180))) * COS((’151.1712194′ * (PI()/180))) * COS((location_long * (PI()/180)))+COS((’-33.91451667′ * (PI()/180))) * SIN((’151.1712194′ * (PI()/180))) * COS((location_lat * (PI()/180))) * SIN((location_long * (PI()/180)))+SIN((’-33.91451667′ * (PI()/180))) * SIN((location_lat * (PI()/180)))) * 6378) AS `distance` FROM `maps_location` WHERE 1 ORDER BY `distance` ASC;

Some of the results out of this, were 1km off over a distance of 3200km’s. Not normally a problem for some people, but hey - I’m a perfectionist, so Mark and I sat and wrote an even better query.  The trick was, to make PI() think it had more decimals, before sending it in as a variable in the equation, so to do this we simply did:

PI()+0.000000000000000000000

It’s that easy!  This will force mySQL to treat this as 22 digit decimal, with the 22,21 logic.

One last thing, another quick way, if your ever stuck in less-logical languages, to get PI() is:

ATAN2(1,1) * 4;

Enjoy.

Google Maps for Mobile (GMM) and KML

Saturday, October 6th, 2007

It has come to my attention and the attention of many programmers who work in multiple spaces (Wired and Wireless, Fixed and Mobile) that Google Maps lacks a certain - oomph, when you are dealing with Mobile Devices. Apart from not being able to do anything with technology as a whole - or, in most cases (except maybe the iphone) where Ajax is not supported on mobile devices - that you can’t get the full benefit of Google Maps!

Well, this sucked. The GMM release more recently has now incorporated a certain level of support however, for KML files.  How does it work? Simple! All you do, is grab your phone, download the latest version of GMM, http://www.google.com/gmm, and click “Find Location”.

Once you’ve got your location box open, simply type in the web accessible address of your KML file! The limitations of the remote KML are many, however as long as your place names are sort of short (not a paragraph!) and that your text boxes and information boxes only contain plain text (with no \r\n chars - thats ^M for you windows weenies) and there are no HTML lines in your info - then your all good!

The limitation is about 100 characters for descriptions - and about 30 for place names! So get Mobile Mapping today!

Windows Crash on Debian File Share

Friday, October 5th, 2007

Yes - you heard me right, another great way to crash your windows machine!

Recently, while rolling out some new samba shares on our network, I did all the good things that one should do to make the experience great for users, part of that was creating some new shares that were quicker to get to!

Unfortunately for me, (and my users) I mistyped one of the directories in the samba share and here is what it said:

The directory I wanted:
/whshare/media/training

The directory I typed:
/whshare/media/trianing

What did this mean to our users? Well of course, the helpdesk started getting nasty questions about why the Windows PC would crash while accessing Samba shares. The helpdesk were mighty confused by this and could not understand why this was happening.

Once it was passed back to us, we took a quick look and sure enough, crashed our VM instances! Not cool. Looking into it a bit closer, I checked the directory permissions, thinking this could be the issue.. They were fine, checked the mods, they were fine, then I checked samba conf and there it was, staring right at me… invalid spelling!

So next time you crash windows while your accessing samba shares, check to make sure your samba shares are valid - one keystroke away from mass bsod!