Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crop change the datatype and lost the colortable #105

Closed
mgageo opened this issue Apr 3, 2020 · 1 comment
Closed

crop change the datatype and lost the colortable #105

mgageo opened this issue Apr 3, 2020 · 1 comment

Comments

@mgageo
Copy link

mgageo commented Apr 3, 2020

When I crop a file with a color table of 256 RGB entries, the new raster have no colortable.

require(raster)
#> Le chargement a nécessité le package : raster
#> Le chargement a nécessité le package : sp
temp <- setwd(tempdir())
on.exit(setwd(temp))
rasterFic <- sprintf("OCS_2017_CESBIO.tif");
print(sprintf("rasterFic: %s", rasterFic))
#> [1] "rasterFic: OCS_2017_CESBIO.tif"
if( !file.exists(rasterFic) ) {
dsn <- "http://osr-cesbio.ups-tlse.fr/echangeswww/TheiaOSO/OCS_2017_CESBIO.tif"
download.file(dsn, rasterFic, mode="wb")
}
img <- brick(rasterFic)
print(sprintf('dataType: %s', dataType(img)))
#> [1] "dataType: INT1U"
head(img@legend@colortable, 255)
#> [1] "#FFFFFF" "#000000" "#000000" "#000000" "#000000" "#000000" "#000000"
#> [8] "#000000" "#000000" "#000000" "#FF5500" "#FF5500" "#FFFF7F" "#000000"
#> [15] "#000000" "#000000" "#000000" "#000000" "#000000" "#000000" "#000000"
#> [22] "#000000" "#000000" "#000000" "#000000" "#000000" "#000000" "#000000"
#> [29] "#000000" "#000000" "#000000" "#009C00" "#003200" "#00FF00" "#AAFF00"
#> [36] "#AAFF7F" "#55AA7F" "#000000" "#000000" "#000000" "#000000" "#FF00FF"
#> [43] "#FF55FF" "#FFAAFF" "#00FFFF" "#FF0000" "#FFB802" "#000000" "#000000"
#> [50] "#000000" "#000000" "#0000FF" "#00007F" "#BEBEBE" "#000000" "#000000"
#> [57] "#000000" "#000000" "#000000" "#000000" "#000000" "#000000" "#000000"
#> [64] "#000000" "#000000" "#000000" "#000000" "#000000" "#000000" "#000000"
#> [71] "#000000" "#000000" "#000000" "#000000" "#000000" "#000000" "#000000"
#> [78] "#000000" "#000000" "#000000" "#000000" "#000000" "#000000" "#000000"
#> [85] "#000000" "#000000" "#000000" "#000000" "#000000" "#000000" "#000000"
#> [92] "#000000" "#000000" "#000000" "#000000" "#000000" "#000000" "#000000"
#> [99] "#000000" "#000000" "#000000" "#000000" "#000000" "#000000" "#000000"
#> [106] "#000000" "#000000" "#000000" "#000000" "#000000" "#000000" "#000000"
#> [113] "#000000" "#000000" "#000000" "#000000" "#000000" "#000000" "#000000"
#> [120] "#000000" "#000000" "#000000" "#000000" "#000000" "#000000" "#000000"
#> [127] "#000000" "#000000" "#000000" "#000000" "#000000" "#000000" "#000000"
#> [134] "#000000" "#000000" "#000000" "#000000" "#000000" "#000000" "#000000"
#> [141] "#000000" "#000000" "#000000" "#000000" "#000000" "#000000" "#000000"
#> [148] "#000000" "#000000" "#000000" "#000000" "#000000" "#000000" "#000000"
#> [155] "#000000" "#000000" "#000000" "#000000" "#000000" "#000000" "#000000"
#> [162] "#000000" "#000000" "#000000" "#000000" "#000000" "#000000" "#000000"
#> [169] "#000000" "#000000" "#000000" "#000000" "#000000" "#000000" "#000000"
#> [176] "#000000" "#000000" "#000000" "#000000" "#000000" "#000000" "#000000"
#> [183] "#000000" "#000000" "#000000" "#000000" "#000000" "#000000" "#000000"
#> [190] "#000000" "#000000" "#000000" "#000000" "#000000" "#000000" "#000000"
#> [197] "#000000" "#000000" "#000000" "#000000" "#000000" "#000000" "#000000"
#> [204] "#000000" "#000000" "#000000" "#000000" "#000000" "#000000" "#000000"
#> [211] "#000000" "#AAAA00" "#000000" "#000000" "#000000" "#000000" "#000000"
#> [218] "#000000" "#000000" "#000000" "#000000" "#AAAAFF" "#550000" "#AA5500"
#> [225] "#240000" "#000000" "#000000" "#000000" "#000000" "#000000" "#000000"
#> [232] "#000000" "#000000" "#000000" "#000000" "#000000" "#000000" "#000000"
#> [239] "#000000" "#000000" "#000000" "#000000" "#000000" "#000000" "#000000"
#> [246] "#000000" "#000000" "#000000" "#000000" "#000000" "#000000" "#000000"
#> [253] "#000000" "#000000" "#000000"
e <- extent(367382.583985, 368146.734166484, 6810487.95078925, 6811631.66159055)
img <- crop(img, e)
print(sprintf('dataType: %s', dataType(img)))
#> [1] "dataType: INT2S"
head(img@legend@colortable, 255)
#> logical(0)
Created on 2020-04-03 by the reprex package (v0.3.0)

