示例#1
0
 def loadSourceDatabaseScenarios(self):
     emmebank = Emmebank(self.FromEmmebankPath)
     
     options = []
     for scenario in emmebank.scenarios():
         text = "%s - %s" %(scenario.id, scenario.title)
         options.append('<option value="%s">%s</option>' %(scenario.id, text))
     
     emmebank.dispose()
     return "\n".join(options)
示例#2
0
 def loadSourceDatabaseScenarios(self):
     emmebank = Emmebank(self.FromEmmebankPath)
     
     options = []
     for scenario in emmebank.scenarios():
         text = "%s - %s" %(scenario.id, scenario.title)
         options.append('<option value="%s">%s</option>' %(scenario.id, text))
     
     emmebank.dispose()
     return "\n".join(options)
示例#3
0
    def emmebank(path: str) -> Emmebank:
        """Open and return the Emmebank at path.

        Args:
            path: valid system path pointing to an Emmebank file
        Returns:
            Emmebank object, see Emme API Reference, Database section for details.
        """
        if not path.endswith("emmebank"):
            path = os.path.join(path, "emmebank")
        return Emmebank(path)
示例#4
0
    def preload_database_scenarios(self):
        self._cahcedSourceScenarioAttributes = {}
        
        with Emmebank(self.SourceEmmebankPath) as bank:
            options = []
            for scenario in bank.scenarios():
                text = "%s - %s" %(scenario.id, scenario.title)
                options.append('<option value="%s">%s</option>' %(scenario.id, text))
                
                attributes = ['<option value="-1">ALL - Copy all transit lines in the source scenario</option>']
                for exatt in scenario.extra_attributes():
                    if exatt.type != 'TRANSIT_LINE': continue
                    text = "%s - TRANSIT LINE - %s" %(exatt.id, exatt.description)
                    attributes.append('<option value="%s">%s</option>' %(exatt.id, text))
                self._cahcedSourceScenarioAttributes[scenario.id] = attributes

            return "\n".join(options)
示例#5
0
    def preload_vehicle_correspondence_file(self):
        try:
            with open(self.TransitVehicleCorrespondenceFile) as reader:
                self._cachedCorrespondence = {}
                header = reader.readline().strip()
                cells = header.split(',')
                if len(cells) >= 2:
                    if cells[0].isdigit() and cells[1].isdigit():
                        #Guessed that there is no header
                        self._cachedCorrespondence[int(cells[0])] = int(
                            cells[1])

                for line in reader:
                    if line.isspace(): continue  #Skip blank lines
                    cells = line.strip().split(',')
                    sourceVehicleId = int(cells[0])
                    targetVehicleId = int(cells[1])

                    self._cachedCorrespondence[
                        sourceVehicleId] = targetVehicleId

            with Emmebank(self.SourceEmmebankPath) as emmebank:
                sourceScenario = emmebank.scenario(self.SourceScenarioId)

            for sourceVehicleId, targetVehicleId in self._cachedCorrespondence.iteritems(
            ):
                sourceVehicle = sourceScenario.transit_vehicle(sourceVehicleId)
                if sourceVehicle is None:
                    return "Vehicle %s does not exist in the source scenario" % sourceVehicleId

                targetVehicle = self.TargetScenario.transit_vehicle(
                    targetVehicleId)
                if targetVehicle is None:
                    return "Vehicle %s does not exist in the target scenario" % targetVehicleId

                if sourceVehicle.mode.id != targetVehicle.mode.id:
                    tup = sourceVehicleId, sourceVehicle.mode.id, targetVehicle, targetVehicle.mode.id
                    return "Source vehicle % mode '%s' does not match target vehicle %s mode '%s'" % tup
            return None

        except Exception as e:
            return str(e)
示例#6
0
 def _LoadSourceNetwork(self):
     
     if self.SourceEmmebankPath == _MODELLER.emmebank.path:
         scenario = _MODELLER.emmebank.scenario(self.SourceScenarioId)
         
         if scenario == None:
             raise Exception("Scenario '%s' does not exist in database at "%self.SourceScenarioId\
                              + self.SourceEmmebankPath)
         
         if scenario == self.TargetScenario:
             raise Exception("Source and target scenarios are the same!")
         
         network = scenario.get_network()
     else:
         with Emmebank(self.SourceEmmebankPath) as emmebank:
             scenario = emmebank.scenario(self.SourceScenarioId)
             
             if scenario == None:
                 raise Exception("Scenario '%s' does not exist in database at "%self.SourceScenarioId\
                                  + self.SourceEmmebankPath)
             network = scenario.get_network()
             
     print "Loaded source network"
     return network
示例#7
0
 def _Execute(self):
     with _m.logbook_trace(name="{classname} v{version}".format(classname=(self.__class__.__name__), version=self.version),
                                  attributes=self._GetAtts()):           
         
         self.TRACKER.startProcess(5)
         with _m.logbook_trace("Loading scenarios and applying node filters"):
             #---Load the source network
             sourceBank = Emmebank(self.FromEmmebankPath)
             try:
                 sourceScenario = sourceBank.scenario(self.FromScenarioId)
                 if sourceScenario == None:
                     #Check, because the scenario could've been deleted in between the time
                     #that the list of scenarios was extracted, and the time that this tool
                     #is running
                     raise Exception("Scenario %s no longer exists" %self.FromScenarioId)
                 
                 with _util.tempExtraAttributeMANAGER(sourceScenario, 'NODE') as sourceTempAtt:
                     if self.FromZonesToIgnoreSelector:
                         self._ApplySelector(sourceScenario, sourceTempAtt, self.FromZonesToIgnoreSelector)
                     self.TRACKER.completeSubtask()
                     
                     sourceNetwork = sourceScenario.get_network()
                     self.TRACKER.completeSubtask()
             finally:
                 if self.FromEmmebankPath != _MODELLER.emmebank.path:
                     sourceBank.dispose()
             
             
             
             #---Load the target network
             with _util.tempExtraAttributeMANAGER(self.ToScenario, 'NODE') as targetTempAtt:
                 if self.ToNodesToIgnoreSelector:
                     self._ApplySelector(self.ToScenario, targetTempAtt, self.ToNodesToIgnoreSelector)
                 self.TRACKER.completeSubtask()
                 
                 targetNetwork = self.ToScenario.get_network()
                 self.TRACKER.completeSubtask()
         
         #---Determine the attributes to copy over
         attsToCopy = self._DetermineAttributesToCopy(sourceNetwork, targetNetwork)
         self.TRACKER.completeSubtask()
         self.TRACKER.completeTask()
         
         #---Get the source nodes to be copied
         zonesToCopy = [zone for zone in sourceNetwork.centroids() if zone[sourceTempAtt.id] == 0]          
         _m.logbook_write("Found %s zones to copy over." %len(zonesToCopy))
         
         #---Clear un-flagged zones from the target network
         if self.ClearTargetZonesFlag:
             with _m.logbook_trace("Clearing zones from target network"):
                 zonesToDelete = [zone.number for zone in targetNetwork.centroids()]
                 for id in zonesToDelete: targetNetwork.delete_node(id, cascade=True)
                 _m.logbook_write("%s zones deleted from target network" %len(zonesToDelete)) 
         
         #---Run the matching process
         if self.MatchOption == 1:
             message = "Copying zones using node coordinates"
             func = self._GetMatchByGeometryLambda(targetNetwork)
         elif self.MatchOption == 2:
             message = "Copying zones using node IDs"
             func = self._GetMatchByIdLambda(targetNetwork)
         else:
             raise KeyError("Unrecognized match option", self.MatchOption)
         with _m.logbook_trace(message):
             count, uncopiedConnectors = self._NewCopyZones(zonesToCopy, targetNetwork, targetTempAtt, attsToCopy, func)
             self._WriteLogbookReport(uncopiedConnectors)                
         
         _m.logbook_write("Copied over %s connectors from source scenario" %count)
         _m.logbook_write("%s connectors could not be copied" %len(uncopiedConnectors))
         
         #---Write  shapefile
         if len(uncopiedConnectors) > 0 and self.ShapefileReport:
             self._WriteShapefile(uncopiedConnectors)
             _m.logbook_write("Wrote shapefile to %s" %self.ShapefileReport)
         self.TRACKER.completeTask()
         
         #---Publish the network
         if self.PublishNetworkFlag:
             self.ToScenario.publish_network(targetNetwork, True)
             _MODELLER.desktop.refresh_needed(True)
         self.TRACKER.completeTask()
