www.pudn.com > Redfish_Venice_shapeFile_boatTraffic.zip > shapeToNetwork.nls
globals [mapScale mapMinX mapMinY]
;; nodes are canal intersections
breed [nodes node]
nodes-own [ID x y]
;; canals are between nodes. They are meta to polypoints
undirected-link-breed [canals canal]
canals-own [canalLength canalWidth myPolys directionNode]
;; these points make up the polylines of the canals
;; canals have a list of polypoints that define their shape
breed [polyPoints polyPoint]
polyPoints-own [x y mycanal]
undirected-link-breed [polyLinks polyLink]
breed [staziones stazion]
staziones-own [ID name x y]
to import-network
set-default-shape nodes "point"
import-nodes
import-canals
import-staziones
scale-nodes-to-world
check-poly-direction
end
;; import nodes as canal intersections
to import-nodes
file-open "nodi.txt"
;; skip the first header line
let dummy file-read-line
;; Read in all the data in the file
;; data is a triplet: ID x y
while [not file-at-end?] [
let items read-from-string (word "[" file-read-line "]")
if inbounds? (read-from-string item 1 items) (read-from-string item 2 items) [
create-nodes 1 [
set size .8
set color gray - 4
set color orange
set ID item 0 items
set x read-from-string item 1 items
set y read-from-string item 2 items
]
]
]
file-close
end
to import-canals
file-open "edges.txt"
let dummy file-read-line
let node1 ""
let node2 ""
let thiscanal ""
while [not file-at-end?] [
;; this reads a single line into a five item list
;; [ node1, node2, length, directionNode, [[point1x point1y] [point2x point2y] .. ] ]
let items read-from-string (word "[" file-read-line "]")
set node1 get-node item 0 items
set node2 get-node item 1 items
if node1 != node2 and node1 != nobody and node2 != nobody [ ;; data has some wierd edges with duplicate and null endpoints
ask node1 [
create-canal-with node2 [
set thiscanal self
set myPolys []
set canalLength read-from-string item 2 items
set canalWidth read-from-string item 3 items
if item 4 items != "" [ set directionNode get-node item 4 items set color yellow ]]
let pointList read-from-string item 5 items
foreach pointList [
hatch-polyPoints 1 [ht set x item 0 ? set y item 1 ? set size .01 set shape "point" set color gray - 4 set mycanal thiscanal set [myPolys] of thiscanal lput self [myPolys] of thiscanal]
]
]
]
]
if create-polylines? [
ask nodes [st]
ask polypoints [ht]
ask canals [
set thiscanal self
(foreach but-last myPolys but-first myPolys [
ask ?1 [create-polylink-with ?2 [if [directionNode] of thiscanal != 0 [set color yellow]]] ])
hide-link
]
]
file-close
end
to check-poly-direction
ask canals [
ask end1[ if (distance (first [MyPolys] of myself)) > (distance (last [myPolys] of myself)) [set [myPolys] of myself reverse [myPolys] of myself]]
]
end
to import-staziones
file-open "staziones.txt"
;; skip the first line
let dummy file-read-line
;; Read in all the data in the file
;; data on the line is in this order:
;; ID attribute1 attribute2
while [not file-at-end?] [
let items read-from-string (word "[" file-read-line "]")
create-staziones 1 [
set size .6
set shape "point"
set color green
set ID item 0 items
set name item 1 items
set x item 2 items
set y item 3 items
set label ID
]
]
file-close
end
to scale-nodes-to-world
ask nodes with [count my-canals = 0] [die]
let myPoints (turtle-set nodes polyPoints staziones)
set mapMinx min [x] of myPoints
set mapMiny min [y] of myPoints
let maxx max [x] of myPoints
let maxy max [y] of myPoints
let width maxx - mapMinx
let height maxy - mapMiny
ifelse width > height
[set mapScale (world-width - 1) / width]
[set mapScale (world-height - 1) / height]
ask myPoints [
set xcor (x - mapMinx) * mapScale
carefully [set ycor (y - mapMiny) * mapScale][show (y - mapMiny) * mapScale]
]
end
to-report get-node [anId]
report one-of nodes with [ID = anId]
end
to-report inbounds? [mX mY]
if not restrict-points-to-rect? [report true]
let minx west
let miny south
let maxx east
let maxy north
report mX > minX and mx < maxX and my > minY and my < maxy
end