使用.shp文件中的开始和结束节点创建和命名链接

时间:2017-06-30 02:27:52

标签: netlogo

.shp文件的属性表格式如下:

street_name  start_node  end_node

street_1     A           B
street_1     B           C
street_2     B           D

如何使用开始和结束节点创建链接,然后为每个链接分配与其开始和结束节点关联的街道名称。例如,与起始节点A和结束节点B的链接应该得到名称" street_1"并且具有起始节点B和结束节点D的街道应该得到名称" street_2"。

我使用foreach gis:feature-list-of来链接数据集的节点,但这样我就无法根据其开始和结束节点命名链接,因为某些节点在街道段之间共享。

非常感谢。

修改

我感兴趣的属性表的列是name1,startNode和endNode。我已经使用下面的代码连接了节点,现在我有一个完全连接的道路网络。我不确定如何集成您的代码,以便节点之间的链接将获得与形成该链接的节点组合相关联的名称。

foreach gis:feature-list-of roads-dataset [ vector-feature ->
    foreach  gis:vertex-lists-of vector-feature [ vertex ->
      let previous-turtle nobody
      foreach vertex [point ->
        let location gis:location-of point
        if not empty? location
        [
          let x item 0 location
          let y item 1 location
          let current-node one-of (turtles-on patch x y) with [ xcor = x and ycor = y ]
          if current-node = nobody [
            create-nodes 1 [
              setxy x y
              set size 0.2
              set shape "circle"
              set color black
              set hidden? true
              set name gis:property-value vector-feature "name1"
              set current-node self
            ]
          ]
          ask current-node [
            if is-turtle? previous-turtle [
              create-link-with previous-turtle
            ]
            set previous-turtle self
          ]
        ]
      ]
    ]
  ]

1 个答案:

答案 0 :(得分:1)

您是说您的节点现在已在模型中正确命名?如果是这种情况,这里是一个可能适合您的方法的简化版本。我会注意到这不是一个非常有效的方法,因为它循环你的链接和你的属性表,所以如果你有很多链接,它将需要一段时间。首先,由于我没有你的shapefile,我已经制作了你的链接示例版本:

extensions [csv]

globals [ whole-file ]
turtles-own [ node ]
links-own [ name ]

to setup
  ca
  reset-ticks
  let names [ "A" "B" "C" "D" ]
  let n 0
  crt 4 [
   setxy random 30 - 15 random 30 - 15
   set node item n names
   set n n + 1
  ] 

  ask turtles with [ node = "A" ] [
    create-links-to turtles with [node = "B" ]
  ]
  ask turtles with [ node = "B" ] [
    create-links-to turtles with [ node = "C" or node = "D" ]
  ]
end

这只是构建了四只乌龟,链接如示例shapefile属性表所示。我正在使用一个名为“node_example.csv”的文件,如下所示:

  street_name start_node end_node
1    street_1          A        B
2    street_1          B        C
3    street_2          B        D

有四列,第一列是观察号。

基本上,方法是遍历列表并从end1end2拉出节点的名称,反之亦然(因为both-ends会随机抽取它们()),并将它们与表格中的每个start_nodeend_node组合进行比较。如果匹配,请将该行的street_name分配给匹配的链接:

to link-name 

  set whole-file csv:from-file "node_example.csv"

  foreach sort links [
    [ i ] ->
    show i
    let way-1 list ( [node] of [end1] of i ) ( [node] of [end2] of i )
    let way-2 list ( [node] of [end2] of i ) ( [node] of [end1] of i )
    foreach whole-file [
      [j] ->
      if sublist j 2 4 = way-1 or sublist j 2 4 = way-2 [
        ask i [
          set name item 1 j
        ]
      ]
    ]
  ]

  ask links [
    print name
    print (word [node] of end1 [node] of end2 )
  ]

end

显然,这取决于你的模型中命名的节点(在这个例子中,使用的变量是node) - 如果不是这样的话,这将不起作用。

