def createMATTrip(trip, locations): time = str(trip.starttime.time()) startnode = planparser.findClosestNode(trip.pickupcoord[0], trip.pickupcoord[1], locations) endnode = planparser.findClosestNode(trip.dropoffcoord[0], trip.dropoffcoord[1], locations) return adjustedtrip(startnode, endnode, time)
def turnCSVIntoPlans(filename, roads): plans = ET.Element("population") rawdata = planparser.loadMatData('bin/zhangNYdata.mat') locations = rawdata['NodesLocation'] with open(filename, 'r') as f: reader = csv.reader(f) for i, trip in enumerate(reader): dropoffx, dropoffy = float(trip[7])*1000000, float(trip[8])*1000000 pickupx, pickupy = float(trip[5])*1000000, float(trip[6])*1000000 closestpickupnode = planparser.findClosestNode(pickupx, pickupy, locations) listoflinks = roads.get(str(closestpickupnode)) pickuplink = str(random.choice(listoflinks)) closestdropoffnode = planparser.findClosestNode(dropoffx, dropoffy, locations) listoflinks = roads.get(str(closestdropoffnode)) dropofflink = random.choice(listoflinks) if (dropofflink != pickuplink): person = ET.SubElement(plans, "person") person.set('id', str(i+1)) plan = ET.SubElement(person, "plan") plan.set('selected', 'yes') hour, minute, second = int(trip[2]), int(trip[3]), int(trip[4]) time = datetime.datetime(100,1,1,hour,minute,second) act = ET.SubElement(plan, 'act') act.set('type', 'dummy') act.set('end_time', str(time.time())) act.set('link', pickuplink) leg = ET.SubElement(plan, 'leg') leg.set('mode', 'taxi') leg.set('dep_time', str(time.time())) leg.set('trav_time', '00:01:00') delta = datetime.timedelta(0,TRAV_TIME_SECONDS) leg.set('arr_time', str((time + delta).time())) route = ET.SubElement(leg, 'route') act2 = ET.SubElement(plan, 'act') act2.set('type', 'dummy') act2.set('link', dropofflink) print(i) print("Cleaning XML...") parsed = pathwriter.cleanXML(plans) parsed = parsed[0:22] + '\n<!DOCTYPE population SYSTEM "http://www.matsim.org/files/dtd/population_v5.dtd">' + \ parsed[22:] print("Writing XML...") outfilename = 'res/amod1820plans.xml' with open(outfilename, 'w') as f: f.write(parsed)
def turnCSVIntoPlans(filename, roadsIn, roadsOut, roadMap): plans = ET.Element("population") rawdata = planparser.loadMatData('bin/zhangNYDataOSM.mat') locations = rawdata['NodesLocation'] with open(filename, 'r') as f: reader = csv.reader(f) possiblepickups = [locations[int(node)-1] for node in list(roadsOut.keys())] possiblepickupsidx = [node for node in list(roadsOut.keys())] possibledropoffs = [locations[int(node)-1] for node in list(roadsIn.keys())] possibledropoffsidx = [node for node in list(roadsIn.keys())] possiblenodes = [locations[int(node)-1] for node in list(set(roadsOut.keys()) & set(roadsIn.keys()))] possiblenodesidx= [node for node in list(set(roadsOut.keys()) & set(roadsIn.keys()))] for i, trip in enumerate(reader): # if i > count: # break hour, minute, second = int(trip[2]), int(trip[3]), int(trip[4]) if (START_HOUR <= hour < END_HOUR): dropoffx, dropoffy = float(trip[7])*1000, float(trip[8])*1000 pickupx, pickupy = float(trip[5])*1000, float(trip[6])*1000 closestpickupnode = planparser.findClosestNode(pickupx, pickupy, possiblepickups) #closestpickupnode = planparser.findClosestNode(pickupx, pickupy, possiblenodes) #print("Pickup distance (short):",planparser.findDistance(pickupx,pickupy,possiblenodes[int(closestpickupnode-1)])) #Map the node back to the original list of nodes (not the list of pickupnodes) #I do not really understand why we need the -1...Federico closestpickupnode = possiblepickupsidx[closestpickupnode-1] #closestpickupnode = possiblenodesidx[closestpickupnode-1] #print("Pickup distance (long) :",planparser.findDistance(pickupx,pickupy,locations[int(closestpickupnode)-1])) listoflinks = roadsOut.get(str(closestpickupnode)) # Now, what nodes do these links lead to? #Let us only keep the links such that the end node (exit) has neighbors #listoflinks = [link for link in listoflinks if len([roadsOut.get(roadMap[link][1])])] #listoflinks = [link for link in listoflinks if roadMap[link][1] in possiblenodesidx] pickuplink = str(random.choice(listoflinks)) closestdropoffnode = planparser.findClosestNode(dropoffx, dropoffy, possibledropoffs) #closestdropoffnode = planparser.findClosestNode(dropoffx, dropoffy, possiblenodes) #print("Dropoff distance (short):",planparser.findDistance(dropoffx,dropoffy,possiblenodes[int(closestdropoffnode-1)])) closestdropoffnode = possibledropoffsidx[closestdropoffnode-1] #closestdropoffnode = possiblenodesidx[closestdropoffnode-1] #print("Dropoff distance (long) :",planparser.findDistance(dropoffx,dropoffy,locations[int(closestdropoffnode)-1])) listoflinks = roadsIn.get(str(closestdropoffnode)) #listoflinks = [link for link in listoflinks if roadMap[link][1] in possiblenodesidx] dropofflink = random.choice(listoflinks) if (dropofflink != pickuplink): person = ET.SubElement(plans, "person") person.set('id', str(i+1)) plan = ET.SubElement(person, "plan") plan.set('selected', 'yes') time = datetime.datetime(100,1,1,hour-START_HOUR,minute,second) act = ET.SubElement(plan, 'act') act.set('type', 'dummy') act.set('end_time', str(time.time())) act.set('link', pickuplink) leg = ET.SubElement(plan, 'leg') leg.set('mode', 'taxi') leg.set('dep_time', str(time.time())) leg.set('trav_time', '00:01:00') delta = datetime.timedelta(0,TRAV_TIME_SECONDS) leg.set('arr_time', str((time + delta).time())) #route = ET.SubElement(leg, 'route') act2 = ET.SubElement(plan, 'act') act2.set('type', 'dummy') act2.set('link', dropofflink) print(i) print("Cleaning XML...") parsed = pathwriter.cleanXML(plans) parsed = parsed[0:22] + '\n<!DOCTYPE population SYSTEM "http://www.matsim.org/files/dtd/population_v5.dtd">' + \ parsed[22:] print("Writing XML...") outfilename = 'res/march05plansOSM.xml' with open(outfilename, 'w') as f: f.write(parsed)