示例#8
0
    def _Execute(self):
        with _m.logbook_trace(name="{classname} v{version}".format(
                classname=(self.__class__.__name__), version=self.version),
                              attributes=self._GetAtts()):

            self.TRACKER.startProcess(5)
            with _m.logbook_trace(
                    "Loading scenarios and applying node filters"):
                #---Load the source network
                sourceBank = Emmebank(self.FromEmmebankPath)
                try:
                    sourceScenario = sourceBank.scenario(self.FromScenarioId)
                    if sourceScenario is None:
                        #Check, because the scenario could've been deleted in between the time
                        #that the list of scenarios was extracted, and the time that this tool
                        #is running
                        raise Exception("Scenario %s no longer exists" %
                                        self.FromScenarioId)

                    with _util.tempExtraAttributeMANAGER(
                            sourceScenario, 'NODE') as sourceTempAtt:
                        if self.FromZonesToIgnoreSelector:
                            self._ApplySelector(sourceScenario, sourceTempAtt,
                                                self.FromZonesToIgnoreSelector)
                        self.TRACKER.completeSubtask()

                        sourceNetwork = sourceScenario.get_network()
                        self.TRACKER.completeSubtask()
                finally:
                    if self.FromEmmebankPath != _MODELLER.emmebank.path:
                        sourceBank.dispose()

                #---Load the target network
                with _util.tempExtraAttributeMANAGER(self.ToScenario,
                                                     'NODE') as targetTempAtt:
                    if self.ToNodesToIgnoreSelector:
                        self._ApplySelector(self.ToScenario, targetTempAtt,
                                            self.ToNodesToIgnoreSelector)
                    self.TRACKER.completeSubtask()

                    targetNetwork = self.ToScenario.get_network()
                    self.TRACKER.completeSubtask()

            #---Determine the attributes to copy over
            attsToCopy = self._DetermineAttributesToCopy(
                sourceNetwork, targetNetwork)
            self.TRACKER.completeSubtask()
            self.TRACKER.completeTask()

            #---Get the source nodes to be copied
            zonesToCopy = [
                zone for zone in sourceNetwork.centroids()
                if zone[sourceTempAtt.id] == 0
            ]
            _m.logbook_write("Found %s zones to copy over." % len(zonesToCopy))

            #---Clear un-flagged zones from the target network
            if self.ClearTargetZonesFlag:
                with _m.logbook_trace("Clearing zones from target network"):
                    zonesToDelete = [
                        zone.number for zone in targetNetwork.centroids()
                    ]
                    for id in zonesToDelete:
                        targetNetwork.delete_node(id, cascade=True)
                    _m.logbook_write("%s zones deleted from target network" %
                                     len(zonesToDelete))

            #---Run the matching process
            if self.MatchOption == 1:
                message = "Copying zones using node coordinates"
                func = self._GetMatchByGeometryLambda(targetNetwork)
            elif self.MatchOption == 2:
                message = "Copying zones using node IDs"
                func = self._GetMatchByIdLambda(targetNetwork)
            else:
                raise KeyError("Unrecognized match option", self.MatchOption)
            with _m.logbook_trace(message):
                count, uncopiedConnectors = self._NewCopyZones(
                    zonesToCopy, targetNetwork, targetTempAtt, attsToCopy,
                    func)
                self._WriteLogbookReport(uncopiedConnectors)

            _m.logbook_write("Copied over %s connectors from source scenario" %
                             count)
            _m.logbook_write("%s connectors could not be copied" %
                             len(uncopiedConnectors))

            #---Write  shapefile
            if len(uncopiedConnectors) > 0 and self.ShapefileReport:
                self._WriteShapefile(uncopiedConnectors)
                _m.logbook_write("Wrote shapefile to %s" %
                                 self.ShapefileReport)
            self.TRACKER.completeTask()

            #---Publish the network
            if self.PublishNetworkFlag:
                self.ToScenario.publish_network(targetNetwork, True)
                _MODELLER.desktop.refresh_needed(True)
            self.TRACKER.completeTask()