I'm trying to use netCDF data, and would like to use sf rather than ncdf4 if it's already built to load netCDF data into sf objects.

However, I'm finding that reading the file using sf::st_read isn't working.

You csn get the data for the below example here: ftp://ftp.cdc.noaa.gov/Datasets/cpc_global_temp/tmax.2018.nc

df <- st_read("tmax.2018.nc",)
# Reading layer `tmax.2018' from data source `tmax.2018.nc' using  driver `netCDF'
# Warning message:
# no simple feature geometries present: returning a data.frame or tbl_df 

It can't find any of the geometries or the data. I'm guessing this is because I am not specifying the layer correctly.

Any help as to how to find the right layer or how to get sf to play nicely with netCDF data?


I don't see any vector data in this NetCDF - so sf is not the right package for reading it. You could use raster:

> library(raster)
> tmax = stack("./tmax.2018.nc")
> dim(tmax)
[1] 360 720 188

Are you expecting 188 layers of 360x720 pixels? Looks like this:

> plot(tmax[[143]])

enter image description here

  • Thanks, very helpful as I wasn't aware of raster::stack. If I want an sf object it should be easy to convert these into polygons and go from there. – luke.sonnet Jul 9 '18 at 2:31
  • This is continuous data, as such there are no discrete areas that would be represented as polygon features. You will, in effect, be getting a polygon for every pixel. If you are fixated on getting out of a raster format into a table like format, then use the SpatialPixelsDataFrame sp object class that will results in a spatial raster object with a data.frame of associated attributes. And please if an answer is helpful or actually answers your question, as in this case, please up-vote or check it as answered. – Jeffrey Evans Aug 9 '18 at 19:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.