4

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)
user2357183
  • 43
  • 1
  • 3
  • Just out of curiosity, is there a reason why you don't just use the Python GDAL binding? (https://pypi.python.org/pypi/GDAL/) Seems like it would be a lot easier. – rici May 08 '13 at 23:19
  • If it's easier then I definitely would like to try that. However, I am not familiar with using gdal in python. Is there an example code of how to use gdal and project coordinates from one coordinate system into another? – user2357183 May 10 '13 at 10:46
  • I don't think it's much different from the R binding. If you just need to transform co-ordinates, you only need the proj4 bindings; it's as simple as this: `import pyproj; p1 = pyproj.Proj("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"); p2 = pyproj.Proj("+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"); pyproj.transform(p1, p2, 52.3, 4.77)` The lat/lon arguments can be lists, tuples, numpy arrays or scalars. See http://pyproj.googlecode.com/svn/trunk/docs/index.html – rici May 10 '13 at 17:06

2 Answers2

1

It looks to me you are almost there, some remarks:

  • x <- coordinates(Snew)[1] selects the first item, use x <- coordinates(Snew)[,1] to get the entire vector. This assumes that you pass vectors of lat and lon, and not single values. There is no reason here why we should not allow passing vectors of lat lon.

  • return(x,y) is not valid, R does not support returning multiple objects. In stead, just return(cbind(x,y)).

  • Inside a function I would use require.

In regard to running the code from Python, I'd have a look at Rpy. This allows you to call R code from within Python. Rpy does a lot of the hard work of passing R objects back and forth for you, so this is a nice option in this case.

The approach I would take in Rpy is to put project in a .R file, source that file, and call project.

Paul Hiemstra
  • 59,984
  • 12
  • 142
  • 149
0

For your first solution -- get R to output the coordinates to the console (using print or cat or whatever it is in R), then capture that in Python (see here: Running shell command from Python and capturing the output)

That will give you a string with teh lat/lon coords in it, you just have to Parse them out using the appropraite Python functions.

Community
  • 1
  • 1
Colin Nichols
  • 714
  • 4
  • 12
  • 1
    There is no need to manually parse the output of the R function, Rpy can do that for you much more easy. – Paul Hiemstra May 08 '13 at 17:33
  • Hi all, I'd like to thank you all for the very good answers. I think Rpy can do the job for me, however, Rpy does not seem to work with python version 2.5.4 and R version 2.14.2? – user2357183 May 08 '13 at 23:00