Customizing Google Maps - Part II - Converting Data to KML

KML isn’t a terribly difficult XML schema to create and edit by hand, but if you want to use some of your GIS data, there’s no need to go typing out coordinates until your carpel-tunnel flairs up. There are a couple of handy ways to get your data into KML.

First, let’s suppose you have your data in an ESRI format, such as a shape file or a geodatabase. You have a lot of options for getting your data into KML, and I’ll highlight a couple of them.

First, if you have an extra $100, the nicest tool I’ve seen for getting your ESRI data into KML is Arc2Earth. This lets you convert pretty much everything in your MXD, including multiple layer support, chart symbol renderers, user defined graphics, rasters, tins - a whole bunch of stuff. They have a demo version you can try. If you are committed to ESRI data formats and you are going to be converting data to KML often, it’s probably worth $100.

There are some free ArcMap extensions that do a reasonable job if you’re short on cash. Export to KML was created by the City of Portland and is an ArcMap extension that allows you to export individual layers to KML. It has a number of GUI-driven options for elevation extruding, transparency, labeling, etc. Another great ArcMap extension along these lines is KML Home Companion, which offers a different set of features (i.e. doesn’t have some of the formers, and has a few the former doesn’t have). Both of those extensions will automatically project your data to WGS84, provided your data has a projection tied to it.*

Of course, you’ll have to re-create your KML file to keep it current with your data, but that’s not so bad.

If you are using the latest version of PostGIS (1.2.1), you can actually dish out your PostGIS data as KML directly with a little server-side scripting via the asKML function. Suppose you have a point layer called town_halls. To get the points as KML and the other data you’d like to see, you can simply run:

select asKML(the_geom, 6) as kmlpoints, name, address from town_halls

The “6” is telling the function how many digits to pass back. Otherwise you’ll have coordinates like “-80.123123123123123”, when “-80.1231” would do just fine. Remember, these are text files that have to be parsed - the smaller the better. Your kmlpoints field will look like this:

<Point><coordinates>-80.6563,35.1739,0</coordinates></Point>


Now all we need is a little wrapping, and we can dish KML out directly from PostGIS. Here’s a PHP example:

# Ye olde varialbe to hold the KML
$kml = ‘<?xml version=”1.0” encoding=”UTF-8”?>’;
$kml .= ‘<kml xmlns=”http://earth.google.com/kml/2.1">';
$kml .= ‘<Document>’;

# Ye olde pdo database connection
$pdo = new PDO(“pgsql:dbname=Database;host=Server”,”username”,”password”);

# Look through our records
foreach ($pdo->query(“select asKML(the_geom, 6) as kmlpoints, name, address from town_halls”) as $row) {
$kml .= ‘<Placemark>’;
$kml .= ‘<name>’.$row[“name”].’</name>’;
$kml .= ‘<description>’.$row[“address”].’</description>’;
$kml .= $row[“kmlpoints”];
$kml .= ‘</Placemark>’;
}

# Finish ye olde KML
$kml .= ‘</Document>’;
$kml .= ‘</kml>’;

echo $kml;
In this way you can convert any of your PostGIS to KML in any fashion you’d like. It isn’t the most straight-forward affair, but it means your KML is in sync with your data, you don’t have duplicate copies of data, you have full control of the conversion process, and you don’t have to bother dumping data out to KML whenever you need an update. Name this file town_halls.kml.php (or whatever), put it on your web server, and it’s out there forever.


*If you have data with no projection assigned to it, grab a shovel and give yourself a good whack in the face.