Netlogo: Using .csv as a cross-reference to raster value

76 views Asked by At

I am trying to import a raster file that contains land-cover codes. Once the raster sets the patch variables to these land-cover codes, I want to link those codes to a separate .csv that has vegetation-specific parameters for each land-cover code. Thus each patch will be assigned the .csv variables based on its land-cover code. I'm completely stumped as to how to do this. More generally, how can I use a .csv as a cross-reference file? I don't have any code examples but here is an example of the kind of .csv I want to use: Table example

So this .csv would assign the GR1 variables to multiple patches with land-cover code GR1

1

There are 1 answers

0
Luke C On

I agree with JenB for sure, especially if your values table is relatively short. However, if you have a lot of values, it might work to use the csv and table extensions together to make a dictionary where the 'land-cover code' acts as the key to retrieve the other data for your patch. So one path would be:

  • Read the csv
  • Take one of the columns as the key
  • Keep the remaining columns as a list of values
  • Combine these two lists into a list of lists
  • Make a dictionary out of those two lists
  • Have each patch query the dictionary for the values of interest

So with this example csv table:

lcover,fuel,type
GR1,15,a
GR2,65,b
GR3,105,a

And these extensions and variables:

extensions [ csv table ]

globals [ csvRaw keyValList dataList patchDataDict ]

patches-own [ land-cover fuel patchType]

We can run a code block to do all these steps (more explanation in comments):

to setup
  ca
  
  ; Load the csv
  set csvRaw but-first csv:from-file "landCoverMeta.csv"
  print csvRaw 
  
  ; Pull first value (land cover)   
  set keyValList map first csvRaw
  print keyValList
  
  ; Pull data values
  set dataList map but-first csvRaw
  print dataList
  
  ; Combine these two lists into a list of lists
  let tempList ( map [ [ a b ] -> list a b ] keyValList dataList ) 
  
  ; Make a dictionary with the land cover as the key
  ; and the other columns as the value (in a list)
  set patchDataDict table:from-list tempList 
  
  ask patches [
    ; Randomly set patch 'land cover' for this example
    set land-cover one-of [ "GR1" "GR2" "GR3" ]
    
    ; Query the dictionary for the fuel column (item 0 since
    ; we've used landcover as the key) and for the type (item 1)
    set fuel item 0 table:get patchDataDict land-cover
    set patchType item 1 table:get patchDataDict land-cover
  ]
  
  ; Do some stuff based on the retrieved values
  ask patches [
    set pcolor fuel
    if patchType = "a" [
      sprout 1
    ]
  ]
  reset-ticks
end

This generates a toy landscape where each patch is assigned a fuel and patchType value according to a query based on the first column of that csv:

enter image description here

Hopefully that gets you started