3

I've been researching days about a problem... I am new to KML and i want to create one file with The folowing informations:

Email
address
postcode
country
telephone
fax
internet
name
image
license
Call number
lat
lng

The csv file is maintained. If there is a tool I would be very happy. Otherwise, I would write it manually, if I know the syntax.

I am using a mac so windows KML creators not come into question... I have tried many tools and none was what I wanted.

The best tool I've found was http://batchgeo.com/de/ but there is no KML file anymore.

[EDIT]

Is there a good way to solve this in a python script? i already have a .csv!

Best regards Curtis

CTSchmidt
  • 1,155
  • 3
  • 17
  • 30
  • You can look into ogr2ogr of GDAL library. If its impossible to convert csv to kml, you can convert csv to shapefile before (using GIS such QuantumGIS or else) and then convert it to kml. – Below the Radar Jan 10 '13 at 14:37
  • Or you can try this tool: http://kmltools.nobletech.com/csv2kml – Below the Radar Jan 10 '13 at 14:38
  • i tryed this tool first but now i test around for a few more times – CTSchmidt Jan 10 '13 at 14:59
  • Another idea, you can import your csv in fusiontablelayer in Google Drive. Then, you can display it in google maps or openlayers, but the table may be public if you have a free google API key. – Below the Radar Jan 10 '13 at 15:51
  • is There maybe a solution to get the file via python? – CTSchmidt Jan 14 '13 at 10:26
  • 1
    Follow this question: http://stackoverflow.com/q/7529538/1914034 – Below the Radar Jan 14 '13 at 16:03
  • When looking at the question linked to by @Burton449, don't forget to check out the newer answer which mentions the `simplekml` library, which most likely can be used on any platform with Python (including Mac). – John Y Jan 17 '13 at 23:02

3 Answers3

4

For reading .csv files, you can use the csv module like this:

reader = csv.reader(open("file.csv"))
for row in reader:
  for value in row:
    ...  

You might get in trouble when your file is UTF-8 encoded, since csv does not support that. But there is a wrapper which will take care of this.

You can, of course, also simply read your file line by line and split it by commas: values=line.split(',').

Being that that the kml format is not very complicated, the toughest part of creating a representation of your data is deciding what it should look like. A very simple piece of code to insert the values read from the csv file could look like this:

# read field labels from first line in file
header = reader.next()
# prepare static output
templates = [('  <Placemark>\n   <name>{}</name>\n', 'name'),
         ('   <description>\n    <![CDATA[\n     <img src="{}"/>\n', 'image'),
         ('     {}\n', 'address'),
         ('     {}\n', 'postcode'),
         ('     {}\n', 'country'),
         ('     Tel: <span class="tel">{}</span>\n', 'telephone'),
         ('     Mail: <span class="mail">{}</span>\n', 'Email'),
         ('   </description>\n   <Point>\n    <coordinates>{},', 'lat'),
         ('{}</coordinates>\n   </Point>\n  </Placemark>\n', 'lng')]
# lookup function for field values. leading and trailing whitespace will be removed
value = lambda field, array: array[header.index(field)].lstrip().rstrip()

# start output
print '''<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
 <Document>'''
# insert values into xml
for row in reader:
    for t, f in templates:
        print t.format(value(f, row)),

print ' </Document>\n</kml>'
J. Katzwinkel
  • 1,923
  • 16
  • 22
4

Use simplekml if you have already imported your csv data.

From its introduction page:

import simplekml
kml = simplekml.Kml()
kml.newpoint(name="Kirstenbosch", coords=[(18.432314,-33.988862)])
kml.save("botanicalgarden.kml")
0

I believe BatchGEO supports KML on the google earth tab: http://batchgeo.com/features/google-earth-kml/

If you are looking to having this run on the fly or be recreated when an update to the CSV has been made I would suggest the following:

  1. Pick any language which an established KML parsing / generation library (ruby, java, C# [I really like SharpKML for .Net], PHP, etc).
  2. Using the library create your KML objects, then iterate your CSV file populating your KML document object with Placemarks (or whatever)
  3. Export your KML as and where needed

The script posted in the comments will also certainly work, but I would use an existing KML library.

Matthew
  • 9,851
  • 4
  • 46
  • 77