cat-cfs / twobilliontoolkit

This repo stands as a singular place for all the tools that will be developed for the processing of 2 Billion Trees data and information
MIT License
1 stars 0 forks source link

Improve Network Transfer GDB interaction #41

Closed AnthonyRodway closed 2 months ago

AnthonyRodway commented 3 months ago

There was a few errors while trying to process data involving the Network Transfer tool and the output GDBs. Andrea has made some changes below and to answer the questions in the code:

These two things need to be addressed and fixed.

def merge_gdbs(src_gdb: str, dest_gdb: str, log_path: str = None) -> None:
    """
    Merge source Geodatabase into destination Geodatabase.

    Args:
        src_gdb (str): Path to the source Geodatabase.
        dest_gdb (str): Path to the destination Geodatabase.
        log_path (str): path to the log file.
    """
    # Set the workplace for the source geodatabase
    arcpy.env.workspace = src_gdb
    try:

        # Copy the whole GDB if it does not exist
        if not arcpy.Exists(dest_gdb):
            if not os.path.exists(os.path.dirname(dest_gdb)):
                os.mkdir(os.path.dirname(dest_gdb))

            arcpy.management.Copy(
                src_gdb,
                dest_gdb
            )
            log(None, Colors.INFO, f'Copy to {dest_gdb} has completed.')
            return

    except Exception as error:
        log(log_path, Colors.ERROR, f'An error has been caught while trying to copy the geodatabase to {dest_gdb}: {error}')
        return # do we need to return here or does the program fail?

    # Get a list of feature classes in the source geodatabase
    feature_classes = arcpy.ListFeatureClasses()
    for feature_class in feature_classes:
        try:
            # Skip if already exists in destination
            if arcpy.Exists(os.path.join(dest_gdb, feature_class)):
                continue      

            arcpy.management.Copy(
                os.path.join(src_gdb, feature_class),
                os.path.join(dest_gdb, feature_class)
            )

            log(None, Colors.INFO, f'Merging to {dest_gdb} has completed.')
        except Exception as error:
            log(log_path, Colors.ERROR, f'An error has been caught while trying to merge the geodatabase to {dest_gdb}: {error}')
            #TODO if error occurs while copying an individual file then do not delete the C:/Local... GDB
AnthonyRodway commented 3 months ago

Changes have been made in this commit: https://github.com/cat-cfs/twobilliontoolkit/commit/3a9437884744131ec3a95e967dbc1a5002d5cfaa

Changes: