我希望能够从Python脚本中运行一个R脚本。这个R脚本用于在不同的坐标系统中投影lat lon坐标。我考虑了两种方式来实现这个目标。第一种方式是将lat和lon坐标解析到下面的R脚本中,最后让R脚本将x和y返回给Python脚本,但我不知道该如何实现。
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)
}
对于我的第二个选择,我考虑使用底部已经有纬度和经度的R脚本。我尝试使用subprocess.Popen('Rscript project.r', shell=True)从python运行它。但是好像并没有起作用。它没有写入xy.txt文件。然而,如果我从命令行运行这个脚本,它可以正常工作。谁能帮我解决其中的一个选择?
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)
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)
lat/lon参数可以是列表、元组、numpy数组或标量。请参见http://pyproj.googlecode.com/svn/trunk/docs/index.html - rici