def getNodIDs(inp_fileName): ''' Function that returns the node id's of the co-ordinates as a list. ''' nsidData=[] getNSID=sncfheaders.showNSID(inp_fileName) startNSID=int(getNSID) numOfNodes=sncfheaders.getNumberOfNodes(inp_fileName) for itr in range(0,numOfNodes): nsidData.append(itr+startNSID) return nsidData
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)