[R-sig-Geo] Question about a spatial non-square cells error in R

Roger Bivand Roger.Bivand at nhh.no
Wed Jan 6 08:45:22 CET 2010


On Tue, 5 Jan 2010, Mailhiot, Joshua wrote:

> Hi folks,
>
> Long-time lurker, first time poster here. I have a conundrum that
> perhaps you might have come across. I'm getting an error when I try to
> compile some of my R spatial code:
>
> Error in write.asciigrid(xx2_res["NPHS"], newHSmap_asc, attr = 1,
> na.value = -9999) :
>  Asciigrid does not support grids with non-square cells
>
> This is telling me that the xy grid I'm working with is using cells that
> aren't exactly square. This is very curious, so I checked out the
> structure of the object I'm working with:

Josh:

You have print()ed the values with more digits, but all.equal() - 
handling fuzz - and identical() may not be the same:

cs <- slot(slot(xx2_res, "grid"), "cellsize")
all.equal(cs[1], cs[2])
identical(cs[1], cs[2])
cs[1] == cs[2]

The function uses == internally. Repair by assigning 
rep(mean(cs), 2) to slot(slot(xx2_res, "grid"), "cellsize")

slot(slot(xx2_res, "grid"), "cellsize") <- rep(mean(cs), 2)

and check:

cs <- slot(slot(xx2_res, "grid"), "cellsize")
all.equal(cs[1], cs[2])
identical(cs[1], cs[2])

If this doesn't resolve the problem, please try the equivalent driver in 
rgdal in writeGDAL(). I'm seeing differences at the 1e-13 level being 
detected on my platform (Linux 64-bit), but all IEEE double precision 
machines should perform in the same way.

Roger
>
>
>> str(xx2_res)
> Formal class 'SpatialGridDataFrame' [package "sp"] with 6 slots
>  ..@ data       :'data.frame': 480760 obs. of  9 variables:
>  .. ..$ v      : num [1:480760] 0 0 0 0 0 0 0 0 0 0 ...
>  .. ..$ paID   : num [1:480760] 0 0 0 0 0 0 0 0 0 0 ...
>  .. ..$ patchD : num [1:480760] 0 0 0 0 0 0 0 0 0 0 ...
>  .. ..$ NP1    : logi [1:480760] NA NA NA NA NA NA ...
>  .. ..$ preNP2 : logi [1:480760] NA NA NA NA NA NA ...
>  .. ..$ NP2    : num [1:480760] NA NA NA NA NA NA NA NA NA NA ...
>  .. ..$ out2   : num [1:480760] 0 0 0 0 0 0 0 0 0 0 ...
>  .. ..$ HSvalue: num [1:480760] 0 0 0 0 0 0 0 0 0 0 ...
>  .. ..$ NPHS   : num [1:480760] 0 0 0 0 0 0 0 0 0 0 ...
>  ..@ grid       :Formal class 'GridTopology' [package "sp"] with 3
> slots
>  .. .. ..@ cellcentre.offset: Named num [1:2] 780952 363416
>  .. .. .. ..- attr(*, "names")= chr [1:2] "s1" "s2"
>  .. .. ..@ cellsize         : Named num [1:2] 500 500
>  .. .. .. ..- attr(*, "names")= chr [1:2] "s1" "s2"
>  .. .. ..@ cells.dim        : Named int [1:2] 808 595
>  .. .. .. ..- attr(*, "names")= chr [1:2] "s1" "s2"
>  ..@ grid.index : int(0)
>  ..@ coords     : num [1:2, 1:2] 780952 1184452 363416 660416
>  .. ..- attr(*, "dimnames")=List of 2
>  .. .. ..$ : NULL
>  .. .. ..$ : chr [1:2] "s1" "s2"
>  ..@ bbox       : num [1:2, 1:2] 780702 363166 1184702 660666
>  .. ..- attr(*, "dimnames")=List of 2
>  .. .. ..$ : chr [1:2] "s1" "s2"
>  .. .. ..$ : chr [1:2] "min" "max"
>  ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slots
>  .. .. ..@ projargs: chr NA
>
>
> Or, the cell sizes extracted from that structure above:
>
>> print(slot(slot(xx2_res, "grid"), "cellsize"), digits=20)
> s1  s2
> 500 500
>
>
> Having both s1 and s2 being equal in the cell size slot indicates a
> square cell, as was shown in advice given in a similar problem in this
> usergroup. When I searched this error elsewhere and here, the problem
> tends to be that there is a "fuzziness" in the cell size numbers (way
> down in the decimals, like 500.000000002 x 500.000000000004) that is
> solved by rounding or forcing. That does not appear to be the case in my
> issue.
>
> Have any of you come across this error before? Is there something
> obvious I'm missing here, perhaps?
>
> Thanks for any assistance!
>
> Cheers,
> Josh
>
> Joshua Mailhiot
> Aquatic Science Biologist
> Pacific Biological Station
> Fisheries and Oceans Canada
> 3190 Hammond Bay Road, Nanaimo, BC V9T 6N7
> Joshua.Mailhiot at dfo-mpo.gc.ca
> Telephone: 250-729-8381
>
> 	[[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
>

-- 
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no



More information about the R-sig-Geo mailing list