assigning raster cell values based on predefined criteria
How about this?: library(spatstat) library(maptools) # A example of polygon: ho_sp <- SpatialPolygons(list(Polygons(list(Polygon(cbind(c(0,1,1,0,0), c(0,0,1,1,0)))), ID="ho"))) plot(ho_sp) ho.win <- as(ho_sp,"owin") ho.mask <- as.mask(ho.win) ho.dist<-bdist.pixels(ho.mask) plot(ho.dist) ho.dist$v[1:10,1:5] HTH, Marcelino
At 14:05 22/04/2010, Roman Lu?trik wrote:
Content-Type: text/plain Content-Disposition: inline Content-length: 3181 This is the normal behavior (see image here<http://imagepaste.nullnetwork.net/viewimage.php?id=837>) when I use option A-ish (calculating distance to a single point). Intuitively, however, I would expect the plot to look something like this (crudely drawn for your amusement<http://imagepaste.nullnetwork.net/viewimage.php?id=838>). Can someone explain the logic behind the "tiled" pattern? They say a picture is worth a thousand words - hopefully, this is the case here and my question is clear. Cheers, Roman On Tue, Apr 20, 2010 at 6:55 PM, Robert J. Hijmans <r.hijmans at gmail.com>wrote:
Dear Roman, I think it depends on your data. Below are two examples (both assuming your polygons do not overlap). A) works if each cell in a polygon is closest to its own centroid (and not that of another polygon). It makes a distance surface with distanceFromPoints() and then uses mask() to mask out the areas not covered by polygons. B) uses linesToRaster() to set up a raster with impenetrable boundaries (the polygon outlines) and then use gridDistance() (i.e. the distance when only allowed to traverse certain cells). This method would fail if you have a polygon representing several islands; or a polygon with a hole with the centroid inside that hole. Also, you do not have values for the outer cells of a polygon. This is perhaps OK when polygons are adjacent (whose cell is it anyway?). If they are not adjacent yo could use edge() with 'outer' argument on the rasterized polygons instead of linesToRaster library(raster) p1 <- rbind(c(-180,-20), c(-140,55), c(-20, 0), c(-140,-60), c(-180,-20)) hole <- rbind(c(-150,-20), c(-100,-10), c(-110,20), c(-150,-20)) p2 <- rbind(c(-10,0), c(140,60), c(160,0), c(140,-55), c(-10,0)) pols <- SpatialPolygons( list( Polygons(list(Polygon(p1)), 1), Polygons(list(Polygon(p2)), 2))) xy <- coordinates(pols) #A r <- raster(nrow=18, ncol=36) r <- polygonsToRaster(pols, r) d <- distanceFromPoints(r, xy) x <- mask(d, r) plot(x) #B r <- raster(nrow=18, ncol=36) r <- polygonsToRaster(pols, r) l <- linesToRaster(pols, r) l[l>0] <- 0 p <- pointsToRaster(r, xy) pp <- cover(p, l) gd <- gridDistance(pp) gd[!is.na(l)] <- NA plot(gd) Robert On Tue, Apr 20, 2010 at 12:23 AM, Roman Lu?trik <roman.lustrik at gmail.com> wrote:
Dear list, I have a polygon that I converted to raster (kudos to
polygonsToRaster()). I
would like to assign values (through a custom function) to cells within
this
polygon (now as raster). Roughly speaking, cell value will depend on its distance from the central cell (polygon centroid). How would you go about doing this? Is there a more efficient way of doing this, or will I have
to
hard code it, cell by cell?
Cheers,
Roman
[[alternative HTML version deleted]]
_______________________________________________ R-sig-Geo mailing list R-sig-Geo at stat.math.ethz.ch https://stat.ethz.ch/mailman/listinfo/r-sig-geo
--
In God we trust, all others bring data.
[[alternative HTML version deleted]]
_______________________________________________ R-sig-Geo mailing list R-sig-Geo at stat.math.ethz.ch https://stat.ethz.ch/mailman/listinfo/r-sig-geo
________________________________ Marcelino de la Cruz Rot Departamento de Biolog?a Vegetal E.U.T.I. Agr?cola Universidad Polit?cnica de Madrid 28040-Madrid Tel.: 91 336 54 35 Fax: 91 336 56 56 marcelino.delacruz at upm.es _________________________________