Search code examples
rkmlr-maptools

Draw line from latitude Longitude points


I am trying to use maptools to draw a series of lines from a set of extracted data. The lat and long data is in a long dataset 72000 points with another column containing a label for each individual line. This label is repeated for each point.

# from the sp vignette:
M2 = cbind(c(28.55841,25.2479,46.24100,25.24810,25.24570,25.24670,25.24840,28.55820,25.2460),      
       c(77.11510,55.3591,6.11329,55.35931,55.36831,55.38071,55.35801,77.11601,55.3684)
       )

L2 = Line(M2)

# I want to create a loop here to add a new line each time there is a break ie when the line label changes.  
# Perhaps there is a better way to organise this step? 

S2 = Lines(list(L2), ID="b")

library(maptools)

kmlLines(S2, kmlfile = "zrivers.kml", name = "R Lines",
     description = "Hello!", col = "blue", visibility = 1, lwd = 1,
     kmlname = "", kmldescription = "")

I get a message

Error in vector("character", length(spatialLines@lines)) : no slot of name "lines" for this object of class "Lines"

I don't see an easy way to create the Line object.

Would it be easier to push the data into a shape object and then use existing code like the rivers example?


Solution

  • I tried the idea of creating a shape file and then processing is using the kmlLines function. I found that I was just hitting new problems due to my lack of knowledge.

    In the end I wrote some R code that will process the whole dataset and create the required KML output. I chose to use the format in the KML file and teh final result is quite pleasing!