0

When I try to convert a .tif raster data object to an sf object using st_as_sf() in R, I get the following error:

Error in abind(x, along = length(dim(x)) + 1) : along must be between 0 and 2

Does anyone know how to solve this?

The .tif data for which to reproduce this I got from https://sedac.ciesin.columbia.edu/data/set/gpw-v4-data-quality-indicators-rev11/data-download.

The error occurs both for the Data Context and Mean Administrative Unit Area data. Choose GeoTiff and 30 sec (1 km) resolution.

The reprex R code is then (for the Data Context data, for example):

library(stars)

# Load data
gpw_raw <- read_stars("gpw_v4_data_quality_indicators_rev11_context_30_sec.tif")

# Attemt to convert .tif into sf object
gpw <- st_as_sf(gpw_raw)

One resource I used, which in the end did not allow me to solve the issue, is: https://r-spatial.github.io/stars/articles/stars5.html#vectorizing-a-raster-object-to-an-sf-object

Thanks for your help!

1

st_as_sf fails if the stars object is a "proxy" object. I've downloaded the smaller 30 minute dataset to demonstrate.

Read in as a proxy object and it fails:

> gpw_raw <- read_stars("gpw_v4_data_quality_indicators_rev11_context_30_min.tif", proxy=TRUE)
> gpw <- st_as_sf(gpw_raw)
Error in abind(x, along = length(dim(x)) + 1) : 
  along must be between 0 and 2

Read in not as a proxy object and it works:

> gpw_raw <- read_stars("gpw_v4_data_quality_indicators_rev11_context_30_min.tif", proxy=FALSE)
> gpw <- st_as_sf(gpw_raw)
> 

This is because somewhere in st_as_sf it is expecting to find the data, but instead a proxy object only has a pointer to the data, reading it on demand, but it seems st_as_sf isn't demanding enough. Possibly a bug in the st_as_sf function, report to the stars package: https://github.com/r-spatial/stars/issues/ issue tracker if you want.

I tried this with the 30 second data but that is 43200 x 21600 cells which means nearly a billion polygons when converted using st_as_sf. If you've got the RAM for that then fine, read it in with proxy=FALSE and it should work. Otherwise, find another way to deal with this data.

1
  • Hi @Spacedman, thanks a lot for your quick help! This solves my issue and also explains why it took an incredible amount of time to load. Have a good day! – Will M Feb 10 at 18:27

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.