hunzikp / velox

https://hunzikp.github.io/velox/
119 stars 23 forks source link

GeoTIFF raster values "reasonable" with raster extract but all NA velox extract #40

Open turbomam opened 5 years ago

turbomam commented 5 years ago

I'm cross posting this from the GIS Stack Exchange: https://gis.stackexchange.com/questions/314950/all-geotiff-raster-values-na-after-converting-to-velox


I'm able to extract average tree cover values for each of the three small counties in the US State of Delaware, but I don't seem to have enough CPU or RAM to do the same thing for all counties in the US.

I'm getting my tree cover data from https://prd-tnm.s3.amazonaws.com/StagedProducts/Small-scale/data/LandCover/tree48i0100a.tif_nt00840.tar.gz. See also https://www.sciencebase.gov/catalog/item/581d0548e4b08da350d52653

library(raster)
library(tigris)

tigris.state.counties <- counties(state = 10)
tree.raster <-
  raster("tree48i0100a.tif")

polygon.trees <-
  extract(tree.raster,
          tigris.state.counties,
          fun = mean,
          na.rm = TRUE)

report <-
  cbind.data.frame(tigris.state.counties$NAME, polygon.trees)

print(report)

  tigris.state.counties$NAME polygon.trees
1                 New Castle      70.75920
2                     Sussex      68.17089
3                       Kent      67.00225

So I have set up an R environment on a 64 GB AWS instance, and I'm trying to take advantage of the velox fast extraction. But all of the values I extract are NA.

What am I doing wrong?

UPDATE: I still get NAs even if I use the small polygon option

FWIW, I was able to replicate the [velox extract tutorial][2] and even modify it to get the night light intensity in the Delaware counties.

My attempt to extract tree cover in Delaware counties with velox:

library(velox)
library(tigris)

tree.velox <- velox("tree48i0100a.tif")

tigris.state.counties <- counties(state = 10)

tree.mean.mat <-
  tree.velox$extract(
    sp = tigris.state.counties,
    fun = function(x)
      mean(x, na.rm = TRUE)
  )

report <-
  cbind.data.frame(tigris.state.counties$NAME, tree.mean.mat)

print(report)

     tigris.state.counties$NAME tree.mean.mat
591                  New Castle            NA
1150                     Sussex            NA
2861                       Kent            NA
mdsumner commented 5 years ago

Are they possibly in different projections? Raster will reproject the second argument to the raster if needed

turbomam commented 5 years ago

I didn't do anything to ensure they used the same projection. Thanks for the suggestion. May not get a chance to try it out for a week or so,