Create kml with time visualization using postGIS

PostGIS is a great tool to work with large spatial data. We can export a query as a txt file using the function COPY TO. Moreover, Postgres have XML-functions which allow you to write and read xml documents. With this functions you can create a KML document with time visualization. This means that using TimeSpan element we can visualize the spatial objects in a time animated way. The following example shows you how to create a kml document populated with points. Note that you need a column with a date/time data type.

	/*begin kml spatial object*/
	xmlelement(name "Placemark",
		/*kml time description*/
		 xmlelement (name "TimeSpan",
			/*start date, when the feature will be shown*/
			xmlelement(name begin,,
			/*end date when the feature will be hide*/
			xmlelement(name end,,
		 /*name of the feature object*/
		 xmlelement(name name, pointdescription),
		 /*feature tipe,point line or polygon */
		 xmlelement (name "Point",
			/*point coordinates*/
			xmlelement(name coordinates,a.x,',',a.y)))
	from tableA as a, tableB as b
	where a.id_a=b.id_b)
/*output path*/
TO '/tmp/output.kml';

Just one on step more to get the kml. You only need to add the header of the kml and the end (I didn’t manage to write it from Postgres). If you are a programmer will be easy to you to write a small code with python, java  or whatever to add those lines. Until then the esiest part is to open the kml doument with a txt editor and paste the following kml header:

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="" xmlns:gx="" xmlns:kml="" xmlns:atom="">

Go to the end of the document and close the elements with this code:


Open the file with Google Earth and enjoy it.


Deixa un comentari

Fill in your details below or click an icon to log in: Logo

Esteu comentant fent servir el compte Log Out /  Canvia )

Twitter picture

Esteu comentant fent servir el compte Twitter. Log Out /  Canvia )

Facebook photo

Esteu comentant fent servir el compte Facebook. Log Out /  Canvia )

S'està connectant a %s