Example #1
0
    def write_persontrip_xml(self, fd, id_trip, indent=2, method_routechoice=None):
        # currently no routes are exported, only origin and destination edges
        # if method_routechoice is None:
        #    method_routechoice = self.get_route_first
        xmltag_person = 'person'
        id_route = method_routechoice(id_trip)

        fd.write(xm.start(xmltag_person, indent))

        self.ids_sumo.write_xml(fd, id_trip)
        self.times_depart.write_xml(fd, id_trip)
        self.ids_vtype.write_xml(fd, id_trip)
        fd.write(xm.stop())

        fd.write(xm.start('walk', indent=indent+2))
        # print 'write walk',id_trip,self.positions_depart[id_trip],self.positions_arrival[id_trip]
        self.ids_edge_depart.write_xml(fd, id_trip)
        if self.positions_depart[id_trip] > 0:
            self.positions_depart.write_xml(fd, id_trip)

        self.ids_edge_arrival.write_xml(fd, id_trip)
        if self.positions_arrival[id_trip] > 0:
            self.positions_arrival.write_xml(fd, id_trip)

        fd.write(xm.stopit())  # ends walk
        fd.write(xm.end(xmltag_person, indent=indent))
Example #2
0
    def write_persontrip_xml(self, fd, id_trip, indent=2, method_routechoice=None):
        # currently no routes are exported, only origin and destination edges
        # if method_routechoice == None:
        #    method_routechoice = self.get_route_first
        xmltag_person = 'person'
        id_route = method_routechoice(id_trip)

        fd.write(xm.start(xmltag_person, indent))

        self.ids_sumo.write_xml(fd, id_trip)
        self.times_depart.write_xml(fd, id_trip)
        self.ids_vtype.write_xml(fd, id_trip)
        fd.write(xm.stop())

        fd.write(xm.start('walk', indent=indent + 2))
        # print 'write
        # walk',id_trip,self.positions_depart[id_trip],self.positions_arrival[id_trip]
        self.ids_edge_depart.write_xml(fd, id_trip)
        if self.positions_depart[id_trip] > 0:
            self.positions_depart.write_xml(fd, id_trip)

        self.ids_edge_arrival.write_xml(fd, id_trip)
        if self.positions_arrival[id_trip] > 0:
            self.positions_arrival.write_xml(fd, id_trip)

        fd.write(xm.stopit())  # ends walk
        fd.write(xm.end(xmltag_person, indent=indent))
Example #3
0
    def _write_xml_body(self, fd, indent, objconfigs, idcolconfig_include_tab,
                        colconfigs, objcolconfigs, xmltag_item, attrconfig_id,
                        xmltag_id, ids, ids_xml):
        print '_write_xml_body ident,ids', self.ident, ids
        print '  xmltag_item,xmltag_id,attrconfig_id', xmltag_item, xmltag_id, attrconfig_id

        # ids_xml not used here!!
        if ids == None:
            ids = self.get_ids()
        for attrconfig in objconfigs:
            attrconfig.get_value().write_xml(fd, indent + 2)

        # check if columns contain objects
        #objcolconfigs = []
        scalarcolconfigs = colconfigs
        # for attrconfig in colconfigs:
        #    if attrconfig.metatype == 'obj':
        #        objcolconfigs.append(attrconfig)
        #    else:
        #        scalarcolconfigs.append(attrconfig)

        for _id in ids:
            fd.write(xm.start(xmltag_item, indent + 2))

            # print '   make tag and id',_id
            if xmltag_id == '':
                # no id tag will be written
                pass
            elif (attrconfig_id is None) & (xmltag_id is not None):
                # use specified id tag and and specified id values
                fd.write(xm.num(xmltag_id, id_xml))

            elif (attrconfig_id is not None):
                # use id tag and values of attrconfig_id
                attrconfig_id.write_xml(fd, _id)

            # print ' write
            # columns',len(scalarcolconfigs)>0,len(idcolconfig_include_tab)>0,len(objcolconfigs)>0
            for attrconfig in scalarcolconfigs:
                # print '    scalarcolconfig',attrconfig.attrname
                attrconfig.write_xml(fd, _id)

            # insert lanechange model here:
            fd.write(
                xm.num('laneChangeModel', self.lanechangemodel.get_value()))

            if (len(idcolconfig_include_tab) > 0) | (len(objcolconfigs) > 0):
                fd.write(xm.stop())

                for attrconfig in idcolconfig_include_tab:
                    # print '    include_tab',attrconfig.attrname
                    attrconfig.write_xml(fd, _id, indent + 4)

                for attrconfig in objcolconfigs:
                    # print '    objcolconfig',attrconfig.attrname
                    attrconfig[_id].write_xml(fd, indent + 4)
                fd.write(xm.end(xmltag_item, indent + 4))
            else:
                fd.write(xm.stopit())
