# [R-sig-Geo] assigning raster cell values based on predefined criteria

Marcelino de la Cruz marcelino.delacruz at upm.es
Thu Apr 22 14:54:11 CEST 2010

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")
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
>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