isciences / exactextractr

R package for fast and accurate raster zonal statistics
https://isciences.gitlab.io/exactextractr/
272 stars 26 forks source link

`frac` calculations are mixed up in the final named list #89

Closed kasra-keshavarz closed 1 year ago

kasra-keshavarz commented 1 year ago

I am using exactextractr 0.9.1 over a few geospatial features and extracting the fraction of landcover (represented by the frac summary operation) from the Landsat 2010 30m dataset and it seems the final returned results in a named list by exactextracter is mixed up. Meaning, the column names for each feature are not accurate.

PSA: I'm not a professional R developer, so my knowledge of R is pretty basic.

Here is the Shapefile I've used: Sample.zip

Here is an example:

p <- sf::st_read('./path/to/sample/shapefile.shp')
r <- raster::raster('./path/to/sample/raster/landcover.tif')

# testing individual features
p1 <- p[p$COMID == "78006727",]
p2 <- p[p$COMID == "78002119",]

# running exactextractr
lcf1 <- exactextractr::exact_extract(r, p1, 'frac')
lcf2 <- exactextractr::exact_extract(r, p2, 'frac')
lcf <- exactextractr::exact_extract(r, p, 'frac')

And now trying to compare the results:

> lcf1
     frac_1     frac_5    frac_6    frac_8    frac_10     frac_14    frac_16     frac_17     frac_18
1 0.4294958 0.07528087 0.2764581 0.1368234 0.01542376 0.004685122 0.003051661 0.05448234 0.004298975

> lcf2
      frac_8   frac_16   frac_19
1 0.01334671 0.1596555 0.8269978

> lcf
      frac_8    frac_16   frac_19    frac_1    frac_5     frac_6     frac_10     frac_14    frac_17     frac_18
1 0.01334671 0.15965545 0.8269978 0.0000000 0.0000000 0.00000000 0.000000000 0.00000000 0.00000000 0.000000000
2 0.42949581 0.07528087 0.0000000 0.2764581 0.1368234 0.01542376 0.004685122 0.00305166 0.05448234 0.004298975

Please let me know if there is any further questions.

dbaston commented 1 year ago

Yikes! My test case for this was written in exactly the right way to not catch this. Can you try again with the latest?

kasra-keshavarz commented 1 year ago

Thanks! Works perfectly fine now!