简体   繁体   中英

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

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")

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>'

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM