Problems converting rasters from float to integer.

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Problems converting rasters from float to integer.

Nevil Amos
I have a large number of rasters ( tiffs) that contain whole number values
between 0 and 100, and NA values. or 0,1,and NA
they are currently in Float format, I am trying to rewrite them as integer
rasters, firstly to save space, and secondly so that I can later read the
values stack of all the rasters into an integer array. using getValues().

To do this I am setting the dataType to INT2U in writeRaster, however when
I read the save file back into R the format is not INT2U but FLT8S

toy example:

v<-c(rep(1.00000,25),rep(0.00000,50),rep(NA,25))
m<-matrix(v,10,10)
r<-raster(m)
dataType(r)
writeRaster(r,"test_int.tif",dataType="INT2U",overwrite=T)
s<-raster("test_int.tif")
dataType(s)

the result I get:

> v<-c(rep(1.00000,25),rep(0.00000,50),rep(NA,25))
> m<-matrix(v,10,10)
> r<-raster(m)
> dataType(r)
[1] "FLT4S"
> writeRaster(r,"test_int.tif",dataType="INT2U",overwrite=T)
> s<-raster("test_int.tif")
> dataType(s)
[1] "FLT8S"
>


Can you suggest how I ensure the values are stored as integer?

Many thanks

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: Problems converting rasters from float to integer.

Michael Sumner-2
I'm pretty sure readGDAL from rgdal (that raster uses) will keep as
integers, so you can build an empty raster and copy the values over. But
will need to derive from the rgdal version to catch all the metadata
(structure, extent, and crs).

I think it's doable and will try later. There are other options but nothing
trivial afaik.

Cheers, Mike

On Fri, 24 Aug 2018, 16:45 nevil amos <[hidden email]> wrote:

