"""
Usage: poi_alongRoads.py <NET> <EDGE_ID>[,<EDGE_ID>]* <DISTANCE>
Spatial distribute of POIs along given edges on a given network.
Edges are separated with comma and without spaces in between.
The distance between POIs may be any positive real number
So far POIs are situated in the middle on all edges without regard to the type of the edge (street, junction).
Edges may be given in arbitrary order, connected edges are found automatically
Therefore: crossing chains of connected edges are not allowed -> this needs two different runs of this script
Output is written in file 'pois.add.xml'
"""
from __future__ import absolute_import
from __future__ import print_function
import os
import sys
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import sumolib.net
if len(sys.argv) < 4:
print("Usage: " +
sys.argv[0] + " <NET> <EDGE_ID>[,<EDGE_ID>]* <DISTANCE>", file=sys.stderr)
sys.exit()
edgeList = sys.argv[2].split(',')
POIdist = float(sys.argv[3])
def findPred(connEdgesTmp, edgeList, success=0):
for edge in edgeList:
if connEdgesTmp[0][1] == edge[2]:
connEdgesTmp.insert(0, edge)
edgeList.remove(edge)
success = 1
return success
return success
def findSucc(connEdgesTmp, edgeList, success=0):
for edge in edgeList:
if connEdgesTmp[len(connEdgesTmp) - 1][2] == edge[1]:
connEdgesTmp.append(edge)
edgeList.remove(edge)
success = 1
return success
return success
def poiAppend(poi_edge, poi_nr, poi_x, poi_y, poi_buf):
poi_buf.append(' <poi id="%s-%s" type="default" color="1,0,0" layer="0" x="%.2f" y="%.2f"/>' %
(poi_edge, poi_nr, poi_x, poi_y))
print("Reading net ...")
net = sumolib.net.readNet(sys.argv[1])
nodeList = []
for edge in net._edges:
for elm in sys.argv[2].split(','):
if str(edge._id) == elm:
data = []
data.append(edge._id)
data.append(edge._from._id)
data.append(edge._to._id)
data.append(edge.getShape())
nodeList.append(data)
connEdges = []
connEdgesTmp = []
pred = 1
succ = 1
tmpList = nodeList[:]
for elm in nodeList:
if elm in tmpList:
while pred == 1:
if elm in tmpList:
tmpList.remove(elm)
if elm not in connEdgesTmp:
connEdgesTmp.append(elm)
pred = findPred(connEdgesTmp, tmpList)
while succ == 1:
succ = findSucc(connEdgesTmp, tmpList)
connEdges.append(connEdgesTmp)
connEdgesTmp = []
pred = 1
succ = 1
POIbuf = []
POIbuf.append("<?xml version=\"1.0\"?>\n")
POIbuf.append("<!--\n\nPOIs for edges:\n\t" +
str(sys.argv[2]) + "\n\non network:\n\t" + str(sys.argv[1]) + "\n-->\n")
POIbuf.append("<POIs>")
for elm in connEdges:
shapes = []
offset = 0
for edge in elm:
for p in edge[3]:
shapes.append([p[0], p[1], edge[0]])
for i in range(0, len(shapes) - 1):
x1 = shapes[i][0]
y1 = shapes[i][1]
x2 = shapes[i + 1][0]
y2 = shapes[i + 1][1]
dX = x2 - x1
dY = y2 - y1
eucDist = pow(pow(dX, 2) + pow(dY, 2), 0.5)
normAscX = 0 if dX == 0 else dX / eucDist
normAscY = 0 if dY == 0 else dY / eucDist
xCur = x1
yCur = y1
if i == 0:
POIid = 1
poiAppend(shapes[i][2], POIid, xCur, yCur, POIbuf)
POIid = POIid + 1
POIpos = POIdist
else:
if shapes[i][2] != shapes[i - 1][2]:
POIid = 1
POIpos = offset
if eucDist + offset > POIdist:
xCur = xCur + normAscX * (POIdist - offset)
yCur = yCur + normAscY * (POIdist - offset)
poiAppend(shapes[i][2], POIid, xCur, yCur, POIbuf)
POIid = POIid + 1
POIpos = POIpos + POIdist
offset = 0
while pow(pow((x2 - xCur), 2) + pow((y2 - yCur), 2), 0.5) > POIdist:
xCur = xCur + normAscX * POIdist
yCur = yCur + normAscY * POIdist
poiAppend(shapes[i][2], POIid, xCur, yCur, POIbuf)
POIid = POIid + 1
POIpos = POIpos + POIdist
offset = offset + pow(pow((x2 - xCur), 2) + pow((y2 - yCur), 2), 0.5)
POIbuf.append("</POIs>")
print("writing output")
outfile = open('pois.add.xml', 'w')
for POIs in POIbuf:
outfile.write(POIs + "\n")
outfile.close()
print("done")