i have a df with a character colum that contains an arrays of wgs84 coordinates.

> head(df$Multipoint)
[1] "[[11.038156,44.525654],[9.768989,45.620678],[7.923145,45.060564]]"                                              
[2] "[[9.768989,45.620678],[11.282019,46.441482],[11.858295,45.651340]]"                                             
[3] "[[11.038156,44.525654],[9.768989,45.620678],[11.126326,43.450791],[11.858295,45.651340]]"                       
[4] "[[11.038156,44.525654],[9.768989,45.620678],[7.923145,45.060564]]"                                              
[5] "[[16.081622,40.499836],[14.840397,40.859856],[12.766940,41.980122],[14.595315,41.684185],[16.619494,40.984473]]"
[6] "[[11.038156,44.525654],[11.126326,43.450791]]" 

As you can observe, there are both points and lines (no polygons). I need to convert it in a spatial dataframe, usually i use

coordinates(object) <- value

but, in this case i need to obtain lines too.

1 Answer 1


Those arrays look a bit like Javascript arrays, and I suspect you might have got them from a web service, and maybe they are JSON data. This means you can decode them with jsonlite or one of the other JSON packages in R:

> fromJSON("[[11.038156,44.525654],[9.768989,45.620678],[7.923145,45.060564]]")
          [,1]     [,2]
[1,] 11.038156 44.52565
[2,]  9.768989 45.62068
[3,]  7.923145 45.06056

with that saved as coords you can then construct spatial lines objects:

> SpatialLines(list(Lines(list(Line(coords)),ID=1)),proj4string=CRS("+init=epsg:4326"))
class       : SpatialLines 
features    : 1 
extent      : 7.923145, 11.03816, 44.52565, 45.62068  (xmin, xmax, ymin, ymax)
coord. ref. : +init=epsg:4326 +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 

Add a sprinkling of basic R code to work over the rows of your data frame and make a spatial lines data frame, if that's what you need.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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