Example #4
0
    def write_prtvehicle_xml(self, fd, id_veh, time_begin, indent=2):
        print 'write_prtvehicle_xml', id_veh, time_begin
        # TODO: actually this should go in prtvehicles
        #time_veh_wait_after_stop = 3600
        net = self.get_scenario().net
        #lanes = net.lanes
        edges = net.edges
        #ind_ride = rides.get_inds(id_stage)
        #id_veh = rides.ids_veh[id_stage]
        prtvehicles = self.prtvehicles
        #ptstops = net.ptstops
        #prtstops = self.parent.prtstops
        #ids_prtstop = prtstops.get_ids()
        #ids_ptstop = prtstops.ids_ptstop[id_prtstop]
        # lanes.ids_edge[ptstops.ids_lane[ids_ptstop]],
        #id_lane_from = parking.ids_lane[id_parking_from]
        #laneindex_from =  lanes.indexes[id_lane_from]
        #pos_from = parking.positions[id_parking_from]

        #id_parking_to = rides.ids_parking_to[id_stage]
        #id_lane_to = parking.ids_lane[id_parking_to]
        #laneindex_to =  lanes.indexes[id_lane_to]
        #pos_to = parking.positions[id_parking_to]

        # write unique veh ID to prevent confusion with other veh declarations
        fd.write(
            xm.start('vehicle id="%s"' % prtvehicles.get_id_sumo(id_veh),
                     indent + 2))

        fd.write(xm.num('depart', '%d' % time_begin))
        fd.write(
            xm.num('type',
                   self.parent.vtypes.ids_sumo[prtvehicles.ids_vtype[id_veh]]))
        fd.write(xm.num('line', prtvehicles.get_id_line_xml()))
        fd.write(xm.stop())

        # write route
        fd.write(xm.start('route', indent + 4))
        # print '  edgeindex[ids_edge]',edgeindex[ids_edge]
        fd.write(
            xm.arr('edges',
                   [edges.ids_sumo[prtvehicles.ids_currentedge[id_veh]]]))

        # does not seem to have an effect, always starts at base????
        fd.write(xm.num('departPos', 'base'))
        #fd.write(xm.num('departLane', laneindex_from ))
        fd.write(xm.stopit())

        # write depart stop
        # fd.write(xm.start('stop',indent+4))
        #fd.write(xm.num('lane', edges.ids_sumo[lanes.ids_edge[id_lane_from]]+'_%d'%laneindex_from ))
        #fd.write(xm.num('duration', time_veh_wait_after_stop))
        #fd.write(xm.num('startPos', pos_from ))
        #fd.write(xm.num('endPos', pos_from + parking.lengths[id_parking_from]))
        #fd.write(xm.num('triggered', "True"))
        # fd.write(xm.stopit())

        fd.write(xm.end('vehicle', indent + 2))
Example #5
0
    def _write_xml_body(self, fd,  indent, objconfigs, idcolconfig_include_tab, colconfigs,
                        objcolconfigs,
                        xmltag_item, attrconfig_id, xmltag_id, ids, ids_xml):
        print '_write_xml_body ident,ids', self.ident, ids
        print '  xmltag_item,xmltag_id,attrconfig_id',    xmltag_item, xmltag_id, attrconfig_id

        # ids_xml not used here!!
        if ids == None:
            ids = self.get_ids()
        for attrconfig in objconfigs:
            attrconfig.get_value().write_xml(fd, indent + 2)

        # check if columns contain objects
        #objcolconfigs = []
        scalarcolconfigs = colconfigs
        # for attrconfig in colconfigs:
        #    if attrconfig.metatype == 'obj':
        #        objcolconfigs.append(attrconfig)
        #    else:
        #        scalarcolconfigs.append(attrconfig)

        for _id in ids:
            fd.write(xm.start(xmltag_item, indent + 2))

            # print '   make tag and id',_id
            if xmltag_id == '':
                # no id tag will be written
                pass
            elif (attrconfig_id is None) & (xmltag_id is not None):
                # use specified id tag and and specified id values
                fd.write(xm.num(xmltag_id, id_xml))

            elif (attrconfig_id is not None):
                # use id tag and values of attrconfig_id
                attrconfig_id.write_xml(fd, _id)

            # print ' write
            # columns',len(scalarcolconfigs)>0,len(idcolconfig_include_tab)>0,len(objcolconfigs)>0
            for attrconfig in scalarcolconfigs:
                # print '    scalarcolconfig',attrconfig.attrname
                attrconfig.write_xml(fd, _id)

            # insert lanechange model here:
            fd.write(xm.num('laneChangeModel', self.lanechangemodel.get_value()))

            if (len(idcolconfig_include_tab) > 0) | (len(objcolconfigs) > 0):
                fd.write(xm.stop())

                for attrconfig in idcolconfig_include_tab:
                    # print '    include_tab',attrconfig.attrname
                    attrconfig.write_xml(fd, _id, indent + 4)

                for attrconfig in objcolconfigs:
                    # print '    objcolconfig',attrconfig.attrname
                    attrconfig[_id].write_xml(fd, indent + 4)
                fd.write(xm.end(xmltag_item, indent + 4))
            else:
                fd.write(xm.stopit())
Example #6
0
    def write_flow_xml(self, fd, id_line, time_begin, indent=0):
        #_idents = self.get_keys()
        #_inds = self.get_inds_from_keys(_idents)
        #_ids_egdes = self.cols.ids_edge[_inds]

        # for  _ind, _id_line, _ids_egde in zip(_inds, _idents, _ids_egdes):
        #vtype = self.cols.vtype[id_line]

        # write vehicle flow data
        fd.write(xm.start('flow id="ptline.%s"' % id_line, indent))
        for attrconfig in [
                self.ids_vtype,
                self.linenames,
                self.times_begin,
                self.times_end,
                self.periods,
        ]:
            # print '    attrconfig',attrconfig.attrname
            attrconfig.write_xml(fd, id_line)

        #fd.write(xm.num('begin', '%d'%self.times_begin[id_line]))
        #fd.write(xm.num('end', '%d'%self.times_end[id_line]))
        #fd.write(xm.num('period', '%d'%self.periods[id_line]))
        #fd.write(xm.num('line', self.linenames[id_line]))
        #fd.write(xm.num('type', self.ids_vtype[id_line]))

        fd.write(xm.stop())

        # write route
        #ids_edge, duration = self.route(_id_line, vtype)
        ids_edge = self.ids_edges[id_line]
        if len(ids_edge) > 0:
            fd.write(xm.start('route', indent + 2))
            self.ids_edges.write_xml(fd, id_line)
            # fd.write(xm.arr('edges',ids_egde,indent+4))
            #fd.write(xm.num('departPos', pos_depart))

            # depart lane is 1 , 0 would be on the side-walk)
            #fd.write(xm.num('departLane', laneind_parking))
            fd.write(xm.stopit())

        # write stops

        ids_stop = self.ids_stops[id_line]

        if len(ids_stop) > 0:
            stopnames = self.ids_stops.get_linktab().stopnames[ids_stop]
            time_dwell = self.times_dwell[id_line]
            for stopname in stopnames:
                fd.write(xm.start('stop', indent + 2))
                fd.write(xm.num('busStop', stopname))
                fd.write(xm.num('duration', time_dwell))
                fd.write(xm.stopit())
        fd.write(xm.end('flow', indent))
Example #7
0
    def write_flow_xml(self, fd, id_line, time_begin, indent=0):
        #_idents = self.get_keys()
        #_inds = self.get_inds_from_keys(_idents)
        #_ids_egdes = self.cols.ids_edge[_inds]

        # for  _ind, _id_line, _ids_egde in zip(_inds, _idents, _ids_egdes):
        #vtype = self.cols.vtype[id_line]

        # write vehicle flow data
        fd.write(xm.start('flow id="ptline.%s"' % id_line, indent))
        for attrconfig in [self.ids_vtype,
                           self.linenames,
                           self.times_begin,
                           self.times_end,
                           self.periods,
                           ]:
            # print '    attrconfig',attrconfig.attrname
            attrconfig.write_xml(fd, id_line)

        #fd.write(xm.num('begin', '%d'%self.times_begin[id_line]))
        #fd.write(xm.num('end', '%d'%self.times_end[id_line]))
        #fd.write(xm.num('period', '%d'%self.periods[id_line]))
        #fd.write(xm.num('line', self.linenames[id_line]))
        #fd.write(xm.num('type', self.ids_vtype[id_line]))

        fd.write(xm.stop())

        # write route
        #ids_edge, duration = self.route(_id_line, vtype)
        ids_edge = self.ids_edges[id_line]
        if len(ids_edge) > 0:
            fd.write(xm.start('route', indent+2))
            self.ids_edges.write_xml(fd, id_line)
            # fd.write(xm.arr('edges',ids_egde,indent+4))
            #fd.write(xm.num('departPos', pos_depart))

            # depart lane is 1 , 0 would be on the side-walk)
            #fd.write(xm.num('departLane', laneind_parking))
            fd.write(xm.stopit())

        # write stops

        ids_stop = self.ids_stops[id_line]

        if len(ids_stop) > 0:
            stopnames = self.ids_stops.get_linktab().stopnames[ids_stop]
            time_dwell = self.times_dwell[id_line]
            for stopname in stopnames:
                fd.write(xm.start('stop', indent+2))
                fd.write(xm.num('busStop', stopname))
                fd.write(xm.num('duration', time_dwell))
                fd.write(xm.stopit())
        fd.write(xm.end('flow', indent))
