0

I meant to ask it, but meanwhile I found the answer. So, as a service to the community, here's a simple script doing it.

import traci, sumolib
from sumolib import checkBinary
from shapely.geometry import Polygon, box
polygon = Polygon([(0, 10000), (10000, 10000), (10000, 0), (0,0)])
traci.start([checkBinary('sumo'), '-c', 'myLuST.sumocfg', "--start", "--quit-on-end", '-W', '-V', 'false', '--no-step-log', 'true'])
net = sumolib.net.readNet(r'../../LuSTScenario/scenario/lust.net.xml')  # net file

totalLength = 0 # Will hold the total length of lanes within the polygon

for edge in traci.edge.getIDList():
  # if edge ID begins with ':' then its a junction according to SUMO docs.
  if edge[0] == ":":
    continue # discard junctions

  curEdge = net.getEdge(edge)
  # get bounding box of the edge
  curEdgeBBCoords = curEdge.getBoundingBox()
  curEdgeBBox = box(*curEdgeBBCoords) # create the bounding box geometrically

  if polygon.contains(curEdgeBBox): # The given polygon contains that edge, so add the edge's length, multiplied by the # of lanes
    totalLength += curEdge.getLength() * len(curEdge.getLanes())

  # If polygon intersects with this edge then, as a rough estimation of the relevant length to add, divide the intersecting area by the total edge area
  
  elif (polygon.intersects(curEdgeBBox)):
    totalLength += curEdge.getLength() * len(curEdge.getLanes()) *  (polygon.intersection(curEdgeBBox).area / curEdgeBBox.area)
traci.close()
print ('total length of lanes in the given rectangle is {}' .format (totalLength))
Itamar cohen
  • 59
  • 1
  • 5
  • This will probably only work well for straight edges, and even then the area of the bounding box intersection is not the best proxy for the length of the edge inside the rectangle. You could simply intersect the shape (centerline) of the edge with the rectangle. Furthermore you forgot to multiply the result with the lane count in the second case. – Michael Nov 21 '21 at 17:54
  • And you don't need traci here, you can simply ask the net directly for all edges `net.getEdges()` – Michael Nov 21 '21 at 17:56
  • Michael, thanks for all your comments. – Itamar cohen Nov 22 '21 at 07:56

0 Answers0