[R-sig-Geo] R to Python - rpy - definition of window pattern, related with 'spatstat library - help needed

volkan kepoglu vkepoglu at gmail.com
Mon Feb 4 15:04:10 CET 2008


I am new in R and Python. I write a couple of lines. The code does the
followings; Read point shp file, Compute a kernel smoothed intensity
function from a point pattern; density function, and convert pixel
image to spatialgriddataframe and export spatialgriddataframe to tif
raster format.

the code is running in R. I am trying to use the code in python, but
could not. Could you help me?

my original R code;

library(maptools)
shpPoint <- readShapePoints ("exmPnt.shp")
library(spatstat)
w <- as.owin(c((shpPoint at bbox["coords.x1","min"]),
(shpPoint at bbox["coords.x1","max"]),
(shpPoint at bbox["coords.x2","min"]),
(shpPoint at bbox["coords.x2","max"])))
MYpppFormat <- as.ppp(shpPoint at coords, w)
z <- density.ppp(MYpppFormat, edge=TRUE)
plot(z, main="Kernel smoothed intensity of point pattern")
plot(shpPoint, add=TRUE)
z_sgdf <- as.SpatialGridDataFrame.im(z)
outfilename <- tempfile(pattern="file", tmpdir = tempdir())
writeGDAL(z_sgdf, outfilename, drivername = "GTiff")
file.rename (outfilename, "exmPnt_density.tif")

conversion of R code to Python with Rpy;

from rpy import *
r.library('maptools')
shpPoint = r.readShapePoints ("exmPnt.shp")
r.library('spatstat')

# rest of them could not be converted to Rpy
w = r.as_owin(r.c((r.bbox(shpPoint)["coords.x1","min"]),
(r.bbox(shpPoint)["coords.x1","max"]),
(r.bbox(shpPoint)["coords.x2","min"]),
(r.bbox(shpPoint)["coords.x2","max"])))
MYpppFormat = r.as_ppp(shpPoint at coords, w)

# possible conversion, not tried.
z = r.density_ppp(MYpppFormat, edge=TRUE)
r.plot(z, main="Kernel smoothed intensity of point pattern")
r.plot(shpPoint, add=TRUE)
z_sgdf = r.as_SpatialGridDataFrame_im(z)
r.library('rgdal')
outfilename = r.tempfile(pattern="file", tmpdir = r.tempdir())
r.writeGDAL(z_sgdf, outfilename, drivername = "GTiff")
r.file.rename (outfilename, "exmPnt_density.tif")

IN R, working;
> w <- as.owin(c((shpPoint at bbox["coords.x1","min"]), (shpPoint at bbox["coords.x1","max"]), (shpPoint at bbox["coords.x2","min"]), (shpPoint at bbox["coords.x2","max"])))
> w
window: rectangle = [575490, 619430] x [4890400, 4934800] units

IN PYTHON, giving the following error;
>>> set_default_mode(NO_CONVERSION)
>>> w = r.as_owin(r.cbind((r.bbox(shpPoint)["coords.x1","min"]),
(r.bbox(shpPoint)["coords.x1","max"]),
(r.bbox(shpPoint)["coords.x2","min"]),
(r.bbox(shpPoint)["coords.x2","max"])))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: sequence index must be integer, not 'tuple'
>>> set_default_mode(BASIC_CONVERSION)
>>> w = r.as_owin(r.cbind((r.bbox(shpPoint)["coords.x1","min"]),
(r.bbox(shpPoint)["coords.x1","max"]),
(r.bbox(shpPoint)["coords.x2","min"]),
(r.bbox(shpPoint)["coords.x2","max"])))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: each subindex must be either a slice, an integer,
Ellipsis, or NewAxis
>>>

What should I do?

Thanks,
Volkan Kepoglu
PHD Candidate,
Department of GGIT in METU,
Ankara, Turkey.




More information about the R-sig-Geo mailing list