Nada Labs

Converting a GeoRSS file to a Google Earth kml

by on May.04, 2010, under Hacks, Software

I had a need to convert GeoRSS files used with iMapPlot to a Google Earth kml which I accomplished with the following shell script. It’s ulgy but gets the job done. The script can also be downloaded: rss_process.zip

#!/bin/bash
infile=$1
outfile=`echo $infile | sed -e 's/\.xml/.kml/'`
grep -E '^<(title|description|georss\:(point|line)|link)>(.*)</\1>' "$infile" | sed -r -e 's/(-?[0-9]{2,3}\.[0-9]{1,}) (-?[0-9]{2,3}\.[0-9]{1,})/\2,\1,0/g' -e 's|<(title)>(.*)</\1>|\t<Placemark>\n\t\t<name>\2</name>|' -e 's|<(georss:point)>(.*),(.*),0</\1>|\t\t<LookAt>\n\t\t\t<longitude>\2</longitude>\n\t\t\t<latitude>\3</latitude>\n\t\t\t<altitude>0</altitude>\n\t\t\t<range>500</range>\n\t\t\t<tilt>0</tilt>\n\t\t\t<heading>0</heading>\n\t\t</LookAt>\n\t\t<Point>\n\t\t\t<coordinates>\2,\3,0</coordinates>\n\t\t</Point>\n\t</Placemark>|' -e 's|<(georss:line)>(.*)</\1>|\t\t<LineString>\n\t\t\t<tessellate>1</tessellate>\n\t\t\t<coordinates>\2</coordinates>\n\t\t</LineString>\n\t</Placemark>|' -e '1 s|.*<Placemark>|<?xml version="1.0" encoding="UTF-8" ?>\n<kml xmlns="http://www.opengis.net/kml/2.2">\n<Document>\n|' -e '$ s|(.*)|\1\n</Document>\n</kml>|' -e 's|<(link)>(http://.*)</\1>|<description><a href="\2">\2</a></description>|' | sed -r -e '/<\/description>/ {
N
/\n.*<description>/ {
s/<\/(description)>.*\n.*<\1>//
}
}' | sed -r -e 's|<(description)>(.*)</\1>|<\1><![CDATA[\2]]></\1>|' > "$outfile"

An attempted explaination

The grep command only grabs the tags from the RSS file that we are interested in.

The first sed statement 's/(-?[0-9]{2,3}\.[0-9]{1,}) (-?[0-9]{2,3}\.[0-9]{1,})/\2,\1,0/g' finds two sets of numbers seperated by a space and swaps their order while seperating them with a space and appending ,0. This converts the ‘latitude longitude’ coordinates into ‘longitude,latitude,altitude’ as used by Google Earth.

The second statement converts the title block into a Placemark and name block.

The third formats a point placemark, providing look at information while the fourth converts a line placemark.

The fifth inserts the kml header while the sixth inserts the kml footer.

The seventh sed statement converts a link block into a description block with a HTML link in it.

The second sed command merges two description blocks into one by removing the first close block and the second open block.

sed -r -e '/<\/description>/ {
N
/\n.*<description>/ {
s/<\/(description)>.*\n.*<\1>//
}
}'

This is accomplished by looking for the </description> block and loading in the next line if it is found. The next line is then checked for the <description> tag and if found a replacement is performed removing the two tags and any characters in between.

The third sed command wraps the description in a CDATA block to allow HTML to be included in the description.

:, , , ,
No comments for this entry yet...

Leave a Reply

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...

Archives

All entries, chronologically...