Example #8
0
    def write_prtvehicle_xml(self,  fd, id_veh, time_begin, indent=2):
        print 'write_prtvehicle_xml', id_veh, time_begin
        # TODO: actually this should go in prtvehicles
        #time_veh_wait_after_stop = 3600
        net = self.get_scenario().net
        #lanes = net.lanes
        edges = net.edges
        #ind_ride = rides.get_inds(id_stage)
        #id_veh = rides.ids_veh[id_stage]
        prtvehicles = self.prtvehicles
        #ptstops = net.ptstops
        #prtstops = self.parent.prtstops
        #ids_prtstop = prtstops.get_ids()
        #ids_ptstop = prtstops.ids_ptstop[id_prtstop]
        # lanes.ids_edge[ptstops.ids_lane[ids_ptstop]],
        #id_lane_from = parking.ids_lane[id_parking_from]
        #laneindex_from =  lanes.indexes[id_lane_from]
        #pos_from = parking.positions[id_parking_from]

        #id_parking_to = rides.ids_parking_to[id_stage]
        #id_lane_to = parking.ids_lane[id_parking_to]
        #laneindex_to =  lanes.indexes[id_lane_to]
        #pos_to = parking.positions[id_parking_to]

        # write unique veh ID to prevent confusion with other veh declarations
        fd.write(xm.start('vehicle id="%s"' % prtvehicles.get_id_sumo(id_veh), indent+2))

        fd.write(xm.num('depart', '%d' % time_begin))
        fd.write(xm.num('type', self.parent.vtypes.ids_sumo[prtvehicles.ids_vtype[id_veh]]))
        fd.write(xm.num('line', prtvehicles.get_id_line_xml()))
        fd.write(xm.stop())

        # write route
        fd.write(xm.start('route', indent+4))
        # print '  edgeindex[ids_edge]',edgeindex[ids_edge]
        fd.write(xm.arr('edges', [edges.ids_sumo[prtvehicles.ids_currentedge[id_veh]]]))

        # does not seem to have an effect, always starts at base????
        fd.write(xm.num('departPos', 'base'))
        #fd.write(xm.num('departLane', laneindex_from ))
        fd.write(xm.stopit())

        # write depart stop
        # fd.write(xm.start('stop',indent+4))
        #fd.write(xm.num('lane', edges.ids_sumo[lanes.ids_edge[id_lane_from]]+'_%d'%laneindex_from ))
        #fd.write(xm.num('duration', time_veh_wait_after_stop))
        #fd.write(xm.num('startPos', pos_from ))
        #fd.write(xm.num('endPos', pos_from + parking.lengths[id_parking_from]))
        #fd.write(xm.num('triggered', "True"))
        # fd.write(xm.stopit())

        fd.write(xm.end('vehicle', indent+2))
Example #9
0
    def export_amitranxml(self, filepath=None, encoding='UTF-8'):
        """
        Export flows to Amitran format that defines the demand per OD pair in time slices for every vehicle type.
        """
        print 'export_amitranxml', filepath, len(self)

        if len(self) == 0:
            return None

        if filepath is None:
            filepath = self.get_amitranfilepath()

        try:
            fd = open(filepath, 'w')
        except:
            print 'WARNING in export_sumoxml: could not open', filepath
            return False

        indent = 0

        ft = self.generate_odflows()
        scenario = self.parent.parent
        ids_zone_sumo = scenario.landuse.zones.ids_sumo
        get_vtype_for_mode = scenario.demand.vtypes.get_vtype_for_mode
        ids = ft.get_ids()
        fd.write(xm.begin('demand', indent))
        # self.parent.vtypes.write_xml(   fd, indent=indent,
        #                                #ids = ids_vtype_selected,
        #                                is_print_begin_end = False)

        # <demand>
        #   <actorConfig id="0">
        #       <timeSlice duration="86400000" startTime="0">
        #           <odPair amount="100" destination="2" origin="1"/>
        #       </timeSlice>
        #   </actorConfig>
        # </demand>

        for id_flow, time_start, time_end, id_mode, id_orig_sumo, id_dest_sumo, tripnumber in zip(
                ids, ft.times_start[ids], ft.times_end[ids], ft.ids_mode[ids],
                ids_zone_sumo[ft.ids_orig[ids]],
                ids_zone_sumo[ft.ids_dest[ids]], ft.tripnumbers[ids]):

            fd.write(xm.start('actorConfig', indent + 2))
            fd.write(
                xm.num('id', get_vtype_for_mode(id_mode=id_mode,
                                                is_sumoid=True)))
            fd.write(xm.stop())

            fd.write(xm.start('timeSlice', indent + 4))
            fd.write(xm.num('duration', int(time_end - time_start)))
            fd.write(xm.num('startTime', int(time_start)))
            fd.write(xm.stop())

            fd.write(xm.start('odPair', indent + 6))
            fd.write(xm.num('origin', id_orig_sumo))
            fd.write(xm.num('destination', id_dest_sumo))
            fd.write(xm.num('amount', int(tripnumber)))
            fd.write(xm.stopit())

            fd.write(xm.end('timeSlice', indent + 4))

            fd.write(xm.end('actorConfig', indent + 2))

        fd.write(xm.end('demand', indent))

        fd.close()
        return filepath
Example #10
0
    def _export_plans(self, fd, indent=0):
        if len(self) == 0:
            return

        scenario = self.parent.get_scenario()
        edges = scenario.net.edges
        lanes = scenario.net.lanes
        landuse = self.get_landuse()
        # ptlines=self.get_ptlines()
        persons = self  # instance holding all arrays for persons
        # instance holding all arrays for individual vehicles
        ivehs = self.individualvehicles.value
        parking = landuse.parking  # instance holding all arrays for parking
        plans = self.plans.value

        # sort users by initial time
        # print '  persons.times_start.value',persons.times_start.value
        # print '  persons.get_inds()',persons.get_inds()
        times_sorted = np.concatenate((persons.times_start.value.reshape(
            -1, 1), persons.get_inds().reshape(-1, 1)), 1).tolist()
        times_sorted.sort()
        # print '  times_sorted',times_sorted
        inds_pers = np.array(times_sorted, int)[:, 1]
        # print '  inds_pers',inds_pers

        # used to start first vehcile appearance
        time_first, ind_first = times_sorted[0]

        vehindex = self.parent.vtypes.ids_sumo  # vehindex.get_index_from_ids()
        edgeindex = edges.ids_sumo
        # pt
        # self.parent.get_ptlines().to_routes(fd,2)

        #----------------------
        # write vehicles first
        inds_veh = ivehs.get_inds()
        for id_veh, id_vtype, ids_edge, ids_parking in zip(
                ivehs.get_ids(inds_veh), ivehs.ids_vtype.value[inds_veh],
                ivehs.routes.value[inds_veh],
                ivehs.ids_parkings.value[inds_veh]):
            if (ids_edge != None) & (ids_parking != None):
                if len(ids_edge) > 0:
                    # print "  vehicle id=", id_veh,ids_edge, ids_parking

                    inds_parking = parking.get_inds(ids_parking)
                    # first depart position  equals position of first parking
                    pos_depart = parking.positions.value[inds_parking][
                        0]  # get pos of first parking

                    fd.write(xm.start('vehicle id="%s"' % id_veh, indent + 2))
                    fd.write(xm.num('depart', '%.1f' % time_first))
                    fd.write(xm.num('type', vehindex[id_vtype]))
                    fd.write(xm.stop())

                    # write route
                    fd.write(xm.start('route', indent + 4))
                    fd.write(xm.arr('edges', edgeindex[ids_edge], indent + 6))
                    fd.write(xm.num('departPos', pos_depart))
                    # depart lane is 1 , 0 would be on the side-walk)
                    fd.write(
                        xm.num(
                            'departLane',
                            lanes.indexes[parking.ids_lane[ids_parking[0]]]))
                    fd.write(xm.stopit())

                    # write stops
                    ids_lane_parking = parking.ids_lane.value[inds_parking]
                    ids_edge_parking = lanes.ids_edge[ids_lane_parking]
                    laneindexes_parking = lanes.indexes[ids_lane_parking]

                    poss_end_parking = parking.positions[inds_parking]
                    poss_start_parking = poss_end_parking - \
                        parking.lengths[inds_parking]
                    for id_edge_parking, laneindex_parking, pos_start_parking, pos_end_parking in zip(
                            ids_edge_parking, laneindexes_parking,
                            poss_start_parking, poss_end_parking):
                        fd.write(xm.start('stop', indent + 4))
                        fd.write(
                            xm.num(
                                'lane', edges.ids_sumo[id_edge_parking] +
                                '_%d' % laneindex_parking))
                        fd.write(xm.num('duration', 3000))
                        fd.write(xm.num('startPos', pos_start_parking))
                        fd.write(xm.num('endPos', pos_end_parking))
                        fd.write(xm.num('triggered', "True"))
                        fd.write(xm.stopit())

                    fd.write(xm.end('vehicle', indent + 2))

        #----------------------
        # write plans

        # print '  inds_pers',inds_pers
        # print '  times_start[inds_pers]',persons.times_start.value[inds_pers]
        for time_start, id_pers, stages in zip(
                persons.times_start.value[inds_pers],
                persons.get_ids(inds_pers),
                plans.stagelists[persons.ids_plan.value[inds_pers]]):
            print '  time_start,id_pers', time_start, id_pers, stages != None
            if stages != None:
                # TODO: self.persons.to_xml()??..pass all attrs?
                fd.write(xm.start('person', indent=indent + 2))
                fd.write(xm.num('id', id_pers))
                fd.write(xm.num('depart', time_start))
                fd.write(xm.num('type', 'pedestrian'))
                fd.write(xm.stop())

                for stage, id_stage in stages:
                    stage.to_xml(id_stage, fd, indent + 4)

                fd.write(xm.end('person', indent=indent + 2))
