Geographic coordinates on createWorld

50 views
Skip to first unread message

Jaewoon Jeong

unread,
Oct 13, 2022, 4:21:24 PM10/13/22
to NetLogoR users
Hello,

I am using raster package to make a map of pixels, and the pixel size is 0.01 by geographic coordinate system. I want to run agent-based modelling by using NetLogoR, in which I can use 'createWorld' to make the plot of map. However, the resolution of 'createWorld' is 1, which is different my pixel size of 0.01. Thus, I wonder if there is any way I can use 'createWorld' with the resolution of 0.01, not 1. Thank you 

NetLogoR users

unread,
Oct 14, 2022, 12:08:38 PM10/14/22
to NetLogoR users
Hello,

Unfortunately there is no possibility to change the resolution of a worldMatrix. We took this from NetLogo which always has a resolution of 1x1 for the patches in the worlds. 
The way to work with this is to think of the 1x1 worldMatrix resolution as your resolution "unit" which represents your real resolution of 0.01x0.01. However, you need to keep that in mind when using other NetLogoR functions. For example, if your individuals actually walk a distance of 2 per day (with your raster units), you'll need to convert it in the worldMatrix unit and make them walk a distance of 200 NetLogoR units per day (I hope my explanations are clear ;)
Then, at the end, if you're interested in raster output values, you can just transfer the values of your worldMatrix back to your raster of resolution 0.01.

Don't hesitate to write me again if it isn't clear, I could show you a quick example !

Thanks!

Sarah

Jaewoon Jeong

unread,
Oct 14, 2022, 2:24:59 PM10/14/22
to NetLogoR users
Hello Sarah,

Thank you for your response. It is good for me to spend more time to do something impossible. It would be very helpful if you can show me an example. Thank you

Jaewoon

NetLogoR users

unread,
Oct 18, 2022, 11:54:54 AM10/18/22
to NetLogoR users
Here is a very simple example. If you want further details, don't hesitate to ask me again

library(raster)
library(NetLogoR)

# Raster of 0.01
r <- raster(xmn = 0, xmx = 1.5, ymn = 0, ymx = 0.5, resolution = c(0.01, 0.01))
r[] <- runif(n = length(r))
plot(r)

# WorldMatrix of resolution 1 with the same number of rows and columns
nrow(r)
ncol(r)
# Either
myWorld <- createWorld(1, 150, 1, 50, data = r[])
# or
myWorld2 <- createWorld(0, 149, 0, 49, data = r[])
# You can start your coordinates wherever you want but you need to keep the same number of rows and colums
# Or you can directly use this function from NetLogoR
myWorld3 <- raster2world(r)

# Build your model with myWorld (or myWorld2 or myWorld3, it works the same)
# But keep in mind that your previous distance of 1.5 is now equal of 150 (100 times longer
# because of your resolution of 0.01 which is 100 times smaller than the resolution of 1 of the worldMatrix)
# So for example if you want to move your individuals of a real distance of 0.03, they need to move a distance of 3 cells
myTurtles <- createTurtles(n = 10, coords = randomXYcor(myWorld, n = 10))
plot(myWorld)
points(myTurtles, pch = 19)
myTurtles <- fd(turtles = myTurtles, dist = 3, torus = TRUE, world = myWorld)
points(myTurtles, pch = 19, col = "red")
# Rotate the turtles (not impacted by the resolution) and then move them of a real distance of 1
# so a distance of 100 in the worldMatrix
myTurtles <- right(turtles = myTurtles, angle = runif(n = 10, min = 0, max = 360))
myTurtles <- fd(turtles = myTurtles, dist = 100, torus = TRUE, world = myWorld)
points(myTurtles, pch = 19, col = "green")

# For example, add 10 to patches where there are turtles
myWorld <- NLset(world = myWorld, agents = patchHere(world = myWorld, turtles = myTurtles),
                 val = of(world = myWorld, agents = patchHere(world = myWorld, turtles = myTurtles)) + 10)
plot(myWorld)

# Then, at the end of your whole simulation (more complicated than this short example) 
# if you want your output map to be the same resolution as before you can switch back to your raster layer
myNewRaster <- r
myNewRaster[] <- myWorld[]


Jaewoon Jeong

unread,
Oct 19, 2022, 8:34:46 AM10/19/22
to netl...@googlegroups.com
Hello Sarah,

I appreciate the example you provided. That is very helpful. However, I have another question. I am calculating the shortest distance between two points on the offshore ocean, where fish, agents, are moving. Before, I did that by using R package, gdistance, but I cannot use this method with NetLogoR, because gdistance requires TransitionLayer. I wonder if there is any way to calculate the shortest path between two points in NetLogoR, or to use TransitionLayer with NetLogoR. Thank you

Jaewoon

--
You received this message because you are subscribed to a topic in the Google Groups "NetLogoR users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/netlogor/KwrGb6aqO1c/unsubscribe.
To unsubscribe from this group and all its topics, send an email to netlogor+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/netlogor/bdd9c4d9-d4f0-4c64-808d-1e8943ac5a63n%40googlegroups.com.

NetLogoR users

unread,
Oct 19, 2022, 9:44:47 AM10/19/22
to NetLogoR users
Hi Jaewoon,

There is the function NLdist() from NetLogoR that calculates the straight distance(s) between one/some patch(es) or turtle(s) to other(s) patch(es) or turtle(s).
I don't know what is a TransitionLayer but NLdist() works the same as gDistance() from rgeos.
When sometimes you really need to use a function that cannot use a worldMatrix or agentMatrix, the thing that I do is I switch to a raster/sp/sf format, use the function which requires a raster/sp/sf format and then switch back to a worldMatrix/agentMatrix and then continues with the IBM.
For example, if I continue with the same previous example :
...
myNewRaster <- r
myNewRaster[] <- myWorld[]

# Now use the raster
myNewRaster <- functionThatCanOnlyUseRaster(myNewRaster)
# Switch back to a worldMatrix
myWorld <- NLset(world = myWorld, agents = patches(myWorld), val = myNewRaster[])
# Then continue the IBM
...

Let me know if you have other questions.

Sarah

Jaewoon Jeong

unread,
Oct 19, 2022, 1:18:06 PM10/19/22
to netl...@googlegroups.com
Hello Sarah,

Thank you for your email. I have one more question about the shortest path calculation. I attached the RasterLayer for my study. In the map, yellow parts are water and value of 1, and white parts are land and value of NA. A fish, an agent, swim from upper-right red point to lower-left red point through only yellow part (water), because the agent is fish. Could you let me know I can calculate the shortest seaway path for the distance and track? Thank you

Jaewoon 

myWorld=raster2world(r)
mySalmon=createTurtles(n=2,coords=matrix(c(11,24,250,110),nrow=2,byrow=T))
plot(myWorld); points(mySalmon,pch=19,col='red')


r.RData

NetLogoR users

unread,
Oct 20, 2022, 3:36:38 AM10/20/22
to NetLogoR users
Hi Jaewoon,

Unfortunately there is no function in NetLogoR to do this. To write a function from scratch to do this while using a worldMatrix/agentMatrix would be quite complicated I guess.
The easiest, as I mentioned in my previous message, is to switch to a raster to do this kind of computation with a function that uses a raster (or another format). I often do that in my model. For example, I built an IBM but I also had to use a spread function which can only use raster. I used both a worldMatrix (for all the IBM-related functions) and a raster (for the spread function) throughout my whole model and transfer values between the two formats each time is was needed.

In the case you'd like to write a function to do this short-path selection while using a worldMatrix, you can send us your code (via our GitHub or by email) and we could incorporate it in NetLogoR in a further version.

Thanks

Sarah

Jaewoon Jeong

unread,
Oct 28, 2022, 10:32:07 AM10/28/22
to netl...@googlegroups.com
Hello,

Can I ask one more question?
I want to move an agent to follow a fixed route deterministically. However, the functions that I have been using to move agents, such as 'fd' and 'right', are not appropriate for that kind of pre-determined movement. Could you recommend an appropriate method for my purpose? Thank you

Jaewoon 

NetLogoR users

unread,
Oct 30, 2022, 7:58:26 AM10/30/22
to NetLogoR users
Hi Jaewoon,

One way would be to have the patches of your route different than the other patches (e.g., value of 1 for the route and 0 for the other patches). Then, using the function neighbors() you can identify the patches surrounding your agent, and with of() you can check, among the neighboring patches, the ones that are part of the route (i.e., the previous one and the next one). The agentMatrix records the previous position so you can remove the previous patch part of the route, and with moveTo() you can move your agent on the selected patch (i.e., the next one part of the route). You would have to redo this for each step though.
I hope this help ! Let me know if you have other questions or if it isn't clear enough !

Sarah

Jaewoon Jeong

unread,
Oct 31, 2022, 3:39:22 PM10/31/22
to netl...@googlegroups.com
Hello Sarah,

Thank you for your reply. That helps a lot. 

I have another question. I would like to simulate a situation in which a fish swims from point A to point B. This time what I want is to track the specific timing (minute, hour, day, month, year) of the location of the fish in every pixel in the map. I wonder if NetLogoR has any function that helps track the timing of turtles in each pixel. Thank you

Jaewoon 

NetLogoR users

unread,
Nov 1, 2022, 5:16:40 AM11/1/22
to NetLogoR users
Hi Jaewoon,

No, NetLogoR doesn't have any function related to time. You have to keep track of the time by hand (e.g., if you use a for() loop to run your model).
You can also use other packages which can schedule your processes in your model and keep track of the time. You can look at the SpaDES package. There is an exemple in the NetLogoR package of an IBM built with NetLogoR and SpaDES: https://github.com/PredictiveEcology/NetLogoR/tree/master/inst/examples/Wolf-Sheep-Predation/WolfSheepPredation

However for simple models, a for() loop may be sufficient :)


Sarah

Jaewoon Jeong

unread,
Jan 2, 2023, 12:04:40 PM1/2/23
to netl...@googlegroups.com
Hello,

I am trying to use diffuse. I wonder if it is possible that some patches in a map do not receive the share of a portion of the value. I tried NA but even NA diffuse to neighbouring patches. 
Thank you 

Jaewoon 

NetLogoR users

unread,
Jan 18, 2023, 11:11:21 AM1/18/23
to NetLogoR users
Hi Jaewoon,
Unfortunately there is no way to exclude some patches in this function. One way is to keep the former version of the world and replace the new values resulting from the diffuse function with the former values for the selected patches.
Regards,

Sarah

Reply all
Reply to author
Forward
0 new messages