|How to Georeference Images taken from GPS Enabled Cameras using Google Earth|
This intermediate Google Earth tutorial explains to users how to georeference images taken from a GPS enabled camera. Users will learn how to use PHP programming to read Exif metadata stored within the JPG images and use the geographic coordinates from the GPS to create placemarks in Google Earth. To complete this tutorial you must have a linux web server environment with Apache and PHP installed, and possess minor PHP programming skills.
I recently went hiking up in Whistler, BC and I brought with me the Olympus TG-810 digital camera. This camera has a GPS built into it, which communicates with satellites to provide an accurate Latitude / Longitude coordinate. When the camera takes a picture, it stores all of the camera settings into JPG’s using Exif (Exchangeable image file format) metadata. To find out more about Exif metadata check out the Wikipedia page here: http://en.wikipedia.org/wiki/Exchangeable_image_file_format
Setting up the Server Environment
For the purpose of this tutorial you can download the sample data here. The sample data contains a set of 4 images that were taken by the Olympus TG-810 with the GPS enabled, as well as a PHP script which we will modify. To start off, download the sample data to a well known location on your web server and extract the files. You should end up with geojpg.php in your parent directory, with two subdirectories: images and output. You will want to setup the ownership and permissions for the PHP script and the images folder so that your personal user has read write and execute permission, while the apache and other user groups only possess read and execute permissions.
Use the following commands as the root user to set the ownership and permissions of the PHP script and the images directory.
Note: The use of -R will also set these ownership and permissions settings for all the JPG’s found within the images/ directory.
Next we must setup the output directory to have Apache ownership with read write and execute permission, as PHP requires that Apache has write permissions to write the KML file to disk.
Use the following commands as the root user to set the ownership and permissions of the output directory.
Modifying the PHP Script
Now that all the ownership and permissions have been configured, we can start to modify the geojpg.php to match your server environment. Using a text editor program open up geojpg.php. First we must tell the PHP script where were want to create our ‘geojpg.kml’ file. Jump to line 12 and set the $kmlPath using a full system path starting from the directory root. Make sure that you place the ‘geojpg.kml’ in the output directory. Use line 13 to set the $documentName.
Lines 16 - 53 set up the header of our KML document. This header provides the tags for the default and mouseover highlight. Using the <StyleMap> tag you can use ‘default0’ to reference the two <Style> tags. This StyleMap will create Placemarks using small camera icons , which will appear larger when the mouse hovers over them. Feel free to change the icon or modify the settings as you see fit.
On lines 56 – 57 you must specify the relative path and full system path to the images directory.
Lines 60 – 61 create a loop which rolls through all the files in the images directory. Line 66 reads the Exif metadata stored within a JPG and stores the values into the $exif array. To print out the Exif data array use lines 69-74 or 77-78. To enable these lines, simply remove the // comments at the beginning of each line. Lines 81 – 93 are used to fetch the File name, date time and the GPS coordinates. Note: If your images were taken from a different camera you must adjust the variables found in lines 81-93 accordingly.
In order to make Placemarks in our Google KML file, we must convert the Latitude / Longitude coordinates from Degrees Minutes Seconds (DMS) into Decimal Degrees (DD).
Convert Degrees Minutes Seconds (DMS) into Decimal Degrees (DD)
DD = # Degrees + # Minutes / 60 + # Seconds / 3600
Lines 102 – 107 are used to convert DMS into DD. Note: For Southern Latitudes the decimal degrees will be represented by a negative value, also for Western Longitudes the decimals degrees will be negative as well.
Lines 110 – 118 create a <Placemark> for each image that contains a GPS coordinate. Note on line 116 that Google KML uses longitude, latitude in the <coordinates> tag to represent the x and y positions on the Earth.
Once the loop has completed we must close off the KML file using lines 125 – 128. Now that the KML is complete, we can create a single ZIP file to combine the images and the KML file together. A KMZ file is essentially a ZIP file designed for Google Earth.
Jump to line 133 and set the $kmzPath using a full system path starting from the directory root. Make sure that you place the ‘geojpg.kmz’ in the output directory.
Now that the script is all ready to run, open up an internet browser and navigate to your geojpg.php script. Once you run it, the ‘geojpg.kmz’ file should be located in your output directory. Download the file and open it using Google Earth.