Creating Dynamic Google Earth Applications with Python

Posted on October 8, 2007. Filed under: Google Earth, Network Links |

In previous posts I’ve covered the general concepts and underlying KML that drives Google Earth Network Link functionality.  What I’d like to do in this post is introduce you to the concept of using CGI scripting with the Python programming language to create dynamic mapping applications.   Python is an easy to use, open-source, extensible, interpreted language which many ESRI users are already familiar with for accomplishing geoprocessing tasks in ArcGIS. 

The Task
The goal of this post is to create a dynamic Google Earth application showing active wildland fire events in North America.  To accomplish this task we’re going to use the Network Link functionality provided by Google Earth in conjunction with a Python CGI script that will process comma delimited text files provided by the Fire Information for Resource Management Systems (FIRMS).  FIRMS generates new files each day and updates are applied to the files at approximately four hour intervals throughout the day.

Server Setup
A Google Earth Network Link gives you the ability to serve data from a remote server and is commonly used to distribute data to large numbers of users.  In this case we’ll be using a web server running IIS to process the Python script.  You will need to make sure that Python has been installed on the server that will run the script(s) and that your web server software (IIS in this case) is properly configured to run Python scripts.  Please click here to get detailed information on configuring IIS to use Python.  Please note that this step is not optional and your Python script will not run correctly until the web server has been configured.

Overview
In the figure below you will see a representation of the basic process of what takes place in a dynamic Google Earth application.  We’ll start with a KML file (WildlandFire.kml) which contains <NetworkLink> and <Link> elements that point to a Python script called generateFirePoints.py.  This script reads the daily FIRMS wildland fire data for North America and parses out the geographic coordinates (latitude, longitude) along with a confidence value and generates a KML data stream containing the wildlife fires to be plotted.  Finally, the data is presented in Google Earth. 

Creating WildlandFire.kml
My previous post gave you the details on the KML elements related to Network Links so I’m not going to rehash this information here, but I do want to cover the relevant elements used to point to your Python script.  As you may recall, the <NetworkLink> element contains a <Link> child element which can be used to point to a KML/KMZ file or CGI script.  The <href> child element of <Link> is where you specify the URL to your CGI script.  For example:

<Link>
   <href>http://www.geospatialtraining.com/GE/scripts/generateFirePoints.py</href&gt;
</Link>

The KML file that contains this link (WildlandFire.kml) makes a call to the Python script generateFirePoints.py located on a remote web server.  Click here to retrieve the full text of the WildlandFire.kml file.  To see the KML code you’ll need to open this file with a text editor.  Note the <refreshMode> and <refreshInterval> elements.  The value “onInterval” in the <refreshMode> element specifies that the Python script will be called on a time interval.  The time interval is given in seconds using the <refreshInterval> element.  In this case we’re using a four hour time interval. 

Python Script Contents
Our Python script (generateFirePoints.py) needs to do a number of things to correctly deliver a KML stream back to Google Earth. 

  • Return a response code of HTTP 200 (the web server takes care of this)
  • Set the response’s content type to text/plain or application/vnd.google-earth.kml+xml
  • Parse the FIRMS wildland fire text file for current fire locations
  • Build the appropriate KML elements and send to GE in a data stream

Response Content Type 
When responding to a request from Google Earth, you must set the response’s content-type to a suitable MIME type.  The MIME type for KML files is

  • application/vnd.google-earth.kml+xml

Before returning the KML stream containing your data you must set this MIME type which can be done with the following line of Python code:

print “Content-Type: application/vnd.google-earth.kml+xml”

Parsing the File
Each day FIRMS integrates remote sensing and GIS technologies to deliver hotspot/wildland fire locations in multiple formats including comma delimited text files containing coordinate information in the form of latitude and longitude coordinates along with other attribute information including the time of acquisition and a confidence value.  For our purposes in this example we’ll primarily be concerned with pulling out the coordinate values for plotting in Google Earth.  The text files are posted to the FIRMS ftp site each morning at 00:00 UTC.  The file continues to be updated throughout the day as new data becomes available.  The format for these files is as follows:

Latitude
Longitude
Brightness Temp
Along scan pixel size
Along track pizel size
Date
Time of acquisition
Satellite (A=Aqua and T=Terra)
Confidence

And here is an example:
36.549,-82.964,318.7,1.0,1.0,10/02/2007/1625/T/70

To download the sample file I’ve used in this post click here.

In this example we’re primarily interested in pulling out the latitude and longitude coordinates which will then be streamed as KML elements to Google Earth.  File manipulation including parsing comma delimited text files is extremely easy to do in Python.  Let’s examine the final generateFirePoints.py file.  Right Click and select Save to download a copy of the file.  Go ahead and open the file using a Python editor such as IDLE or with a text editor like Wordpad.  The first few lines of code simply set content type, KML header, KML body, and KML footer variables.  The only real item of note is in the kmlBody variable where we set up the <Style> element for the icon that we will use to display wildland fires in Google Earth. 

The real heart of the Python script is the next section.  We first open the FIRMS file and read in the contents of the file one line at a time into a list variable.  For purposes of this example we’ve hard-coded the path to the file name.  FIRMS creates new files each day for North America and the rest of the world and the filenames reflect the continent plus the year and Julian date so obviously hard-coding the file name would not work in a real application since the most up to date file name changes daily.  However, for our purposes in this example it will suffice.

We then loop through each line in the file and pull out the latitude, longitude, and confidence values, and write them into a KML stream.  The end result is a unique <Placemark> for each wildland fire that will be displayed in Google Earth.

Finally, we send the KML stream to Google Earth through the print statement along with the final KML output.  Notice that we first send the content type through the “print contentType” statement.  You will also need to close the FIRMS file.

The resulting display in Google Earth will look something like the figure below.

Our “Mastering KML in Google Earth” and upcoming “Creating Dynamic Google Earth Applications” cover these subjects in great detail.  For more information regarding training services from GeoSpatial Training Services please click here.

About these ads

Make a Comment

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Liked it here?
Why not try sites on the blogroll...

Follow

Get every new post delivered to your Inbox.

Join 47 other followers

%d bloggers like this: