|
| 1 | +/* |
| 2 | + * Copyright 2006-2009, 2017, 2020 United States Government, as represented by the |
| 3 | + * Administrator of the National Aeronautics and Space Administration. |
| 4 | + * All rights reserved. |
| 5 | + * |
| 6 | + * The NASA World Wind Java (WWJ) platform is licensed under the Apache License, |
| 7 | + * Version 2.0 (the "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software distributed |
| 12 | + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 13 | + * CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 14 | + * specific language governing permissions and limitations under the License. |
| 15 | + * |
| 16 | + * NASA World Wind Java (WWJ) also contains the following 3rd party Open Source |
| 17 | + * software: |
| 18 | + * |
| 19 | + * Jackson Parser – Licensed under Apache 2.0 |
| 20 | + * GDAL – Licensed under MIT |
| 21 | + * JOGL – Licensed under Berkeley Software Distribution (BSD) |
| 22 | + * Gluegen – Licensed under Berkeley Software Distribution (BSD) |
| 23 | + * |
| 24 | + * A complete listing of 3rd Party software notices and licenses included in |
| 25 | + * NASA World Wind Java (WWJ) can be found in the WorldWindJava-v2.2 3rd-party |
| 26 | + * notices and licenses PDF found in code directory. |
| 27 | + */ |
| 28 | +package gov.nasa.worldwindx.examples.util; |
| 29 | + |
| 30 | +import gov.nasa.worldwind.cache.BasicDataFileStore; |
| 31 | +import gov.nasa.worldwind.geom.LatLon; |
| 32 | +import gov.nasa.worldwind.geom.Sector; |
| 33 | +import gov.nasa.worldwind.globes.Earth; |
| 34 | +import gov.nasa.worldwind.globes.Globe; |
| 35 | +import gov.nasa.worldwind.layers.BasicLayerFactory; |
| 36 | +import gov.nasa.worldwind.layers.BasicTiledImageLayer; |
| 37 | +import gov.nasa.worldwind.layers.BasicTiledImageLayerBulkDownloader; |
| 38 | +import gov.nasa.worldwind.retrieve.Progress; |
| 39 | + |
| 40 | +import java.io.File; |
| 41 | +import java.io.IOException; |
| 42 | +import java.nio.file.FileVisitResult; |
| 43 | +import java.nio.file.Files; |
| 44 | +import java.nio.file.Path; |
| 45 | +import java.nio.file.SimpleFileVisitor; |
| 46 | +import java.nio.file.attribute.BasicFileAttributes; |
| 47 | +import java.util.StringTokenizer; |
| 48 | + |
| 49 | +public class BulkDownloadCli { |
| 50 | + |
| 51 | + private static class FileUtils extends SimpleFileVisitor<Path> { |
| 52 | + |
| 53 | + @Override |
| 54 | + public FileVisitResult visitFile(Path file, BasicFileAttributes attr) throws IOException { |
| 55 | + Files.delete(file); |
| 56 | + return FileVisitResult.CONTINUE; |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public FileVisitResult postVisitDirectory(Path dir, IOException ex) throws IOException { |
| 61 | + Files.delete(dir); |
| 62 | + return FileVisitResult.CONTINUE; |
| 63 | + } |
| 64 | + |
| 65 | + static void deleteDirectory(File f) throws IOException { |
| 66 | + Files.walkFileTree(Path.of(f.getAbsolutePath()), new FileUtils()); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + private static final String VERSION = "1.1"; |
| 71 | + private static final long OSM_AVERAGE_TILE_SIZE = 15000; |
| 72 | + |
| 73 | + public static String makeSizeDescription(long size) { |
| 74 | + double sizeInMegaBytes = size / 1024 / 1024; |
| 75 | + if (sizeInMegaBytes < 1024) { |
| 76 | + return String.format("%,.1f MB", sizeInMegaBytes); |
| 77 | + } else if (sizeInMegaBytes < 1024 * 1024) { |
| 78 | + return String.format("%,.1f GB", sizeInMegaBytes / 1024); |
| 79 | + } |
| 80 | + return String.format("%,.1f TB", sizeInMegaBytes / 1024 / 1024); |
| 81 | + } |
| 82 | + |
| 83 | + public static void cliDownload(String[] args) { |
| 84 | + System.out.println("WorldWind Bulk Download Tool v" + VERSION); |
| 85 | + Globe globe = new Earth(); |
| 86 | + String usage = "Usage: BulkDownload -sector [centerLat,centerLon,radius meters] -path [path for download] -estimate"; |
| 87 | + String outputPath = null; |
| 88 | + Sector sector = null; |
| 89 | + boolean estimate = false; |
| 90 | + int i = 0; |
| 91 | + while (i < args.length) { |
| 92 | + switch (args[i]) { |
| 93 | + case "-help": |
| 94 | + System.out.println(usage); |
| 95 | + return; |
| 96 | + case "-sector": |
| 97 | + i++; |
| 98 | + StringTokenizer st = new StringTokenizer(args[i], ","); |
| 99 | + if (st.countTokens() != 3) { |
| 100 | + System.out.println("Error: Invalid sector specification."); |
| 101 | + System.out.println(usage); |
| 102 | + return; |
| 103 | + } |
| 104 | + double lat = Double.parseDouble(st.nextToken()); |
| 105 | + double lon = Double.parseDouble(st.nextToken()); |
| 106 | + double radius = Double.parseDouble(st.nextToken()); |
| 107 | + sector = Sector.boundingSector(globe, LatLon.fromDegrees(lat, lon), radius); |
| 108 | + break; |
| 109 | + case "-path": |
| 110 | + i++; |
| 111 | + outputPath = args[i]; |
| 112 | + break; |
| 113 | + case "-estimate": |
| 114 | + estimate = true; |
| 115 | + break; |
| 116 | + default: |
| 117 | + System.out.println("Unknown argument: " + args[i]); |
| 118 | + System.out.println(usage); |
| 119 | + break; |
| 120 | + } |
| 121 | + i++; |
| 122 | + } |
| 123 | + |
| 124 | + if (sector == null) { |
| 125 | + System.out.println("Error: Sector not specified."); |
| 126 | + System.out.println(usage); |
| 127 | + return; |
| 128 | + } |
| 129 | + |
| 130 | + System.out.println("Sector: " + sector); |
| 131 | + BasicLayerFactory factory = new BasicLayerFactory(); |
| 132 | + BasicTiledImageLayer layer = (BasicTiledImageLayer) factory.createFromConfigSource("config/Earth/OpenStreetMap2.xml", null); |
| 133 | + layer.setAverageFileSize(OSM_AVERAGE_TILE_SIZE); |
| 134 | + if (estimate) { |
| 135 | + System.out.println(layer.getName() + " estimated download size: " + makeSizeDescription(layer.getEstimatedMissingDataSize(sector, 0))); |
| 136 | + return; |
| 137 | + } |
| 138 | + |
| 139 | + if (outputPath == null) { |
| 140 | + System.out.println("Error: Output path not specified."); |
| 141 | + System.out.println(usage); |
| 142 | + return; |
| 143 | + } |
| 144 | + |
| 145 | + try { |
| 146 | + String earthPath = outputPath + "/Earth"; |
| 147 | + String osmPath = earthPath + "/OpenStreetMap2"; |
| 148 | + File previousTilePath = new File(earthPath); |
| 149 | + File previousOsmPath = new File(osmPath); |
| 150 | + if (previousTilePath.exists() && previousTilePath.isDirectory() && previousOsmPath.exists()) { |
| 151 | + FileUtils.deleteDirectory(previousTilePath); |
| 152 | + } |
| 153 | + |
| 154 | + BasicDataFileStore cache = new BasicDataFileStore(new File(outputPath)); |
| 155 | + BasicTiledImageLayerBulkDownloader downloadThread = (BasicTiledImageLayerBulkDownloader) layer.makeLocal(sector, 0, cache, null); |
| 156 | + downloadThread.setAverageFileSize(OSM_AVERAGE_TILE_SIZE); |
| 157 | + Progress progress = downloadThread.getProgress(); |
| 158 | + System.out.println("Downloading " + layer.getName() + " Tiles"); |
| 159 | + int lastPercent = -1; |
| 160 | + long lastCurrentSize = -1; |
| 161 | + while (downloadThread.isAlive()) { |
| 162 | + int percent = 0; |
| 163 | + if (progress.getTotalCount() > 0) { |
| 164 | + percent = (int) ((float) progress.getCurrentCount() / progress.getTotalCount() * 100f); |
| 165 | + } |
| 166 | + if (lastPercent != percent || lastCurrentSize != progress.getCurrentSize()) { |
| 167 | + lastPercent = percent; |
| 168 | + lastCurrentSize = progress.getCurrentSize(); |
| 169 | + String text = percent + "% "; |
| 170 | + text += " (" + makeSizeDescription(lastCurrentSize) |
| 171 | + + " / " + makeSizeDescription(progress.getTotalSize()) |
| 172 | + + ")"; |
| 173 | + System.out.println(text); |
| 174 | + } |
| 175 | + Thread.sleep(1000); |
| 176 | + } |
| 177 | + int nRemoveLevels = 9; |
| 178 | + for (i = 0; i < nRemoveLevels; i++) { |
| 179 | + File levelPath = new File(osmPath + "/" + i); |
| 180 | + if (levelPath.exists() && levelPath.isDirectory()) { |
| 181 | + FileUtils.deleteDirectory(levelPath); |
| 182 | + } |
| 183 | + } |
| 184 | + } catch (Exception ex) { |
| 185 | + ex.printStackTrace(); |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + public static void main(String[] args) { |
| 190 | + cliDownload(args); |
| 191 | + } |
| 192 | +} |
0 commit comments