Search code examples
netlogoagent-based-modeling

Exporting a list of patches to a shapefile in NetLogo


I'm building an agent-based model in NetLogo where the agents walk to a target. I'm using the GIS Extension for NetLogo 6.3. At each tick they record the patch where they are currently standing on a list called "path".

I want to include a button to export this list to a shapefile, but I don't know how to do it. I believe I need to use the "gis:store-dataset" function. I'm using it like this:

to export-path  
let file (word "path_output.shp")
  if file-exists? file [file-delete file]
  file-open file
  
  let exported-path path
  gis:store-dataset exported-path file
end

At the interface page I've set up a button calling the procedure with an ask turtles []. However I got the error message saying that this is not a dataset. Anyone can help me with this? Thanks.


Solution

  • For computation and precision (depending on how big of an area your patches represent) I would suggest that instead of storing the patches in their lists, the turtles simply record their coordinates (using something like envelope-of) so that you can use your GIS to translate their coordinates into a shapefile with finer control:

    extensions [ gis csv ]
    
    turtles-own [ path ]
    
    to setup
      ca
      reset-ticks
      let shp_path "C:/gis_example/british_columbia_administrative.shp"
      let prj_path "C:/gis_example/british_columbia_administrative.prj"
    
      gis:load-coordinate-system prj_path
      let shp gis:load-dataset shp_path
      let base_envelope gis:envelope-of shp
      gis:set-world-envelope-ds base_envelope
      gis:set-drawing-color white
      gis:draw shp 1
      
      ask n-of 3 patches [
        sprout 1 [
          set path ( list self-ticks-coords )
          show path
        ]
      ]
      
    end
    
    to-report self-ticks-coords 
      ; Report the current ticks and then middle two 'envelope' values of the turtle
      report  sentence ticks (reduce sentence sublist gis:envelope-of self 1 3) 
    end
    
    to go
      ask turtles [
        rt random 60 - 30
        fd 1
        set path lput self-ticks-coords path
      ]
      tick
    end
    
    to go-10-then-export
      repeat 10 [
        go 
      ] 
      let out-list reduce sentence [self-who-tick-coords] of turtles
      set out-list fput [ "who" "tick" "x" "y" ] out-list
      csv:to-file "C:/gis_example/example_coords.csv" out-list
    end
    
    to-report self-who-tick-coords
      ; Report a formatted list of who, tick, and coordinate vlaues
      let who-tick-coord-list map [ i -> ( sentence who i ) ] path
      report who-tick-coord-list
    end
    

    This exports a csv that stores the turtle identifier, the timestep, and coordinates (and flexibly can store whatever info you need) which I find more useful. My two cents!

    enter image description here

    Dataset downloaded from MapCruzin.com