NetLogo中如何将海龟路径列表导出为Shapefile?
问题原因
你直接把path(patch组成的普通列表)传给gis:store-dataset会报错,因为这个函数要求的是NetLogo GIS扩展定义的矢量数据集(比如点、线、面类型的数据集对象),而不是原生的patch列表。
解决方法
需要先把patch列表转换成GIS兼容的要素(点或线),再封装成数据集后导出。以下是两种常用方案:
方案1:导出路径为点Shapefile(每个patch对应一个点)
适合需要查看路径上每个停留点位的场景,每只海龟单独导出文件避免覆盖:
to export-path ; 给每只海龟的路径文件加编号,防止互相覆盖 let file (word "path_points_" + who + ".shp") if file-exists? file [file-delete file] ; 创建空的点数据集,定义属性字段(这里包含海龟ID和patch坐标) let point-dataset gis:make-vector-dataset "point" [["turtle-id" "integer"] ["patch-x" "integer"] ["patch-y" "integer"]] ; 遍历路径中的每个patch,转成GIS点并添加到数据集 foreach path [ p -> ; 转成GIS点(如果模型用了投影,替换成gis:location-of (pxcor p) (pycor p)) let point gis:make-point (list (pxcor p) (pycor p)) ; 给点添加属性:海龟ID、patch的x/y坐标 gis:add-feature point point-dataset (list who (pxcor p) (pycor p)) ] ; 导出数据集到Shapefile gis:store-dataset point-dataset file end
调用方式:ask turtles [export-path]
方案2:导出路径为线Shapefile(整个路径作为一条连续线)
更符合“轨迹”的直观展示,把所有patch的中心坐标连成一条线:
to export-path let file (word "path_line_" + who + ".shp") if file-exists? file [file-delete file] ; 收集路径中每个patch的坐标(用投影坐标的话,替换成gis:location-of (pxcor p) (pycor p)) let path-coords map [p -> list (pxcor p) (pycor p)] path ; 创建线要素 let path-line gis:make-line path-coords ; 创建线数据集,定义属性字段(比如海龟ID) let line-dataset gis:make-vector-dataset "line" [["turtle-id" "integer"]] gis:add-feature path-line line-dataset (list who) ; 导出数据集 gis:store-dataset line-dataset file end
调用方式:ask turtles [export-path]
额外提示
- 如果你想把所有海龟的路径导出到同一个Shapefile,不要在
ask turtles里调用,改用全局过程统一处理:to export-all-paths let file "all_turtle_paths.shp" if file-exists? file [file-delete file] let line-dataset gis:make-vector-dataset "line" [["turtle-id" "integer"]] ask turtles [ let path-coords map [p -> list (pxcor p) (pycor p)] path let path-line gis:make-line path-coords gis:add-feature path-line line-dataset (list who) ] gis:store-dataset line-dataset file end - 原代码中的
file-open file是多余的,gis:store-dataset会自动处理文件创建,不需要手动打开文件。
内容的提问来源于stack exchange,提问作者Henrique Kozlowski
相关产品推荐
相关产品推荐

