Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create KML file for Google Maps from CSV

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

like image 798
CTSchmidt Avatar asked May 31 '26 07:05

CTSchmidt


2 Answers

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>'
like image 112
J. Katzwinkel Avatar answered Jun 01 '26 22:06

J. Katzwinkel


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")
like image 32
Theja Tulabandhula Avatar answered Jun 01 '26 22:06

Theja Tulabandhula



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!