Search code examples
pythonrshapelyreticulate

How to transform MultiLineString produced in python to R object?


I'm integrating a python function in R code using the reticulate package, the original output in python is MultiLineString. Class of output in Rstudio is :

> class(result)
[1] "centerline.geometry.Centerline"                   "shapely.geometry.multilinestring.MultiLineString"
[3] "shapely.geometry.base.BaseMultipartGeometry"      "shapely.geometry.base.BaseGeometry"              
[5] "python.builtin.object"

> typeof(result)
[1] "environment"

Output in Rstudio :

> result
MULTILINESTRING ((188.84310143 -25.44115933212567, 189.0883871453183 -25.39224242194983),
(189.34310143 -25.40412148482432, 189.0883871453183 -25.39224242194983))

I'm new to python, is there any way to transfer the output in python or R, so I can use it to make Spatiallines or SF object?


Solution

  • #output of python function in R 
    > class(result)
    [1] "centerline.geometry.Centerline"                   "shapely.geometry.multilinestring.MultiLineString"
    [3] "shapely.geometry.base.BaseMultipartGeometry"      "shapely.geometry.base.BaseGeometry"              
    [5] "python.builtin.object"
    
    > typeof(result)
    [1] "environment"
    
    #Convert from Python to R object 
    processed <- reticulate::py_to_r(result)
    
    #change type from environment to character 
    result_2<-type.convert(processed,as.is = TRUE)
    
    #read WKT and construct SpatialLines
    x<-readWKT(result_2)
    
    
    > x
    class       : SpatialLines 
    features    : 1 
    extent      : 187.6103, 191.6382, -25.8519, -24.55464  (xmin, xmax, ymin, ymax)
    crs         : NA