标签:
package com;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Scanner;
public class ThreadTileCreate extends Thread{
private int minxt;
private int minyt;
private int maxxt;
private int maxyt;
private int zoomt;
private static int threadCount = 0;
private int threadNum = ++threadCount;
public static void main(String[] args)
{
//System.out.println("最小经度");
double minlon = 73.5;
//System.out.println("最小纬度");
double minlat = 3.5;
//System.out.println("最大经度");
double maxlon = 135;
//System.out.println("最大纬度");
double maxlat = 53.8;
//下载级别
int zoom = 5; // 6,7,8,9,10
int nGrid = (int)Math.pow(2, zoom);
double dx = nGrid;
dx = dx/360;
int minx = nGrid - (int)(dx * (180.0-minlon));
double y = Math.sin(maxlat *Math.PI / 180);
y = Math.log((1.0+y)/(1.0-y));
int miny = (int)Math.abs((0.5-y/(4.0*Math.PI))*nGrid);//注意最大纬度及最小行号
int maxx = nGrid - (int)(dx * (180.0-maxlon));
y = Math.sin(minlat *Math.PI / 180);
y = Math.log((1.0+y)/(1.0-y));
int maxy = (int)Math.abs((0.5-y/(4.0*Math.PI))*nGrid);//注意最小纬度及最大行号
int il = 10;
int jr = 10;
//启用的线程数
int threadCount = il*jr;
System.out.println("即将启用的线程数为:"+threadCount+"个!");
//各个线程要切的范围
int ax = (maxx-minx)/jr;
int by = (maxy-miny)/il;
//System.out.println(maxx+"=="+minx+"=="+maxy+"=="+miny+"=="+ax+"=="+by);
int minxv =0;
int maxxv =0;
int maxyv =0;
int minyv =0;
for(int j=0;j<jr;j++)
{
for(int i=0; i<il; i++)
{
try
{
Thread.sleep(50);
minxv = ax*i+minx;
if(i==il-1)
{
maxxv = maxx;
}else
{
maxxv = ax*(i+1)+minx;
}
minyv = by*j+miny;
if(j==jr-1)
{
maxyv = maxy;
}else
{
maxyv =by*(j+1)+miny;
}
//System.out.println(maxxv+"=="+minxv+"=="+maxyv+"=="+minyv+"=="+ax+"=="+by);
ThreadTileCreate t = new ThreadTileCreate();
t.setMaxxt(maxxv);
t.setMaxyt(maxyv);
t.setMinxt(minxv);
t.setMinyt(minyv);
t.setZoomt(zoom);
t.start();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public void run()
{
System.out.println(threadNum+"开始下载数据..."+maxxt+"=="+minxt+"=="+maxyt+"=="+minyt+"=="+zoomt);
DownImage(minxt, minyt, maxxt, maxyt,zoomt);
System.out.println(threadNum+"结束下载数据.");
}
public static void DownImage(int minx,int miny,int maxx,int maxy,int zoom) {
System.out.println("正在下载数据第" + zoom+ "层...");
for(int i=minx;i<maxx+1; i++){
for(int j=miny; j<maxy+1; j++){
//道路标注
String url = "http://t3.tianditu.com/cva_w/wmts?SERVICE=WMTS&VERSION=1.0.0&REQUEST=GetTile&LAYER=cva&STYLE=default&FORMAT=tiles&TILEMATRIXSET=w&TILEMATRIX=" + zoom + "&TILEROW=" + j + "&TILECOL=" + i;
//道路图
String url1 = "http://t3.tianditu.com/vec_w/wmts?SERVICE=WMTS&VERSION=1.0.0&REQUEST=GetTile&LAYER=vec&STYLE=default&FORMAT=tiles&TILEMATRIXSET=w&TILEMATRIX=" + zoom + "&TILEROW=" + j + "&TILECOL=" + i;
//地形标注
//String url2 = "http://t0.tianditu.com/cta_w/wmts?SERVICE=WMTS&VERSION=1.0.0&REQUEST=GetTile&LAYER=cta&STYLE=default&FORMAT=tiles&TILEMATRIXSET=w&TILEMATRIX=" + zoom + "&TILEROW=" + j + "&TILECOL=" + i;
//地形
//String url3 = "http://t0.tianditu.com/ter_w/wmts?SERVICE=WMTS&VERSION=1.0.0&REQUEST=GetTile&LAYER=ter&STYLE=default&FORMAT=tiles&TILEMATRIXSET=w&TILEMATRIX=" + zoom + "&TILEROW=" + j + "&TILECOL=" + i;
//道路标注
//String url = "http://t7.tianditu.com/DataServer?T=cva_c&x="+i+"&y="+j+"&l=" + zoom;
//道路图
//String url1 = "http://t2.tianditu.com/DataServer?T=vec_c&x="+i+"&y="+j+"&l=" + zoom;
try {
if(downImage(i,j,zoom,url,0) == -1)
{
System.out.println(url);
}
if(downImage(i,j,zoom,url1,1) == -1)
{
System.out.println(url1);
}
/*if(zoom<15){
if(downImage(i,j,
zoom,url2,2) == -1)
{
System.out.println(url2);
}
if(downImage(i,j,zoom,url3,3) == -1)
{
System.out.println(url3);
}
}*/
} catch (IOException e) {
//TODO Auto-generated catch block
//e.printStackTrace();
}
}
}
}
private static int downImage(int x,int y,int z,String ul,int num) throws IOException{
URL url;
try {
url = new URL(ul);
String path = "";
if(num == 0)
path = "f:\\TDTMap\\Label\\" + z + "\\";
else if(num == 1)
path = "f:\\TDTMap\\Map\\" + z + "\\";
else if(num == 2)
path = "f:\\TDTMap\\TLabel\\" + z + "\\";
else if(num == 3)
path = "f:\\TDTMap\\TMap\\" + z + "\\";
String fname = x + "-" + y + ".png";
File f = new File(path);
if(!f.exists()){
f.mkdirs();
}
if(new File(path + fname).exists())
{
return 0;
}
HttpURLConnection con;
con = (HttpURLConnection) url.openConnection();
int state = con.getResponseCode();
if (state == 200) {
File outFile = new File(path + fname);
OutputStream os = new FileOutputStream(outFile);
InputStream is = url.openStream();
byte[] buff = new byte[1024];
while(true) {
int readed = is.read(buff);
if(readed == -1) {
break;
}
byte[] temp = new byte[readed];
System.arraycopy(buff, 0, temp, 0, readed);
os.write(temp);
}
is.close();
os.close();
}
else
{
return -1;
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return 0;
}
public int getMinxt() {
return minxt;
}
public void setMinxt(int minxt) {
this.minxt = minxt;
}
public int getMinyt() {
return minyt;
}
public void setMinyt(int minyt) {
this.minyt = minyt;
}
public int getMaxxt() {
return maxxt;
}
public void setMaxxt(int maxxt) {
this.maxxt = maxxt;
}
public int getMaxyt() {
return maxyt;
}
public void setMaxyt(int maxyt) {
this.maxyt = maxyt;
}
public int getZoomt() {
return zoomt;
}
public void setZoomt(int zoomt) {
this.zoomt = zoomt;
}
}
-----------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------
标签:
原文地址:http://www.cnblogs.com/gispathfinder/p/5769951.html