Exemplo n.º 1
0
def test_insert_row_master():
    """ One row has inserted in master, no changes in WP """
    config_file = os.path.join(this_dir, "config-farm-basic.yml")
    tmp_dir_1 = _make_initial_farm_work_packages(config_file)
    tmp_dir_2 = _prepare_next_run_work_packages(tmp_dir_1)

    # modify master dataset - add a row
    open_layer_and_create_feature(
        os.path.join(tmp_dir_2.name, "input", "master.gpkg"),
        "trees",
        "POINT(9 19)",
        {
            "tree_species_id": 1,
            "farm_id": 4
        },
    )

    # run work packaging
    wp_config = load_config_from_yaml(config_file)
    make_work_packages(tmp_dir_2.name, wp_config)
    output_dir = os.path.join(tmp_dir_2.name, "output")

    # there should be one new tree in master and one new tree for Kyle
    _assert_row_counts(os.path.join(output_dir, "master.gpkg"),
                       expected_farms=4,
                       expected_trees=10)
    _assert_row_counts(os.path.join(output_dir, "Kyle.gpkg"),
                       expected_farms=1,
                       expected_trees=3)
    _assert_row_counts(os.path.join(output_dir, "Emma.gpkg"),
                       expected_farms=2,
                       expected_trees=6)
    _assert_row_exists(os.path.join(output_dir, "Kyle.gpkg"), "trees", 1000002)
Exemplo n.º 2
0
def test_update_row_master():
    """ One row has been updated in master, no changes in WP """
    config_file = os.path.join(this_dir, "config-farm-basic.yml")
    tmp_dir_1 = _make_initial_farm_work_packages(config_file)
    tmp_dir_2 = _prepare_next_run_work_packages(tmp_dir_1)

    # modify master dataset - update a tree (master fid 9 mapped to 1000001 for Kyle)
    open_layer_and_update_feature(
        os.path.join(tmp_dir_2.name, "input", "master.gpkg"), "trees", 9,
        {"age_years": 20})

    # run work packaging
    wp_config = load_config_from_yaml(config_file)
    make_work_packages(tmp_dir_2.name, wp_config)
    output_dir = os.path.join(tmp_dir_2.name, "output")

    # there should be the same number of rows as initially
    # and updated age both in master + kyle
    _assert_row_counts(os.path.join(output_dir, "master.gpkg"),
                       expected_farms=4,
                       expected_trees=9)
    _assert_row_counts(os.path.join(output_dir, "Kyle.gpkg"),
                       expected_farms=1,
                       expected_trees=2)
    _assert_row_counts(os.path.join(output_dir, "Emma.gpkg"),
                       expected_farms=2,
                       expected_trees=6)
    _assert_value_equals(os.path.join(output_dir, "master.gpkg"), "trees", 9,
                         "age_years", 20)
    _assert_value_equals(os.path.join(output_dir, "Kyle.gpkg"), "trees",
                         1000001, "age_years", 20)
Exemplo n.º 3
0
def test_delete_row_master():
    """ One row deleted in master, no changes in WP """
    config_file = os.path.join(this_dir, "config-farm-basic.yml")
    tmp_dir_1 = _make_initial_farm_work_packages(config_file)
    tmp_dir_2 = _prepare_next_run_work_packages(tmp_dir_1)

    # modify a WP dataset - delete a tree (master fid 9 mapped to 1000001 for Kyle)
    open_layer_and_delete_feature(
        os.path.join(tmp_dir_2.name, "input", "master.gpkg"), "trees", 9)

    # run work packaging
    wp_config = load_config_from_yaml(config_file)
    make_work_packages(tmp_dir_2.name, wp_config)
    output_dir = os.path.join(tmp_dir_2.name, "output")

    # there should be one tree missing for master and for Kyle
    _assert_row_counts(os.path.join(output_dir, "master.gpkg"),
                       expected_farms=4,
                       expected_trees=8)
    _assert_row_counts(os.path.join(output_dir, "Kyle.gpkg"),
                       expected_farms=1,
                       expected_trees=1)
    _assert_row_counts(os.path.join(output_dir, "Emma.gpkg"),
                       expected_farms=2,
                       expected_trees=6)
    _assert_row_missing(os.path.join(output_dir, "Kyle.gpkg"), "trees",
                        1000001)
Exemplo n.º 4
0
def _make_initial_farm_work_packages(config_file):
    """
    1. create the initial "farms" dataset
    2. run the WP algorithm with the initial dataset and given config file
    Returns temporary directory object.
    """
    tmp_dir_obj = TemporaryDirectory(prefix="test-mergin-work-packages-")
    tmp_dir = tmp_dir_obj.name
    os.makedirs(os.path.join(tmp_dir, "input"))

    # get data
    create_farm_dataset(os.path.join(tmp_dir, "input", "master.gpkg"))
    # get config
    wp_config = load_config_from_yaml(
        os.path.join(this_dir, "config-farm-basic.yml"))
    # run alg
    make_work_packages(tmp_dir, wp_config)
    return tmp_dir_obj
Exemplo n.º 5
0
        print("Downloading work package project " + wp_mergin + "...")
        mc.download_project(wp_mergin, wp_dir)
        print("Done.")

        shutil.copy(os.path.join(wp_dir, gpkg_path),
                    os.path.join(wp_alg_input_dir, wp_name + ".gpkg"))
    else:
        print("First time encountered WP " + wp_name +
              " - not collecting input")
        wp_new.add(wp_name)

#
# 2. run alg
#

make_work_packages(wp_alg_dir, wp_config)

#
# 3. push data to all projects
#


def push_mergin_project(mc, directory):
    job = mergin.client_push.push_project_async(mc, directory)
    if job is None:
        return False  # there is nothing to push (or we only deleted some files)
    mergin.client_push.push_project_wait(job)
    mergin.client_push.push_project_finalize(job)
    return True