1

Having the dataframe:

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

And using the R code:

coordinates(mydata)<-~lon_start+lat_start+lon_end+lat_end
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:

<Placemark>
  <ExtendedData><SchemaData schemaUrl="#some_val">
      <SimpleData name="some_val">3228</SimpleData>
  </SchemaData></ExtendedData>
   <Point>
<coordinates>3.xxxxxx,47.yyyyyyy,54.zzzzzzzz</coordinates></Point>
   </Placemark>
   <Placemark>

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

1

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.
data.next()
#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("<Document>\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")
f.write("</Document>\n")
f.write("</kml>\n")
print "File Created. "
print "Press ENTER to exit. "
raw_input()
f.close()
1

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:

 <Placemark>
      <MultiGeometry>
       <Point><coordinates>3.2,4.0</coordinates></Point>
       <Point><coordinates>3.0,4.6</coordinates></Point>
       <Point><coordinates>3.8,4.4</coordinates></Point>
       <Point><coordinates>3.5,3.8</coordinates></Point>

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.