How to create moving turtles from shapefiles in Netlogo
I am just getting started with using Netlogo to create an agent based model. I have two shapefiles that I want to use: a city network map (line-shapefile) and a city scooter dot file. The idea is to make them travel through the city along the lines of the network shapefile. Since I'm new to Netlogo, I was able to load these shapefiles in my model. Can anyone give me a headgear helping me create turtles from scooter (dot) registrations and letting them navigate network lines. So far, I've found a little help on the internet and it won't work with trial and error. So far, my code was as follows:
extensions [ gis ]
to load
ca
let network gis:load-dataset "Roads_Asmterdam.shp"
foreach gis:feature-list-of network
[ gis:set-drawing-color white
gis:draw ? 0.3
]
let people gis:load-dataset "scooters_Amsterdam.shp"
foreach gis:feature-list-of people
[ gis:set-drawing-color blue
gis:draw people 3
]
end
So, as far as I know, I need a function where I want to move the turtles. And I need a function to generate possible moving turtles from a point-shape file, but also I need to tell them that they are only using lines and not the whole area.
Thank you very much in advance!
source to share
After downloading the line shape file, you need to convert them to agent / turtles network and link them. NetLogo doesn't do this for you, you have to iterate over all the features, line segments and coordinate yourself. Then you need to place the scooters in coordinates from the linear grid, and then you can "ask" them to move.
This is what I came up with:
extensions [ gis ]
globals [ roads-dataset scooter-dataset ]
breed [ nodes node ]
breed [ scooters scooter ]
breed [ walkers walker ]
walkers-own [ wlocation ]
scooters-own [slocation]
to setup
; reset
clear-all
reset-ticks
; load data set
gis:load-coordinate-system (word "C:/Program Files/NetLogo 5.3.1/app/models/Code Examples/GIS/data/WGS_84_Geographic.prj")
set roads-dataset gis:load-dataset "C:/shape/roads.shp"
set scooter-dataset gis:load-dataset "C:/shape/scooter.shp"
gis:set-world-envelope (gis:envelope-of roads-dataset)
; draw data set
gis:set-drawing-color blue
gis:draw roads-dataset 1
make-road-network
end
to make-road-network
clear-links
let first-node nobody
let previous-node nobody
foreach gis:feature-list-of roads-dataset [ ; each polyline
foreach gis:vertex-lists-of ? [ ; each polyline segment / coordinate pair
foreach ? [ ; each coordinate
let location gis:location-of ?
if not empty? location [ ; some coordinates are empty []
create-nodes 1 [
set color green
set size 1
set xcor item 0 location
set ycor item 1 location
set hidden? true
if first-node = nobody [
set first-node self
]
if previous-node != nobody [
create-link-with previous-node
]
set previous-node self
]
]
]
set previous-node nobody
]
]
; connect adjacent polylines/roads
ask nodes [ create-links-with other nodes in-radius 0.001 ]
end
to add-agents
create-walkers 5 [
set color red
set wlocation one-of nodes
move-to wlocation
]
end
to add-scooters
foreach gis:feature-list-of scooter-dataset [
foreach gis:vertex-lists-of ? [
let location gis:location-of (first ?)
create-scooters 1 [
set color yellow
set size 1
set xcor item 0 location
set ycor item 1 location
let nearest-node min-one-of (nodes in-radius 10)[distance myself]
set slocation nearest-node
move-to slocation
]
]
]
end
to go
ask walkers [
let new-location one-of [link-neighbors] of wlocation
move-to new-location
set wlocation new-location
]
ask scooters [
let new-location one-of [link-neighbors] of slocation
move-to new-location
set slocation new-location
]
end
Some resources and sample code that I found particularly helpful:
- NetLogo Programming Guide and Sample Walk and Network Models That Come with NetLogo
- NetLogo Bag of Tricks - Venice Code Sample
- Duncan Golicher: Importing Points into Netlogo and Forming a Mesh
source to share