Having the dataframe:

lon-1 lat-1 lon-2 lat-2 some_val

And using the R code:

proj4string(mydata) <- CRS("+init=epsg:28992")
mydata_ll <- spTransform(mydata, CRS("+proj=longlat +datum=WGS84"))
writeOGR(wwverkeer_ll["some_val"], "mydata.kml", layer="some_val", driver="KML") 

I get an unexpected output (kml file) containing:

  <ExtendedData><SchemaData schemaUrl="#some_val">
      <SimpleData name="some_val">3228</SimpleData>

Where I would actually like to have 4 coordinate points, instead of 3. And some certainty on this being the correct coordinates.

2 Answers 2


Adapting python code from: Converting SpatialPoints to Normal for exporting .csv

One gets:

import csv
#Input the file name.
fname = raw_input("Enter file name WITHOUT extension: ")
data = csv.reader(open(fname + '.csv'), delimiter = ',')
#Skip the 1st header row.
#Open the file to be written.
f = open('csv2kml.kml', 'w')

#Writing the kml file.
f.write("<?xml version='1.0' encoding='UTF-8'?>\n")
f.write("<kml xmlns='http://earth.google.com/kml/2.1'>\n")
f.write("   <name>" + fname + '.kml' +"</name>\n")
for row in data:
    f.write("   <Placemark>\n")
    f.write("       <name>" + str(row[6]) + "</name>\n")
    f.write("       <Point>\n")
    f.write("           <coordinates>" + str(row[1]) + "," +     str(row[2]) + "," + str(row[3]) +  "," + str(row[4]) + "</coordinates>\n")
    f.write("       </Point>\n")
    f.write("   </Placemark>\n")
print "File Created. "
print "Press ENTER to exit. "

The three numbers in the <coordinates> tag are long, lat, and height. I think what you really want is two <point> tags within each <placemark>. Your data is a MULTIPOINT data set, and the sp class you are using doesn't support that very well (if at all).

If you switch to the newer sf classes then you have full support for MULTIPOINT data, including writing to KML. The example in ?read_sf shows the creation of a MULTIPOINT data (via GeoJSON) and when writing that to a KML using st_write I get:


etc, which could be what you want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.