0

I am using R for GIS applications with spatstat and related packages. I would like to generate a kernel density raster, which I have already succeeded in doing using the following:

spatialgrid <- as(density(mypattern,5000,eps=50),'SpatialGridDataFrame')
rastergrid <- raster(spatialgrid)
writeRaster(rastergrid, filename=‘/file.tif’,format=‘GTiff’)

However, when I load the resulting raster into QGIS I have issues due to the fact that the cell values are written in scientific notation, rather than as simple numbers.

Based on this question, I tried format(density(),scientific=FALSE) but that caused a heavy spike in CPU and took a very long time to run, such that I eventually killed the process.

I'd like to find a way to get the density() function to output integer values. Alternatively, perhaps there is a way to convert the dataframe to integer data type?

Community
  • 1
  • 1
corvus
  • 556
  • 7
  • 18
  • Two possibilities: change the `scipen` option using e.g. `options(scipen=3)`; use `round` instead of `format`. I don't know if they will be faster but worth trying. –  Aug 09 '16 at 15:30

2 Answers2

0

I see two options here.

  1. You could use the options(scipen=99) to remove scientific notation, as suggested by @dash2.
  2. Multiply your values by a factor, for example 1000. Try something until your values aren't in scientific notation anymore. The advantage is that your raster won't have as many leading zeroes as option 1 and will take less hard drive space, but the disadvantage is that you have to multiply your values by that factor in QGIS.
jgadoury
  • 293
  • 2
  • 13
0

"Scientific notation" has nothing to do with it. That is for text representation, but with writeRaster you write to a numeric format. They are just real numbers.

I think you can simplify:

d <- density(mypattern,5000,eps=50)
r <- raster(d)

The problem is probably that the densities are very low. If you want larger numbers (as jgadoury suggested)

r <- r * 1000000

Possibly rounded

r <- round(r)

And then

writeRaster(r, 'file.tif')
Robert Hijmans
  • 40,301
  • 4
  • 55
  • 63