Update: my CSV to KML python script is now available as an online converter, right here:
Woah! This means I went through all the trouble of developing a Google App Engine App just so some of you can convert their POI data files! (You can thank me later.) However, it uses the same core code as posted below (which is very basic), so some problems might arise. I’m not saying they will! It’s just a warning. If you have any problems with the convertor or remarks, please post them in the comment section of this post.
Update #2: I revised the code for this project. It now no longer needs to replace characters that are unsupported in certain encodings! Additionally, the online convertor now lets you choose what encoding the CSV file is in. It will assume ISO-Latin 1 if you don’t provide an encoding type.
Let’s say you have a CSV (Comma Separated Values) file with locations that you want to add to Google Maps. Tough luck! Google Maps only swallows KML files.
After fruitlessly googling for “csv2kml” etc. for a while, I frustratingly put on my robe and wizard hat and said: “I’ll do it myself. DAMMIT!”. And so I did. (Well, minus the robe, hat and talking to myself parts.)
The following Python script takes a CSV file ‘file.csv’ and generates a brand spanking new ‘file.kml’, ready to be parsed by Google Maps
f = open('file.csv')
a = f.read()
b = a.split('\n')
r = '<?xml version="1.0" encoding="UTF-8"?>\n<kml xmlns="http://www.opengis.net/kml/2.2">'
for x in b:
x = x.replace(', ',',').decode('latin-1','ignore')
y = x.split(',')
if len(y) < 3:
elif len(y) > 3:
desc = ' '.join(y[3:])
desc = 'No description'
# Replacing non-XML-allowed characters here (add more if needed)
y = y.replace('&','&')
desc = desc.replace('&','&')
r += '\n<Placemark><name>'+y.encode('utf-8','xmlcharrefreplace')+'</name>' \
r += '\n</kml>'
f = open('file.kml','w')
In order to use the resulting KML file, select ‘Create new map’, then ‘import’ in the ‘My Maps’ section. Note that after importing, Google might cut off the list of landmarks after a certain number. However, if you save the map and return to it later, you’ll find that it indeed included all data points.
I used the KML tutorial on code.google.com
as a reference for this project. It mentions that you can add additional fields to the ones mentioned here, but for this code to work, the CSV file should have a format like: “latitude, longitude, name [,description]” (description is optional). Everything after ‘name’ will just be lumped together in the description field. Feel free to add your own fields where necessary.
Note how I’m replacing some characters like ‘&’ and ‘ë’, ‘ö’, etc. You will want to check your own CSV file for any special characters unsupported in XML (or just KML?) fields and replace those as well. Otherwise, Google will present you with a delightfully uninformative ‘Upload of file failed’-alert.
An example of a successfully imported converted CSV file, can be found here
. It was made with this
parking space data from ANWB.nl (Netherlands-only, and still disappointingly limited).
Note: I’m well aware that this post is quite Google-centric, as was the previous. This is purely coincidental. Google is evil, etc. etc., but it is also quite useful sometimes.