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;
}
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;
}
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;
}
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;
}
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;
}
Aggregations