/3A/res/sumo/tools/projects/TaxiFCD_Krieg/src/fcdToRoutes/GenerateTaxiRoutes.py

https://bitbucket.org/sieben/ensiie · Python · 205 lines · 173 code · 14 blank · 18 comment · 18 complexity · 65af36c6f53c8881ffe735d3ae27462d MD5 · raw file

  1. # -*- coding: Latin-1 -*-
  2. """
  3. @file GenerateTaxiRoutes.py
  4. @author Sascha.Krieg@dlr.de
  5. @date 2008-04-07
  6. Creates an Route-File, which contains for each Taxi the route, from an FCD-File.
  7. Copyright (C) 2008-2011 DLR (http://www.dlr.de/) and contributors
  8. All rights reserved
  9. """
  10. from util.CalcTime import getTimeInSecs
  11. from util.CalcTime import getNiceTimeLabel
  12. import util.Path as path
  13. import util.Reader as reader
  14. from cPickle import load
  15. #global vars
  16. taxis=[]
  17. routes=[]
  18. vlsEdges=[]
  19. taxiIdDict={} #contains for each Taxi the actual "TaxiId" based on the number of single routes which are created for them
  20. fcdDict={}
  21. vehIdListKeys=[]
  22. vehIdListValues=[]
  23. def getTaxiId(taxi):
  24. return "%s_%s" %(taxi,taxiIdDict.setdefault(taxi,0))
  25. def getSimTaxiId(taxi):
  26. global vehIdListKeys, vehIdListValues
  27. if len(vehIdListKeys)<1:
  28. vehIdList=load(open(path.rawFcdVehIdList,'r'))
  29. vehIdListKeys=vehIdList.keys()
  30. vehIdListValues=vehIdList.values()
  31. return "%s" %( vehIdListKeys[vehIdListValues.index(int(taxi))])
  32. def readFCD():
  33. """Reads the FCD and creates a list of Taxis and for each a list of routes"""
  34. vlsEdges=reader.readVLS_Edges()
  35. inputFile=open(path.fcd,'r')
  36. for line in inputFile:
  37. words= line.split("\t")
  38. #add route
  39. taxiId=getTaxiId(words[4])
  40. actTime=getTimeInSecs(words[0])
  41. if taxiId in taxis:
  42. prevTime=routes[taxis.index(taxiId)][-1][0]
  43. if words[1] in vlsEdges and (actTime-prevTime)<180: #check if time lies not to far away from each other
  44. routes[taxis.index(taxiId)].append((actTime, words[1]))
  45. elif words[1] in vlsEdges: #if time diff >3min add a new taxiId and start a new route
  46. taxiIdDict[words[4]]+=1 #create new taxiId
  47. taxis.append(getTaxiId(words[4])) #append new created id
  48. routes.append([(actTime,words[1])]) #append new list (list will be filled with edges)
  49. else:
  50. taxiIdDict[words[4]]+=1
  51. elif words[1] in vlsEdges: #if the edge is in the VLS-Area a new route is created
  52. taxis.append(taxiId)
  53. # departTime
  54. routes.append([(actTime,words[1])])
  55. inputFile.close()
  56. print len(taxis)
  57. def readFCDOLD():
  58. """Reads the FCD and creates a list of Taxis and for each a list of routes"""
  59. vlsEdges=reader.readVLS_Edges()
  60. inputFile=open(path.fcd,'r')
  61. for line in inputFile:
  62. words= line.split("\t")
  63. #add route
  64. taxiId=getTaxiId(words[4])
  65. if taxiId in taxis:
  66. if words[1] in vlsEdges:
  67. routes[taxis.index(taxiId)].append(words[1])
  68. else:
  69. taxiIdDict[words[4]]+=1
  70. elif words[1] in vlsEdges: #if the edge is in the VLS-Area a new route is created
  71. taxis.append(taxiId)
  72. # departTime
  73. routes.append([getTimeInSecs(words[0]),words[1]])
  74. inputFile.close()
  75. print len(taxis)
  76. def readFCDCompleteOLD(fcdPath):
  77. """Reads the FCD-File and creates a list of Id's with a belonging List of Data tuples."""
  78. #reset all
  79. global taxis, routes, vlsEdges, taxiIdDict, fcdDict
  80. taxis=[]
  81. routes=[]
  82. vlsEdges=[]
  83. taxiIdDict={}
  84. fcdDict={}
  85. vlsEdges=reader.readVLS_Edges()
  86. inputFile=open(fcdPath,'r')
  87. for line in inputFile:
  88. words= line.split("\t")
  89. #add route
  90. taxiId=getTaxiId(words[4])
  91. if taxiId in taxis:
  92. if words[1] in vlsEdges:
  93. #routes[taxis.index(taxiId)].append(words[1])
  94. fcdDict[taxiId].append((getTimeInSecs(words[0]),words[1],words[2]))
  95. else:
  96. taxiIdDict[words[4]]+=1
  97. elif words[1] in vlsEdges: #if the edge is in the VLS-Area a new route is created
  98. taxis.append(taxiId)
  99. # departTime
  100. #routes.append([(int)(mktime(strptime(words[0],format))-simDate),words[1]])
  101. fcdDict[taxiId]=[(getTimeInSecs(words[0]),words[1],words[2])]
  102. inputFile.close()
  103. return fcdDict
  104. def readFCDComplete(fcdPath):
  105. """Reads the FCD and creates a list of Taxis and for each a list of routes"""
  106. #reset all
  107. global taxis, routes, vlsEdges, taxiIdDict, fcdDict
  108. taxis=[]
  109. routes=[]
  110. vlsEdges=[]
  111. taxiIdDict={}
  112. fcdDict={}
  113. vlsEdges=reader.readVLS_Edges()
  114. inputFile=open(path.fcd,'r')
  115. for line in inputFile:
  116. words= line.split("\t")
  117. #add route
  118. taxiId=getTaxiId(words[4])
  119. actTime=getTimeInSecs(words[0])
  120. if taxiId in taxis:
  121. #prevTime=routes[taxis.index(taxiId)][-1][0]
  122. prevTime=fcdDict[taxiId][-1][0]
  123. if words[1] in vlsEdges and (actTime-prevTime)<180: #check if time lies not to far away from each other
  124. #routes[taxis.index(taxiId)].append((actTime, words[1]))
  125. fcdDict[taxiId].append((actTime,words[1],words[2]))
  126. elif words[1] in vlsEdges: #if time diff >3min add a new taxiId and start a new route
  127. taxiIdDict[words[4]]+=1 #create new taxiId
  128. taxis.append(getTaxiId(words[4])) #append new created id
  129. fcdDict[getTaxiId(words[4])]=[(actTime,words[1],words[2])] #append new list (list will be filled with edges)
  130. else:
  131. taxiIdDict[words[4]]+=1
  132. elif words[1] in vlsEdges: #if the edge is in the VLS-Area a new route is created
  133. taxis.append(taxiId)
  134. # departTime
  135. #routes.append([(actTime,words[1])])
  136. fcdDict[taxiId]=[(actTime,words[1],words[2])]
  137. inputFile.close()
  138. return fcdDict
  139. def readSimFCDComplete(fcdPath):
  140. """Reads the FCD-File and creates a list of Id's with a belonging List of Data tuples. Uses the given taxiIds."""
  141. #reset all
  142. global taxis, routes, vlsEdges, taxiIdDict, fcdDict
  143. taxis=[]
  144. routes=[]
  145. vlsEdges=[]
  146. taxiIdDict={}
  147. fcdDict={}
  148. inputFile=open(fcdPath,'r')
  149. for line in inputFile:
  150. words= line.split("\t")
  151. #add route
  152. taxiId=getSimTaxiId(words[4])
  153. if taxiId in taxis:
  154. fcdDict[taxiId].append((getTimeInSecs(getNiceTimeLabel(words[0])),words[1],words[2]))
  155. else:
  156. taxis.append(taxiId)
  157. fcdDict[taxiId]=[(getTimeInSecs(getNiceTimeLabel(words[0])),words[1],words[2])]
  158. inputFile.close()
  159. return fcdDict
  160. def writeRoutes():
  161. """Writes the collected values in a Sumo-Routes-File"""
  162. outputFile=open(path.taxiRoutes,'w')
  163. outputFile.write("<routes>\n")
  164. # known for like used in java
  165. for i in xrange(len(taxis)):
  166. if len(routes[i])>3:
  167. outputFile.write("\t<vehicle id=\""+taxis[i]+"\" type=\"taxi\" depart=\""+ str(routes[i][0][0])+"\" color=\"1,0,0\">\n")
  168. outputFile.write("\t\t<route>")
  169. for time,edge in routes[i]:
  170. outputFile.write(edge+" ")
  171. outputFile.seek(-1,1) #delete the space between the last edge and </route>
  172. outputFile.write("</route>\n")
  173. outputFile.write("\t</vehicle>\n")
  174. outputFile.write("</routes>")
  175. outputFile.close()