Example #11
0
    def _export_plans(self, fd, indent=0):
        if len(self) == 0:
            return

        scenario = self.parent.get_scenario()
        edges = scenario.net.edges
        lanes = scenario.net.lanes
        landuse = self.get_landuse()
        # ptlines=self.get_ptlines()
        persons = self  # instance holding all arrays for persons
        # instance holding all arrays for individual vehicles
        ivehs = self.individualvehicles.value
        parking = landuse.parking  # instance holding all arrays for parking
        plans = self.plans.value

        # sort users by initial time
        # print '  persons.times_start.value',persons.times_start.value
        # print '  persons.get_inds()',persons.get_inds()
        times_sorted = np.concatenate(
            (persons.times_start.value.reshape(-1, 1),  persons.get_inds().reshape(-1, 1)), 1).tolist()
        times_sorted.sort()
        # print '  times_sorted',times_sorted
        inds_pers = np.array(times_sorted, int)[:, 1]
        # print '  inds_pers',inds_pers

        # used to start first vehcile appearance
        time_first, ind_first = times_sorted[0]

        vehindex = self.parent.vtypes.ids_sumo  # vehindex.get_index_from_ids()
        edgeindex = edges.ids_sumo
        # pt
        # self.parent.get_ptlines().to_routes(fd,2)

        #----------------------
        # write vehicles first
        inds_veh = ivehs.get_inds()
        for id_veh, id_vtype, ids_edge, ids_parking in zip(ivehs.get_ids(inds_veh), ivehs.ids_vtype.value[inds_veh], ivehs.routes.value[inds_veh], ivehs.ids_parkings.value[inds_veh]):
            if (ids_edge != None) & (ids_parking != None):
                if len(ids_edge) > 0:
                    # print "  vehicle id=", id_veh,ids_edge, ids_parking

                    inds_parking = parking.get_inds(ids_parking)
                    # first depart position  equals position of first parking
                    pos_depart = parking.positions.value[
                        inds_parking][0]  # get pos of first parking

                    fd.write(xm.start('vehicle id="%s"' % id_veh, indent + 2))
                    fd.write(xm.num('depart', '%.1f' % time_first))
                    fd.write(xm.num('type', vehindex[id_vtype]))
                    fd.write(xm.stop())

                    # write route
                    fd.write(xm.start('route', indent + 4))
                    fd.write(xm.arr('edges', edgeindex[ids_edge], indent + 6))
                    fd.write(xm.num('departPos', pos_depart))
                    # depart lane is 1 , 0 would be on the side-walk)
                    fd.write(xm.num('departLane', lanes.indexes[
                             parking.ids_lane[ids_parking[0]]]))
                    fd.write(xm.stopit())

                    # write stops
                    ids_lane_parking = parking.ids_lane.value[inds_parking]
                    ids_edge_parking = lanes.ids_edge[ids_lane_parking]
                    laneindexes_parking = lanes.indexes[ids_lane_parking]

                    poss_end_parking = parking.positions[inds_parking]
                    poss_start_parking = poss_end_parking - \
                        parking.lengths[inds_parking]
                    for id_edge_parking, laneindex_parking, pos_start_parking, pos_end_parking in zip(ids_edge_parking, laneindexes_parking, poss_start_parking, poss_end_parking):
                        fd.write(xm.start('stop', indent + 4))
                        fd.write(xm.num('lane', edges.ids_sumo[
                                 id_edge_parking] + '_%d' % laneindex_parking))
                        fd.write(xm.num('duration', 3000))
                        fd.write(xm.num('startPos', pos_start_parking))
                        fd.write(xm.num('endPos', pos_end_parking))
                        fd.write(xm.num('triggered', "True"))
                        fd.write(xm.stopit())

                    fd.write(xm.end('vehicle', indent + 2))

        #----------------------
        # write plans

        # print '  inds_pers',inds_pers
        # print '  times_start[inds_pers]',persons.times_start.value[inds_pers]
        for time_start, id_pers, stages in zip(persons.times_start.value[inds_pers], persons.get_ids(inds_pers), plans.stagelists[persons.ids_plan.value[inds_pers]]):
            print '  time_start,id_pers', time_start, id_pers, stages != None
            if stages != None:
                # TODO: self.persons.to_xml()??..pass all attrs?
                fd.write(xm.start('person', indent=indent + 2))
                fd.write(xm.num('id', id_pers))
                fd.write(xm.num('depart', time_start))
                fd.write(xm.num('type', 'pedestrian'))
                fd.write(xm.stop())

                for stage, id_stage in stages:
                    stage.to_xml(id_stage, fd, indent + 4)

                fd.write(xm.end('person', indent=indent + 2))
