자바 점 좌표 와 기와 조각 상호 변환 도구
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
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.