def import_data_to_db(fips=[]): """ Imports shape file data into the database, requires shp2pgsql to be avialable on the path (it is called via os.popen) :param fips: An optional list of state fips codes (integers) to import. :return: """ for data_dir in (PLACES_DIR, ROADS_DIR): for f in os.listdir(data_dir): if f.endswith(".shp"): # If we don't have a fips list specified, # OR our file name contains one of the fips codes we care about... if fips == [] or True in ["_" + str(x) + "_" in f for x in fips]: command = "shp2pgsql -s 4269 -a -W latin1 {0} gis.{1}"\ .format(data_dir + os.path.sep + f, data_dir.split(os.path.sep)[-1]) DEFAULT_LOGGER.info("Running " + command) import_lines = os.popen(command).readlines() DEFAULT_LOGGER.info("Importing {0} values into database".format(str(len(import_lines)))) execute_import_statements(import_lines) vacuum_full()
def wrapper(*args, **kwargs): conn = None c = None try: # The calling function might want to supply the connections themselves and close them later. if 'connection' not in kwargs: conn = get_connection() kwargs['connection'] = conn if 'cursor' not in kwargs: c = kwargs['connection'].cursor() kwargs['cursor'] = c return function(*args, **kwargs) except Exception as e: DEFAULT_LOGGER.error("Error running DB query: " + str(e)) finally: if c is not None: c.close() if conn is not None: conn.close()
def get_graph(): try: g = RoadGraph.load_graph("graph.pickle") return g except: DEFAULT_LOGGER.error("Could not load graph")