[R-sig-Geo] distances values from a point exhibit a hexagon-like distribution

Michael Sumner mdsumner at gmail.com
Tue Jun 22 10:37:16 CEST 2010


Sorry, but it still doesn't work for me. I'm surprised you could get
passed the first line, since "projs" is not an argument to raster -
it's not necessary for your example to have that set though, so I
ignore it.

I had to install igraph package as well, but still this fails at the
gridDistance line. Can you let us know your OS, R and package versions
for raster, sp, and igraph?  Use sessionInfo()  please.

library(raster)
library(sp)
library(igraph)

rst <- raster(nrow = 200, ncol = 200, xmn = -100, xmx = 100, ymn =
-100, ymx = 100)
sample.matrix <- t(matrix(c(50,-50, 50,50, -50,50, -50,-50), 2, 4))
sample.matrix <- rbind(sample.matrix, sample.matrix[1,])
sq.polygon <- SpatialPolygons(list(Polygons(list(Polygon(sample.matrix)),
ID = "effect_dist")))
rst[] <- runif(ncell(rst))
xy <- matrix(c(0,0), 1, 2)

rst.poly <- polygonsToRaster(sq.polygon, rst)
rst.lines <- linesToRaster(sq.polygon, rst)

rst.lines[rst.lines > 0] <- 0

rst.point <- pointsToRaster(rst, xy)

rst.cover <- cover(rst.point, rst.lines)

## fails at this line
rst.grid <- gridDistance(rst.cover)
rst.grid[!is.na(rst.lines)] <- NA
plot(rst.grid)

sessionInfo()

R version 2.11.1 (2010-05-31)
x86_64-pc-mingw32

locale:
[1] LC_COLLATE=English_Australia.1252  LC_CTYPE=English_Australia.1252
   LC_MONETARY=English_Australia.1252
[4] LC_NUMERIC=C                       LC_TIME=English_Australia.1252

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
[1] igraph_0.5.3 raster_1.1.7 sp_0.9-64

loaded via a namespace (and not attached):
[1] grid_2.11.1    lattice_0.18-8 tools_2.11.1


On Tue, Jun 22, 2010 at 6:28 PM, Roman Luštrik <roman.lustrik at gmail.com> wrote:
> Thank you Michael for taking interest in my problem. Here is the code that
> should work. The only difference is that here, for a polygon, I use a square
> instead of a circle (no biggie). I suspect gridDistance may have something
> to do with the "hexagon".
>
> library(raster)
> library(sp)
> rst <- raster(nrow = 200, ncol = 200, xmn = -100, xmx = 100, ymn = -100, ymx
> = 100, projs = "+proj=NA")
> sample.matrix <- t(matrix(c(50,-50, 50,50, -50,50, -50,-50), 2, 4))
> sample.matrix <- rbind(sample.matrix, sample.matrix[1,])
> sq.polygon <- SpatialPolygons(list(Polygons(list(Polygon(sample.matrix)), ID
> = "effect_dist")))
> rst[] <- runif(ncell(rst))
> xy <- matrix(c(0,0), 1, 2)
>
> rst.poly <- polygonsToRaster(sq.polygon, rst)
> rst.lines <- linesToRaster(sq.polygon, rst)
>
> rst.lines[rst.lines > 0] <- 0
> rst.point <- pointsToRaster(rst, xy)
>
> rst.cover <- cover(rst.point, rst.lines)
> rst.grid <- gridDistance(rst.cover)
> rst.grid[!is.na(rst.lines)] <- NA
> plot(rst.grid)
>
>
> Cheers,
> Roman
>
>
>
>
> On Tue, Jun 22, 2010 at 9:55 AM, Michael Sumner <mdsumner at gmail.com> wrote:
>
>> Could you please provide a reproducible example with useable code?
>>
>> Cheers, Mike.
>>
>> On Tue, Jun 22, 2010 at 5:36 PM, Roman Luštrik <roman.lustrik at gmail.com>
>> wrote:
>> > Can someone explain to me why values of distances from a point are not in
>> a
>> > uniform, circle-like fashion, but rather form a hexagon shape (see
>> > image<http://imagepaste.nullnetwork.net/viewimage.php?id=1075>)?
>> > Here is the code I'm using to produce this plot (kudos to
>> > Robert<
>> http://r-sig-geo.2731867.n2.nabble.com/assigning-raster-cell-values-based-on-predefined-criteria-td4929775.html#a4929775
>> >).
>> > Raster projection is "+projs = NA".
>> >
>> > rst.poly <- polygonsToRaster(circle.polygon, rst) #circle.polygon is the
>> > polygon I'm overlaying, rst is the raster
>> > rst.lines <- linesToRaster(circle.polygon, rst)
>> > rst.lines[rst.lines > 0] <- 0
>> > rst.point <- pointsToRaster(rst, xy[[i]][j,]) #xy is the point from where
>> > the distances are calculated
>> > rst.cover <- cover(rst.point, rst.lines)
>> > rst.grid <- gridDistance(rst.cover)
>> > rst.grid[!is.na(rst.lines)] <- NA
>> > plot(rst.grid)
>> >
>> >
>> > Cheers,
>> > Roman
>> >
>> >
>> > --
>> > 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
>> >
>>
>
>
>
> --
> 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 at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
>



More information about the R-sig-Geo mailing list