示例#1
0
    def insertableTravelTimeMatrixGeoDataFrame(self, travelTimeMatrix,
                                               tableName, column1, column2):

        sql = "select * from %s where %s = %s and %s = %s" % (
            tableName, column1, "%s", column2, "%s")

        self.travelTimeMatrixCopy = travelTimeMatrix.copy()

        ################################################################################################################
        # for index, row in travelTimeMatrix.iterrows():
        #     travelTimeMatrixCopy = verifyPairOfPointsExistence(row, sql, travelTimeMatrixCopy)
        ################################################################################################################

        with Parallel(
                n_jobs=int(
                    getConfigurationProperties(
                        section="PARALLELIZATION")["jobs"]),
                backend="threading",
                verbose=int(
                    getConfigurationProperties(
                        section="PARALLELIZATION")["verbose"])) as parallel:
            returns = parallel(
                delayed(verifyPairOfPointsExistence)(self, row[column1],
                                                     row[column2], sql)
                for index, row in travelTimeMatrix.iterrows())

        return self.travelTimeMatrixCopy
def selectIdsQueryParallelized(self, inputFilesList, searchIDs, searchColumn,
                               sep):
    ''' Searches YKR-IDs from files based on inputIDs (YKR-ID) from the "inputFilesList" and returns a pandas DataFrame from the results '''
    self.selectedData = pd.DataFrame()

    with Parallel(n_jobs=getConfigurationProperties(
            section="PARALLELIZATION")["jobs"],
                  backend="threading",
                  verbose=getConfigurationProperties(
                      section="PARALLELIZATION")["verbose"]) as parallel:
        # while len(verticesID) <= len(geojson["features"]):
        returns = parallel(
            delayed(appendSelectedData)(self, file, searchColumn, searchIDs,
                                        sep) for file in inputFilesList)

    data = self.selectedData
    self.selectedData = None
    return data
示例#3
0
    def getEngine(self):
        if not self.__engine:
            config = getConfigurationProperties(section="DATABASE_CONFIG")
            # engine = create_engine('postgresql://<yourUserName>:postgres@localhost:5432/postgres', echo=False)
            self.__engine = create_engine(
                'postgresql://%s:%s@%s/%s' %
                (config["user"], config["password"], config["host"],
                 config["database_name"]),
                echo=False)

        return self.__engine
示例#4
0
    def getConnection(self):
        """
        Creates a new connection to the pg_database

        :return: New connection.
        """
        config = getConfigurationProperties(section="DATABASE_CONFIG")
        con = psycopg2.connect(database=config["database_name"],
                               user=config["user"],
                               password=config["password"],
                               host=config["host"])
        return con
def runTravelTimeMatrixOperations(querying, uploading, outputFolder, zippath,
                                  directionality, targets):
    try:
        comparison = Comparison()
        postGISServiceProvider = PostGISServiceProvider()
        spatialPatterns = SpatialPatterns(
            comparison=comparison,
            postGISServiceProvider=postGISServiceProvider)
        fileActions = FileActions()

        attributes = getConfigurationProperties(section="ATTRIBUTES_MAPPING")
        columns = {}
        columnList = []
        for attribute_key in attributes:
            attribute_splitted = attributes[attribute_key].split(",")
            key = attribute_splitted[0]
            value = attribute_splitted[1]
            columns[key] = value
            columnList.append(value)

        tableName = getConfigurationProperties(
            section="DATABASE_CONFIG")["travel_time_table_name"]

        if uploading:
            log_filename = "uploading_" + os.path.basename(zippath).split(
                ".")[-2]
            Logger.configureLogger(outputFolder, log_filename)

            try:
                zip_ref = zipfile.ZipFile(zippath, 'r')

                members = zip_ref.namelist()
                Logger.getInstance().info("%s geojson files to be uploaded." %
                                          (len(members)))
                for member in members:
                    extractZipFile(zip_ref, member, outputFolder)
                    f = os.path.join(outputFolder, member)
                    isExecuted = spatialPatterns.insertData(
                        f, tableName, tuple(columnList), outputFolder)
                    os.remove(f)

                    if not isExecuted:
                        raise NotUploadedTravelTimeMatrixException(member)

            finally:
                zip_ref.close()

            Logger.getInstance().info("Uploaded: %s" % zippath)

        if querying:
            targetList = targets.split(",")
            for target in targetList:
                log_filename = "querying_travel_time_matrix_%s_%s" % (
                    directionality, target)
                Logger.configureLogger(outputFolder, log_filename)

                traveltimeMatrixFilename = "travel_time_matrix_%s_%s.geojson" % (
                    directionality, target)

                Logger.getInstance().info("Querying %s: %s" %
                                          (directionality, target))
                if "TO".__eq__(directionality):
                    geodataframe = postGISServiceProvider.getTravelTimeMatrixTo(
                        ykrid=target, tableName=tableName)
                else:
                    geodataframe = postGISServiceProvider.getTravelTimeMatrixFrom(
                        ykrid=target, tableName=tableName)

                geojson = postGISServiceProvider.convertToGeojson(geodataframe)

                fileActions.writeFile(folderPath=outputFolder,
                                      filename=traveltimeMatrixFilename,
                                      data=geojson)
                Logger.getInstance().info(
                    "Find the the travel time matrix geojson file in this path: %s"
                    % (os.path.join(outputFolder, traveltimeMatrixFilename)))

    except Exception as err:
        exc_type, exc_value, exc_traceback = sys.exc_info()
        lines = traceback.format_exception(exc_type, exc_value, exc_traceback)
        Logger.getInstance().exception(''.join('>> ' + line for line in lines))