本文主要是介绍GIS瓦片1-google瓦片规则,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
谷歌瓦片
地址
- http://mt0.google.cn/vt?lyrs=y@178&hl=zh-Hans-CN&gl=CN&x=26056&y=12694&z=15
- http://mt2.google.cn/vt/lyrs=m@167000000&hl=zh-CN&gl=cn&x=420&y=193&z=9
- mt0:服务器名 mt = (x + y) % 4
- hl语言
- x:瓦片X轴的信息编号
- y:瓦片Y轴的信息编号
- z:瓦片的缩放级别
- 谷歌lyrs类型:
* h =仅限道路
* m =标准路线图
* p =地形
* r =某种改变的路线图
* s =仅限卫星
* t =仅限地形
* y =杂
参考系
标准墨卡托投影(等角圆术地图投影) EPSG:900913
纬度范围:[-85.05113,85.05113]
经度范围:[-180,180]
金字塔
- 瓦片尺寸 256*256
- x范围[0,2^level-1], level为级别z
- y范围[0,2^level-1],level为级别z
- level层每一块数据的横向经度差为 360/2^level
- level层每一块数据的纵向纬度差为180/2^level
xyz 计算左上角经纬度
(lon,lat)=(360/2^zzxx-180, 180/2^zzyy-90)
知道经纬度计算 z层xy
x=(lon+180)*2^z /360
y=(lat+90)*z^z / 360
代码实现
java
Downloader:
package XXXXXXpackage;import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;public class Downloader {private static int minLevel = 0;private static int maxLevel = 10;private static String dir = "D:\\data\\google_v\\";private static int maxRunningCount = 16;private static int maxRequestLength = 100;public static void download() {ExecutorService pool = Executors.newFixedThreadPool(maxRunningCount);for (int z = minLevel; z <= maxLevel; z++) {int curDt = 0;int requests[][] = null;int maxD = (int) (Math.pow(2, z));for (int x = 0; x < maxD; x++) {for (int y = 0; y < maxD; y++) {if (curDt % maxRequestLength == 0) {String threadName = "dt_" + z + "_" + curDt;DownloadThread dt = new DownloadThread(threadName, dir, requests);pool.execute(dt);curDt = 0;requests = new int[maxRequestLength][3];}requests[curDt][0] = y;requests[curDt][1] = x;requests[curDt][2] = z;curDt++;}}DownloadThread dt = new DownloadThread("", dir, requests);pool.execute(dt);}pool.shutdown();}public static void main(String[] strs) {download();}
}
DownLoadThread
package ??;import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.Proxy;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;public class DownloadThread extends Thread {private static int BUFFER_SIZE = 1024 * 8;// 缓冲区大小private static int MAX_TRY_DOWNLOAD_TIME = 128;private static int CURRENT_PROXY = 0;private String threadName = "";private String dir;// private int level;private String tmpDir;private Proxy proxy;private int[][] requests;private String ext = ".png";private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");public DownloadThread(String threadName, String dir, int[][] requests) {this.threadName = threadName;this.dir = dir;this.requests = requests;}@Overridepublic void run() {Date now = new Date();System.out.println(dateFormat.format(now) + "\t" + threadName + ":\t开始运行");long t1 = System.currentTimeMillis();long totalLength = download();long t2 = System.currentTimeMillis();double speed = (double) totalLength / (t2 - t1);now = new Date();if (speed < 0.5) {CURRENT_PROXY++;}System.out.println(dateFormat.format(now) + "\t" + threadName + ":\t完成运行\t" + speed + "kB/s");}public long download() {long totalLength = 0;if (requests == null) {return 0;}//System.out.println(requests.length);for (int i = 0; i < requests.length; i++) {int yy = requests[i][0];int xx = requests[i][1];int zz = requests[i][2];int yyg = (int) (Math.pow(2, zz) - 1 - requests[i][0]);this.tmpDir = dir + "/tmp/" + zz + "/";File tmpDirFile = new File(tmpDir);if (tmpDirFile.exists() == false) {tmpDirFile.mkdirs();}String dirStr = dir + "/download/" + zz + "/" + yy + "/";File fileDir = new File(dirStr);if (fileDir.exists() == false) {fileDir.mkdirs();}String fileStr = dirStr + yy + "_" + xx + ext;File file = new File(fileStr);// double lat1 = (yy) * dDegree - 90;// double lat2 = (yy + 1) * dDegree - 90;String url = "http://mt0.google.com/vt/lyrs=m@174000000&hl=zh-CN&src=app&x=" + xx + "&y=" + yyg + "&z=" + zz+ "&s=";// System.out.println(url);if (file.exists() == false) {String tmpFileStr = tmpDir + yy + "_" + xx + ext;boolean r = saveToFile(url, tmpFileStr);if (r == true) {totalLength += cut(tmpFileStr, fileStr);Date now = new Date();System.out.println(dateFormat.format(now) + "\t" + threadName + ":\t" + zz + "\\" + yy + "_" + xx + ext + "\t"+proxy+"\t完成!");} else {Date now = new Date();System.out.println(dateFormat.format(now) + "\t" + threadName + ":\t" + zz + "\\" + yy + "_" + xx + ext + "\t"+proxy+"\t失败!");}} else {Date now = new Date();System.out.println(dateFormat.format(now) + "\t" + threadName + ":\t" + zz + "\\" + yy + "_" + xx + ext + "已经下载!");}}return totalLength;}public static long cut(String srcFileStr, String descFileStr) {try {// int bytesum = 0;int byteread = 0;File srcFile = new File(srcFileStr);File descFile = new File(descFileStr);if (srcFile.exists()) { // 文件存在时InputStream is = new FileInputStream(srcFileStr); // 读入原文件FileOutputStream os = new FileOutputStream(descFileStr);byte[] buffer = new byte[1024 * 32];// int length;while ((byteread = is.read(buffer)) != -1) {// bytesum += byteread; //字节数 文件大小// System.out.println(bytesum);os.write(buffer, 0, byteread);}is.close();os.close();}srcFile.delete();return descFile.length();} catch (Exception e) {System.out.println("复制单个文件操作出错");e.printStackTrace();}return 0;}public boolean saveToFile(String destUrl, String fileName) {int currentTime = 0;while (currentTime < MAX_TRY_DOWNLOAD_TIME) {try {FileOutputStream fos = null;BufferedInputStream bis = null;HttpURLConnection httpConnection = null;URL url = null;byte[] buf = new byte[BUFFER_SIZE];int size = 0;// 建立链接url = new URL(destUrl);// url.openConnection(arg0)currentTime++;proxy = ProxyConfig.getProxy(CURRENT_PROXY);//if (proxy != null) {// System.out.println(threadName + ":\t切换代理\t" + proxy.address().toString());//} else {// System.out.println(threadName + ":\t使用本机IP");//}if (proxy == null) {httpConnection = (HttpURLConnection) url.openConnection();} else {httpConnection = (HttpURLConnection) url.openConnection(proxy);}httpConnection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)"); httpConnection.setConnectTimeout(60000);httpConnection.setReadTimeout(60000);// 连接指定的资源httpConnection.connect();// 获取网络输入流bis = new BufferedInputStream(httpConnection.getInputStream());// 建立文件fos = new FileOutputStream(fileName);// System.out.println("正在获取链接[" + destUrl + "]的内容;将其保存为文件[" +// fileName + "]");// 保存文件while ((size = bis.read(buf)) != -1){// System.out.println(size);fos.write(buf, 0, size);}fos.close();bis.close();httpConnection.disconnect();// currentTime = MAX_TRY_DOWNLOAD_TIME;break;} catch (Exception e) {//e.printStackTrace();CURRENT_PROXY++;}}if (currentTime < MAX_TRY_DOWNLOAD_TIME) {return true;} else {return false;}}}
ProxyConfig
package org.gfg.downloader.google.vctor;import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.Proxy.Type;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;public class ProxyConfig {private static List<Proxy> proxies;private static int getTime = 0;@SuppressWarnings("unchecked")public static void inital() {// if (proxies == null) {proxies = null;proxies = new ArrayList<Proxy>();// } else {// proxies.clear();// }try {URL url = new URL("http://www.18daili.com/");URLConnection urlConnection = url.openConnection();urlConnection.setConnectTimeout(30000);urlConnection.setReadTimeout(30000);SAXReader reader = new SAXReader();// System.out.println(url);reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);Document doc = reader.read(urlConnection.getInputStream());if (doc != null) {Element root = doc.getRootElement();Element proxyListTable = getElementById(root, "proxyListTable");// System.out.println(proxyListTable.asXML());Iterator<Element> trs = proxyListTable.elementIterator();trs.next();while (trs.hasNext()) {Element tr = trs.next();Iterator<Element> tds = tr.elementIterator();String ip = tds.next().getText();String port = tds.next().getText();// System.out.println(ip+":"+port);Proxy proxy = new Proxy(Type.HTTP, new InetSocketAddress(ip, Integer.valueOf(port)));proxies.add(proxy);System.out.println("添加代理\t" + proxy);}}} catch (Exception e) {// e.printStackTrace();}}private static Element getElementById(Element element, String id) {Element needElement = null;Iterator<Element> subElements = element.elementIterator();while (subElements.hasNext()) {Element subElement = subElements.next();String getId = subElement.attributeValue("id");if (getId != null && getId.equals(id)) {needElement = subElement;break;} else {needElement = getElementById(subElement, id);if (needElement != null) {break;}}}return needElement;}synchronized public static Proxy getProxy(int i) {getTime++;if (getTime % 1024 == 0 || proxies == null) {inital();getTime = 0;System.out.println("重新生成代理列表!");System.out.println("当前共有" + proxies.size() + "个代理!");}if (i % 8 == 0) {return null;}int index = i % proxies.size();index = Math.abs(index);return proxies.get(index);}public static void main(String... str) {inital();}}
这篇关于GIS瓦片1-google瓦片规则的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!