Thank you Marcelino and Robert for your valuable input. It was helpful. Marcelino, your solution seems to resolve the issue, however I find Robert's suggestion "easier" (more compliant with the existing code I already have). What I would like to add to the discussion is that I set the projection of the raster like so:
projection(r) <- "+proj=NA" This is the result I got<http://imagepaste.nullnetwork.net/viewimage.php?id=841> . Cheers, Roman On Thu, Apr 22, 2010 at 7:21 PM, Robert J. Hijmans <r.hijm...@gmail.com>wrote: > I suspect this is because the raster you are using has a > longitude/latitude CRS. This is the default if you create it using > raster() -- I may change that. > Set it to NA (or to what it really is) > > projection(r) <- NA > > And try again... > > Robert > > > The picture suggest that you are not computing the distance to a > single point, but rather to a checkerboard (for one 'color' the black > or the white fields) like distributed set of points. > > 2010/4/22 Roman Luštrik <roman.lust...@gmail.com>: > > This is the normal behavior (see image here) 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). 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.hijm...@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.lust...@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@stat.math.ethz.ch > >> > https://stat.ethz.ch/mailman/listinfo/r-sig-geo > >> > > > > > > > > > -- > > In God we trust, all others bring data. > > > -- In God we trust, all others bring data. [[alternative HTML version deleted]]
_______________________________________________ R-sig-Geo mailing list R-sig-Geo@stat.math.ethz.ch https://stat.ethz.ch/mailman/listinfo/r-sig-geo