distance between raster cell centroids and spatial points
Gabriele, I think you can do: library(raster) x <- distanceFromPoints(ras, villages) Robert
On Mon, Jul 28, 2014 at 6:56 AM, Gabriele Cozzi <gab.cozzi at gmail.com> wrote:
Dear list,
I want to calculate the distance between the centroid of each cell in a
raster ?ras? and the closest village (villages are stored as
SpatialPointsDataFrame).
ras is as follow:
class : RasterLayer
dimensions : 5027, 3386, 17021422 (nrow, ncol, ncell)
resolution : 27.28532, 27.28532 (x, y)
I can obtain what I want with a sub-sample of ras that has less cells, and
here are my codes:
*# sub sample ras*
*sub_ras <- aggregate(ras, fact=20, fun=mean)*
class : RasterLayer
dimensions : 252, 170, 42840 (nrow, ncol, ncell)
resolution : 545.7063, 545.7063 (x, y)
*# extract x and y coord from each cell of the raster*
*centroid <- xyFromCell(sub_ras, c(1:ncell(sub_ras))) *
*# combine x and y into a spatial point object*
*centroid <- SpatialPoints(cbind(centroid[,1], centroid[,2]))*
*# calculate distance of each centroid to the closest village and then turn
it into a raster of *
*# extent ?sub_ras' and values 'dist'*
*dist <- nncross(as(centroid,"ppp"), as(villages, "ppp"))$dist *
*rasterdist2village <- rasterize(centroid, sub_ras, dist)*
*plot(rasterdist2village)*
I then tried to run the same codes using all cells of ras. After 15 minutes
the ?rasterize? function was still running and so I aborted the procedure.
I wonder whether there is a more elegant and computationally faster way of
doing what explained above?
Any input is highly appreciated
Best,
Gabriele
--
Gabriele Cozzi
Postdoctoral Research Associate
Population Ecology Research Group
http://www.popecol.org
Zurich University
Institute of Evolutionary Biology and Environmental Studies
Winterthurerstr. 190
8057 Zurich - Switzerland
E-mail: gabriele.cozzi at uzh.ch
Phone: +41(0)44 635 49 12
Fax: +41(0)16355711
http://www.ieu.uzh.ch
[[alternative HTML version deleted]]
_______________________________________________ R-sig-Geo mailing list R-sig-Geo at r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-geo