this space intentionally left blank

November 11, 2008

Filed under: tech»mobile

Given Latitude

Here are two Python scripts for getting latitude and longitude position from Google based on GSM cell tower ID numbers:

The first script is a slightly altered version of the script written by Google Maps Internals, with the struct.unpack() code updated (it originally cast the longitude into the wrong type). It hooks into the low-level data format used by the Google Mobile Maps application on S60 and other platforms instead of using JSON. This is very cool from a hack position, but it's obviously also very fragile, since the GMM API is unpublished and changes on Google's end can break it easily (and may have already done so, hence the improper cast).

The proper way to do this, then, is using the second bit of code, which accesses Google's JSON-based services. The primary function in that file, gGSMLocation(), is actually not specific to S60. It takes as its only argument the four components of a GSM cell tower ID in a tuple: the mobile country code, mobile network code, local area code, and cell ID, in that order. It returns the Google JSON object as a string. I left the return format serialized because working with JSON on PyS60 (or, as far as I can tell, any Python) can be a real hassle, and you may have a favorite approach. In Python 2.3 or later (S60 uses 2.2), you should just be able to use json.load() from the included modules.

The second function in the file, gGSM(), is useful only on Nokia phones. It's just a simple wrapper that feeds location.gsm_location() directly into gGSMLocation(), no arguments needed.

Working on this kind of code is frustrating in equal parts due to Nokia's design decisions and the limitations of Python itself. On Nokia's part, its idiotic code-signing initiative means that loading new libraries (such as JSON or XML parsing) into Python without cracking the signature check is way more trouble than it's worth. If you've broken the signature, you can just copy .py modules to c:\\resource\\, and .pyd modules to c:\\sys\\bin\\, but you can still run into platform security errors.

None of this is helped by the fact that Python is a pretty weird place to work. If you're used to ECMAScript, it's frustrating working with a scripting language that doesn't allow dynamic object creation, or doesn't support an alternate dot syntax for dictionary properties like Javascript does. The implicit declaration of variables, likewise, is kind of jarring--they just pop into existence, compared to a language with a dedicated "var" or type keyword. On the other hand, Python's significant whitespace is almost as annoying as the forest of brackets and parentheses used in Lisp or Objective-C, particularly on a mobile platform where screen space is at a premium.

Python actually has a quirk, I think, in that it's often pitched as a good learning language due (in part) to its lack of punctuation for blocks and line endings. But the flip side to that approach is that advanced code contains underscores (which are used to signal metaprogramming and low-level functions) and weird punctuation in direct proportion to its complexity. I feel like the learning curve goes from flat to nearly vertical the moment those underscores make their appearance, which seems like a serious flaw in a teaching language. Perhaps it's different when you're not teaching yourself.

In any case, I don't know what you might want to do with your latitude and longitude, but I hope people might find it useful. I have a few ideas I'd like to try, like creating a lost-phone-locator over SMS and a geotagged link generator, that could be useful for some work and hobby projects coming up. But these kinds of scripts are useful for all kinds of problems--Ethan Zuckerman recently wrote about farmers in Kenya who are using phones and GSM IDs to track elephant movement and protect their crops from pachyderm predation. You never know what you could do, given a little latitude (and longitude).

Future - Present - Past