eoway / Dja_dispersal_forest_structure

Scripts and files related to the NASA funded project on understanding seed disperser movements and their consequences across rainforest gradients of structural and phenological diversity.
2 stars 0 forks source link

Diff rast error #9

Open karendutko opened 2 years ago

karendutko commented 2 years ago

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)

warnings() Warning messages: 1: In GFC - (JRC_deforestation + JRC_degradation) : Raster objects have different extents. Result for their intersection is returned 2: In v1 - v2 : longer object length is not a multiple of shorter object length 3: In v1 - v2 : longer object length is not a multiple of shorter object length 4: In v1 - v2 : longer object length is not a multiple of shorter object length

eoway commented 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.

karendutko commented 2 years ago

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()

eoway commented 2 years ago

I just uploaded a slightly newer version of the code. Make sure you specify spatial = T when using the rasterToPoints() function.

karendutko commented 2 years ago

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')

eoway commented 2 years ago

try

GFC_e <- raster::extract(GFC, grid)

eoway commented 2 years ago

Did that work or are you still getting an error when using the extract() function?

karendutko commented 2 years ago

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.

karendutko commented 2 years ago

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

eoway commented 2 years ago

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.

karendutko commented 2 years ago

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.

karendutko commented 2 years ago

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?

eoway commented 2 years ago

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.