@rhijmans
Copy link
Member

rhijmans commented Apr 4, 2020

It works if you use raster instead of brick. In the next version it will work for both

library(raster)
rasterFic <- sprintf("OCS_2017_CESBIO.tif");
if( !file.exists(rasterFic) ) {
  dsn <- "http://osr-cesbio.ups-tlse.fr/echangeswww/TheiaOSO/OCS_2017_CESBIO.tif"
  download.file(dsn, rasterFic, mode="wb")
}
r <- raster(rasterFic)
head(r@legend@colortable)
#[1] "#FFFFFF" "#000000" "#000000" "#000000" "#000000" "#000000"
 
e <- extent(367382.583985, 368146.734166484, 6810487.95078925, 6811631.66159055)
img <- crop(b, e)
head(img@legend@colortable)
#[1] "#FFFFFF" "#000000" "#000000" "#000000" "#000000" "#000000"

After fixing this, I can now also do

b <- brick(rasterFic)
head(b@legend@colortable)
#[1] "#FFFFFF" "#000000" "#000000" "#000000" "#000000" "#000000"

img2 <- crop(b, e)
head(img2@legend@colortable)
#[1] "#FFFFFF" "#000000" "#000000" "#000000" "#000000" "#000000"

@rhijmans rhijmans closed this as completed Apr 4, 2020
rhijmans added a commit that referenced this issue Apr 4, 2020
netbsd-srcmastr pushed a commit to NetBSD/pkgsrc that referenced this issue Sep 20, 2021
(From: NEWS)

--- 17-July-2020, version 3.3-13

overhaul of crs to be ready for PROJ6
several bug fixes (see https://github.com/rspatial/raster/issues?q=is%3Aissue+)

--- 18-April-2020, version 3.1-5
improvements:

new arguments "smin", "smax" and "samplesize" to "stretch"; and bug
fixes. See rspatial/raster#70

layer names in GTiff files are now read from file. Requested by Kyle Taylor
rspatial/raster#88

bug fixes:

parallel version of extract with polygons and "fun" messed up the
order of the values (reported by
Jacory). rspatial/raster#79

raster::predict bug when using factors (reported by
vvirkki). rspatial/raster#73

distanceFromPoints with large files:
rspatial/raster#103

colortable lost when cropping RasterBrick:
rspatial/raster#105

Error in stack() when providing both bands and varname:
rspatial/raster#97

rasterize and multi-polygon containing hole: rspatial/raster#93

Upside down raster: rspatial/raster#95 (a
warning is given, not really fixed yet)

Extract to data.frame bug:
https://stackoverflow.com/questions/61174280/r-rasterextract-fails-to-create-data-frame
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants