[R-sig-Geo] rast() in terra doesn't show cell values

Sarah Goslee @@r@h@go@|ee @end|ng |rom gm@||@com
Wed Mar 8 23:39:37 CET 2023


Hi Noah,

It appears to be the same problem as this: [reading rasters in r using
terra package](https://gis.stackexchange.com/questions/398061/reading-rasters-in-r-using-terra-package).
As in that question, adding 0 forces the conversion, but loses all the
information about color table, etc.

canopy <- rast('nlcd_2016_treecanopy_2019_08_31.img', lyrs=1)

c2 <- canopy + 0

> c2
class       : SpatRaster
dimensions  : 104424, 161190, 1  (nrow, ncol, nlyr)
resolution  : 30, 30  (x, y)
extent      : -2493045, 2342655, 177285, 3310005  (xmin, xmax, ymin, ymax)
coord. ref. : Albers Conical Equal Area
source      : spat_t0Rj6gEgi2CzsAZ_16778.tif
name        : Histogram
min value   :         0
max value   :       255

I didn't try the devel version of terra, though.

Sarah

On Wed, Mar 8, 2023 at 6:17 AM Noah Charney <noah.charney using maine.edu> wrote:
>
> Hi,
> Can someone tell me what I'm missing?  I can't get the rast() function in
> terra to return the actual cell values from an .img file, it just keeps
> giving strange histogram values.  The plot() looks ok, except the legend
> has those same useless values.  Similarly on the categorical NLCD Land
> Cover layer it gives me data associated with each cell value (e.g. land
> cover type), which is fine to work with, but still not the actual cell
> values as I'd expect.  The old raster() function in raster package works
> fine (see below). Thanks for insights! -Noah
>
> >   #Download Tree Canopy Layer from NLCD
> >   #   https://www.mrlc.gov/data/nlcd-2016-usfs-tree-canopy-cover-conus
> >
> >   #terra package gives me nonsensical histogram values
> >   library(terra)
> >   canopy <- rast('D:/GIS/Land
> Cover/nlcd_2016_treecanopy_2019_08_31/nlcd_2016_treecanopy_2019_08_31.img')
> >   canopy
> class       : SpatRaster
> dimensions  : 104424, 161190, 1  (nrow, ncol, nlyr)
> resolution  : 30, 30  (x, y)
> extent      : -2493045, 2342655, 177285, 3310005  (xmin, xmax, ymin, ymax)
> coord. ref. : Albers Conical Equal Area
> source      : nlcd_2016_treecanopy_2019_08_31.img
> color table : 1
> categories  : Histogram, Red, Green, Blue
> name        :  Histogram
> min value   : 5737352684
> max value   : 7854320129
> >   plot(canopy)
> >   canopy[50000,50000]
>   Histogram
> 1  24314964
> >   extract(canopy,data.frame(x=1000000,y=1000000))
>   ID Histogram
> 1  1  44832754
> >
> >   #raster package works fine
> >   library(raster)
> >   canopy <- raster('D:/GIS/Land
> Cover/nlcd_2016_treecanopy_2019_08_31/nlcd_2016_treecanopy_2019_08_31.img')
> >   canopy
> class      : RasterLayer
> dimensions : 104424, 161190, 16832104560  (nrow, ncol, ncell)
> resolution : 30, 30  (x, y)
> extent     : -2493045, 2342655, 177285, 3310005  (xmin, xmax, ymin, ymax)
> crs        : +proj=aea +lat_0=23 +lon_0=-96 +lat_1=29.5 +lat_2=45.5 +x_0=0
> +y_0=0 +datum=NAD83 +units=m +no_defs
> source     : nlcd_2016_treecanopy_2019_08_31.img
> names      : Histogram
> values     : 0, 255  (min, max)
> attributes :
>         ID  Histogram Red Green Blue
>  from:   0 5737352684 255   255  255
>   to : 255 7854320129 255   255  255
> >   canopy[50000,50000]
> 24
> >   extract(canopy,data.frame(x=1000000,y=1000000))
> 94
> >
> >   #Land Cover (categorical) layer gives associated categories (not
> values) in terra
> >   #     https://www.mrlc.gov/data/nlcd-2019-land-cover-conus
> >
> >   nlcd <- rast('D:/GIS/Land
> Cover/nlcd_2019_land_cover_l48_20210604/nlcd_2019_land_cover_l48_20210604.img')
> >   nlcd
> class       : SpatRaster
> dimensions  : 104424, 161190, 1  (nrow, ncol, nlyr)
> resolution  : 30, 30  (x, y)
> extent      : -2493045, 2342655, 177285, 3310005  (xmin, xmax, ymin, ymax)
> coord. ref. : Albers Conical Equal Area
> source      : nlcd_2019_land_cover_l48_20210604.img
> color table : 1
> categories  : NLCD Land Cover Class, Histogram, Red, Green, Blue, Opacity
> name        :        NLCD Land Cover Class
> min value   :                 Unclassified
> max value   : Emergent Herbaceous Wetlands
> >   nlcd[50000,50000]
>   NLCD Land Cover Class
> 1      Evergreen Forest
> >   extract(nlcd,data.frame(x=1000000,y=1000000))
>   ID NLCD Land Cover Class
> 1  1      Evergreen Forest
> >
>
>
> ~~~
> Dr. Noah Charney (he/him)
> Assistant Professor of Conservation Biology
> Department of Wildlife, Fisheries, and Conservation Biology
> University of Maine, Orono, ME
> 230 Nutting Hall
> (207) 581-1284
>
>         [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo using r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo



-- 
Sarah Goslee (she/her)
http://www.numberwright.com



More information about the R-sig-Geo mailing list