修改1

好的,我使用你的shapefile玩了一下。这还不完美,我暂时不能继续工作了,但也许它会让你开始。使用此设置:

extensions [gis]

breed [ nodes node ]
globals [ roads-dataset ]
turtles-own [ name line-start line-end]
links-own [ lname ]

我的想法是将开始和结束节点名称分配给沿线要素的每个点,以便链接可以检查要素列表。评论中有更具体的注释,但我基本上修改了你的gis-feature-node代码来做到这一点。稍微玩一下(需要一段时间才能运行),你会看到我还没有想到的差距 - 也许你可以取得进展。

to gis-feature-node
  set roads-dataset gis:load-dataset  "road_links.shp"
  foreach gis:feature-list-of roads-dataset [ vector-feature ->

    ; First, grab the names of the starting and ending node for the current 
    ; vector feature in order to assign common names to all nodes within
    ; the feature

    let first-vertex gis:property-value vector-feature "startNode"
    let last-vertex gis:property-value vector-feature "endNode"

    foreach  gis:vertex-lists-of vector-feature [ vertex ->
      let previous-turtle nobody

      foreach vertex [ point ->
        let location gis:location-of point
        if not empty? location
        [
          let x item 0 location
          let y item 1 location
          let current-node one-of (turtles-on patch x y) with [ xcor = x and ycor = y ]
          if current-node = nobody [
            create-nodes 1 [
              setxy x y
              set size 0.05
              set shape "circle"
              set color white
              set hidden? false
              set name gis:property-value vector-feature "name1"

              ; Here you assign the first-vertex and last-vertex of the entire line
              ; to each node
              set line-start first-vertex 
              set line-end last-vertex
              set current-node self 
            ]
          ]
          ask current-node [
            if is-turtle? previous-turtle [
              create-link-with previous-turtle
            ]
            set previous-turtle self
          ]
        ]
      ]
    ]
  ]

  ask links [ 
    ;; Here is a major slowdown- reiterate through the entire roads-dataset
    ;  and, if the names in "startNode" and "endNode" match, assign the
    ;  value from "name1" to the link currently being created.
    let way-1 list [line-start] of end1 [line-end] of end2 
    let way-2 list [line-end] of end1 [line-start] of end2  
    foreach gis:feature-list-of roads-dataset [ vector-feature-sub ->
      let vector-start gis:property-value vector-feature-sub "startNode"
      let vector-end gis:property-value vector-feature-sub "endNode"
      let start-end list vector-start vector-end

      if way-1 = start-end or way-2 = start-end [
        set lname gis:property-value vector-feature-sub "name1"
      ]
    ]
  ] 

  ask links with [ lname = "Hamilton Place" ] [
    set color red 
    set thickness 0.2
  ]
  ask links with [ lname = "Whitcomb Street" ] [
    set color yellow
    set thickness 0.2
  ]

end

编辑2

下面的代码经过测试并且有效 - 问题已排序。

    ask links [
    set is-road? true
    ;; Here is a major slowdown- reiterate through the entire roads-dataset
    ;  and, if the names in "startNode" and "endNode" match, assign the
    ;  value from "name1" to the link currently being created.
    let way-1 list [line-start] of end1 [line-end] of end2
    let way-2 list [line-end] of end1 [line-start] of end2
    let way-3 list [ line-start ] of end1 [ line-end ] of end1
    let way-4 list [ line-start ] of end2 [ line-end ] of end2
    foreach gis:feature-list-of roads-dataset [ vector-feature-sub ->
      let vector-start gis:property-value vector-feature-sub "startNode"
      let vector-end gis:property-value vector-feature-sub "endNode"
      let start-end list vector-start vector-end
      let end-start list vector-end vector-start

      if way-1 = start-end or way-2 = start-end or way-3 = start-end or way-4 = start-end [
        set lname gis:property-value vector-feature-sub "name1"
      ]
    ]
  ]