Search in sources :

Example 6 with CountryBoundaryMap

use of org.openstreetmap.atlas.geography.boundary.CountryBoundaryMap in project atlas by osmlab.

the class AtlasDebugTool method onRun.

@Override
protected int onRun(final CommandMap command) {
    final File pbf = (File) command.get(PBF);
    final File atlasFile = (File) command.get(ATLAS);
    final File geojson = (File) command.get(GEOJSON);
    final File text = (File) command.get(TEXT);
    final java.io.File boundaryFile = (java.io.File) command.get(BOUNDARY);
    final String country = (String) command.get(COUNTRY);
    final Rectangle bound = (Rectangle) command.get(BOUND);
    final MultiPolygon inputMultipolygon = (MultiPolygon) command.get(MULTIPOLYGON);
    @SuppressWarnings("unchecked") final List<Location> startEndRoute = (List<Location>) command.get(ROUTE);
    Atlas atlas;
    if (pbf != null && pbf.exists()) {
        final AtlasLoadingOption option;
        MultiPolygon multiPolygon = MultiPolygon.forPolygon(Rectangle.MAXIMUM);
        if (boundaryFile != null) {
            final CountryBoundaryMap boundaryMap = CountryBoundaryMap.fromShapeFile(boundaryFile);
            option = AtlasLoadingOption.createOptionWithAllEnabled(boundaryMap);
            if (country != null) {
                if (new CountryListTwoWayStringConverter().convert(country).size() == 1) {
                    multiPolygon = boundaryMap.countryBoundary(country).get(0).getBoundary();
                }
                option.setCountryCode(country);
            }
        } else {
            option = AtlasLoadingOption.createOptionWithNoSlicing();
        }
        if (bound != null) {
            multiPolygon = MultiPolygon.forPolygon(bound);
        }
        if (inputMultipolygon != null) {
            multiPolygon = inputMultipolygon;
        }
        atlas = new RawAtlasGenerator(pbf, option, multiPolygon).build();
        if (option.isCountrySlicing()) {
            atlas = new RawAtlasSlicer(option, atlas).slice();
        }
        atlas = new WaySectionProcessor(atlas, option).run();
        atlas.save(atlasFile);
    } else if (atlasFile != null && atlasFile.exists()) {
        atlas = new AtlasResourceLoader().load(atlasFile);
    } else {
        logger.error("Must have at least one source, -pbf or -atlas");
        atlas = null;
        System.exit(1);
    }
    logger.info("Loaded {}", atlas.summary());
    if (geojson != null) {
        atlas.saveAsGeoJson(geojson);
    }
    if (text != null) {
        atlas.saveAsText(text);
    }
    if (startEndRoute != null) {
        logger.info("Route between {} and {} = {}", startEndRoute.get(0), startEndRoute.get(1), AStarRouter.dijkstra(atlas, Distance.TEN_MILES).route(startEndRoute.get(0), startEndRoute.get(1)));
    }
    return 0;
}
Also used : Atlas(org.openstreetmap.atlas.geography.atlas.Atlas) WaySectionProcessor(org.openstreetmap.atlas.geography.atlas.raw.sectioning.WaySectionProcessor) Rectangle(org.openstreetmap.atlas.geography.Rectangle) RawAtlasGenerator(org.openstreetmap.atlas.geography.atlas.raw.creation.RawAtlasGenerator) AtlasResourceLoader(org.openstreetmap.atlas.geography.atlas.AtlasResourceLoader) MultiPolygon(org.openstreetmap.atlas.geography.MultiPolygon) ArrayList(java.util.ArrayList) List(java.util.List) StringList(org.openstreetmap.atlas.utilities.collections.StringList) AtlasLoadingOption(org.openstreetmap.atlas.geography.atlas.pbf.AtlasLoadingOption) CountryBoundaryMap(org.openstreetmap.atlas.geography.boundary.CountryBoundaryMap) CountryListTwoWayStringConverter(org.openstreetmap.atlas.geography.boundary.converters.CountryListTwoWayStringConverter) RawAtlasSlicer(org.openstreetmap.atlas.geography.atlas.raw.slicing.RawAtlasSlicer) File(org.openstreetmap.atlas.streaming.resource.File) Location(org.openstreetmap.atlas.geography.Location)

Example 7 with CountryBoundaryMap

use of org.openstreetmap.atlas.geography.boundary.CountryBoundaryMap in project atlas by osmlab.

the class OsmPbfToAtlasSubCommand method getCountryBoundaryMap.

