자바 점 좌표 와 기와 조각 상호 변환 도구

5983 단어
package com.appleyk.utils;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Envelope;
 
/**
 *     (  ) --          
 * 
 * @author [email protected]
 * @blob http://blog.csdn.net/appleyk
 * @date 2018 4 4 -  1:04:50
 */
public class WebMercatorUtils {
 
	/**
	 * Web    --               ,                
	 * Bounds(    )[minx,miny,maxx,maxy]
	 *          :-20037580.3427892,-20037508.3427892,20037580.3427892,20037580.
	 * 3427892       ---     
	 */
 
	public static double minx = -20037508.3427892;
	public static double maxx = 20037508.3427892;
 
	/**
	 *      
	 * 
	 * @param x
	 * @param y
	 * @param level
	 * @return
	 */
	public static Envelope tileXYToNativeRectangle(int x, int y, int level) {
		int xTiles = getNumberOfXTilesAtLevel(level);
		int yTiles = getNumberOfYTilesAtLevel(level);
 
		double xTileWidth = (maxx - minx) / xTiles;
		double west = minx + x * xTileWidth;
		double east = minx + (x + 1) * xTileWidth;
 
		double yTileHeight = (maxx - minx) / yTiles;
		double north = maxx - y * yTileHeight;
		double south = maxx - (y + 1) * yTileHeight;
		Envelope envelope = new Envelope(west, east, north, south);
 
		return envelope;
	}
 
	/**
	 *       +      ---    x:? y:?
	 * 
	 * @param level
	 * @param longitude
	 * @param latitude
	 * @return
	 */
	public static Map positionToTileXY(int level, double longitude, double latitude) {
 
		Map map = new HashMap();
		Coordinate coordinate = new Coordinate(longitude, latitude);
		Coordinate c = lonLat2Mercator(coordinate);
 
		//        X      
		int xTiles = getNumberOfXTilesAtLevel(level);
		//        Y      
		int yTiles = getNumberOfYTilesAtLevel(level);
 
		double overallWidth = maxx - minx;
		double xTileWidth = overallWidth / xTiles;
		double overallHeight = maxx - minx;
		double yTileHeight = overallHeight / yTiles;
 
		double distanceFromWest = c.x - minx;
		double distanceFromNorth = maxx - c.y;
 
		double xTileCoordinate = distanceFromWest / xTileWidth;
		if (xTileCoordinate >= xTiles) {
			xTileCoordinate = xTiles - 1;
		}
		map.put("X", (int) xTileCoordinate);
 
		double yTileCoordinate = distanceFromNorth / yTileHeight;
		if (yTileCoordinate >= yTiles) {
			yTileCoordinate = yTiles - 1;
		}
		map.put("Y", (int) yTileCoordinate);
 
		return map;
	}
 
	/**
	 * level   ,X(       ,        ,   0)        -- 2 level  
	 * 
	 * @param level
	 * @return
	 */
	private static Integer getNumberOfXTilesAtLevel(int level) {
		return 1 << level;
	}
 
	/**
	 * level   ,Y(       ,        ,   0)        -- 2 level  
	 * 
	 * @param level
	 * @return
	 */
	private static Integer getNumberOfYTilesAtLevel(int level) {
		return 1 << level;
	}
 
	// ----          4 level  
 
	/**
	 *        
	 * 
	 * @param lonLat
	 * @return
	 */
	public static Coordinate lonLat2Mercator(Coordinate lonLat) {
 
		double x = lonLat.x * 20037508.34 / 180;
		double y = Math.log(Math.tan((90 + lonLat.y) * Math.PI / 360)) / (Math.PI / 180);
		y = y * maxx / 180;
 
		return new Coordinate(x, y);
	}
 
 
	/**
	 *        
	 * 
	 * @param mercator
	 * @return
	 */
	public static Coordinate Mercator2lonLat(Coordinate mercator) {
 
		double x = mercator.x / 20037508.34 * 180;
		double y = mercator.y / 20037508.34 * 180;
		y = 180 / Math.PI * (2 * Math.atan(Math.exp(y * Math.PI / 180)) - Math.PI / 2);
 
		return new Coordinate(x, y);
	}
 
	/**
	 *                XY    ,  X:[10,20], Y:[20,30]
	 * 
	 * @param level
	 *            --     
	 * @param minX
	 *            --   X ==   (west)
	 * @param maxX
	 *            --   X ==   (east)
	 * @param minY
	 *            --   Y ==   (north)
	 * @param maxY
	 *            --   Y ==   (south)
	 * @return
	 */
	public static Map> GetTileXYRange(int level, double minX, double maxX, double minY,
			double maxY) {
 
		Map minXY = positionToTileXY(level, minX, minY);
		Map maxXY = positionToTileXY(level, maxX, maxY);
 
		Integer minTileX = minXY.get("X");
		Integer maxTileX = maxXY.get("X");
		Integer minTileY = minXY.get("Y");
		Integer maxTileY = maxXY.get("Y");
 
		List tileXrange = new ArrayList<>();
		tileXrange.add(minTileX);
		tileXrange.add(maxTileX);
 
		List tileYrange = new ArrayList<>();
		tileYrange.add(minTileY);
		tileYrange.add(maxTileY);
 
		Map> mapResult = new HashMap<>();
		mapResult.put("X", tileXrange);
		mapResult.put("Y", tileYrange);
 
		return mapResult;
	}
 
	public static void main(String[] args)  {
 
		/**
		 *     
		 */
		 double minX = 112.729502;
		 double maxX = 114.210982;
		 double minY = 34.914062;
		 double maxY = 34.351984;
 
 
		Map minXY = positionToTileXY(15, minX, minY);
		Map maxXY = positionToTileXY(15, maxX, maxY);
 
		Integer minTileX = minXY.get("X");
		Integer maxTileX = maxXY.get("X");
		Integer minTileY = minXY.get("Y");
		Integer maxTileY = maxXY.get("Y");
		System.err.println("x    :[" + minTileX + "," + maxTileX + "]");
		System.err.println("Y    :[" + minTileY + "," + maxTileY + "]");
 
		String url = "http://www.google.cn/maps/vt?lyrs=s@781&gl=cn&";
 
		// x=46057&y=32219&z=16
		int n = 0;
		for (int i = minTileX; i <= maxTileX; i++) {
			for (int j = minTileY; j <= maxTileY; j++) {
				// System.err.println(url+"x="+i+"&y="+j+"&z=15");
				n++;
			}
		}
 
		System.err.println("      15      :" + n);
	}
 
}

 
자세 한 내용 은:https://blog.csdn.net/Appleyk/article/details/79816064

좋은 웹페이지 즐겨찾기