> I have a large number of rasters ( tiffs) that contain whole number values
> between 0 and 100, and NA values. or 0,1,and NA
> they are currently in Float format, I am trying to rewrite them as integer
> rasters, firstly to save space, and secondly so that I can later read the
> values stack of all the rasters into an integer array. using getValues().
>
> To do this I am setting the dataType to INT2U in writeRaster, however when
> I read the save file back into R the format is not INT2U but FLT8S
>
> toy example:
>
> v<-c(rep(1.00000,25),rep(0.00000,50),rep(NA,25))
> m<-matrix(v,10,10)
> r<-raster(m)
> dataType(r)
> writeRaster(r,"test_int.tif",dataType="INT2U",overwrite=T)
> s<-raster("test_int.tif")
> dataType(s)
>
> the result I get:
>
> > v<-c(rep(1.00000,25),rep(0.00000,50),rep(NA,25))
> > m<-matrix(v,10,10)
> > r<-raster(m)
> > dataType(r)
> [1] "FLT4S"
> > writeRaster(r,"test_int.tif",dataType="INT2U",overwrite=T)
> > s<-raster("test_int.tif")
> > dataType(s)
> [1] "FLT8S"
> >
>
>
> Can you suggest how I ensure the values are stored as integer?
>
> Many thanks
>
>         [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> [hidden email]
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
--
Dr. Michael Sumner
Software and Database Engineer
Australian Antarctic Division
203 Channel Highway
Kingston Tasmania 7050 Australia

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: Problems converting rasters from float to integer.

Roger Bivand
Administrator
Thanks, Mike. I agree that the lower level interface in rgdal is flexible enough, but as you say non-trivial. Nevil: could you please provide a small reproducible example to point people in the right direction?

Roger

Roger Bivand
Norwegian School of Economics
Bergen, Norway



Fra: Michael Sumner
Sendt: fredag 24. august, 09.14
Emne: Re: [R-sig-Geo] Problems converting rasters from float to integer.
Til: nevil amos
Kopi: [hidden email]


I'm pretty sure readGDAL from rgdal (that raster uses) will keep as integers, so you can build an empty raster and copy the values over. But will need to derive from the rgdal version to catch all the metadata (structure, extent, and crs). I think it's doable and will try later. There are other options but nothing trivial afaik. Cheers, Mike On Fri, 24 Aug 2018, 16:45 nevil amos wrote: > I have a large number of rasters ( tiffs) that contain whole number values > between 0 and 100, and NA values. or 0,1,and NA > they are currently in Float format, I am trying to rewrite them as integer > rasters, firstly to save space, and secondly so that I can later read the > values stack of all the rasters into an integer array. using getValues(). > > To do this I am setting the dataType to INT2U in writeRaster, however when > I read the save file back into R the format is not INT2U but FLT8S > > toy example: > > v m r dataType(r) > writeRaster(r,"test_int.tif",dataType="INT2U",overwrite=T) > s d
 ataType(s) > > the result I get: > > > v > m > r > dataType(r) > [1] "FLT4S" > > writeRaster(r,"test_int.tif",dataType="INT2U",overwrite=T) > > s > dataType(s) > [1] "FLT8S" > > > > > Can you suggest how I ensure the values are stored as integer? > > Many thanks > > [[alternative HTML version deleted]] > > _______________________________________________ > R-sig-Geo mailing list > [hidden email] > https://stat.ethz.ch/mailman/listinfo/r-sig-geo > -- Dr. Michael Sumner Software and Database Engineer Australian Antarctic Division 203 Channel Highway Kingston Tasmania 7050 Australia [[alternative HTML version deleted]] _______________________________________________ R-sig-Geo mailing list [hidden email] https://stat.ethz.ch/mailman/listinfo/r-sig-geo


        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Roger Bivand
Department of Economics
Norwegian School of Economics
Helleveien 30
N-5045 Bergen, Norway
Reply | Threaded
Open this post in threaded view
|

Re: Problems converting rasters from float to integer.

Nevil Amos
Roger,

there was a reproducible example in my post:  it seems to have got a bit
scrambled in your reply.

from the help in raster I thought that if I included  datatype="INT2S" in
the writeRaster() command the values would be formatted in the output file
( and the subsequent reading using raster() of that file) as integer  is
this not the case?

Here it is the reproducible example

v<-c(rep(1.00000,25),rep(0.00000,50),rep(NA,25))
m<-matrix(v,10,10)
r<-raster(m)
dataType(r)
writeRaster(r,"test_int.tif",dataType="INT2U",overwrite=T)
s<-raster("test_int.tif")
dataType(s)

On Fri, 24 Aug 2018 at 18:05, Roger Bivand <[hidden email]> wrote:

> Thanks, Mike. I agree that the lower level interface in rgdal is flexible
> enough, but as you say non-trivial. Nevil: could you please provide a small
> reproducible example to point people in the right direction?
>
> Roger
>
> Roger Bivand
> Norwegian School of Economics
> Bergen, Norway
>
>
>
> Fra: Michael Sumner
> Sendt: fredag 24. august, 09.14
> Emne: Re: [R-sig-Geo] Problems converting rasters from float to integer.
> Til: nevil amos
> Kopi: [hidden email]
>
>
> I'm pretty sure readGDAL from rgdal (that raster uses) will keep as
> integers, so you can build an empty raster and copy the values over. But
> will need to derive from the rgdal version to catch all the metadata
> (structure, extent, and crs). I think it's doable and will try later. There
> are other options but nothing trivial afaik. Cheers, Mike On Fri, 24 Aug
> 2018, 16:45 nevil amos wrote: > I have a large number of rasters ( tiffs)
> that contain whole number values > between 0 and 100, and NA values. or
> 0,1,and NA > they are currently in Float format, I am trying to rewrite
> them as integer > rasters, firstly to save space, and secondly so that I
> can later read the > values stack of all the rasters into an integer array.
> using getValues(). > > To do this I am setting the dataType to INT2U in
> writeRaster, however when > I read the save file back into R the format is
> not INT2U but FLT8S > > toy example: > > v m r dataType(r) >
> writeRaster(r,"test_int.tif",dataType="INT2U",overwrite=T) > s dataType(s)
> > > the result I get: > > > v > m > r > dataType(r) > [1] "FLT4S" > >
> writeRaster(r,"test_int.tif",dataType="INT2U",overwrite=T) > > s >
> dataType(s) > [1] "FLT8S" > > > > > Can you suggest how I ensure the values
> are stored as integer? > > Many thanks > > [[alternative HTML version
> deleted]] > > _______________________________________________ > R-sig-Geo
> mailing list > [hidden email] >
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo > -- Dr. Michael Sumner
> Software and Database Engineer Australian Antarctic Division 203 Channel
> Highway Kingston Tasmania 7050 Australia [[alternative HTML version
> deleted]] _______________________________________________ R-sig-Geo mailing
> list [hidden email]
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
>

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: Problems converting rasters from float to integer.

Michael Sumner-2
Sorry, I was wrong - the actual issue is that the writeRaster argument is
"datatype", it gets conflated with the function that is dataType().

f <- "test_int.tif"
writeRaster(r, f, datatype = "INT2U", overwrite = TRUE)
s <- raster(f)
dataType(s)

[1] "INT2U"

Not the first time this has caught me out. I found it by checking
mode(readGDAL(f)[[1]]) and realizing that the TIFF type was Float32, prior
to the next steps.

HTH

:)

Cheers, Mike

On Fri, 24 Aug 2018 at 18:26 nevil amos <[hidden email]> wrote:

> Roger,
>
> there was a reproducible example in my post:  it seems to have got a bit
> scrambled in your reply.
>
> from the help in raster I thought that if I included  datatype="INT2S" in
> the writeRaster() command the values would be formatted in the output file
> ( and the subsequent reading using raster() of that file) as integer  is
> this not the case?
>
> Here it is the reproducible example
>
> v<-c(rep(1.00000,25),rep(0.00000,50),rep(NA,25))
> m<-matrix(v,10,10)
> r<-raster(m)
> dataType(r)
> writeRaster(r,"test_int.tif",dataType="INT2U",overwrite=T)
> s<-raster("test_int.tif")
> dataType(s)
>
> On Fri, 24 Aug 2018 at 18:05, Roger Bivand <[hidden email]> wrote:
>
>> Thanks, Mike. I agree that the lower level interface in rgdal is flexible
>> enough, but as you say non-trivial. Nevil: could you please provide a small
>> reproducible example to point people in the right direction?
>>
>> Roger
>>
>> Roger Bivand
>> Norwegian School of Economics
>> Bergen, Norway
>>
>>
>>
>> Fra: Michael Sumner
>> Sendt: fredag 24. august, 09.14
>> Emne: Re: [R-sig-Geo] Problems converting rasters from float to integer.
>> Til: nevil amos
>> Kopi: [hidden email]
>>
>>
>> I'm pretty sure readGDAL from rgdal (that raster uses) will keep as
>> integers, so you can build an empty raster and copy the values over. But
>> will need to derive from the rgdal version to catch all the metadata
>> (structure, extent, and crs). I think it's doable and will try later. There
>> are other options but nothing trivial afaik. Cheers, Mike On Fri, 24 Aug
>> 2018, 16:45 nevil amos wrote: > I have a large number of rasters ( tiffs)
>> that contain whole number values > between 0 and 100, and NA values. or
>> 0,1,and NA > they are currently in Float format, I am trying to rewrite
>> them as integer > rasters, firstly to save space, and secondly so that I
>> can later read the > values stack of all the rasters into an integer array.
>> using getValues(). > > To do this I am setting the dataType to INT2U in
>> writeRaster, however when > I read the save file back into R the format is
>> not INT2U but FLT8S > > toy example: > > v m r dataType(r) >
>> writeRaster(r,"test_int.tif",dataType="INT2U",overwrite=T) > s dataType(s)
>> > > the result I get: > > > v > m > r > dataType(r) > [1] "FLT4S" > >
>> writeRaster(r,"test_int.tif",dataType="INT2U",overwrite=T) > > s >
>> dataType(s) > [1] "FLT8S" > > > > > Can you suggest how I ensure the values
>> are stored as integer? > > Many thanks > > [[alternative HTML version
>> deleted]] > > _______________________________________________ > R-sig-Geo
>> mailing list > [hidden email] >
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo > -- Dr. Michael Sumner
>> Software and Database Engineer Australian Antarctic Division 203 Channel
>> Highway Kingston Tasmania 7050 Australia
>> <https://maps.google.com/?q=203+Channel%0D%0A+Highway+Kingston+Tasmania+7050+Australia&entry=gmail&source=g>
>> [[alternative HTML version deleted]]
>> _______________________________________________ R-sig-Geo mailing list
>> [hidden email] https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>
>> --
Dr. Michael Sumner
Software and Database Engineer
Australian Antarctic Division
203 Channel Highway
Kingston Tasmania 7050 Australia

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: Problems converting rasters from float to integer.

Michael Sumner-2
That doesn't help though!

Sorry, I thought that was it, will sleep on it.

Cheers, Mike

On Fri, 24 Aug 2018 at 22:00 Michael Sumner <[hidden email]> wrote:

> Sorry, I was wrong - the actual issue is that the writeRaster argument is
> "datatype", it gets conflated with the function that is dataType().
>
> f <- "test_int.tif"
> writeRaster(r, f, datatype = "INT2U", overwrite = TRUE)
> s <- raster(f)
> dataType(s)
>
> [1] "INT2U"
>
> Not the first time this has caught me out. I found it by checking
> mode(readGDAL(f)[[1]]) and realizing that the TIFF type was Float32, prior
> to the next steps.
>
> HTH
>
> :)
>
> Cheers, Mike
>
> On Fri, 24 Aug 2018 at 18:26 nevil amos <[hidden email]> wrote:
>
>> Roger,
>>
>> there was a reproducible example in my post:  it seems to have got a bit
>> scrambled in your reply.
>>
>> from the help in raster I thought that if I included  datatype="INT2S" in
>> the writeRaster() command the values would be formatted in the output file
>> ( and the subsequent reading using raster() of that file) as integer  is
>> this not the case?
>>
>> Here it is the reproducible example
>>
>> v<-c(rep(1.00000,25),rep(0.00000,50),rep(NA,25))
>> m<-matrix(v,10,10)
>> r<-raster(m)
>> dataType(r)
>> writeRaster(r,"test_int.tif",dataType="INT2U",overwrite=T)
>> s<-raster("test_int.tif")
>> dataType(s)
>>
>> On Fri, 24 Aug 2018 at 18:05, Roger Bivand <[hidden email]> wrote:
>>
>>> Thanks, Mike. I agree that the lower level interface in rgdal is
>>> flexible enough, but as you say non-trivial. Nevil: could you please
>>> provide a small reproducible example to point people in the right direction?
>>>
>>> Roger
>>>
>>> Roger Bivand
>>> Norwegian School of Economics
>>> Bergen, Norway
>>>
>>>
>>>
>>> Fra: Michael Sumner
>>> Sendt: fredag 24. august, 09.14
>>> Emne: Re: [R-sig-Geo] Problems converting rasters from float to integer.
>>> Til: nevil amos
>>> Kopi: [hidden email]
>>>
>>>
>>> I'm pretty sure readGDAL from rgdal (that raster uses) will keep as
>>> integers, so you can build an empty raster and copy the values over. But
>>> will need to derive from the rgdal version to catch all the metadata
>>> (structure, extent, and crs). I think it's doable and will try later. There
>>> are other options but nothing trivial afaik. Cheers, Mike On Fri, 24 Aug
>>> 2018, 16:45 nevil amos wrote: > I have a large number of rasters ( tiffs)
>>> that contain whole number values > between 0 and 100, and NA values. or
>>> 0,1,and NA > they are currently in Float format, I am trying to rewrite
>>> them as integer > rasters, firstly to save space, and secondly so that I
>>> can later read the > values stack of all the rasters into an integer array.
>>> using getValues(). > > To do this I am setting the dataType to INT2U in
>>> writeRaster, however when > I read the save file back into R the format is
>>> not INT2U but FLT8S > > toy example: > > v m r dataType(r) >
>>> writeRaster(r,"test_int.tif",dataType="INT2U",overwrite=T) > s dataType(s)
>>> > > the result I get: > > > v > m > r > dataType(r) > [1] "FLT4S" > >
>>> writeRaster(r,"test_int.tif",dataType="INT2U",overwrite=T) > > s >
>>> dataType(s) > [1] "FLT8S" > > > > > Can you suggest how I ensure the values
>>> are stored as integer? > > Many thanks > > [[alternative HTML version
>>> deleted]] > > _______________________________________________ > R-sig-Geo
>>> mailing list > [hidden email] >
>>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo > -- Dr. Michael Sumner
>>> Software and Database Engineer Australian Antarctic Division 203
>>> Channel Highway Kingston Tasmania 7050 Australia
>>> <https://maps.google.com/?q=203+Channel%0D%0A+Highway+Kingston+Tasmania+7050+Australia&entry=gmail&source=g>
>>> [[alternative HTML version deleted]]
>>> _______________________________________________ R-sig-Geo mailing list
>>> [hidden email] https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>>
>>> --
> Dr. Michael Sumner
> Software and Database Engineer
> Australian Antarctic Division
> 203 Channel Highway
> Kingston Tasmania 7050 Australia
>
> --
Dr. Michael Sumner
Software and Database Engineer
Australian Antarctic Division
203 Channel Highway
Kingston Tasmania 7050 Australia

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: Problems converting rasters from float to integer.

Nevil Amos
Thanks,

I think it did help
datatype vs dataType was indeed the problem!

On Fri, 24 Aug 2018 at 22:14, Michael Sumner <[hidden email]> wrote:

> That doesn't help though!
>
> Sorry, I thought that was it, will sleep on it.
>
> Cheers, Mike
>
> On Fri, 24 Aug 2018 at 22:00 Michael Sumner <[hidden email]> wrote:
>
>> Sorry, I was wrong - the actual issue is that the writeRaster argument is
>> "datatype", it gets conflated with the function that is dataType().
>>
>> f <- "test_int.tif"
>> writeRaster(r, f, datatype = "INT2U", overwrite = TRUE)
>> s <- raster(f)
>> dataType(s)
>>
>> [1] "INT2U"
>>
>> Not the first time this has caught me out. I found it by checking
>> mode(readGDAL(f)[[1]]) and realizing that the TIFF type was Float32, prior
>> to the next steps.
>>
>> HTH
>>
>> :)
>>
>> Cheers, Mike
>>
>> On Fri, 24 Aug 2018 at 18:26 nevil amos <[hidden email]> wrote:
>>
>>> Roger,
>>>
>>> there was a reproducible example in my post:  it seems to have got a bit
>>> scrambled in your reply.
>>>
>>> from the help in raster I thought that if I included  datatype="INT2S"
>>> in the writeRaster() command the values would be formatted in the output
>>> file ( and the subsequent reading using raster() of that file) as integer
>>> is this not the case?
>>>
>>> Here it is the reproducible example
>>>
>>> v<-c(rep(1.00000,25),rep(0.00000,50),rep(NA,25))
>>> m<-matrix(v,10,10)
>>> r<-raster(m)
>>> dataType(r)
>>> writeRaster(r,"test_int.tif",dataType="INT2U",overwrite=T)
>>> s<-raster("test_int.tif")
>>> dataType(s)
>>>
>>> On Fri, 24 Aug 2018 at 18:05, Roger Bivand <[hidden email]> wrote:
>>>
>>>> Thanks, Mike. I agree that the lower level interface in rgdal is
>>>> flexible enough, but as you say non-trivial. Nevil: could you please
>>>> provide a small reproducible example to point people in the right direction?
>>>>
>>>> Roger
>>>>
>>>> Roger Bivand
>>>> Norwegian School of Economics
>>>> Bergen, Norway
>>>>
>>>>
>>>>
>>>> Fra: Michael Sumner
>>>> Sendt: fredag 24. august, 09.14
>>>> Emne: Re: [R-sig-Geo] Problems converting rasters from float to integer.
>>>> Til: nevil amos
>>>> Kopi: [hidden email]
>>>>
>>>>
>>>> I'm pretty sure readGDAL from rgdal (that raster uses) will keep as
>>>> integers, so you can build an empty raster and copy the values over. But
>>>> will need to derive from the rgdal version to catch all the metadata
>>>> (structure, extent, and crs). I think it's doable and will try later. There
>>>> are other options but nothing trivial afaik. Cheers, Mike On Fri, 24 Aug
>>>> 2018, 16:45 nevil amos wrote: > I have a large number of rasters ( tiffs)
>>>> that contain whole number values > between 0 and 100, and NA values. or
>>>> 0,1,and NA > they are currently in Float format, I am trying to rewrite
>>>> them as integer > rasters, firstly to save space, and secondly so that I
>>>> can later read the > values stack of all the rasters into an integer array.
>>>> using getValues(). > > To do this I am setting the dataType to INT2U in
>>>> writeRaster, however when > I read the save file back into R the format is
>>>> not INT2U but FLT8S > > toy example: > > v m r dataType(r) >
>>>> writeRaster(r,"test_int.tif",dataType="INT2U",overwrite=T) > s dataType(s)
>>>> > > the result I get: > > > v > m > r > dataType(r) > [1] "FLT4S" > >
>>>> writeRaster(r,"test_int.tif",dataType="INT2U",overwrite=T) > > s >
>>>> dataType(s) > [1] "FLT8S" > > > > > Can you suggest how I ensure the values
>>>> are stored as integer? > > Many thanks > > [[alternative HTML version
>>>> deleted]] > > _______________________________________________ > R-sig-Geo
>>>> mailing list > [hidden email] >
>>>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo > -- Dr. Michael
>>>> Sumner Software and Database Engineer Australian Antarctic Division 203
>>>> Channel Highway Kingston Tasmania 7050 Australia
>>>> <https://maps.google.com/?q=203+Channel%0D%0A+Highway+Kingston+Tasmania+7050+Australia&entry=gmail&source=g>
>>>> [[alternative HTML version deleted]]
>>>> _______________________________________________ R-sig-Geo mailing list
>>>> [hidden email] https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>>>
>>>> --
>> Dr. Michael Sumner
>> Software and Database Engineer
>> Australian Antarctic Division
>> 203 Channel Highway
>> Kingston Tasmania 7050 Australia
>>
>> --
> Dr. Michael Sumner
> Software and Database Engineer
> Australian Antarctic Division
> 203 Channel Highway
> Kingston Tasmania 7050 Australia
>
>

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: Problems converting rasters from float to integer.

Roger Bivand
Administrator
On Fri, 24 Aug 2018, nevil amos wrote:

> Thanks,
>
> I think it did help
> datatype vs dataType was indeed the problem!

The underlying problem is that dataType= is going into ..., and because
nothing needs it, it isn't spotted. Could someone contribute a PR to trap
this, from https://github.com/rspatial/raster?

Roger

>
> On Fri, 24 Aug 2018 at 22:14, Michael Sumner <[hidden email]> wrote:
>
>> That doesn't help though!
>>
>> Sorry, I thought that was it, will sleep on it.
>>
>> Cheers, Mike
>>
>> On Fri, 24 Aug 2018 at 22:00 Michael Sumner <[hidden email]> wrote:
>>
>>> Sorry, I was wrong - the actual issue is that the writeRaster argument is
>>> "datatype", it gets conflated with the function that is dataType().
>>>
>>> f <- "test_int.tif"
>>> writeRaster(r, f, datatype = "INT2U", overwrite = TRUE)
>>> s <- raster(f)
>>> dataType(s)
>>>
>>> [1] "INT2U"
>>>
>>> Not the first time this has caught me out. I found it by checking
>>> mode(readGDAL(f)[[1]]) and realizing that the TIFF type was Float32, prior
>>> to the next steps.
>>>
>>> HTH
>>>
>>> :)
>>>
>>> Cheers, Mike
>>>
>>> On Fri, 24 Aug 2018 at 18:26 nevil amos <[hidden email]> wrote:
>>>
>>>> Roger,
>>>>
>>>> there was a reproducible example in my post:  it seems to have got a bit
>>>> scrambled in your reply.
>>>>
>>>> from the help in raster I thought that if I included  datatype="INT2S"
>>>> in the writeRaster() command the values would be formatted in the output
>>>> file ( and the subsequent reading using raster() of that file) as integer
>>>> is this not the case?
>>>>
>>>> Here it is the reproducible example
>>>>
>>>> v<-c(rep(1.00000,25),rep(0.00000,50),rep(NA,25))
>>>> m<-matrix(v,10,10)
>>>> r<-raster(m)
>>>> dataType(r)
>>>> writeRaster(r,"test_int.tif",dataType="INT2U",overwrite=T)
>>>> s<-raster("test_int.tif")
>>>> dataType(s)
>>>>
>>>> On Fri, 24 Aug 2018 at 18:05, Roger Bivand <[hidden email]> wrote:
>>>>
>>>>> Thanks, Mike. I agree that the lower level interface in rgdal is
>>>>> flexible enough, but as you say non-trivial. Nevil: could you please
>>>>> provide a small reproducible example to point people in the right direction?
>>>>>
>>>>> Roger
>>>>>
>>>>> Roger Bivand
>>>>> Norwegian School of Economics
>>>>> Bergen, Norway
>>>>>
>>>>>
>>>>>
>>>>> Fra: Michael Sumner
>>>>> Sendt: fredag 24. august, 09.14
>>>>> Emne: Re: [R-sig-Geo] Problems converting rasters from float to integer.
>>>>> Til: nevil amos
>>>>> Kopi: [hidden email]
>>>>>
>>>>>
>>>>> I'm pretty sure readGDAL from rgdal (that raster uses) will keep as
>>>>> integers, so you can build an empty raster and copy the values over. But
>>>>> will need to derive from the rgdal version to catch all the metadata
>>>>> (structure, extent, and crs). I think it's doable and will try later. There
>>>>> are other options but nothing trivial afaik. Cheers, Mike On Fri, 24 Aug
>>>>> 2018, 16:45 nevil amos wrote: > I have a large number of rasters ( tiffs)
>>>>> that contain whole number values > between 0 and 100, and NA values. or
>>>>> 0,1,and NA > they are currently in Float format, I am trying to rewrite
>>>>> them as integer > rasters, firstly to save space, and secondly so that I
>>>>> can later read the > values stack of all the rasters into an integer array.
>>>>> using getValues(). > > To do this I am setting the dataType to INT2U in
>>>>> writeRaster, however when > I read the save file back into R the format is
>>>>> not INT2U but FLT8S > > toy example: > > v m r dataType(r) >
>>>>> writeRaster(r,"test_int.tif",dataType="INT2U",overwrite=T) > s dataType(s)
>>>>>>> the result I get: > > > v > m > r > dataType(r) > [1] "FLT4S" > >
>>>>> writeRaster(r,"test_int.tif",dataType="INT2U",overwrite=T) > > s >
>>>>> dataType(s) > [1] "FLT8S" > > > > > Can you suggest how I ensure the values
>>>>> are stored as integer? > > Many thanks > > [[alternative HTML version
>>>>> deleted]] > > _______________________________________________ > R-sig-Geo
>>>>> mailing list > [hidden email] >
>>>>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo > -- Dr. Michael
>>>>> Sumner Software and Database Engineer Australian Antarctic Division 203
>>>>> Channel Highway Kingston Tasmania 7050 Australia
>>>>> <https://maps.google.com/?q=203+Channel%0D%0A+Highway+Kingston+Tasmania+7050+Australia&entry=gmail&source=g>
>>>>> [[alternative HTML version deleted]]
>>>>> _______________________________________________ R-sig-Geo mailing list
>>>>> [hidden email] https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>>>>
>>>>> --
>>> Dr. Michael Sumner
>>> Software and Database Engineer
>>> Australian Antarctic Division
>>> 203 Channel Highway
>>> Kingston Tasmania 7050 Australia
>>>
>>> --
>> Dr. Michael Sumner
>> Software and Database Engineer
>> Australian Antarctic Division
>> 203 Channel Highway
>> Kingston Tasmania 7050 Australia
>>
>>
>

--
Roger Bivand
Department of Economics, Norwegian School of Economics,
Helleveien 30, N-5045 Bergen, Norway.
voice: +47 55 95 93 55; e-mail: [hidden email]
http://orcid.org/0000-0003-2392-6140
https://scholar.google.no/citations?user=AWeghB0AAAAJ&hl=en

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Roger Bivand
Department of Economics
Norwegian School of Economics
Helleveien 30
N-5045 Bergen, Norway