I want to run an R-script from a Python script. An R-script is required to project the coordinates of a laton in another coordinate system. I examined two options for this. In the first version, I like to analyze the lat and lon coordinates in an R-script, which is shown below. Then, finally, I would like the R-script to return x and y back to the python script, but I cannot figure out how to do this.
project<-function(lat,lon){ library(sp) library(rgdal) xy <- cbind(x = lon, y = lat) S <- SpatialPoints(xy) proj4string(S) <- CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs") Snew <- spTransform(S, CRS("+proj=sterea +lat_0=52.15616055555555 +lon_0=5.38763888888889 +k=0.9999079 +x_0=155000 +y_0=463000 +ellps=bessel +units=m +no_defs")) x <- coordinates(Snew)[1] y <- coordinates(Snew)[2] return(x,y) }
For my second option, I considered using an R-script at the bottom with lat and lon already in it. I am trying to run this from python using subprocess.Popen ('Rscript project.r', shell = True) .wait () But this does not work. He does not write the xy.txt file. However, if I run this from the cmd line, the R-script does the job. Who can help me with one of these two options?
library(sp) library(rgdal) lat <- 52.29999924 lon <- 4.76999998 xy <- cbind(x = lon, y = lat) S <- SpatialPoints(xy) proj4string(S) <- CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs") Snew <- spTransform(S, CRS("+proj=sterea +lat_0=52.15616055555555 +lon_0=5.38763888888889 +k=0.9999079 +x_0=155000 +y_0=463000 +ellps=bessel +units=m +no_defs")) x <- coordinates(Snew)[1] y <- coordinates(Snew)[2] cat(x, file="xy.txt",sep="") cat(y,file="xy.txt",append=TRUE)
source share