Exemplo n.º 1
0
    def __init__(self, config, query_path, sql_subs, db_connection_string):
        """Sets up a new category of BNA destinations and retrieves data from
        the given db table

        config -- dictionary of config settings (usually from yaml passed to parent BNA object)
        query_path -- path to the SQL file for destination calculations
        sql_subs -- dictionary of SQL substitutes from the main BNA

        return: None
        """
        DBUtils.__init__(self, db_connection_string)
        self.config = config
        self.query_path = query_path
        self.category = self.config["name"]
        self.schema, self.table = self.parse_table_name(self.config["table"])
        if self.schema is None:
            self.schema = self.get_schema(self.table)

        self.method = self.config["method"]

        self.hs_column_name = self.category + "_hs"
        self.ls_column_name = self.category + "_ls"
        self.score_column_name = self.category + "_score"

        # self.set_destinations()
        self.query = self._choose_query(config, sql_subs)
Exemplo n.º 2
0
    def __init__(self,
                 config=None,
                 verbose=False,
                 debug=False,
                 host=None,
                 db_name=None,
                 user=None,
                 password=None):
        """
        Reads the config file and sets up a connection to the database

        args
        config -- path to the config file
        verbose -- output useful messages
        debug -- set to debug mode
        host -- hostname or address (overrides the config file if given)
        db -- name of database on server (overrides the config file if given)
        user -- username to connect to database (overrides the config file if given)
        password -- password to connect to database (overrides the config file if given)
        """
        Conf.__init__(self)
        self.verbose = verbose
        self.debug = debug
        self.module_dir = os.path.dirname(os.path.abspath(__file__))
        if config is None:
            config = os.path.join(self.module_dir, "config.yaml")
        self.config = self.parse_config(yaml.safe_load(open(config)))
        print("Connecting to database")
        if host is None:
            host = self.config.db.host
        if db_name is None:
            db_name = self.config.db.dbname
        if user is None:
            user = self.config.db.user
        if password is None:
            password = self.config.db.password
        db_connection_string = " ".join([
            "dbname=" + db_name, "user="******"host=" + host,
            "password="******"DB connection: %s" % db_connection_string)
        DBUtils.__init__(self, db_connection_string, self.verbose, self.debug)
        self.sql_subs = self.make_bna_substitutions(self.config)

        # mi/km
        if "units" in self.config:
            if self.config.units == "mi":
                self.km = False
            elif self.config.units == "km":
                self.km = True
            else:
                raise ValueError("Invalid units \"{}\" in config".format(
                    self.config.units))
        else:
            self.km = False
Exemplo n.º 3
0
 def __init__(self):
     DBUtils.__init__(self,"")
     self.config = None
     self.net_config = None
     self.verbose = None
     self.debug = None
     self.srid = None
     self.net_blocks = None
     self.module_dir = None
     self.db_connectivity_table = None
     self.db_connection_string = None
Exemplo n.º 4
0
 def __init__(self):
     DBUtils.__init__(self, "")
     self.segment_subs = None
Exemplo n.º 5
0
    def __init__(self,
                 config=None,
                 force_net_build=False,
                 verbose=False,
                 debug=False,
                 host=None,
                 db_name=None,
                 user=None,
                 password=None):
        """Connects to the BNA database

        kwargs:
        config -- path to the config file, if not given use the default config.yaml
        force_net_build -- force a rebuild of the network even if an existing one is found
        verbose -- output useful messages
        debug -- set to debug mode
        host -- hostname or address (overrides the config file if given)
        db -- name of database on server (overrides the config file if given)
        user -- username to connect to database (overrides the config file if given)
        password -- password to connect to database (overrides the config file if given)

        return: pyBNA object
        """
        Destinations.__init__(self)
        Connectivity.__init__(self)
        Core.__init__(self)
        Conf.__init__(self)
        self.verbose = verbose
        self.debug = debug
        self.module_dir = os.path.dirname(os.path.abspath(__file__))
        if config is None:
            config = os.path.join(self.module_dir, "config.yaml")
        self.config = self.parse_config(yaml.safe_load(open(config)))
        self.config["bna"]["connectivity"]["max_detour"] = float(
            100 + self.config["bna"]["connectivity"]["max_detour"]) / 100
        self.db_connectivity_table = self.config["bna"]["connectivity"][
            "table"]
        self.net_config = self.config["bna"]["network"]

        # km/mi
        if "units" in self.config:
            if self.config.units == "mi":
                self.km = False
            elif self.config.units == "km":
                self.km = True
            else:
                raise ValueError("Invalid units \"{}\" in config".format(
                    self.config.units))
        else:
            self.km = False

        if self.verbose:
            print("")
            print("---------------pyBNA---------------")
            print("   Create and test BNA scenarios")
            print("-----------------------------------")
            print("")

        # set up db connection
        print("Connecting to database")
        if host is None:
            host = self.config["db"]["host"]
        if db_name is None:
            db_name = self.config["db"]["dbname"]
        if user is None:
            user = self.config["db"]["user"]
        if password is None:
            password = self.config["db"]["password"]
        db_connection_string = " ".join([
            "dbname=" + db_name, "user="******"host=" + host,
            "password="******"DB connection: %s" % db_connection_string)
        DBUtils.__init__(self, db_connection_string, self.verbose, self.debug)

        # srid
        if "srid" in self.config:
            self.srid = self.config["srid"]
        elif not self.debug:
            self.srid = self.get_srid(self.config.bna.blocks.table)

        # destinations
        self.destinations = dict()
        self.destination_blocks = set()
        if not self.debug:
            pass
            # self.set_destinations()

        self.sql_subs = self.make_bna_substitutions(self.config)

        if force_net_build:
            print("Building network tables in database")
            self.build_network()
        elif self.debug:
            pass
        elif not self.check_network():
            print("Network tables not found in database...building")
            self.build_network()
        elif self.verbose:
            print("Network tables found in database")
Exemplo n.º 6
0
    def __init__(self, config=None, create_lookups=True, verbose=False):
        """
        Reads the config file, sets up a connection

        args
        config -- path to the config file, if not given use the default config.yaml
        create_lookups -- creates lookup tables in the db if none are found
        verbose -- output useful messages
        """
        Conf.__init__(self)
        self.verbose = verbose
        self.module_dir = os.path.dirname(os.path.abspath(__file__))
        if config is None:
            config = os.path.join(self.module_dir, "config.yaml")
        self.config = self.parse_config(yaml.safe_load(open(config)))
        print("Connecting to database")
        host = self.config.db.host
        db_name = self.config.db.dbname
        user = self.config.db.user
        password = self.config.db.password
        db_connection_string = " ".join([
            "dbname=" + db_name, "user="******"host=" + host,
            "password="******"Checking lookup tables")
        missing = self._missing_lookup_tables()
        if create_lookups and len(missing) > 0:
            for t in missing:
                self._create_lookup_table(*t)

        # add functions to db
        self._run_sql_script("bna_CompareAzimuths.sql",
                             dict(),
                             dirs=["sql", "stress", "db_functions"])
        self._run_sql_script("bna_IsCorridor.sql",
                             dict(),
                             dirs=["sql", "stress", "db_functions"])
        self._run_sql_script("bna_MultiEndPoint.sql",
                             dict(),
                             dirs=["sql", "stress", "db_functions"])
        self._run_sql_script("bna_MultiStartPoint.sql",
                             dict(),
                             dirs=["sql", "stress", "db_functions"])

        # build SQL substitutions
        self.segment_subs = dict()
        for direction in [FORWARD_DIRECTION, BACKWARD_DIRECTION]:
            self.segment_subs[
                direction] = self._build_segment_sql_substitutions(direction)

        self.crossing_subs = dict()
        for direction in [FORWARD_DIRECTION, BACKWARD_DIRECTION]:
            self.crossing_subs[
                direction] = self._build_crossing_sql_substitutions(direction)
Exemplo n.º 7
0
 def __init__(self):
     DBUtils.__init__(self, "")
     self.config = None
     self.verbose = None
     self.debug = None
     self.srid = None