/**
 * Get or create a {@link CountryBoundaryMap}. If the country-boundary-map parameter is set,
 * this will attempt to load the text or shape file from that parameter. Else, this will load
 * using the entire world as the country UNK (unknown).
 *
 * @param map
 *            {@link CommandMap} containing the {@code COUNTRY_MAP_PARAMETER}
 * @return {@link CountryBoundaryMap} loaded from a file or default
 */
private CountryBoundaryMap getCountryBoundaryMap(final CommandMap map) {
    final Optional<File> countryMapOption = (Optional<File>) map.getOption(COUNTRY_MAP_PARAMETER);
    CountryBoundaryMap countryMap = CountryBoundaryMap.fromBoundaryMap(Collections.singletonMap("UNK", MultiPolygon.MAXIMUM));
    if (countryMapOption.isPresent()) {
        final File countryMapFile = countryMapOption.get();
        if (FilenameUtils.isExtension(countryMapFile.getName(), "txt")) {
            countryMap = CountryBoundaryMap.fromPlainText(countryMapFile);
        } else if (FilenameUtils.isExtension(countryMapFile.getName(), "shp")) {
            countryMap = CountryBoundaryMap.fromShapeFile(countryMapFile.getFile());
        }
    }
    return countryMap;
}
Also used : Optional(java.util.Optional) CountryBoundaryMap(org.openstreetmap.atlas.geography.boundary.CountryBoundaryMap) File(org.openstreetmap.atlas.streaming.resource.File)

Example 8 with CountryBoundaryMap

use of org.openstreetmap.atlas.geography.boundary.CountryBoundaryMap in project atlas by osmlab.

the class CountryBoundaryMapGeoJsonConverter method convert.

@Override
public JsonObject convert(final CountryBoundaryMap map) {
    final MultiMap<String, Polygon> countryNameToBoundaryMap = map.getCountryNameToBoundaryMap();
    final JsonObject featureCollectionObject = new JsonObject();
    featureCollectionObject.addProperty("type", "FeatureCollection");
    final JsonArray features = new JsonArray();
    for (final Map.Entry<String, List<Polygon>> entry : countryNameToBoundaryMap.entrySet()) {
        final String countryCode = entry.getKey();
        final List<Polygon> polygons = entry.getValue();
        if ((this.countryDenyList != null && this.countryDenyList.contains(countryCode)) || (this.countryAllowList != null && !this.countryAllowList.contains(countryCode))) {
            continue;
        }
        polygons.forEach(polygon -> {
            final JsonObject featureObject = new JsonObject();
            featureObject.addProperty("type", "Feature");
            if (this.usePolygons) {
                final org.openstreetmap.atlas.geography.Polygon atlasPolygon = new JtsPolygonConverter().backwardConvert(polygon);
                featureObject.add("geometry", atlasPolygon.asGeoJsonGeometry());
            } else {
                final org.openstreetmap.atlas.geography.Polygon atlasPolygon = new JtsPolygonConverter().backwardConvert(polygon);
                featureObject.add("geometry", new PolyLine(atlasPolygon.closedLoop()).asGeoJsonGeometry());
            }
            final JsonObject propertiesObject = new JsonObject();
            propertiesObject.addProperty("iso_country_code", countryCode);
            featureObject.add("properties", propertiesObject);
            features.add(featureObject);
        });
        logger.trace("Finished processing polygons for {}", countryCode);
    }
    featureCollectionObject.add("features", features);
    return featureCollectionObject;
}
Also used : JtsPolygonConverter(org.openstreetmap.atlas.geography.converters.jts.JtsPolygonConverter) JsonObject(com.google.gson.JsonObject) PolyLine(org.openstreetmap.atlas.geography.PolyLine) JsonArray(com.google.gson.JsonArray) List(java.util.List) Polygon(org.locationtech.jts.geom.Polygon) Map(java.util.Map) CountryBoundaryMap(org.openstreetmap.atlas.geography.boundary.CountryBoundaryMap) MultiMap(org.openstreetmap.atlas.utilities.maps.MultiMap)

Example 9 with CountryBoundaryMap

use of org.openstreetmap.atlas.geography.boundary.CountryBoundaryMap in project atlas by osmlab.

the class WKTShardCommand method loadCountryBoundaryMap.

private Optional<CountryBoundaryMap> loadCountryBoundaryMap() {
    final Optional<CountryBoundaryMap> countryBoundaryMap;
    final File boundaryMapFile = new File(this.optionAndArgumentDelegate.getOptionArgument(COUNTRY_BOUNDARY_OPTION_LONG).orElseThrow(AtlasShellToolsException::new), this.getFileSystem());
    if (!boundaryMapFile.exists()) {
        this.outputDelegate.printlnErrorMessage("boundary file " + boundaryMapFile.getAbsolutePathString() + " does not exist");
        return Optional.empty();
    }
    if (this.optionAndArgumentDelegate.hasVerboseOption()) {
        this.outputDelegate.printlnCommandMessage("loading country boundary map...");
    }
    countryBoundaryMap = Optional.of(CountryBoundaryMap.fromPlainText(boundaryMapFile));
    if (this.optionAndArgumentDelegate.hasVerboseOption()) {
        this.outputDelegate.printlnCommandMessage("loaded boundary map");
    }
    return countryBoundaryMap;
}
Also used : CountryBoundaryMap(org.openstreetmap.atlas.geography.boundary.CountryBoundaryMap) File(org.openstreetmap.atlas.streaming.resource.File)

Example 10 with CountryBoundaryMap

use of org.openstreetmap.atlas.geography.boundary.CountryBoundaryMap in project atlas by osmlab.

the class CountryShardToBoundsCommand method executeCountryContext.

private int executeCountryContext() {
    final CountryBoundaryMap countryBoundaryMap;
    final File boundaryMapFile = new File(this.optionAndArgumentDelegate.getOptionArgument(COUNTRY_BOUNDARY_OPTION_LONG).orElseThrow(AtlasShellToolsException::new), this.getFileSystem());
    if (!boundaryMapFile.exists()) {
        this.outputDelegate.printlnErrorMessage("boundary file " + boundaryMapFile.getAbsolutePathString() + " does not exist");
        return 1;
    }
    if (this.optionAndArgumentDelegate.hasVerboseOption()) {
        this.outputDelegate.printlnCommandMessage("loading country boundary map...");
    }
    countryBoundaryMap = CountryBoundaryMap.fromPlainText(boundaryMapFile);
    if (this.optionAndArgumentDelegate.hasVerboseOption()) {
        this.outputDelegate.printlnCommandMessage("loaded boundary map");
    }
    final List<String> countryCodes = this.optionAndArgumentDelegate.getVariadicArgument(COUNTRY);
    for (int i = 0; i < countryCodes.size(); i++) {
        final String countryCode = countryCodes.get(i).toUpperCase();
        this.outputDelegate.printlnStdout(countryCode + " boundary:", TTYAttribute.BOLD);
        final List<CountryBoundary> boundaries = countryBoundaryMap.countryBoundary(countryCode);
        if (boundaries == null || boundaries.isEmpty()) {
            this.outputDelegate.printlnWarnMessage("no boundaries found for " + countryCode);
        } else {
            for (final CountryBoundary boundary : boundaries) {
                this.outputDelegate.printlnStdout(boundary.getBoundary().toWkt(), TTYAttribute.GREEN);
            }
        }
        if (i < countryCodes.size() - 1) {
            this.outputDelegate.printlnStdout("");
        }
    }
    return 0;
}
Also used : CountryBoundary(org.openstreetmap.atlas.geography.boundary.CountryBoundary) CountryBoundaryMap(org.openstreetmap.atlas.geography.boundary.CountryBoundaryMap) File(org.openstreetmap.atlas.streaming.resource.File)

Aggregations

CountryBoundaryMap (org.openstreetmap.atlas.geography.boundary.CountryBoundaryMap)27 Test (org.junit.Test)11 Atlas (org.openstreetmap.atlas.geography.atlas.Atlas)11 File (org.openstreetmap.atlas.streaming.resource.File)10 InputStreamResource (org.openstreetmap.atlas.streaming.resource.InputStreamResource)9 MultiPolygon (org.openstreetmap.atlas.geography.MultiPolygon)6 RawAtlasGenerator (org.openstreetmap.atlas.geography.atlas.raw.creation.RawAtlasGenerator)6 RawAtlasSlicer (org.openstreetmap.atlas.geography.atlas.raw.slicing.RawAtlasSlicer)6 StringList (org.openstreetmap.atlas.utilities.collections.StringList)6 AtlasLoadingOption (org.openstreetmap.atlas.geography.atlas.pbf.AtlasLoadingOption)5 WaySectionProcessor (org.openstreetmap.atlas.geography.atlas.raw.sectioning.WaySectionProcessor)5 StandardConfiguration (org.openstreetmap.atlas.utilities.configuration.StandardConfiguration)5 Time (org.openstreetmap.atlas.utilities.time.Time)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 Optional (java.util.Optional)4 CountryShard (org.openstreetmap.atlas.geography.sharding.CountryShard)4 Resource (org.openstreetmap.atlas.streaming.resource.Resource)4 StringResource (org.openstreetmap.atlas.streaming.resource.StringResource)4 HashMap (java.util.HashMap)3