Example #12
0
    def export_routes_xml(self, filepath=None, method_routechoice=None, encoding='UTF-8'):
        """
        Export routes to SUMO xml file.
        Method takes care of sorting trips by departure time.
        """
        if method_routechoice is None:
            method_routechoice = self.get_route_first

        if filepath is None:
            filepath = self.get_routefilepath()
        print 'export_routes_xml', filepath
        try:
            fd = open(filepath, 'w')
        except:
            print 'WARNING in write_obj_to_xml: could not open', filepath
            return False

        xmltag_routes, xmltag_veh, attrname_id = ("routes", "vehicle", "ids_sumo")
        xmltag_trip = "trip"
        xmltag_rou = "route"

        fd.write('<?xml version="1.0" encoding="%s"?>\n' % encoding)
        fd.write(xm.begin(xmltag_routes))
        indent = 2

        #ids_modes_used = set(self.parent.vtypes.ids_mode[self.ids_vtype.get_value()])
        self.parent.vtypes.write_xml(fd, indent=indent,
                                     ids=set(self.ids_vtype.get_value()),
                                     is_print_begin_end=False
                                     )

        ids_mode = self.parent.vtypes.ids_mode
        id_pedestrian = MODES['pedestrian']
        routes = self.routes.get_value()

        # here we could write the route info
        # but we do write it inside each trip so that it can be parsed
        # in the same way as duarouter output
        # routes.write_xml(   fd, indent=indent,
        #                    attrconfigs_excluded = [routes.costs, routes.probabilities],
        #                    is_print_begin_end = False)

        # let's write trip info manually
        tripconfigs = [self.ids_vtype,
                       self.times_depart,
                       self.ids_edge_depart,
                       self.ids_edge_arrival,
                       self.inds_lane_depart,
                       self.positions_depart,
                       self.speeds_depart,
                       self.inds_lane_arrival,
                       self.positions_arrival,
                       self.speeds_arrival,
                       ]

        routeconfigs = [routes.ids_edges,
                        routes.colors,
                        ]

        attrconfig_id = getattr(self.get_attrsman(), attrname_id)
        xmltag_id = attrconfig_id.xmltag

        for id_trip in self.times_depart.get_ids_sorted():

            if ids_mode[self.ids_vtype[id_trip]] == id_pedestrian:
                self.write_persontrip_xml(fd, id_trip,
                                          method_routechoice=method_routechoice,
                                          indent=indent+2)

            else:
                id_route = method_routechoice(id_trip)
                if id_route >= 0:  # a valid route has been found
                    # init vehicle route only if valid route exists
                    fd.write(xm.start(xmltag_veh, indent+2))
                else:
                    # init trip instead of route
                    fd.write(xm.start(xmltag_trip, indent+2))

                # print '   make tag and id',_id
                fd.write(xm.num(xmltag_id, attrconfig_id[id_trip]))

                # print ' write columns',len(scalarcolconfigs)>0,len(idcolconfig_include_tab)>0,len(objcolconfigs)>0
                for attrconfig in tripconfigs:
                    # print '    attrconfig',attrconfig.attrname
                    attrconfig.write_xml(fd, id_trip)

                if id_route >= 0:  # a valid route has been found
                    # write route id
                    #fd.write(xm.num('route', id_route ))

                    # instead of route id we write entire route here
                    fd.write(xm.stop())
                    fd.write(xm.start(xmltag_rou, indent+4))
                    for attrconfig in routeconfigs:
                        # print '    attrconfig',attrconfig.attrname
                        attrconfig.write_xml(fd, id_route)

                    # end route and vehicle
                    fd.write(xm.stopit())
                    fd.write(xm.end(xmltag_veh, indent+2))

                else:
                    # end trip without route
                    fd.write(xm.stopit())

        fd.write(xm.end(xmltag_routes))
        fd.close()
        return filepath
