def writeCoordinates (inp_fileName,out_fileName): ''' A follow up routine for getCoordinates() method. Reads the co-ordinates from sncf file inp_fileName Writes the co-ordinates to an XML file out_fileName, that contains node information. ''' # Since sncfheaders.showNSID() returns the NSID in string literal, it would be wise to convert it to integer to able to operate on it. # An empty list that makes space for data to write to XML files xmlBuff=[] # Initial setup nsidData=[] # Container for rest of NSID's getNSID=sncfheaders.showNSID(inp_fileName) startNSID=int(getNSID) # Get n number of nodes numOfNodes=sncfheaders.getNumberOfNodes(inp_fileName) # Append data into nsidData # Has NSIDs for the rest of the nodes. for itr in range(0,numOfNodes): nsidData.append(itr+startNSID) # Store the data of coordinates in coordData coordData=getCoordinates(inp_fileName) ## Structure design of XML and replace variables with XML data. ## XML initial tags nL = sncfheaders.printNew() xmlBegin='''<?xml version="1.0" encoding="UTF-8"?> <nodes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://sumo.sf.net/xsd/nodes_file.xsd">'''+sncfheaders.printNew() xmlEnd='</nodes>' xmlLineBegin=' <node id=\"' xmlLineEnd='/>' xmlX='x=\"' xmlY='y=\"' ## For the fact that 'number' of nodes equal the number of NSIDs, generate the exact 'number' of intermediate XML tags. fileHandle=open(out_fileName,'w') fileHandle.write(xmlBegin) for itr in range(0,numOfNodes): xmlLine=xmlLineBegin+str (nsidData[itr])+'\"'+' '+xmlX+str(coordData[itr].split(',')[0])+'\"'+' '+xmlY+str(coordData[itr].split(',')[1].split('\n')[0])+'\"'+' '+xmlLineEnd+nL fileHandle.write(xmlLine) xmlBuff.append(xmlLine) ## Keep the line below for diagnostics! ## print xmlBuff fileHandle.write(xmlEnd) fileHandle.close() baseName=os.path.basename (out_fileName) extName=os.path.splitext(baseName)[1] if extName=='.xml': print''' [writeCoordinates says]: The file %s has been written to the location %s. ''' %(out_fileName,os.getcwd()) else: print''' [writeCoordinates says]: The file %s should have an extension *.nod.xml. Please rename it manually. ''' %(out_fileName)
def writeEdgeList(inp_fileName,out_fileName): ''' Function that writes edge list to an XML file. Requires an edge list to write to a file. ''' nL=sncfheaders.printNew() xmlLine='' getEdges=getEdgeList(inp_fileName) lenEdgeList=len(getEdges) # Prepare the initial setup of the XML file xmlBegin='''<?xml version="1.0" encoding="UTF-8"?> <edges xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://sumo.sf.net/xsd/edges_file.xsd">''' xmlEnd='</edges>' xmlEachEdgeFrom=' <edge from=\"' xmlEachEdgeTo='to=\"' xmlEachEdgeEnd='/>' xmlId='id=\"' fileHandler=open(out_fileName,'w') fileHandler.write(xmlBegin+nL) for itr in range(0,lenEdgeList): xmlLine=xmlEachEdgeFrom+str(getEdges[itr].split(',')[1])+'\"'+' '+xmlId+getEdges[itr].split(',')[0]+'\"'+' '+xmlEachEdgeTo+str(getEdges[itr].split(',')[2].split('\n')[0])+'\"'+xmlEachEdgeEnd+nL fileHandler.write(xmlLine) fileHandler.write(xmlEnd) fileHandler.close() baseName=os.path.basename(out_fileName) extName=os.path.splitext(baseName)[1] if extName=='.xml': print''' [writeEdgeList says]: The file %s has been written to location %s. ''' %(out_fileName,os.getcwd()) else: print''' [writeEdgeList says]: The file %s should have an extension *.edg.xml. Please rename it manually. '''%(out_fileName)
def makeCFG(networkFile,routeXMLFile,guiSettingFile,timeBeginVal,timeEndVal,cfgFileName): ''' Function that writes the configuration(*.sumo.cfg) file from input network (*.net.xml),route(*.rou.xml) and settings(*.settings.xml) files. ''' if os.path.exists(networkFile)==True and os.path.exists(routeXMLFile)==True and os.path.exists(guiSettingFile)==True: netBase=os.path.split(networkFile)[1] routeBase=os.path.split(routeXMLFile)[1] guiBase=os.path.split(guiSettingFile)[1] endTag='\"/>'+sncfheaders.printNew() cfgBegin='<configuration>'+sncfheaders.printNew() cfgEnd='</configuration>'+sncfheaders.printNew() inpBegin=' <input>'+sncfheaders.printNew() inpEnd=' </input>'+sncfheaders.printNew() netFile=' <net-file value=\"' routeFile=' <route-files value=\"' guiFile=' <gui-settings-file value=\"' timeBegin=' <time>'+sncfheaders.printNew() timeEnd=' </time>'+sncfheaders.printNew() beginValue=' <begin value=\"' endValue=' <end value=\"' cfgWrite=cfgBegin+inpBegin+netFile+str(netBase)+endTag+routeFile+str(routeBase)+endTag+guiFile+str(guiBase)+endTag+inpEnd+timeBegin+beginValue+str(timeBeginVal)+endTag+endValue+str(timeEndVal)+endTag+timeEnd+cfgEnd try: fileHandler=open(cfgFileName,'w') fileHandler.write(cfgWrite) fileHandler.close() print''' [makeCFG says]:Done! The configuration file %s has been written to the location %s. '''%(cfgFileName,os.getcwd()) except IOError,WindowsError: print''' [makeCFG says]: Error creating the file %s. Check if the user has the appropriate privileges to create the file. '''%(cfgFileName)
def makeViewSetting(xValue,yValue,zoomValue,delay,outputSettingFile): ''' Function that writes the settings file with viewport values to set the camera position and we used delay values to set the delay between each step of the simulation in ms. ''' viewSettingBegin='<viewsettings>'+sncfheaders.printNew() viewPortY=' <viewport y=\"' viewPortX='\" x=\"' viewPortZoom='\" zoom=\"' viewPortEnd='\" />'+sncfheaders.printNew() delayValue=' <delay value=\"' delayEnd='\" />'+sncfheaders.printNew() viewSettingEnd='</viewsettings>'+sncfheaders.printNew() try: fileHandler=open(outputSettingFile,'w') viewsettingsWrite=viewSettingBegin+viewPortY+str(yValue)+viewPortX+str(xValue)+viewPortZoom+str(zoomValue)+viewPortEnd+delayValue+str(delay)+delayEnd+viewSettingEnd fileHandler.write(viewsettingsWrite) fileHandler.close() print''' [makeViewSetting says]:Done! The file %s has been written to the location %s. '''%(outputSettingFile,os.getcwd()) except IOError,WindowsError: print''' [makeViewSetting says]:Error! Could not write the view settings file %s. Check if the user has administrative privileges and try again. '''%(outputSettingFile)
def writeRoute(sncfFileName,outputFileName): ''' Function that writes the *.rou.xml file from an existing sncf file sncfFileName. ''' if dependencychecker.depChecker(sncfFileName)==True: # Initial setup # Routes start and end routesStart='''<?xml version="1.0" encoding="UTF-8"?> <routes>''' routesEnd='</routes>' # vType start and end vTypeID='<vType id=\"' vTypeAccel='\" accel=\"' vTypeDecel='\" decel=\"' vTypeSigma='\" sigma=\"' vTypeTau='\" tau=\"' vTypeLength='\" length=\"' vTypeminGap='\" minGap=\"' vTypemaxSpeed='\" maxSpeed=\"' vTypespeedFactor='\" speedFactor=\"' vTypespeedDev='\" speedDev=\"' vTypecolor='\" color=\"' vTypevClass='\" vClass=\"' vTypeemissionClass='\" emissionClass=\"' vTypeguiShape='\" guiShape=\"' vTypewidth='\" width=\"' vTypeimgFile='\" imgFile=\"' vTypeimpatience='\" impatience=\"' vTypelaneChangeModel='\" laneChangeModel=\"' vTypeEnd='\" />' # route start and end routeID='<route id=\"' routeColor='\" color=\"' routeEdges='\" edges=\"' routeEnd='\" />' # vehicle start and end vehicleID='< vehicle id=\"' vehicleType='\" type=\"' vehicleRoute='\" route=\"' vehicleColor='\" color=\"' vehicleDepart='\" depart=\"' vehicledepartLane='\" departLane=\"' vehicledepartPos='\" departPos=\"' vehicledepartSpeed='\" departSpeed=\"' vehiclearrivalLane='\" arrivalLane=\"' vehiclearrivalPos='\" arrivalPos=\"' vehiclearrivalSpeed='\" arrivalSpeed=\"' vehicleline='\" line=\"' vehicleEnd='\" />' lenVtype=len(getattribs.getvTypeAttribs(sncfFileName)) lenRoute=len(getattribs.getrouteEntryAttribs(sncfFileName)) lenVehicle=len(getattribs.getvehicleAttribs(sncfFileName)) # Begin writing the file try: fileHandler=open(outputFileName,'w') fileHandler.write(routesStart+sncfheaders.printNew()) # Returns the list containing vType values. vType=getattribs.getvTypeDefaults(sncfFileName) for itr in range(0,lenVtype): vTypeWrite=vTypeID+vType[itr][0]+vTypeAccel+vType[itr][1]+vTypeDecel+vType[itr][2]+vTypeSigma+vType[itr][3]+vTypeTau+vType[itr][4]+vTypeLength+vType[itr][5]+vTypeminGap+vType[itr][6]+vTypemaxSpeed+vType[itr][7]+vTypespeedFactor+vType[itr][8]+vTypespeedDev+vType[itr][9]+vTypecolor+getattribs.getvTypeColor(sncfFileName)[itr]+vTypevClass+vType[itr][11]+vTypeemissionClass+vType[itr][12]+vTypeguiShape+vType[itr][13]+vTypewidth+vType[itr][14]+vTypeimgFile+vType[itr][15]+vTypeimpatience+vType[itr][16]+vTypelaneChangeModel+vType[itr][17]+vTypeEnd+sncfheaders.printNew() fileHandler.write(vTypeWrite) # Returns the list containing route entry values. routeEntry=getattribs.getrouteEntryAttribs(sncfFileName) for itr in range(0,lenRoute): routeWrite=routeID+routeEntry[itr][0]+routeColor+getattribs.getrouteColor(sncfFileName)[itr]+routeEdges+strutils.whatsInBetween('[',']',getattribs.getroute_edgeName(sncfFileName)[itr])+routeEnd+sncfheaders.printNew() fileHandler.write(routeWrite) # Returns the list containing vehicle values. vehicle=getattribs.getvehicleAttribs(sncfFileName) for itr in range(0,lenVehicle): vehicleWrite=vehicleID+vehicle[itr][0]+vehicleType+vehicle[itr][1]+vehicleRoute+vehicle[itr][2]+vehicleColor+getattribs.getvehicleColor(sncfFileName)[itr]+vehicleDepart+vehicle[itr][4]+vehicledepartLane+vehicle[itr][5]+vehicledepartPos+vehicle[itr][6]+vehicledepartSpeed+vehicle[itr][7]+vehiclearrivalLane+vehicle[itr][8]+vehiclearrivalPos+vehicle[itr][9]+vehiclearrivalSpeed+vehicle[itr][10]+vehicleline+vehicle[itr][11]+vehicleEnd+sncfheaders.printNew() fileHandler.write(vehicleWrite) fileHandler.write(routesEnd+sncfheaders.printNew()) fileHandler.close() print''' [writeRoute says]:The route file %s has been written to the location %s. '''%(outputFileName,os.getcwd()) except IOError,WindowsError: print''' [writeRoute says]:Error! Could not open the file %s for writing. Check the administrative rights or check if the location exists. '''%(outputFileName)