first commit
0 parents
Showing
22 changed files
with
434 additions
and
0 deletions
.idea/compiler.xml
0 → 100644
1 | <?xml version="1.0" encoding="UTF-8"?> | ||
2 | <project version="4"> | ||
3 | <component name="CompilerConfiguration"> | ||
4 | <option name="DEFAULT_COMPILER" value="Javac" /> | ||
5 | <resourceExtensions /> | ||
6 | <wildcardResourcePatterns> | ||
7 | <entry name="!?*.java" /> | ||
8 | <entry name="!?*.form" /> | ||
9 | <entry name="!?*.class" /> | ||
10 | <entry name="!?*.groovy" /> | ||
11 | <entry name="!?*.scala" /> | ||
12 | <entry name="!?*.flex" /> | ||
13 | <entry name="!?*.kt" /> | ||
14 | <entry name="!?*.clj" /> | ||
15 | </wildcardResourcePatterns> | ||
16 | <annotationProcessing> | ||
17 | <profile default="true" name="Default" enabled="false"> | ||
18 | <processorPath useClasspath="true" /> | ||
19 | </profile> | ||
20 | </annotationProcessing> | ||
21 | </component> | ||
22 | </project> | ||
23 |
.idea/description.html
0 → 100644
1 | <html>Simple <b>Java</b> application that includes a class with <code>main()</code> method</html> | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
.idea/encodings.xml
0 → 100644
.idea/misc.xml
0 → 100644
1 | <?xml version="1.0" encoding="UTF-8"?> | ||
2 | <project version="4"> | ||
3 | <component name="EntryPointsManager"> | ||
4 | <entry_points version="2.0" /> | ||
5 | </component> | ||
6 | <component name="ProjectKey"> | ||
7 | <option name="state" value="project://63537948-39a4-48a0-9c97-34259a0fa913" /> | ||
8 | </component> | ||
9 | <component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8" project-jdk-type="JavaSDK"> | ||
10 | <output url="file://$PROJECT_DIR$/out" /> | ||
11 | </component> | ||
12 | </project> | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
.idea/modules.xml
0 → 100644
1 | <?xml version="1.0" encoding="UTF-8"?> | ||
2 | <project version="4"> | ||
3 | <component name="ProjectModuleManager"> | ||
4 | <modules> | ||
5 | <module fileurl="file://$PROJECT_DIR$/TiandituDownloader.iml" filepath="$PROJECT_DIR$/TiandituDownloader.iml" /> | ||
6 | </modules> | ||
7 | </component> | ||
8 | </project> | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
.idea/vcs.xml
0 → 100644
.idea/workspace.xml
0 → 100644
This diff is collapsed.
Click to expand it.
TiandituDownloader.iml
0 → 100644
1 | <?xml version="1.0" encoding="UTF-8"?> | ||
2 | <module type="JAVA_MODULE" version="4"> | ||
3 | <component name="NewModuleRootManager" inherit-compiler-output="true"> | ||
4 | <exclude-output /> | ||
5 | <content url="file://$MODULE_DIR$"> | ||
6 | <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> | ||
7 | </content> | ||
8 | <orderEntry type="inheritedJdk" /> | ||
9 | <orderEntry type="sourceFolder" forTests="false" /> | ||
10 | <orderEntry type="module-library"> | ||
11 | <library> | ||
12 | <CLASSES> | ||
13 | <root url="jar://$USER_HOME$/Downloads/sqlite-jdbc-3.21.0-2.jar!/" /> | ||
14 | </CLASSES> | ||
15 | <JAVADOC /> | ||
16 | <SOURCES /> | ||
17 | </library> | ||
18 | </orderEntry> | ||
19 | </component> | ||
20 | </module> | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
my.db
0 → 100644
No preview for this file type
No preview for this file type
out/production/TiandituDownloader/Main.class
0 → 100644
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
src/CalEnv.java
0 → 100644
1 | public class CalEnv { | ||
2 | |||
3 | private int startX; | ||
4 | |||
5 | private int startY; | ||
6 | |||
7 | public CalEnv(int startX, int startY) { | ||
8 | this.startX = startX; | ||
9 | this.startY = startY; | ||
10 | } | ||
11 | |||
12 | public int getStartX() { | ||
13 | return startX; | ||
14 | } | ||
15 | |||
16 | public void setStartX(int startX) { | ||
17 | this.startX = startX; | ||
18 | } | ||
19 | |||
20 | public int getStartY() { | ||
21 | return startY; | ||
22 | } | ||
23 | |||
24 | public void setStartY(int startY) { | ||
25 | this.startY = startY; | ||
26 | } | ||
27 | } |
src/Main.java
0 → 100644
1 | import java.sql.SQLException; | ||
2 | |||
3 | public class Main { | ||
4 | |||
5 | |||
6 | public static void main(String[] args) throws SQLException { | ||
7 | System.out.println("Hello World!"); | ||
8 | |||
9 | TiandituDownload tiandituDownload = new TiandituDownload("my.db"); | ||
10 | Point pt = new Point(29.76,106.64); | ||
11 | Point pt2 = new Point(35.33, 108.12); | ||
12 | // tiandituDownload.Download(pt, pt2, 9, TianDiTuTiledMapServiceType.IMG_C); | ||
13 | tiandituDownload.Download(pt, pt2, 2,9, TianDiTuTiledMapServiceType.IMG_C); | ||
14 | } | ||
15 | } |
src/Point.java
0 → 100644
1 | public class Point { | ||
2 | |||
3 | private double lat; | ||
4 | |||
5 | private double lng; | ||
6 | |||
7 | public Point(){ | ||
8 | |||
9 | } | ||
10 | |||
11 | public Point(double lat, double lng){ | ||
12 | this.lat = lat; | ||
13 | this.lng = lng; | ||
14 | } | ||
15 | |||
16 | public double getLat() { | ||
17 | return lat; | ||
18 | } | ||
19 | |||
20 | public void setLat(double lat) { | ||
21 | this.lat = lat; | ||
22 | } | ||
23 | |||
24 | public double getLng() { | ||
25 | return lng; | ||
26 | } | ||
27 | |||
28 | public void setLng(double lng) { | ||
29 | this.lng = lng; | ||
30 | } | ||
31 | |||
32 | } |
src/TDTUrl.java
0 → 100644
1 | import java.util.Random; | ||
2 | |||
3 | /** | ||
4 | * Created by jiangbotao on 2018/3/23. | ||
5 | */ | ||
6 | |||
7 | public class TDTUrl { | ||
8 | |||
9 | private TianDiTuTiledMapServiceType _tiandituMapServiceType; | ||
10 | private int _level; | ||
11 | private int _col; | ||
12 | private int _row; | ||
13 | |||
14 | public TDTUrl(int level, int col, int row, TianDiTuTiledMapServiceType tiandituMapServiceType) { | ||
15 | this._level = level; | ||
16 | this._col = col; | ||
17 | this._row = row; | ||
18 | this._tiandituMapServiceType = tiandituMapServiceType; | ||
19 | } | ||
20 | |||
21 | public String generatUrl() { | ||
22 | |||
23 | StringBuilder url = new StringBuilder("http://t"); | ||
24 | Random random = new Random(); | ||
25 | int subdomain = (random.nextInt(6) + 1); | ||
26 | url.append(subdomain); | ||
27 | switch (this._tiandituMapServiceType) { | ||
28 | case VEC_C: | ||
29 | url.append(".tianditu.com/DataServer?T=vec_c&X=").append(this._col).append("&Y=").append(this._row).append("&L=").append(this._level); | ||
30 | break; | ||
31 | case CVA_C: | ||
32 | url.append(".tianditu.com/DataServer?T=cva_c&X=").append(this._col).append("&Y=").append(this._row).append("&L=").append(this._level); | ||
33 | break; | ||
34 | case CIA_C: | ||
35 | url.append(".tianditu.com/DataServer?T=cia_c&X=").append(this._col).append("&Y=").append(this._row).append("&L=").append(this._level); | ||
36 | break; | ||
37 | case IMG_C: | ||
38 | url.append(".tianditu.com/DataServer?T=img_c&X=").append(this._col).append("&Y=").append(this._row).append("&L=").append(this._level); | ||
39 | break; | ||
40 | case VEC_W: | ||
41 | url.append(".tianditu.com/DataServer?T=vec_w&X=").append(this._col).append("&Y=").append(this._row).append("&L=").append(this._level); | ||
42 | break; | ||
43 | case CVA_W: | ||
44 | url.append(".tianditu.com/DataServer?T=cva_w&X=").append(this._col).append("&Y=").append(this._row).append("&L=").append(this._level); | ||
45 | break; | ||
46 | case CIA_W: | ||
47 | url.append(".tianditu.com/DataServer?T=cia_w&X=").append(this._col).append("&Y=").append(this._row).append("&L=").append(this._level); | ||
48 | break; | ||
49 | case IMG_W: | ||
50 | url.append(".tianditu.com/DataServer?T=img_w&X=").append(this._col).append("&Y=").append(this._row).append("&L=").append(this._level); | ||
51 | break; | ||
52 | default: | ||
53 | return null; | ||
54 | } | ||
55 | return url.toString(); | ||
56 | } | ||
57 | } |
src/TianDiTuTiledMapServiceType.java
0 → 100644
1 | |||
2 | /** | ||
3 | * Created by jiangbotao on 2018/3/23. | ||
4 | */ | ||
5 | |||
6 | public enum TianDiTuTiledMapServiceType { | ||
7 | |||
8 | /** | ||
9 | * 矢量数据,CGCS2000 | ||
10 | * */ | ||
11 | VEC_C, | ||
12 | /** | ||
13 | * 影像数据 | ||
14 | * */ | ||
15 | IMG_C, | ||
16 | /** | ||
17 | * 矢量注记 | ||
18 | * */ | ||
19 | CVA_C, | ||
20 | /** | ||
21 | * 影像注记 | ||
22 | * */ | ||
23 | CIA_C, | ||
24 | /** | ||
25 | * 矢量数据,web墨卡托4326 | ||
26 | */ | ||
27 | VEC_W, | ||
28 | /** | ||
29 | * 影像数据 | ||
30 | * */ | ||
31 | IMG_W, | ||
32 | /** | ||
33 | * 矢量注记 | ||
34 | * */ | ||
35 | CVA_W, | ||
36 | /** | ||
37 | * 影像注记 | ||
38 | * */ | ||
39 | CIA_W; | ||
40 | |||
41 | } |
src/TiandituDownload.java
0 → 100644
1 | import java.io.BufferedInputStream; | ||
2 | import java.io.ByteArrayOutputStream; | ||
3 | import java.net.HttpURLConnection; | ||
4 | import java.net.URL; | ||
5 | import java.sql.*; | ||
6 | |||
7 | public class TiandituDownload { | ||
8 | |||
9 | double[] res = { | ||
10 | 1.40625, | ||
11 | 0.703125, | ||
12 | 0.3515625, | ||
13 | 0.17578125, | ||
14 | 0.087890625, | ||
15 | 0.0439453125, | ||
16 | 0.02197265625, | ||
17 | 0.010986328125, | ||
18 | 0.0054931640625, | ||
19 | 0.00274658203125, | ||
20 | 0.001373291015625, | ||
21 | 0.0006866455078125, | ||
22 | 0.00034332275390625, | ||
23 | 0.000171661376953125, | ||
24 | 8.58306884765629E-05, | ||
25 | 4.29153442382814E-05, | ||
26 | 2.14576721191407E-05, | ||
27 | 1.07288360595703E-05, | ||
28 | 5.36441802978515E-06, | ||
29 | 2.68220901489258E-06, | ||
30 | 1.34110450744629E-06 | ||
31 | }; | ||
32 | double[] scale = { | ||
33 | 400000000, | ||
34 | 295497598.5708346, | ||
35 | 147748799.285417, | ||
36 | 73874399.6427087, | ||
37 | 36937199.8213544, | ||
38 | 18468599.9106772, | ||
39 | 9234299.95533859, | ||
40 | 4617149.97766929, | ||
41 | 2308574.98883465, | ||
42 | 1154287.49441732, | ||
43 | 577143.747208662, | ||
44 | 288571.873604331, | ||
45 | 144285.936802165, | ||
46 | 72142.9684010827, | ||
47 | 36071.4842005414, | ||
48 | 18035.7421002707, | ||
49 | 9017.87105013534, | ||
50 | 4508.93552506767, | ||
51 | 2254.467762533835, | ||
52 | 1127.2338812669175, | ||
53 | 563.616940 | ||
54 | }; | ||
55 | |||
56 | int minLevel = 1; | ||
57 | int maxLevel = 2; | ||
58 | |||
59 | // 保存数据的sqlite文件路径 | ||
60 | String dbpath; | ||
61 | |||
62 | Connection conn = null; | ||
63 | |||
64 | Statement stmt = null; | ||
65 | |||
66 | public TiandituDownload(String dbpath){ | ||
67 | this.dbpath =dbpath; | ||
68 | } | ||
69 | |||
70 | private CalEnv calculateEnv(Point originPoint, int level){ | ||
71 | int startX = (int) Math.floor((originPoint.getLng() + 180)/(256 * res[level])); | ||
72 | int startY = (int) Math.floor((90 - originPoint.getLat())/(256 * res[level])); | ||
73 | if(startX<0)startX = 0; | ||
74 | if(startY<0)startY = 0; | ||
75 | |||
76 | return new CalEnv(startX, startY); | ||
77 | } | ||
78 | |||
79 | private void Download(Point minPoint, Point maxPoint, int level, TianDiTuTiledMapServiceType type){ | ||
80 | |||
81 | try{ | ||
82 | CalEnv startEnv = calculateEnv(minPoint, level); | ||
83 | CalEnv stopEnv = calculateEnv(maxPoint, level); | ||
84 | |||
85 | // System.out.println(startEnv.getStartX() + ":" +startEnv.getStartY()); | ||
86 | // System.out.println(stopEnv.getStartX() + ":" +stopEnv.getStartY()); | ||
87 | PreparedStatement preparedStatement; | ||
88 | ResultSet rs; | ||
89 | |||
90 | for(int i = startEnv.getStartX(); i <= stopEnv.getStartX(); i++){ | ||
91 | for(int j = stopEnv.getStartY(); j<= startEnv.getStartY(); j++){ | ||
92 | boolean hasData = false; | ||
93 | try{ | ||
94 | // 查询瓦片是否存在 | ||
95 | String querySQL = "select * from " + type + " where TILELEVEL = " + level + " and TILECOL = " + i + " and TILEROW = " + j; | ||
96 | rs = stmt.executeQuery(querySQL); | ||
97 | while(rs.next()){ | ||
98 | hasData = true; | ||
99 | } | ||
100 | // 如果不存在 | ||
101 | if(!hasData){ | ||
102 | String url = new TDTUrl(level, i, j, type).generatUrl(); | ||
103 | // 获得地图瓦片 | ||
104 | byte[] img = getTile(url); | ||
105 | System.out.println("正在下载 "+url); | ||
106 | // 将数据写入sqlite | ||
107 | String insertSQL = "INSERT INTO "+ type + "(TILELEVEL,TILECOL,TILEROW,TILEDATA) VALUES (?,?,?,?)"; | ||
108 | preparedStatement = conn.prepareStatement(insertSQL); | ||
109 | preparedStatement.setInt(1, level); | ||
110 | preparedStatement.setInt(2, i); | ||
111 | preparedStatement.setInt(3, j); | ||
112 | preparedStatement.setBytes(4, img); | ||
113 | preparedStatement.execute(); | ||
114 | } | ||
115 | }catch (Exception e){ | ||
116 | System.out.println(e.getMessage()); | ||
117 | } | ||
118 | } | ||
119 | conn.commit(); | ||
120 | } | ||
121 | }catch (Exception e){ | ||
122 | System.out.println(e.getMessage()); | ||
123 | } | ||
124 | } | ||
125 | |||
126 | public void Download(Point minPoint, Point maxPoint, int minLevel, int maxLevel, TianDiTuTiledMapServiceType type) throws SQLException { | ||
127 | try { | ||
128 | Class.forName("org.sqlite.JDBC"); | ||
129 | conn = DriverManager.getConnection("jdbc:sqlite:" + this.dbpath); | ||
130 | conn.setAutoCommit(false); | ||
131 | stmt = conn.createStatement(); | ||
132 | // 新建数据表 | ||
133 | String sql = "CREATE TABLE IF NOT EXISTS " + type + | ||
134 | "(TILELEVEL INT," + | ||
135 | "TILECOL INT," + | ||
136 | "TILEROW INT," + | ||
137 | "TILEDATA BLOB)"; | ||
138 | stmt.executeUpdate(sql); | ||
139 | stmt.close(); | ||
140 | |||
141 | for(int m = minLevel; m<= maxLevel; m++){ | ||
142 | this.Download(minPoint, maxPoint, m, type); | ||
143 | } | ||
144 | conn.close(); | ||
145 | System.out.println("下载完成"); | ||
146 | |||
147 | }catch (Exception e){ | ||
148 | |||
149 | } | ||
150 | } | ||
151 | |||
152 | protected byte[] getTile(String url) throws Exception { | ||
153 | byte[] result = null; | ||
154 | try { | ||
155 | // 如果没有就直接从web上读取 | ||
156 | ByteArrayOutputStream bos = new ByteArrayOutputStream(); | ||
157 | |||
158 | URL sjwurl = new URL(url); | ||
159 | HttpURLConnection httpUrl = null; | ||
160 | BufferedInputStream bis = null; | ||
161 | byte[] buf = new byte[1024]; | ||
162 | |||
163 | httpUrl = (HttpURLConnection) sjwurl.openConnection(); | ||
164 | httpUrl.connect(); | ||
165 | bis = new BufferedInputStream(httpUrl.getInputStream()); | ||
166 | |||
167 | while (true) { | ||
168 | int bytes_read = bis.read(buf); | ||
169 | if (bytes_read > 0) { | ||
170 | bos.write(buf, 0, bytes_read); | ||
171 | } else { | ||
172 | break; | ||
173 | } | ||
174 | } | ||
175 | |||
176 | bis.close(); | ||
177 | httpUrl.disconnect(); | ||
178 | |||
179 | result = bos.toByteArray(); | ||
180 | } catch (Exception ex) { | ||
181 | ex.printStackTrace(); | ||
182 | } | ||
183 | |||
184 | return result; | ||
185 | } | ||
186 | } |
-
Please register or sign in to post a comment