⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mercatortiledimagelayer.java

📁 world wind java sdk 源码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
			return null;

		if (WWIO.isFileOutOfDate(url, tile.getLevel().getExpiryTime()))
		{
			// The file has expired. Delete it.
			WorldWind.getDataFileStore().removeFile(url);
			String message = Logging.getMessage("generic.DataFileExpired", url);
			Logging.logger().fine(message);
		}
		else
		{
			try
			{
				File imageFile = new File(url.toURI());
				BufferedImage image = ImageIO.read(imageFile);
				if (image == null)
				{
					String message = Logging.getMessage(
							"generic.ImageReadFailed", imageFile);
					throw new RuntimeException(message);
				}

				this.levels.unmarkResourceAbsent(tile);
				return image;
			}
			catch (IOException e)
			{
				// Assume that something's wrong with the file and delete it.
				gov.nasa.worldwind.WorldWind.getDataFileStore().removeFile(url);
				this.levels.markResourceAbsent(tile);
				String message = Logging.getMessage(
						"generic.DeletedCorruptDataFile", url);
				Logging.logger().info(message);
			}
		}

		return null;
	}

	private void downloadImage(final MercatorTextureTile tile, String mimeType)
			throws Exception
	{
		//        System.out.println(tile.getPath());
		final URL resourceURL = tile.getResourceURL(mimeType);
		Retriever retriever;

		String protocol = resourceURL.getProtocol();

		if ("http".equalsIgnoreCase(protocol))
		{
			retriever = new HTTPRetriever(resourceURL,
					new HttpRetrievalPostProcessor(tile));
		}
		else
		{
			String message = Logging
					.getMessage("layers.TextureLayer.UnknownRetrievalProtocol",
							resourceURL);
			throw new RuntimeException(message);
		}

		retriever.setConnectTimeout(10000);
		retriever.setReadTimeout(20000);
		retriever.call();
	}

	public int computeLevelForResolution(Sector sector, Globe globe,
			double resolution)
	{
		if (sector == null)
		{
			String message = Logging.getMessage("nullValue.SectorIsNull");
			Logging.logger().severe(message);
			throw new IllegalStateException(message);
		}

		if (globe == null)
		{
			String message = Logging.getMessage("nullValue.GlobeIsNull");
			Logging.logger().severe(message);
			throw new IllegalStateException(message);
		}

		double texelSize = 0;
		Level targetLevel = this.levels.getLastLevel();
		for (int i = 0; i < this.getLevels().getLastLevel().getLevelNumber(); i++)
		{
			if (this.levels.isLevelEmpty(i))
				continue;

			texelSize = this.levels.getLevel(i).getTexelSize();
			if (texelSize > resolution)
				continue;

			targetLevel = this.levels.getLevel(i);
			break;
		}

		Logging.logger().info(
				Logging.getMessage("layers.TiledImageLayer.LevelSelection",
						targetLevel.getLevelNumber(), texelSize));
		return targetLevel.getLevelNumber();
	}

	public BufferedImage composeImageForSector(Sector sector, int imageWidth,
			int imageHeight, int levelNumber, String mimeType,
			boolean abortOnError, BufferedImage image)
	{
		if (sector == null)
		{
			String message = Logging.getMessage("nullValue.SectorIsNull");
			Logging.logger().severe(message);
			throw new IllegalStateException(message);
		}

		if (levelNumber < 0)
		{
			levelNumber = this.levels.getLastLevel().getLevelNumber();
		}
		else if (levelNumber > this.levels.getLastLevel().getLevelNumber())
		{
			Logging.logger().warning(
					Logging.getMessage(
							"generic.LevelRequestedGreaterThanMaxLevel",
							levelNumber, this.levels.getLastLevel()
									.getLevelNumber()));
			levelNumber = this.levels.getLastLevel().getLevelNumber();
		}

		MercatorTextureTile[][] tiles = this.getTilesInSector(sector,
				levelNumber);

		if (tiles.length == 0 || tiles[0].length == 0)
		{
			Logging
					.logger()
					.severe(
							Logging
									.getMessage("layers.TiledImageLayer.NoImagesAvailable"));
			return null;
		}

		if (image == null)
			image = new BufferedImage(imageWidth, imageHeight,
					BufferedImage.TYPE_INT_RGB);

		Graphics2D g = image.createGraphics();

		for (MercatorTextureTile[] row : tiles)
		{
			for (MercatorTextureTile tile : row)
			{
				if (tile == null)
					continue;

				BufferedImage tileImage;
				try
				{
					tileImage = this.getImage(tile, mimeType);

					double sh = ((double) imageHeight / (double) tileImage
							.getHeight())
							* (tile.getSector().getDeltaLat().divide(sector
									.getDeltaLat()));
					double sw = ((double) imageWidth / (double) tileImage
							.getWidth())
							* (tile.getSector().getDeltaLon().divide(sector
									.getDeltaLon()));

					double dh = imageHeight
							* (-tile.getSector().getMaxLatitude().subtract(
									sector.getMaxLatitude()).degrees / sector
									.getDeltaLat().degrees);
					double dw = imageWidth
							* (tile.getSector().getMinLongitude().subtract(
									sector.getMinLongitude()).degrees / sector
									.getDeltaLon().degrees);

					AffineTransform txf = g.getTransform();
					g.translate(dw, dh);
					g.scale(sw, sh);
					g.drawImage(tileImage, 0, 0, null);
					g.setTransform(txf);
				}
				catch (Exception e)
				{
					if (abortOnError)
						throw new RuntimeException(e);

					String message = Logging.getMessage(
							"generic.ExceptionWhileRequestingImage", tile
									.getPath());
					Logging.logger().log(java.util.logging.Level.WARNING,
							message, e);
				}
			}
		}

		return image;
	}

	public int countImagesInSector(Sector sector, int levelNumber)
	{
		if (sector == null)
		{
			String msg = Logging.getMessage("nullValue.SectorIsNull");
			Logging.logger().severe(msg);
			throw new IllegalArgumentException(msg);
		}

		Level targetLevel = this.levels.getLastLevel();
		if (levelNumber >= 0)
		{
			for (int i = levelNumber; i < this.getLevels().getLastLevel()
					.getLevelNumber(); i++)
			{
				if (this.levels.isLevelEmpty(i))
					continue;

				targetLevel = this.levels.getLevel(i);
				break;
			}
		}

		// Collect all the tiles intersecting the input sector.
		LatLon delta = targetLevel.getTileDelta();
        Angle latOrigin = this.levels.getTileOrigin().getLatitude();
        Angle lonOrigin = this.levels.getTileOrigin().getLongitude();
		final int nwRow = Tile.computeRow(delta.getLatitude(), sector
				.getMaxLatitude(), latOrigin);
		final int nwCol = Tile.computeColumn(delta.getLongitude(), sector
				.getMinLongitude(), lonOrigin);
		final int seRow = Tile.computeRow(delta.getLatitude(), sector
				.getMinLatitude(), latOrigin);
		final int seCol = Tile.computeColumn(delta.getLongitude(), sector
				.getMaxLongitude(), lonOrigin);

		int numRows = nwRow - seRow + 1;
		int numCols = seCol - nwCol + 1;

		return numRows * numCols;
	}

	private MercatorTextureTile[][] getTilesInSector(Sector sector,
			int levelNumber)
	{
		if (sector == null)
		{
			String msg = Logging.getMessage("nullValue.SectorIsNull");
			Logging.logger().severe(msg);
			throw new IllegalArgumentException(msg);
		}

		Level targetLevel = this.levels.getLastLevel();
		if (levelNumber >= 0)
		{
			for (int i = levelNumber; i < this.getLevels().getLastLevel()
					.getLevelNumber(); i++)
			{
				if (this.levels.isLevelEmpty(i))
					continue;

				targetLevel = this.levels.getLevel(i);
				break;
			}
		}

		// Collect all the tiles intersecting the input sector.
		LatLon delta = targetLevel.getTileDelta();
        Angle latOrigin = this.levels.getTileOrigin().getLatitude();
        Angle lonOrigin = this.levels.getTileOrigin().getLongitude();
		final int nwRow = Tile.computeRow(delta.getLatitude(), sector
				.getMaxLatitude(), latOrigin);
		final int nwCol = Tile.computeColumn(delta.getLongitude(), sector
				.getMinLongitude(), lonOrigin);
		final int seRow = Tile.computeRow(delta.getLatitude(), sector
				.getMinLatitude(), latOrigin);
		final int seCol = Tile.computeColumn(delta.getLongitude(), sector
				.getMaxLongitude(), lonOrigin);

		int numRows = nwRow - seRow + 1;
		int numCols = seCol - nwCol + 1;
		MercatorTextureTile[][] sectorTiles = new MercatorTextureTile[numRows][numCols];

		for (int row = nwRow; row >= seRow; row--)
		{
			for (int col = nwCol; col <= seCol; col++)
			{
				TileKey key = new TileKey(targetLevel.getLevelNumber(), row,
						col, targetLevel.getCacheName());
				Sector tileSector = this.levels.computeSectorForKey(key);
				MercatorSector mSector = MercatorSector.fromSector(tileSector); //TODO: check
				sectorTiles[nwRow - row][col - nwCol] = new MercatorTextureTile(
						mSector, targetLevel, row, col);
			}
		}

		return sectorTiles;
	}

	private BufferedImage getImage(MercatorTextureTile tile, String mimeType)
			throws Exception
	{
		// Read the image from disk.
		BufferedImage image = this.requestImage(tile, mimeType);
		if (image != null)
			return image;

		// Retrieve it from the net since it's not on disk.
		this.downloadImage(tile, mimeType);

		// Try to read from disk again after retrieving it from the net.
		image = this.requestImage(tile, mimeType);
		if (image == null)
		{
			String message = Logging.getMessage(
					"layers.TiledImageLayer.ImageUnavailable", tile.getPath());
			throw new RuntimeException(message);
		}

		return image;
	}

	private class HttpRetrievalPostProcessor implements RetrievalPostProcessor
	{
		private MercatorTextureTile tile;

		public HttpRetrievalPostProcessor(MercatorTextureTile tile)
		{
			this.tile = tile;
		}

		public ByteBuffer run(Retriever retriever)
		{
			if (!retriever.getState().equals(
					Retriever.RETRIEVER_STATE_SUCCESSFUL))
				return null;

			HTTPRetriever htr = (HTTPRetriever) retriever;
			if (htr.getResponseCode() == HttpURLConnection.HTTP_NO_CONTENT)
			{
				// Mark tile as missing to avoid excessive attempts
				MercatorTiledImageLayer.this.levels.markResourceAbsent(tile);
				return null;
			}

			if (htr.getResponseCode() != HttpURLConnection.HTTP_OK)
				return null;

			URLRetriever r = (URLRetriever) retriever;
			ByteBuffer buffer = r.getBuffer();

			String suffix = WWIO.makeSuffixForMimeType(htr.getContentType());
			if (suffix == null)
			{
				return null; // TODO: log error
			}

			String path = tile.getPath().substring(0,
					tile.getPath().lastIndexOf("."));
			path += suffix;

			final File outFile = WorldWind.getDataFileStore().newFile(path);
			if (outFile == null)
				return null;

			try
			{
				WWIO.saveBuffer(buffer, outFile);
				return buffer;
			}
			catch (IOException e)
			{
				e.printStackTrace(); // TODO: log error
				return null;
			}
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -