如何通过NetLogo GIS扩展基于Shapefile点生成指定数量的海龟
问题:从Shapefile点集直接生成指定数量的海龟
我有一个包含约50000个点的Shapefile,希望基于这些点生成一定数量的海龟,但并非为每个点都生成。需要生成的海龟数量由全局变量globals [num-of-turtles]定义,该变量可在界面中修改。
目前我仅能实现每个点生成一个海龟,再通过以下代码删除多余的海龟(已完成坐标系加载、数据集加载、设置世界范围等操作):
gis:create-turtles-from-points my-point-map turtles [ set shape "house" set size 0.30 ] ask n-of (count turtles - num-of-turtles) turtles [die]
但这种方法略显繁琐,请问有没有办法直接生成预设数量的海龟?
解决方案
当然可以直接生成指定数量的海龟,核心思路是先从Shapefile的点集中随机挑选出num-of-turtles个点,再基于这些选中的点创建海龟,无需先创建全部海龟再删除,效率会高很多(尤其适合你这种大点数的场景)。
具体代码如下:
; 先从点集中随机选取指定数量的点 let selected-points n-of num-of-turtles my-point-map ; 基于选中的点创建海龟 gis:create-turtles-from-points selected-points turtles [ set shape "house" set size 0.30 ]
需要注意:如果num-of-turtles的值大于点集的总数量(count my-point-map),NetLogo会报错,你可以根据需求加个判断逻辑,避免参数错误:
let target-num min (list num-of-turtles (count my-point-map)) let selected-points n-of target-num my-point-map gis:create-turtles-from-points selected-points turtles [ set shape "house" set size 0.30 ]
这段代码会自动取num-of-turtles和点集总数中较小的那个值来生成海龟。
内容的提问来源于stack exchange,提问作者Pol
相关产品推荐
相关产品推荐

