rspatial / terra

R package for spatial data handling https://rspatial.github.io/terra/reference/terra-package.html
GNU General Public License v3.0
543 stars 90 forks source link

invalid tiff on windows #1574

Closed JosiahParry closed 3 months ago

JosiahParry commented 3 months ago

There's this reproducible issue that we're running into that appears to be related to how terra::rast() works on different OS. When using terra on windows we run into an issue that generates a broken tiff file. See the below (attached tiff file as well)

library(arcgis)
#> Attaching core arcgis packages:
#> → arcgisutils v0.3.0
#> → arcgislayers v0.3.0
#> → arcgisgeocode v0.2.0
#> → arcgisplaces v0.1.0
library(terra)
bigmap.url <- 'https://di-usfsdata.img.arcgis.com/arcgis/rest/services/FIA_BIGMAP_2018_Species_Aboveground_Biomass/ImageServer'  
bigmap.dataset <- arc_open(bigmap.url)
bigmap.dataset
#> <ImageServer <1 bands, 27 fields>>
#> Name: FIA_BIGMAP_2018_Species_Aboveground_Biomass
#> Description: BIGMAP Species Aboveground Biomass
#> Extent: -2911620 2933940 164760 3254760 (xmin, xmax, ymin, ymax)
#> Resolution: 30 x 30
#> CRS: 102039
#> Capabilities: Image,Metadata,Catalog,Mensuration
res <- arc_raster(bigmap.dataset, xmin=-2911620, ymin=164760, xmax=2933940, ymax=3254760, bbox_crs=102039)
#> Warning in CPL_crs_from_input(x): GDAL Error 1: PROJ:
#> proj_create_from_database: crs not found
#> Warning in CPL_crs_from_input(x): GDAL Error 1: PROJ:
#> proj_create_from_database: crs not found
#> Warning: TIFFFetchNormalTag:Defined set_field_type of custom tag 45312 (Tag
#> 45312) is TIFF_SETGET_UNDEFINED and thus tag is not read from file (GDAL error
#> 1)
#> Warning: TIFFFetchNormalTag:Defined set_field_type of custom tag 32768 (Tag
#> 32768) is TIFF_SETGET_UNDEFINED and thus tag is not read from file (GDAL error
#> 1)
res
#> class       : SpatRaster 
#> dimensions  : 400, 400, 1  (nrow, ncol, nlyr)
#> resolution  : 4.667072e-62, 4.667312e-62  (x, y)
#> extent      : 0, 1.866829e-59, -1.866925e-59, 4.101272e-312  (xmin, xmax, ymin, ymax)
#> coord. ref. : lon/lat WGS 84 (CRS84) (OGC:CRS84) 
#> source      : file47203b0269c0.tiff 
#> name        : Band_1

Created on 2024-07-23 with reprex v2.1.1 Tiff is in a zip file b/c thats what GitHub supports :) filec881c18754b.zip

However when running this on MacOS the TIFFFetchNormalTag**** warnings do not occur and the tiff can be plotted and read.

JosiahParry commented 3 months ago

Compare this to the output on Mac:

library(arcgis)
#> Attaching core arcgis packages:
#> → arcgisutils v0.3.0
#> → arcgislayers v0.3.0
#> → arcgisgeocode v0.1.3
#> → arcgisplaces v0.1.0
library(terra)
#> terra 1.7.71
#> 
#> Attaching package: 'terra'
#> 
#> The following object is masked from 'package:arcgisplaces':
#> 
#>     categories
bigmap.url <- 'https://di-usfsdata.img.arcgis.com/arcgis/rest/services/FIA_BIGMAP_2018_Species_Aboveground_Biomass/ImageServer'

bigmap.dataset <- arc_open(bigmap.url)
bigmap.dataset
#> <ImageServer <1 bands, 27 fields>>
#> Name: FIA_BIGMAP_2018_Species_Aboveground_Biomass
#> Description: BIGMAP Species Aboveground Biomass
#> Extent: -2911620 2933940 164760 3254760 (xmin, xmax, ymin, ymax)
#> Resolution: 30 x 30
#> CRS: 102039
#> Capabilities: Image,Metadata,Catalog,Mensuration
res <- arc_raster(
  bigmap.dataset,
  xmin=-2911620,
  ymin=164760,
  xmax=2933940,
  ymax=3254760,
  bbox_crs=102039
)
#> Warning in CPL_crs_from_input(x): GDAL Error 1: PROJ:
#> proj_create_from_database: crs not found
#> Warning in CPL_crs_from_input(x): GDAL Error 1: PROJ:
#> proj_create_from_database: crs not found

terra::plot(res, 1)

Created on 2024-07-23 with reprex v2.1.0

rhijmans commented 3 months ago

This is because the file is corrupted on Windows. Did you check if you can use the file created on the Mac on your Windows machine? And can you read the file created on Windows in ArcGIS?

The file is created by arcgis::arc_raster (not by "terra"), and I think that it creates a corrupted file on Windows because it has this line

utils::download.file(exported_image_path, tmp, quiet = TRUE)

Where it should be

utils::download.file(exported_image_path, tmp, quiet = TRUE, mode="wb")
rhijmans commented 3 months ago

Fixed in the PR mentioned above