Friday, October 30, 2015

Tutorial on Using and Exporting GIS Vector Dataset in Netlogo

Hi, this is a tutorial on how to import and export Vector dataset in Netlogo. It could be helpful when you want to study a specific area, and you have the data in ArcGIS, for example a shapefile. In this post, you will learn how to import the data into Netlogo and run simulations with it, as well as how to export the data back to ArcGIS for further analysis. Note that this tutorial is for Vector data, if you are interested in using Raster dataset in Netlogo, please check my post on the Urban Growth Model.

In this post, I will talk about how I developed a Schelling segregation model using the map of Washington DC. The model and data are available here:

And a video showing how it works:

Importing and drawing Vector data

Netlogo has a GIS extension that allows us to read data files from GIS and copy the values to patches or turtles for simulation. I am using the following lines to load the Vector data into Netlogo. Note that by loading the shapefile, you will also load the attributes in the .dbf file and the projection information in the .prj file.

Next, I drew the map on the display. To do that, I am using the following lines. In these lines I go through each vector-feature (each polygon), change the drawing color according to the SOC attribute, and then fill the polygon with the corresponding color. In the last two lines, I use gis:draw to draw the boundary of polygon data using white color.

Copying attributes to patches

In Netlogo, we can not ask a polygon to perform anything. Therefore, in order to study the area, we need to copy the attributes into patches. To do that, I loop through each polygon and copy the color attribute to the patch at its centroid. In this way, I am using one patch to represent one polygon. Mind that you may need to have a larger size of Netlogo map, so that two centroids will not lay on the same patch.

Since this is a segregation model, I have also found the neighbors of all polygons. ArcGIS has a tool called Polygon Neighbors. For each polygon, the tool finds all the polygons that have coincident edges with it, and reports the information in a table. I exported this file to a text file and deleted all the headers and labeling numbers. Then, I use the following lines to ask each patch that represents a polygon to read the file and set neighbors.

Exporting Vector dataset

After running the simulation, how do we export the final map into ArcGIS for further analysis? So far the GIS extension does not allow us to modify or create a vector dataset. My idea is to write the information into a text file, open it in ArcGIS, save it as a .dbf file, and replace the original .dbf file. Here are the codes I used to do that.

The color information is stored in the patches that represent polygons. Therefore, I loop through those patches and ask them to write down their ID and then color. Note that more attributes could be easily included in the table if necessary.

It does take an extra step to use this attribute table in ArcGIS.  Simply open the text file in ArcGIS, save as .dbf, and replace the original one (remember to make a copy).

I wish you find this tutorial helpful!


  1. This comment has been removed by the author.

  2. Hello Yang Zhou:

    Thanks for your time and effort for making this tutorial. I'm trying to make a spatial suitability analysis in NetLogo that will be the base for an urban growth model. I want to ask you if it's possible to load several shp files, each one with a single range of values like slope, land value, natural areas, roads, etc. and then ask the model to take into account a particular combination of shps and pass their corresponding sum of values to patches. For example, identify places for potential residential zoning where there are low slopes, low land value, near natural areas and adjacent to main roads. Once the combined zones are identified, urban growth occurs according to certain rules that I'm designing at the moment. Thanks in advance for any help/advice you can give.

    1. Hi Javier,

      Yes, I think you can load different shp files into Netlogo, copy the values to patches (use patches-own variables), and then do the analysis.

  3. Hi, I'm learning how to code in netlogo and hope you can help. Copying the code to display the map in Netlogo 6 looks that it does not work, can you help? Thanks!