Example #13
0
    def export_routes_xml(self, filepath=None, method_routechoice=None, encoding='UTF-8'):
        """
        Export routes to SUMO xml file.
        Method takes care of sorting trips by departure time.
        """
        if method_routechoice == None:
            method_routechoice = self.get_route_first

        if filepath == None:
            filepath = self.get_routefilepath()
        print 'export_routes_xml', filepath
        try:
            fd = open(filepath, 'w')
        except:
            print 'WARNING in write_obj_to_xml: could not open', filepath
            return False

        xmltag_routes, xmltag_veh, attrname_id = (
            "routes", "vehicle", "ids_sumo")
        xmltag_trip = "trip"
        xmltag_rou = "route"

        fd.write('<?xml version="1.0" encoding="%s"?>\n' % encoding)
        fd.write(xm.begin(xmltag_routes))
        indent = 2

        #ids_modes_used = set(self.parent.vtypes.ids_mode[self.ids_vtype.get_value()])
        self.parent.vtypes.write_xml(fd, indent=indent,
                                     ids=set(self.ids_vtype.get_value()),
                                     is_print_begin_end=False
                                     )

        ids_mode = self.parent.vtypes.ids_mode
        id_pedestrian = MODES['pedestrian']
        routes = self.routes.get_value()

        # here we could write the route info
        # but we do write it inside each trip so that it can be parsed
        # in the same way as duarouter output
        # routes.write_xml(   fd, indent=indent,
        #                    attrconfigs_excluded = [routes.costs, routes.probabilities],
        #                    is_print_begin_end = False)

        # let's write trip info manually
        tripconfigs = [self.ids_vtype,
                       self.times_depart,
                       self.ids_edge_depart,
                       self.ids_edge_arrival,
                       self.inds_lane_depart,
                       self.positions_depart,
                       self.speeds_depart,
                       self.inds_lane_arrival,
                       self.positions_arrival,
                       self.speeds_arrival,
                       ]

        routeconfigs = [routes.ids_edges,
                        routes.colors,
                        ]

        attrconfig_id = getattr(self.get_attrsman(), attrname_id)
        xmltag_id = attrconfig_id.xmltag

        for id_trip in self.times_depart.get_ids_sorted():

            if ids_mode[self.ids_vtype[id_trip]] == id_pedestrian:
                self.write_persontrip_xml(fd, id_trip,
                                          method_routechoice=method_routechoice,
                                          indent=indent + 2)

            else:
                id_route = method_routechoice(id_trip)
                if id_route >= 0:  # a valid route has been found
                    # init vehicle route only if valid route exists
                    fd.write(xm.start(xmltag_veh, indent + 2))
                else:
                    # init trip instead of route
                    fd.write(xm.start(xmltag_trip, indent + 2))

                # print '   make tag and id',_id
                fd.write(xm.num(xmltag_id, attrconfig_id[id_trip]))

                # print ' write
                # columns',len(scalarcolconfigs)>0,len(idcolconfig_include_tab)>0,len(objcolconfigs)>0
                for attrconfig in tripconfigs:
                    # print '    attrconfig',attrconfig.attrname
                    attrconfig.write_xml(fd, id_trip)

                if id_route >= 0:  # a valid route has been found
                    # write route id
                    #fd.write(xm.num('route', id_route ))

                    # instead of route id we write entire route here
                    fd.write(xm.stop())
                    fd.write(xm.start(xmltag_rou, indent + 4))
                    for attrconfig in routeconfigs:
                        # print '    attrconfig',attrconfig.attrname
                        attrconfig.write_xml(fd, id_route)

                    # end route and vehicle
                    fd.write(xm.stopit())
                    fd.write(xm.end(xmltag_veh, indent + 2))

                else:
                    # end trip without route
                    fd.write(xm.stopit())

        fd.write(xm.end(xmltag_routes))
        fd.close()
        return filepath