Drawing over GIS data in Netlogo - netlogo

I want a turtle to colour an area (in-radius 5), on top of the GIS data I have imported.
Please find attached the line of code I am using which is not working.
Is there any way how to do this?
Thanks!
extensions[gis]
globals [map-boundary]
to setup
ca
set map-boundary gis:load-dataset "/Users.shp"
create-turtles 50
ask turtles [setxy random-xcor random-ycor set size 1 set color grey]
gis:set-world-envelope (gis:envelope-union-of (gis:envelope-of map-boundary))
gis:import-wms-drawing "https://ows.terrestris.de/osm/service?" "EPSG:4326" "OSM-WMS" 1
reset-ticks
end
to go
ask turtle 1 [ask patches in-radius 5 [set pcolor blue]]
end

I'm not sure what you mean "not working", and lacking the datasets you have I can't reproduce the problem.
The Models Library has some GIS examples. What's not obvious is where Uri Walenski's "shared-dataset" files are that you need to run them models, but I found some version of them here:
https://ccl.northwestern.edu/netlogo/5.0/models/Code%20Examples/GIS/data/
and when I double-click countries.shp and countries.dbf they download and I can move them to my own new "shared-dataset" folder under the folder my model is in. That done, the model "create-turtles-inside-polygon" runs.
I removed most of and added a line to draw a blue region around turtle #1 and it seems to work. What does your attempt to draw the blue region do or not do that's different?
extensions [gis]
globals [dataset]
breed [citizens citizen]
citizens-own [cntry_name curr_type]
breed [manual-citizens manual-citizen]
manual-citizens-own [country-name currency-type]
to test-turtles
clear-all
set dataset gis:load-dataset "./shared-datasets/countries.shp"
gis:set-world-envelope gis:envelope-of dataset
gis:set-drawing-color red
gis:draw dataset 1
foreach gis:feature-list-of dataset [ country ->
gis:create-turtles-inside-polygon country turtles 1
;;clear-turtles
]
ask turtle 1 [ ask patches in-radius 5 [set pcolor blue]]
end

Related

What is causing my patches to cover my turtles?

I have been following the tutorial listed here Netlogo tutorial pt3
In the section "Patches and variables" I simply do not observe the image shown when I followed the steps exactly.
Instead, I can only see a green cube with my agents seemingly stuck inside.
What am I missing here? My word is only a single cube.
These are the routines that I've copied from the tutorial that should show turtles scattered across the green patch.
to setup
clear-all
setup-patches
setup-turtles
reset-ticks
end
to setup-turtles
create-turtles 100
ask turtles [ setxy random-xcor random-ycor ]
end
to setup-patches
ask patches [ set pcolor green ]
end
It turns out the tutorial omits to tell you that you need to set the max-pzcor value in model settings to zero for this to work.

NetLogo: Having a turtle remember its starting location

I want to have my turtles move back and forth between a central area and their starting location. I have set the central area (patch 0 0, and its neighbouring patches). I have set these turtles to begin from random locations on setup.
Now I need them to move to the central area and be able to remember and return to their respective starting positions. Here is my attempt, but one that is not working.
ask patches
[ set target-patch patch 0 0
ask target-patch
[ set pcolor green
ask neighbors [set pcolor green]
set hold-time 5
]
]
create-turtles 10
[ set shape "car"
set size 1
set color white
setxy random-xcor random-ycor
if (patches != patches with [pcolor = green])
[ set start-position (random-xcor random-ycor)] ;; line with error
]
to go
ask turtles
[ set heading target-patch move-to target-patch
set hold-time hold-time + 5
]
ask turtles
[ if hold-time >= 10
[ set heading start-position move-to start-position]
]
end
There are several problems with your code. I strongly suggest that you code in smaller pieces. That is, add some code and make sure it works before writing the next piece. Making sure it works is not just making it through without error messages, it needs to do what you expect it to do.
On your specific question. The line if (patches != patches with [pcolor = green]) is causing an error. First, patches is the set of all patches, not just a particular patch. So you are (sort of) asking whether the set of all patches is not equal to the set of patches that are green. Is that really what you intended? If so, it is easier to simply ask whether there is any patch that is not green:
if any? patches with [pcolor != green]
or to check whether they are all green and continue if not:
if not all? patches [pcolor = green]
However, since you are asking about moving back and forth to and from the central green patches, I think you really want to have the turtle check whether the patch they happen to be located on is green. This code looks at the patch where the turtle is located (patch-here) and checks whether the color (pcolor) is green:
if [pcolor] of patch-here = green [ ]
However, one of the tricks of NetLogo is that turtles can access the variables of the patch they are on directly. Note that a patch cannot access a turtle's variables because there may be multiple turtles on the patch so the patch doesn't know which turtle you want. But a turtle can only ever be on one patch at once. So you could write:
if pcolor = green [ ]
You also need to rethink this code:
ask patches
[ set target-patch patch 0 0
ask target-patch
[ set pcolor green
ask neighbors [set pcolor green]
set hold-time 5
]
]
This suggests to me that you have misunderstood something very fundamental to NetLogo programming. You need to think from the perspective of an individual agent. Looking at this code, you first do ask turtles, so that is going to run through all the turtles in random order. Let's call them A, then B, then C and so on.
What is each turtle going to do? Everything in the [ ]. So, A sets the value of the global variable named "target-patch" to patch 0 0. Then A asks that patch to turn green, have the 8 surrounding patches to turn green, and to set the variable "hold-time" to the value 5.
So far, so good. But then turtle B does exactly the same thing - it assigns "target-patch", turns it and its neighbors green etc. Then turtle C. If you have 100 turtles, this block of code will run 100 times and do exactly the same thing each time.

