All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.openstreetmap.atlas.geography.boundary.CountryBoundaryMapCompareCommand Maven / Gradle / Ivy

There is a newer version: 7.0.8
Show newest version
package org.openstreetmap.atlas.geography.boundary;

import java.util.Set;

import org.junit.Assert;
import org.openstreetmap.atlas.streaming.resource.File;
import org.openstreetmap.atlas.utilities.runtime.Command;
import org.openstreetmap.atlas.utilities.runtime.CommandMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Compares given {@link CountryBoundaryMap}s to find if there are any differences between them.
 *
 * @author mkalender
 */
public class CountryBoundaryMapCompareCommand extends Command
{
    private static final Logger logger = LoggerFactory
            .getLogger(CountryBoundaryMapCompareCommand.class);

    // Parameters
    private static final Switch BASELINE_MAP = new Switch<>("baseline",
            "Path to file generated by CountryBoundaryMap to use as a baseline",
            path -> CountryBoundaryMap.fromPlainText(new File(path)), Optionality.REQUIRED);
    private static final Switch OTHER_MAP = new Switch<>("new",
            "Path to file generated by CountryBoundaryMap to compare against baseline",
            path -> CountryBoundaryMap.fromPlainText(new File(path)), Optionality.REQUIRED);

    public static void main(final String[] args)
    {
        new CountryBoundaryMapCompareCommand().run(args);
    }

    @Override
    protected int onRun(final CommandMap command)
    {
        // Read inputs
        final CountryBoundaryMap baselineMap = (CountryBoundaryMap) command.get(BASELINE_MAP);
        final CountryBoundaryMap newMap = (CountryBoundaryMap) command.get(OTHER_MAP);

        // Compare loaded countries
        final Set baselineCountries = baselineMap.getLoadedCountries();
        final Set newCountries = newMap.getLoadedCountries();
        baselineCountries.forEach(country -> Assert.assertTrue(
                String.format("New map is missing country %s.", country),
                newCountries.contains(country)));
        newCountries.forEach(country -> Assert.assertTrue(
                String.format("New map has additional country %s.", country),
                baselineCountries.contains(country)));
        Assert.assertEquals("Map sizes are not equal.", baselineMap.size(), newMap.size());

        // Compare country boundaries
        baselineMap.getLoadedCountries().forEach(country -> baselineMap.countryBoundary(country)
                .forEach(countryBoundary -> Assert.assertTrue(
                        String.format("New map is missing a boundary for %s.", country),
                        newMap.countryBoundary(country).contains(countryBoundary.getBoundary()))));

        newMap.getLoadedCountries()
                .forEach(country -> newMap.countryBoundary(country)
                        .forEach(countryBoundary -> Assert.assertTrue(
                                String.format("New map is missing a boundary for %s.", country),
                                baselineMap.countryBoundary(country)
                                        .contains(countryBoundary.getBoundary()))));
        logger.info("Compared given maps and found no difference.");
        return 0;
    }

    @Override
    protected SwitchList switches()
    {
        return new SwitchList().with(BASELINE_MAP, OTHER_MAP);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy