I want to run an R-script from a Python script. The R-script is required for the projection of lat lon coordinates in a different coordinate system. I have considered two options to do this. In the first option I like to parse the lat and lon coordinates to the R-script which is shown below. Then finally I would like that the R-script returns the x and y back to the python script, but I can't 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've considered using the R-script at the bottom with the lat and lon already in it. I try to run this from python with subprocess.Popen('Rscript project.r', shell=True).wait() But this does not seem to work. It does not write an xy.txt file. If I run this from the cmd line, however, the R-script does the job. Who can help me out 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)