How to get turtles run back and forth a coloured patch?

I have a simple image as a map. I would like the turtles to start from the lighter grey colour and run to the darker patches for resources. How can I do that?
My Code
to setup-patches
import-drawing "01.png"
import-pcolors "01.png"
ask patches [
setup-house
;setup-resource
]
end
to setup-house
create-turtles [setxy where pcolor = grey]
set house? where pcolor = grey
end
The image is at https://i.imgur.com/dmODyUW.png.
I can provide more details on request.
Okay, a patch is actually a NetLogo term for one of the grid cells in the World. Your image suggests that the grey areas will cover multiple NetLogo patches each. The following code creates some random grey multi-patch areas and a house at one of the patches in one of the grey areas.
breed [houses house]
to testme
clear-all
setup-patches
setup-houses
end
to setup-patches
ask n-of 3 patches
[ set pcolor gray
ask neighbors
[ set pcolor gray
ask neighbors
[ set pcolor gray
]
]
]
end
to setup-houses
ask one-of patches with [pcolor = gray]
[ sprout-houses 1
[ set color red
]
]
end
Your question is too vague to properly answer, but hopefully this will get you on the right track. I suggest you redo the NetLogo tutorials and look at some of the models in the library included in the software to find pieces of code that do tasks you are going to need.

How would I "target" patches within a certain area?

I have set up agents and nodes to represent people and stores and it is my intention that the agents will "target" the store in their "awareness" space with the highest value ("vulnerability"). I've largely coded what I have so far through trial and error however setting the turtle's target to the patch with the highest value within a 10 unit radius is a hurdle I can't get over. Currently they target the patch with the highest value regardless of its position in the world. Could somebody suggest what I might consider to achieve this please? I have pasted what I have written so far for reference.
Thanks.
breed [shoplifters a-shoplifter]
patches-own [vulnerability]
shoplifters-own [target
awareness]
to setup
clear-all
setup-patches
setup-turtles
reset-ticks
end
to setup-patches
setup-stores
end
to setup-stores
ask n-of num-stores patches [ set pcolor lime ] ;; create 'num-stores' randomly
ask patches [
if pcolor = lime
[ set vulnerability random 100
]
]
end
to setup-turtles
setup-shoplifters
setup-target
end
to setup-shoplifters
create-shoplifters num-shoplifters [ ;; create 'num-turtles' shoplifters randomly
set xcor random-xcor
set ycor random-ycor
set shape "person"
set color red
]
end
to setup-awareness
ask turtles [
set awareness
patches in-radius 10
]
end
to setup-target
ask turtles [
set target
max-one-of patches [vulnerability]
]
end
You are on the right track using max-one-of. At the moment, however, you are sending patches as the space to search through to look for the one with maximum vulnerability value, when you really want patches in-radius 10. So you could simply do this:
to setup-target
ask turtles [
set target max-one-of patches in-radius 10 [vulnerability]
]
end
However, this is going to be inefficient because NetLogo will have to first work out which are the patches within the radius. You have already asked the turtles to work this out and assign it to their variable 'awareness'. What you really want to do is therefore:
to setup-target
ask shoplifters [
set target max-one-of patches awareness [vulnerability]
]
end
Note that I also changed ask turtles to ask shoplifters. It is only shoplifters who have the attribute 'target' so you should only be asking them to calculate it. Same thing goes for 'awareness'. At the moment you don't have any other breeds so it's not causing an error, but it is good practice to use the breed, otherwise there is no point in creating it.

NetLogo - change color of patch when agent is on top

I am trying to make my turtle change the color of a patch when it comes into contact with it and have tried the following code:
to deesculateviolence
ask turtles [
if pcolor = red [set pcolor blue]
]
end
The code does not come up with any errors but when I play the model, the color of the patch does not change. I have tried similar codes from different models and still cannot get the patch to change color. If anyone knows where I am going wrong I'd really appreciate your help.
I think your code does the right thing :
to setup
clear-all
create-turtles 5 [
move-to patch random 20 random 20
]
ask n-of 25 patches [set pcolor red]
reset-ticks
end
to go
ask turtles [
rt random 10
fd 1
if pcolor = red [set pcolor blue]
]
tick
end
you can see the effect in following example better