【问题标题】:Creating and naming links using start and end nodes from .shp file使用 .shp 文件中的开始和结束节点创建和命名链接
【发布时间】:2017-06-30 02:27:52
【问题描述】:

.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
          ]
        ]
      ]
    ]
  ]

【问题讨论】:

    标签: netlogo


    【解决方案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 ]
    

    我的想法是将开始和结束节点名称分配给沿线要素的每个点,以便链接可以检查要素列表。 cmets 中有更具体的注释,但我基本上已经修改了您的 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"
          ]
        ]
      ]
    

    【讨论】:

    • 我编辑了我的帖子,还上传了 .shp 和其他相关文件,这些文件可以从这个共享的link 访问
    • 好的,第二轮!看看是否有帮助
    • 你打赌!我确实发现它跳过了一些片段,而且我现在没有时间弄清楚原因——但绝对要提醒一下(至少对我而言)它在 ~6000 个中丢失了大约 1800 个左右的链接。也许通过一些调整它会完全工作......祝你好运!
    • 我去看看。我刚刚查了一下,确实有 2358 个与lname = 0 的链接。但谢谢你的开始。这是一种显示我想要的结果的方式。
    • 你打赌-如果你最终完全弄清楚了,我很想听听你是怎么做的。干杯!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-29
    • 2017-05-07
    相关资源
    最近更新 更多