package com.etrip.osmdroid.tiles; import java.io.File; import java.util.ArrayList; import java.util.List; import java.util.Scanner; import org.osmdroid.mtp.adt.OSMTileInfo; import org.osmdroid.mtp.download.DownloadManager; import org.osmdroid.mtp.util.DbCreator; import org.osmdroid.mtp.util.FolderDeleter; import org.osmdroid.mtp.util.FolderFileCounter; import org.osmdroid.mtp.util.FolderZipper; import org.osmdroid.mtp.util.Util; import org.osmdroid.util.GEMFFile; /** * *源代码出处来自 OSMMapTilePackage.jar中OSMMapTilePackageLoader的类 * *官方地址: * http://code.google.com/p/osmdroid/ * *OSMMapTilePackage的使用 * http://code.google.com/p/osmdroid/wiki/HowToUsePackager * * * * @author longgangbai */ public class OSMMapTilePackageLoader { // =========================================================== // Constants // =========================================================== private static final int THREADCOUNT_DEFAULT = 2; private static boolean FORCE = false; // =========================================================== // Fields // =========================================================== // =========================================================== // Constructors // =========================================================== public static void main(final String[] args) { final String pServerURL="http://tile.openstreetmap.org/%d/%d/%d.png"; final String pDestinationFile="Mapnik"; final String pTempFolder="D://haarlem.zip"; final int pThreadCount=18; final String pFileAppendix="/"; final int pMinZoom=10; final int pMaxZoom=15; final double pNorth=52.4244d; final double pSouth=52.3388d; final double pEast=4.6746d; final double pWest=4.5949; run( pServerURL, pDestinationFile, pTempFolder, pThreadCount, pFileAppendix, pMinZoom, pMaxZoom, pNorth, pSouth, pEast, pWest); } private static void run(final String pServerURL, final String pDestinationFile, final String pTempFolder, final int pThreadCount, final String pFileAppendix, final int pMinZoom, final int pMaxZoom, final double pNorth, final double pSouth, final double pEast, final double pWest) { if (pServerURL != null) { System.out.println("---------------------------"); final int expectedFileCount = runFileExpecter(pMinZoom, pMaxZoom, pNorth, pSouth, pEast, pWest); System.out.println("---------------------------"); runDownloading(pServerURL, pTempFolder, pThreadCount, pFileAppendix, pMinZoom, pMaxZoom, pNorth, pSouth, pEast, pWest); System.out.println("---------------------------"); runFileExistenceChecker(expectedFileCount, pTempFolder, pMinZoom, pMaxZoom, pNorth, pSouth, pEast, pWest); } if (pDestinationFile != null) { System.out.println("---------------------------"); if (pDestinationFile.endsWith(".zip")) { runZipToFile(pTempFolder, pDestinationFile); } else if (pDestinationFile.endsWith(".gemf")) { runCreateGEMFFile(pTempFolder, pDestinationFile); } else { runCreateDb(pTempFolder, pDestinationFile); } System.out.println("---------------------------"); if (pServerURL != null) { runCleanup(pTempFolder); } } System.out.println("---------------------------"); } private static void runFileExistenceChecker(final int pExpectedFileCount, final String pTempFolder, final int pMinZoom, final int pMaxZoom, final double pNorth, final double pSouth, final double pEast, final double pWest) { abortIfUserIsNotSure("This will check if the actual filecount is the same as the expected (" + pExpectedFileCount + ")."); /* Quickly count files in the tempFolder. */ System.out.print("Counting existing files ..."); final int actualFileCount = FolderFileCounter .getTotalRecursiveFileCount(new File(pTempFolder)); if (pExpectedFileCount == actualFileCount) { System.out.println(" done."); } else { System.out.println(" FAIL!"); abortIfUserIsNotSure("Reason: Actual files:" + actualFileCount + " Expected: " + pExpectedFileCount + ". Proceed?"); } } private static void printUsageAndExit() { System.out.println("Usage:\n" + "-u\t[OSM-style tile URL: http://_URL_/%d/%d/%d.png]\n" + "-t\t[Temporary Folder]\n" + "-d\t[Destination-file: C:\\mappack.zip]\n" + "-zmin\t[Minimum zoomLevel to download. Default: 0]\n" + "-zmax\t[Maximum zoomLevel to download]\n" + "-fa\t[Filename-Appendix. Default: \"\"]\n" + "-n\t[North Latitude]\n" + "-s\t[South Latitude]\n" + "-e\t[East Longitude]\n" + "-w\t[West Longitude]\n" + "-nthreads\t[Number of Download-Threads. Default: 2]\n"); System.exit(0); } // =========================================================== // Getter & Setter // =========================================================== // =========================================================== // Methods from SuperClass/Interfaces // =========================================================== // =========================================================== // Methods // =========================================================== private static void runCreateGEMFFile(final String pTempFolder, final String pDestinationFile) { try { System.out.println("Creating GEMF archive from " + pTempFolder + " to " + pDestinationFile + " ..."); final List<File> sourceFolders = new ArrayList<File>(); sourceFolders.add(new File(pTempFolder)); final GEMFFile file = new GEMFFile(pDestinationFile, sourceFolders); System.out.println(" done."); } catch (final Exception e) { e.printStackTrace(); } } private static void runZipToFile(final String pTempFolder, final String pDestinationFile) { try { System.out.print("Zipping files to " + pDestinationFile + " ..."); FolderZipper.zipFolderToFile(new File(pDestinationFile), new File( pTempFolder)); System.out.println(" done."); } catch (final Exception e) { e.printStackTrace(); } } private static void runCreateDb(final String pTempFolder, final String pDestinationFile) { try { System.out.print("Putting files into db : " + pDestinationFile + " ..."); DbCreator.putFolderToDb(new File(pDestinationFile), new File( pTempFolder)); System.out.println(" done."); } catch (final Exception e) { e.printStackTrace(); } } private static void runCleanup(final String pTempFolder) { abortIfUserIsNotSure("This will delete the temp folder: " + pTempFolder + " !"); /* deleteDirecto */ System.out.print("Deleting temp folder ..."); FolderDeleter.deleteFolder(new File(pTempFolder)); System.out.println(" done."); } private static void runDownloading(final String pBaseURL, final String pTempFolder, final int pThreadCount, final String pFileAppendix, final int pMinZoom, final int pMaxZoom, final double pNorth, final double pSouth, final double pEast, final double pWest) { final String pTempBaseURL = pTempFolder + File.separator + "%d" + File.separator + "%d" + File.separator + "%d" + pBaseURL.substring(pBaseURL.lastIndexOf('.')) + pFileAppendix.replace(File.separator + File.separator, File.separator); final DownloadManager dm = new DownloadManager(pBaseURL, pTempBaseURL, pThreadCount); /* For each zoomLevel. */ for (int z = pMinZoom; z <= pMaxZoom; z++) { final OSMTileInfo upperLeft = Util.getMapTileFromCoordinates( pNorth, pWest, z); final OSMTileInfo lowerRight = Util.getMapTileFromCoordinates( pSouth, pEast, z); System.out.print("ZoomLevel: " + z + " "); for (int x = upperLeft.x; x <= lowerRight.x; x++) { for (int y = upperLeft.y; y <= lowerRight.y; y++) { dm.add(new OSMTileInfo(x, y, z)); } } try { dm.waitEmpty(); System.out.println(" done."); } catch (final InterruptedException e) { e.printStackTrace(); } } try { System.out.print("Awaiting termination of all threads ..."); dm.waitFinished(); System.out.println(" done."); } catch (final InterruptedException e) { e.printStackTrace(); } } private static int runFileExpecter(final int pMinZoom, final int pMaxZoom, final double pNorth, final double pSouth, final double pEast, final double pWest) { /* Calculate file-count. */ int fileCnt = 0; for (int z = pMinZoom; z <= pMaxZoom; z++) { final OSMTileInfo upperLeft = Util.getMapTileFromCoordinates( pNorth, pWest, z); final OSMTileInfo lowerRight = Util.getMapTileFromCoordinates( pSouth, pEast, z); final int dx = lowerRight.x - upperLeft.x + 1; final int dy = lowerRight.y - upperLeft.y + 1; fileCnt += dx * dy; } abortIfUserIsNotSure("This will download: " + fileCnt + " Maptiles!"); return fileCnt; } private static void abortIfUserIsNotSure(final String message) { if (FORCE) { return; } System.out.println(message); System.out.print("Are you sure? [Y/N] ?: "); try { java.awt.Toolkit.getDefaultToolkit().beep(); } catch (final Throwable t) { /* Ignore */ } final String line = new Scanner(System.in).nextLine().toUpperCase() .trim(); if (!line.equals("Y") && !line.equals("YES")) { System.err.println("User aborted."); System.exit(0); } } // =========================================================== // Inner and Anonymous Classes // =========================================================== }