Netlogo:使用.csv作为栅格值的交叉引用

问题描述

我正在尝试导入包含陆地覆盖代码的栅格文件。栅格将补丁变量设置为这些土地覆盖代码后,我想将这些代码链接一个单独的.csv,该代码具有每个土地覆盖代码的特定于植被的参数。因此,每个补丁将根据其土地覆盖代码分配.csv变量。我完全不知道该怎么做。更一般而言,如何将.csv用作交叉引用文件?我没有任何代码示例,但这是我要使用的.csv类型的示例: Table example

因此,此.csv会将GR1变量分配给具有土地覆盖代码GR1的多个补丁

解决方法

我当然同意JenB,尤其是在您的价值观表相对较短的情况下。但是,如果您有很多值,可以将csvtable扩展名一起使用以创建字典,其中“土地覆盖代码”充当检索其他数据的关键为您的补丁。因此,一种方法是:

  • 阅读csv
  • 将其中一列作为键
  • 将其余的列保留为值列表
  • 将这两个列表合并为列表列表
  • 从这两个列表中制作字典
  • 让每个补丁程序在字典中查询感兴趣的值

因此,此示例csv表:

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

以及这些扩展名和变量:

extensions [ csv table ]

globals [ csvRaw keyValList dataList patchDataDict ]

patches-own [ land-cover fuel patchType]

我们可以运行一个代码块来完成所有这些步骤(注释中有更多解释):

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

这会生成一个玩具景观,其中根据基于该csv第一列的查询为每个补丁分配了fuelpatchType值:

enter image description here

希望这可以帮助您入门