Text File To kml – Perl Script

Google Earth reads and writes a special form of xml file called a kml (keyhole markup language). Many other geographic viewers and GISs can also read kml files so it’s not a bad thing to be able to make kml files for sample location data. I assume there are many ways to do this. The way I have done it is via a perlscript that I wrote. This post provides that script and explains what it does.

Here is the script, its called texttokml.pl.

It’s very simple and I commented it heavily so even the most naive perl programmer should be able to figure it out and change it but if you want me to hold your hand just ask.

Explanation follows . . .


The user specifies an input file on the command line when invoking the script. This file should be a tab-delimited text file with no header row (although the presence of a header row is unlikely to cause a serious problem). Each row should contain five columns of data for each sample/location:

  1. Group ID (this could be species or other taxon or population or whatever grouping variable you like). This will be the folder in Google Earth that the sample will be in.
  2. Sample ID. This will be displayed on the Google Earth map with the icon.
  3. Comments/notes (this is the text that will appear in the bubble that Google Earth will display when you click on the icon for sample). I use the “comment” or “history” text from the USDA accession.
  4. Latitude. I use the decimal minutes lat from the USDA accession info but any lat-long data that Google Earth can interpret will work.
  5. Longitude. As above.


The script will  create a new file in the same directory as the input file. It should have the same name as the input file but with a .kml extension.

This file is a very simple .kml file. A lot more elaboration is possible. I haven’t bothered with anything over and above the minimum that I require.

When you open the file in Google Earth you should see all of the samples on the map marked with the default location icon – yellow push-pin – and their sample names. You should see the samples arranged by group ID into sub-directories. When you click an individual sample’s icon you should see the comments/notes in the bubble.

It’s easy to do simple formatting, like changing the colour of the push-pin icon for each group, in Google Earth.

Example File

Here is an example/demo file. It’s a tab-delimited text file for a small selection of my samples. The data are all from the USDA accession details for each sample. The file was made in LibreOffice. Use this file to check that the perl script works for you.

Dan E.