Open karendutko opened 2 years ago
Hi Karen,
I'll take a look. Can you tell me exactly which code you're using and which datasets, and make sure the code is on GitHub and the datasets are on box?
It looks like the extents need to be aligned.
Hi Elsa, the code is under the deforestation/degradation folder on github named "spatial overlap" and I copied it here as well. I used the original tif files for deforestation, degradation, and tree loss from the jrc and gfc datasets. I did not make them into a dataframe because it seems as though the rasters can be added and subtracted without that extra step, but I am wondering how I should align the extents.
install.packages("raster") install.packages("rgdal") install.packages("dplyr") install.packages("tidyverse") install.packages("ggplot2") install.packages("devtools") install.packages("benford")
library(raster) library(rgdal) library(dplyr) library(tidyverse) library(devtools) library(ggplot2) library(benford)
memory.limit(size=40000000)
setwd("C:/Users/ForestsGlobalChange1/Desktop/karen files")
GFC <- raster("clipped hansen lossyear.tif")
JRC_deforestation <- raster("clipped deforestation year.tif") str(JRC_deforestation)
JRC_degradation <- raster("clipped degredation year.tif") str(JRC_degradation)
RADD <- raster("RADD_NASA_Biodiv_clipped_b2.tif")
GFC[GFC > 0] <- 1 str(GFC)
JRC_deforestation[JRC_deforestation > 1999] <- 1 JRC_deforestation[JRC_deforestation < 2000] <- 0 str(JRC_deforestation) dim(JRC_deforestation)
JRC_degradation[JRC_degradation > 1999] <- 1 JRC_degradation[JRC_degradation < 2000] <- 0 str(JRC_degradation) dim(JRC_degradation)
diff_rast <- GFC - JRC_deforestation warnings()
I just uploaded a slightly newer version of the code. Make sure you specify spatial = T when using the rasterToPoints() function.
I updated the code but I am currently receiving this error message
GFC_e <- extract(GFC, grid) Error in UseMethod("extract") : no applicable method for 'extract' applied to an object of class "c('RasterLayer', 'Raster', 'BasicRaster')
try
GFC_e <- raster::extract(GFC, grid)
Did that work or are you still getting an error when using the extract() function?
When I was running it there was no error, but the computer wasn't able to process it because it was interrupted with the connection. I will try it again right now but I am waiting for it to restart R because the desktop froze. Sorry about the hassle, its been slower than expected but if there is a way to try this in Arc I would love to give it a shot.
Just an update, I was trying to get the line of code to process but its been over an hour and there is still no progress, I restarted the desktop but please let me know if you have any other ideas on how to proceed
We could do it in ArcGIS, but let's keep trying with R until we get there.
Can you log on and try it again. Let me know if the computer is still intermittently kicking you off. The code will take time to run. Just leave it running and maybe put a note (in Word or a notepad) on the desktop telling others to leave that code in R running.
Make sure you export/write the file once it's done so that you don't have to extract each time.
Keep me posted on how all of the above goes. If it still isn't extracted by just before we meet at 4pm tomorrow, we'll figure out how to do it in ArcGIS.
Just an update, the raster to points function worked and now I am running the extract multi values function in Arcmap. I started it at 3:38 and left a sticky note to let the function keep running, I think it should be finished in the next few hours but it was running for over four hours so far. I will keep you updated with how it goes.
I downloaded the defor_dat_points file again and tried to open it in R, however I am still receiving this error:
arc <- readOGR(dsn=getwd(), layer="defor_dat_points") Error in readOGR(dsn = getwd(), layer = "defor_dat_points") : no features found In addition: Warning message: In ogrFIDs(dsn = dsn, layer = layer) : no features found arc <- readOGR(dsn="D:/karen files", layer="defor_dat_points") Error in readOGR(dsn = "D:/karen files", layer = "defor_dat_points") : no features found In addition: Warning message: In ogrFIDs(dsn = dsn, layer = layer) : no features found
Do you have any suggestions with how to proceed?
It sounds like it's still exporting improperly.
I would recommend re-running everything from the start (when you create the points shapefile of the grid from the GFC raster). Make sure you save that to your folder on the D drive when you're creating the file. In other words, change the path and file name before you hit OK to run the conversion of the raster to points.
Then re-run the extract multi points. When that's complete, check the attribute table in ArcMap and if there are columns with values for each of the raster datasets, try loading that file in R.
Let me know how it goes.
When I try the code, I receive the following warning. Do you have any suggestions?
diff_rast <- GFC - JRC_deforestation
Error in .local(x, row, nrows, ...) : validRow(x, row) is not TRUE In addition: There were 49 warnings (use warnings() to see them)