I used the function landgenreport from the package popgenreport in R to create an object result.

This object is a list with various objects including SpatialLines class objects. I am trying to write these lines to a file readable in GIS software such as ArcGIS or QGIS. The problem is that I can not find a way to do that. I've tried several functions from several packages and I always get an error.


results$leastcost$paths[[1]][[78]] #this is the specific SpatialLine
Error in writeSpatialShape(results$leastcost$paths[[1]][[78]],"linha78.shp"):x is aSpatialLinesobject, not a compatible Spatial*DataFrame

So I tried to transform the SpatialLines object to a SpatialDataFrame and got another error message.

x = data.frame(linha78)
Error in as.data.frame.default(x[[i]], optional = TRUE) : 
cannot coerce class "structure("SpatialLines", package = "sp")" to a data.frame

I've tried a lot a different possible solutions I read here and there, but it simply does not work. As such:

writeOGR with a spatialpolygon simplified by gSimplify

How to export a dataset with "SpatialPolygonsDataFrame" as a shapefile



1 Answer 1


If you're using maptools it says not to specify the extension ".shp"


results$leastcost$paths[[1]][[78]] #this is the line

Update: You need to create a SpatialLinesDataFrame in order to write it out. In the OP you just created a data.frame (not spatial). id=1:length creates data.frame that has rows 1 to the length of your linha78. Try typing length(linha78 into the console, it should give you the number of rows for linha78 :

results$leastcost$paths[[1]][[78]] #this is the line
df<-SpatialLinesDataFrame(linha78, data.frame(id=1:length(linha78)))
writeOGR(df, dsn="c:/Ouputs" ,layer="linha78",driver="ESRI Shapefile")
  • I got an error. Error in writeSpatialShape(linha78, "linha78") : x is aSpatialLinesobject, not a compatible Spatial*DataFrame Nov 23, 2016 at 16:07
  • when you run class(linha78) what is the output?
    – GISHuman
    Nov 23, 2016 at 16:13
  • > linha78 class : SpatialLines features : 1 extent : -40.59125, -39.80625, -20.37875, -19.08125 (xmin, xmax, ymin, ymax) coord. ref. : +proj=merc +units=m +ellps=WGS84 Nov 23, 2016 at 16:15
  • It worked just fine. Thank you very much! One question, what does the id=1 do? Nov 23, 2016 at 16:32
  • 1
    @Kazuhito I think so, I think OGR is better in this instance because it includes the projection.
    – GISHuman
    Nov 23, 2016 at 19:49

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.