0

I have a TIFF output from FRAGSTATS along with an attribute table with different variables. I'd like to add this to R with the end goal of extracting a map of each variable from the attribute table so I can then add these into an occupancy model.

The TIFF from FRAGSTATS has been manipulated in ArcGIS to be a ESRI GRID, then back to a TIFF so the resulting tif has a 'values' field which identifies the grid location. This field matches to the patch ID (PID) field in the attribute table.

FRAGTIF<- raster("/patchmetrics.tif")
> FRAGTIF
class      : RasterLayer 
dimensions : 600, 1055, 633000  (nrow, ncol, ncell)
resolution : 33.54191, 33.54191  (x, y)
extent     : 5305396, 5340783, -2247132, -2227007  (xmin, xmax, ymin, ymax)
crs        : +proj=merc +a=6378137 +b=6378137 +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +k=1 +units=m +nadgrids=@null +wktext +no_defs 
source     : C:/Users/e77-smith/Desktop/MadOccMod/FRAG Layers/patchmetrics.tif 
names      : patchmetrics 
values     : 1, 8374  (min, max)

patch.df <- read.csv(file = "/Users/e77-smith/Desktop/MadOccMoD/210622 fragout.csv")
patch.df
    PID     TYPE       AREA       PERIM    GYRATE   SHAPE       CORE      PROX       SIMI       ENN
1        1  cls_0    107.2188   33944.504 3833.3469  8.1613   107.2188     0.000      0.000 3152.9480

patch.df has 8374obs of 10 variable so I'm pretty sure it matches.

I can't figure out how to join them, I've tried left_join but I think that only works for vector files.

1 Answer 1

0

Things work a bit differently in R than ArcGIS, at least on the surface. You cannot "join" a data.frame to a raster in the way you are thinking. Rasters store a numeric array and do not contain attribute tables. The ESRI raster model is the same, even when there is an associated VAT, it is just obscured by the GUI.

You could emulate this behavior using ratify but, it really is not necessary and could cause some headaches down the road. The only good reason that I have found to ratify a raster with an attribute table is for plotting purposes else wise, you can just use a data.frame. All you need here is a data.frame with a unique identifier that matches the raster values and columns containing new values. In this case I believe that you are simply looking for reclassify.

The idea is that you have a patch ID raster and a corresponding flat file relating the patch ID to each patch-level landscape metric. Now, the intent is to create rasters representing each metric, correct?

First, lets create a patch ID raster and an associate data.frame with PID, AREA and iji columns.

library(raster)

frag <- raster(extent(5305396, 5340783, -2247132, -2227007),
               nrow=300, ncol=528)
    frag[] <- sample(1:500, ncell(frag), replace=TRUE)
    
( patch.df <- data.frame(PID=1:500, 
                         AREA=runif(500, 10, 500), 
                         iji=runif(500, 0, 1) ) )

Now, the reclassify function can take a two or three column matrix. In the case of three columns the first two represent a range and the third the new value. In the case of two columns, the first is the original value (ie., patch ID) and the second is the new value (ie., iji). This syntax simply pulls the Patch ID and iji columns (1,3) and coerces to a matrix within reclassify. You can then write the result out to a new on-disk raster.

( iji <- reclassify(frag, as.matrix(patch.df[,c(1,3)])) )

****Edit (09/10/24) For terra rasters use the levels function with a data.frame where the value column represents the values in your raster that match the attribution and the second column contains attributes. eg., assuming you have a raster with values 1-10 and attributes 10-20

levels(x) <- data.frame(value=1:10, att=10:20) 
1
  • Forgive me as I am very new to R but I am confused as to how I fit my real data into the fake data frames here. As I understand it you have created a raster to the same extent as mine, but with 1/2 the resolution and then turned this into a matrix with 500 rows? You've then created a fake df with 500 rows and 2 variables (AREA and iji) then linked columns 1 and 3 (PID and iji) to the raster? So iji is now a raster with iji variables linked to the 500 cells that can be mapped. If i've understood this correctly how do I then add my real data? Commented Jun 29, 2021 at 11:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.