Skip to content

Instantly share code, notes, and snippets.

@mdsumner
Last active February 16, 2018 01:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mdsumner/ca332f9c5112e00edcc40fdcf2a4b968 to your computer and use it in GitHub Desktop.
Save mdsumner/ca332f9c5112e00edcc40fdcf2a4b968 to your computer and use it in GitHub Desktop.

Edit: ignore this first block, it's a mistake. See here for my better example. I think this works already on Windows rgdal-CRAN binary, so that's great. I'll use travis to find out if it works on OSX.

http://rpubs.com/cyclemumner/358029

The conversation started here: https://stat.ethz.ch/pipermail/r-sig-geo/2018-February/026328.html

END EDIT 2018-02-09

library(raster)
yesterday <- Sys.Date() - 1
TileLevel <- 3
xml_specification <-
  paste0('<GDAL_WMS>
         <Service name="TMS">
         <ServerUrl>
         https://gibs.earthdata.nasa.gov/wmts/epsg3413/best/MODIS_Aqua_CorrectedReflectance_Bands721/default/
         ',
         format(yesterday, "%Y-%m-%d"),
         '/250m/${z}/${y}/${x}.jpg</ServerUrl>
         </Service>
         <DataWindow>
         <UpperLeftX>-4194304</UpperLeftX>
         <UpperLeftY>4194304</UpperLeftY>
         <LowerRightX>4194304</LowerRightX>
         <LowerRightY>-4194304</LowerRightY>
         <TileLevel>', TileLevel, '</TileLevel>
         <TileCountX>2</TileCountX>
         <TileCountY>2</TileCountY>
         <YOrigin>top</YOrigin>
         </DataWindow>
         <Projection>EPSG:3413</Projection>
         <BlockSizeX>512</BlockSizeX>
         <BlockSizeY>512</BlockSizeY>
         <BandsCount>3</BandsCount>
         </GDAL_WMS>
         ')
## raster is *lazy* here, we only get metadata
rastersource <- raster(xml_specification)
localproj <- projection(rastersource)
llproj <- "+init=epsg:4326"
local_ex <- projectExtent(raster(extent(c(-135, 170), c(52, 75)), 
                                 crs = llproj), localproj)

x <- crop(rastersource, extent(local_ex))

Error in rgdal::getRasterData(con, offset = offs, region.dim = reg, band = object@data@band) : Failure during raster IO

sessionInfo() R version 3.4.3 (2017-11-30) Platform: x86_64-pc-linux-gnu (64-bit) Running under: Debian GNU/Linux buster/sid

Matrix products: default BLAS: /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.7.1 LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.7.1

locale: [1] LC_CTYPE=en_AU.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_AU.UTF-8 LC_COLLATE=en_AU.UTF-8
[5] LC_MONETARY=en_AU.UTF-8 LC_MESSAGES=en_AU.UTF-8
[7] LC_PAPER=en_AU.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_AU.UTF-8 LC_IDENTIFICATION=C

attached base packages: [1] stats graphics grDevices utils datasets methods
[7] base

other attached packages: [1] raster_2.6-7 sp_1.2-7 devtools_1.13.4

loaded via a namespace (and not attached): [1] compiler_3.4.3 rgdal_1.2-16 tools_3.4.3 withr_2.1.1
[5] yaml_2.1.16 Rcpp_0.12.15 memoise_1.1.0 grid_3.4.3
[9] digest_0.6.15 lattice_0.20-35

@afischbach
Copy link

afischbach commented Feb 8, 2018

sessionInfo()
R version 3.4.3 (2017-11-30)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 LC_MONETARY=English_United States.1252 LC_NUMERIC=C LC_TIME=English_United States.1252

attached base packages:
[1] stats graphics grDevices utils datasets methods base

other attached packages:
[1] maptools_0.9-2 raster_2.6-7 rgdal_1.2-16 sp_1.2-7

loaded via a namespace (and not attached):
[1] compiler_3.4.3 foreign_0.8-69 Rcpp_0.12.14 grid_3.4.3 lattice_0.20-35

I get this error on the raster function call.

cat(xml_specification)
<GDAL_WMS>


https://gibs.earthdata.nasa.gov/wmts/epsg3413/best/MODIS_Aqua_CorrectedReflectance_Bands721/default/
2018-02-07/250m/${z}/${y}/${x}.jpg


-4194304
4194304
4194304
-4194304
3
2
2
top

EPSG:3413
512
512
3
</GDAL_WMS>
rastersource <- raster(xml_specification)
Error in basename(x) : path too long

@btupper
Copy link

btupper commented Feb 8, 2018

For what it is worth, I get the same error that Michael shows.

> sessionInfo()
R version 3.4.2 (2017-09-28)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Sierra 10.12.6

Matrix products: default
BLAS: /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] raster_2.6-7 sp_1.2-5    

loaded via a namespace (and not attached):
[1] compiler_3.4.2  rgdal_1.2-16    Rcpp_0.12.14    grid_3.4.2     
[5] lattice_0.20-35

@mdsumner
Copy link
Author

mdsumner commented Feb 8, 2018

In the first document here there's a stray newline in the ServerUrl - so raster gets the template fine from GDAL, but then fails when it tries to poll the actual data source. Also, the extents in longitude/latitude don't actually crop the source in any significant way.

Here's a better answer, using the slightly obscure extent(raster, r1, r2, c1, c2) idiom:

http://rpubs.com/cyclemumner/358029

@afischbach
Copy link

afischbach commented Feb 9, 2018

I have run the RPubs code through the raster function call and get the same error. My sessionInfo is listed below.
rastersource <- raster(xml_specification)
Error in basename(x) : path too long
sessionInfo()
R version 3.4.3 (2017-11-30)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1
Matrix products: default
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 LC_MONETARY=English_United States.1252 LC_NUMERIC=C LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] raster_2.6-7 sp_1.2-7
loaded via a namespace (and not attached):
[1] compiler_3.4.3 Rcpp_0.12.15 grid_3.4.3 lattice_0.20-35

@mdsumner
Copy link
Author

mdsumner commented Feb 16, 2018

Hey that might mean that the WMS driver is not available on Windows on that machine, can you see if "WMS" is in this list?

sort(rgdal::gdalDrivers()$name)

Is rgdal installed, perhaps not?

I just checked and the current CRAN version does have WMS, so if it doesn't work with rgdal installed then I'd be keen to find out why. Thanks! (I'm guessing that raster silently tries to read it as a file after attempting with rgdal, but if rgdal is not available or not capable then it will just ignore it and keep trying).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment