{ End Bracket }
Geopegging
Joshua Trupin
Code download available at:
EndBracket2007_04.exe
(174 KB)
Browse the Code Online
As part of my birthday haul last year, I received a gem of a gift: a new camera. And not just any camera, either. It was a new Nikon D200.
One of the features that really grabbed my interest was its built-in GPS capabilities. If you had a standard serial-port GPS device, you could plug it into the camera via a converter cable and location data would be written into each shot you took.
The problem was that the serial-port GPS receivers I owned were museum pieces that didn't work with the camera, and new GPS devices tend to have USB connections. Arrgh! I decided to take the most foolhardy plunge since I bought fajitas at a Manhattan street fair two years ago. I created my own homebrew solution out of OEM parts. To make a long story short, it actually worked. If you want the heartstopping play-by-play, you can find it at
my blog.
Geopegging Adds GPS Location Data to a JPG(Click the image for a larger view)
I've been putting my photos up on my hosting site,
trupin.smugmug.com. Smugmug has a nice feature that automatically maps all your tagged photos, and even though it used Google Earth, I liked it. But I knew I could take this idea a step further. Hundreds, perhaps thousands, of photos could soon be turned from throwaway snapshots into vitally important records of time and place. Adding location data to photos is known as geotagging, but my program would extend that term with a slightly more specific concept, "geopegging."
Geopegging, which is now defined as "adding GPS location data to a JPG through a map-based interface," is a concept that took a few steps to drag into reality. First, I had to figure out where the geo data actually went inside a JPG file. There's surprisingly little information about this online, but I used a variety of sources and the power of the Microsoft® .NET Framework to create a simple EXIF editor. If you want to find the camera's lens aperture at the time of the photo, you look at the field tagged 0x9202. It turns out that the GPS data all goes in tags numbered 0 through 8. This didn't fill me with confidence, because I imagined someone choosing the first numbers they could think of, well after the fact. The Framework includes a GetPropertyItem member in the System.Drawing.Image class, so it's easy to extract header data if you know the EXIF tag value.
Next, I wrote a program that hosted Microsoft Internet Explorer® inside a Windows® Forms app so that I could display Virtual Earth™ and allow a user to select the location to be added to a photo. That was a bit easier. The fun part was getting Virtual Earth working within a frame of my desktop app so that it could communicate back and forth with the Windows Forms code in the app. This part involves a bit of JavaScript inside the window plus some tricks (documented in the Virtual Earth online help) that gets the two parts of the program talking to one another.
The trick here is to wire up the WebBrowser control with some script through its DocumentText property. You use AttachEvent to call a script function when the browser control is clicked; this function then calls window.external.YourFunctionName and passes it a string.
There's more complexity, including handling the Virtual Earth control properly, converting values, and writing to the JPG file. I've put my basic geopegging program up as a download and invite you to take a look at it. Now I've got a world full of tagged photos, and I can update them any time I want. I don't need the homemade GPS device at all...but I still use it, if only to show off a bit.
Joshua Trupin is the Executive Editor of MSDN Magazine and TechNet Magazine.