update3
Showing
11 changed files
with
97 additions
and
55 deletions
This diff is collapsed.
Click to expand it.
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
... | @@ -3,16 +3,16 @@ | ... | @@ -3,16 +3,16 @@ |
3 | TiandituDownload主要用于下载天地图数据,其坐标系为CGCS2000. | 3 | TiandituDownload主要用于下载天地图数据,其坐标系为CGCS2000. |
4 | 4 | ||
5 | ```java | 5 | ```java |
6 | 6 | ||
7 | // 设置要下载的图片存储sqlite路径 | 7 | // 设置要下载的图片存储sqlite路径 |
8 | TiandituDownload tiandituDownload = new TiandituDownload("my.db"); | 8 | TiandituDownload tiandituDownload = new TiandituDownload("my.db"); |
9 | // 范围起始点(左下角、右上角) | 9 | // 范围起始点(左下角、右上角) |
10 | Point pt = new Point(29.76,106.64); | 10 | Point pt = new Point(29.76,106.64); |
11 | Point pt2 = new Point(35.33, 108.12); | 11 | Point pt2 = new Point(35.33, 108.12); |
12 | // 下载器 | 12 | // 下载器 |
13 | // 2 为最小level,9为最大level | 13 | // 2 为最小level,9为最大level |
14 | // TianDiTuTiledMapServiceType.IMG_C 为下载数据类型 | 14 | // TianDiTuTiledMapServiceType.IMG_C 为下载数据类型 |
15 | // 最后一个参数为merge,为true时,会将地图及其注记一并下载融合到一张图片中去 | 15 | // 最后一个参数为merge,为true时,会将地图及其注记一并下载融合到一张图片中去 |
16 | tiandituDownload.Download(pt, pt2, 2,9, TianDiTuTiledMapServiceType.IMG_C, true); | 16 | tiandituDownload.Download(pt, pt2, 2,9, TianDiTuTiledMapServiceType.IMG_C, true); |
17 | 17 | ||
18 | ``` | 18 | ``` |
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
... | @@ -12,6 +12,7 @@ public class Main{ | ... | @@ -12,6 +12,7 @@ public class Main{ |
12 | TiandituDownload tiandituDownload = new TiandituDownload("my.db"); | 12 | TiandituDownload tiandituDownload = new TiandituDownload("my.db"); |
13 | Point pt = new Point(29.76,106.64); | 13 | Point pt = new Point(29.76,106.64); |
14 | Point pt2 = new Point(35.33, 108.12); | 14 | Point pt2 = new Point(35.33, 108.12); |
15 | tiandituDownload.Download(pt, pt2, 2,9, TianDiTuTiledMapServiceType.VEC_C, true); | 15 | |
16 | tiandituDownload.Download(pt, pt2, 7,9, TianDiTuTiledMapServiceType.VEC_C, true); | ||
16 | } | 17 | } |
17 | } | 18 | } | ... | ... |
... | @@ -16,6 +16,10 @@ import java.sql.*; | ... | @@ -16,6 +16,10 @@ import java.sql.*; |
16 | 16 | ||
17 | public class TiandituDownload { | 17 | public class TiandituDownload { |
18 | 18 | ||
19 | // 级别 | ||
20 | int[] level = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}; | ||
21 | |||
22 | // 分辨率 | ||
19 | double[] res = { | 23 | double[] res = { |
20 | 1.40625, | 24 | 1.40625, |
21 | 0.703125, | 25 | 0.703125, |
... | @@ -39,6 +43,7 @@ public class TiandituDownload { | ... | @@ -39,6 +43,7 @@ public class TiandituDownload { |
39 | 2.68220901489258E-06, | 43 | 2.68220901489258E-06, |
40 | 1.34110450744629E-06 | 44 | 1.34110450744629E-06 |
41 | }; | 45 | }; |
46 | // 比例尺 | ||
42 | double[] scale = { | 47 | double[] scale = { |
43 | 400000000, | 48 | 400000000, |
44 | 295497598.5708346, | 49 | 295497598.5708346, |
... | @@ -60,69 +65,83 @@ public class TiandituDownload { | ... | @@ -60,69 +65,83 @@ public class TiandituDownload { |
60 | 4508.93552506767, | 65 | 4508.93552506767, |
61 | 2254.467762533835, | 66 | 2254.467762533835, |
62 | 1127.2338812669175, | 67 | 1127.2338812669175, |
63 | 563.616940 | 68 | 563.61694063345875 |
64 | }; | 69 | }; |
65 | 70 | // 最小级别 | |
66 | int minLevel = 1; | 71 | int minLevel = 1; |
72 | |||
73 | // 最大级别 | ||
67 | int maxLevel = 2; | 74 | int maxLevel = 2; |
68 | 75 | ||
69 | // 保存数据的sqlite文件路径 | 76 | // 保存数据的sqlite文件路径 |
70 | String dbpath; | 77 | String dbpath; |
71 | 78 | ||
79 | // SQLite链接 | ||
72 | Connection conn = null; | 80 | Connection conn = null; |
73 | |||
74 | Statement stmt = null; | 81 | Statement stmt = null; |
75 | 82 | ||
76 | public TiandituDownload(String dbpath){ | 83 | public TiandituDownload(String dbpath) { |
77 | this.dbpath =dbpath; | 84 | this.dbpath = dbpath; |
78 | } | 85 | } |
79 | 86 | ||
80 | private CalEnv calculateEnv(Point originPoint, int level){ | 87 | /** |
81 | int startX = (int) Math.floor((originPoint.getLng() + 180)/(256 * res[level])); | 88 | * |
82 | int startY = (int) Math.floor((90 - originPoint.getLat())/(256 * res[level])); | 89 | * @param originPoint 坐标点 |
83 | if(startX<0)startX = 0; | 90 | * @param level 级别 |
84 | if(startY<0)startY = 0; | 91 | * @return 改点在该级别所属的地图瓦片行列号 |
92 | */ | ||
93 | private CalEnv calculateEnv(Point originPoint, int level) { | ||
94 | int startX = (int) Math.floor((originPoint.getLng() + 180) / (256 * res[level])); | ||
95 | int startY = (int) Math.floor((90 - originPoint.getLat()) / (256 * res[level])); | ||
96 | if (startX < 0) startX = 0; | ||
97 | if (startY < 0) startY = 0; | ||
85 | 98 | ||
86 | return new CalEnv(startX, startY); | 99 | return new CalEnv(startX, startY); |
87 | } | 100 | } |
88 | 101 | ||
89 | private void Download(Point minPoint, Point maxPoint, int level, TianDiTuTiledMapServiceType type, boolean merge){ | 102 | /** |
103 | * | ||
104 | * @param minPoint 下载范围左下角坐标 | ||
105 | * @param maxPoint 下载范围右上角坐标 | ||
106 | * @param level 下载级别 | ||
107 | * @param type 下载数据类型 | ||
108 | * @param merge 数据与其注记是否融合 | ||
109 | */ | ||
110 | private void downloadSingLevelTiles(Point minPoint, Point maxPoint, int level, TianDiTuTiledMapServiceType type, boolean merge) { | ||
90 | 111 | ||
91 | try{ | 112 | try { |
92 | CalEnv startEnv = calculateEnv(minPoint, level); | 113 | CalEnv startEnv = calculateEnv(minPoint, level); |
93 | CalEnv stopEnv = calculateEnv(maxPoint, level); | 114 | CalEnv stopEnv = calculateEnv(maxPoint, level); |
94 | 115 | ||
95 | // System.out.println(startEnv.getStartX() + ":" +startEnv.getStartY()); | ||
96 | // System.out.println(stopEnv.getStartX() + ":" +stopEnv.getStartY()); | ||
97 | PreparedStatement preparedStatement; | 116 | PreparedStatement preparedStatement; |
98 | ResultSet rs; | 117 | ResultSet rs; |
99 | 118 | ||
100 | for(int i = startEnv.getStartX(); i <= stopEnv.getStartX(); i++){ | 119 | for (int i = startEnv.getStartCol(); i <= stopEnv.getStartCol(); i++) { |
101 | for(int j = stopEnv.getStartY(); j<= startEnv.getStartY(); j++){ | 120 | for (int j = stopEnv.getStartRow(); j <= startEnv.getStartRow(); j++) { |
102 | boolean hasData = false; | 121 | boolean hasData = false; |
103 | try{ | 122 | try { |
104 | // 查询瓦片是否存在 | 123 | // 查询瓦片是否存在 |
105 | String querySQL = "select * from " + type + " where TILELEVEL = " + level + " and TILECOL = " + i + " and TILEROW = " + j; | 124 | String querySQL = "select * from " + type + " where TILELEVEL = " + level + " and TILECOL = " + i + " and TILEROW = " + j; |
106 | rs = stmt.executeQuery(querySQL); | 125 | rs = stmt.executeQuery(querySQL); |
107 | while(rs.next()){ | 126 | while (rs.next()) { |
108 | hasData = true; | 127 | hasData = true; |
109 | } | 128 | } |
110 | // 如果不存在 | 129 | // 如果不存在则从网络获取 |
111 | if(!hasData){ | 130 | if (!hasData) { |
112 | String url = new TDTUrl(level, i, j, type).generatUrl(); | 131 | String url = new TDTUrl(level, i, j, type).generatUrl(); |
113 | // 获得地图瓦片 | 132 | // 获得地图瓦片数据流 |
114 | byte[] img = getTile(url); | 133 | byte[] img = getTile(url); |
115 | System.out.println("正在下载 "+url); | 134 | System.out.println("正在下载 " + url); |
116 | 135 | ||
117 | // 如果merge为true,则在下载img_c或vec_c时将其注记图片一并下载 | 136 | // 如果merge为true,则在下载img_c或vec_c时将其注记图片一并下载 |
118 | if(merge) { | 137 | if (merge) { |
119 | InputStream img_stream = new ByteArrayInputStream(img); | 138 | InputStream img_stream = new ByteArrayInputStream(img); |
120 | BufferedImage bImageFromConvert = ImageIO.read(img_stream); | 139 | BufferedImage bImageFromConvert = ImageIO.read(img_stream); |
121 | 140 | ||
122 | String url2 = ""; | 141 | String url2 = ""; |
123 | if(type == TianDiTuTiledMapServiceType.IMG_C) { | 142 | if (type == TianDiTuTiledMapServiceType.IMG_C) { |
124 | url2 = new TDTUrl(level, i, j, TianDiTuTiledMapServiceType.CIA_C).generatUrl(); | 143 | url2 = new TDTUrl(level, i, j, TianDiTuTiledMapServiceType.CIA_C).generatUrl(); |
125 | }else{ | 144 | } else { |
126 | url2 = new TDTUrl(level, i, j, TianDiTuTiledMapServiceType.CVA_C).generatUrl(); | 145 | url2 = new TDTUrl(level, i, j, TianDiTuTiledMapServiceType.CVA_C).generatUrl(); |
127 | } | 146 | } |
128 | byte[] img2 = getTile(url2); | 147 | byte[] img2 = getTile(url2); |
... | @@ -140,7 +159,7 @@ public class TiandituDownload { | ... | @@ -140,7 +159,7 @@ public class TiandituDownload { |
140 | baos.close(); | 159 | baos.close(); |
141 | } | 160 | } |
142 | // 将数据写入sqlite | 161 | // 将数据写入sqlite |
143 | String insertSQL = "INSERT INTO "+ type + "(TILELEVEL,TILECOL,TILEROW,TILEDATA) VALUES (?,?,?,?)"; | 162 | String insertSQL = "INSERT INTO " + type + "(TILELEVEL,TILECOL,TILEROW,TILEDATA) VALUES (?,?,?,?)"; |
144 | preparedStatement = conn.prepareStatement(insertSQL); | 163 | preparedStatement = conn.prepareStatement(insertSQL); |
145 | preparedStatement.setInt(1, level); | 164 | preparedStatement.setInt(1, level); |
146 | preparedStatement.setInt(2, i); | 165 | preparedStatement.setInt(2, i); |
... | @@ -148,17 +167,27 @@ public class TiandituDownload { | ... | @@ -148,17 +167,27 @@ public class TiandituDownload { |
148 | preparedStatement.setBytes(4, img); | 167 | preparedStatement.setBytes(4, img); |
149 | preparedStatement.execute(); | 168 | preparedStatement.execute(); |
150 | } | 169 | } |
151 | }catch (Exception e){ | 170 | } catch (Exception e) { |
152 | System.out.println(e.getMessage()); | 171 | System.out.println(e.getMessage()); |
153 | } | 172 | } |
154 | } | 173 | } |
155 | conn.commit(); | 174 | conn.commit(); |
156 | } | 175 | } |
157 | }catch (Exception e){ | 176 | } catch (Exception e) { |
158 | System.out.println(e.getMessage()); | 177 | System.out.println(e.getMessage()); |
159 | } | 178 | } |
160 | } | 179 | } |
161 | 180 | ||
181 | /** | ||
182 | * | ||
183 | * @param minPoint 下载范围左下角坐标 | ||
184 | * @param maxPoint 下载范围右上角坐标 | ||
185 | * @param minLevel 下载最小级别 | ||
186 | * @param maxLevel 下载最大级别 | ||
187 | * @param type 下载数据类型 | ||
188 | * @param merge 数据与其注记是否融合 | ||
189 | * @throws SQLException | ||
190 | */ | ||
162 | public void Download(Point minPoint, Point maxPoint, int minLevel, int maxLevel, TianDiTuTiledMapServiceType type, boolean merge) throws SQLException { | 191 | public void Download(Point minPoint, Point maxPoint, int minLevel, int maxLevel, TianDiTuTiledMapServiceType type, boolean merge) throws SQLException { |
163 | try { | 192 | try { |
164 | Class.forName("org.sqlite.JDBC"); | 193 | Class.forName("org.sqlite.JDBC"); |
... | @@ -174,17 +203,23 @@ public class TiandituDownload { | ... | @@ -174,17 +203,23 @@ public class TiandituDownload { |
174 | stmt.executeUpdate(sql); | 203 | stmt.executeUpdate(sql); |
175 | stmt.close(); | 204 | stmt.close(); |
176 | 205 | ||
177 | for(int m = minLevel; m<= maxLevel; m++){ | 206 | for (int m = minLevel; m <= maxLevel; m++) { |
178 | this.Download(minPoint, maxPoint, m, type, merge); | 207 | this.downloadSingLevelTiles(minPoint, maxPoint, m, type, merge); |
179 | } | 208 | } |
180 | conn.close(); | 209 | conn.close(); |
181 | System.out.println("下载完成"); | 210 | System.out.println("下载完成"); |
182 | 211 | ||
183 | }catch (Exception e){ | 212 | } catch (Exception e) { |
184 | 213 | ||
185 | } | 214 | } |
186 | } | 215 | } |
187 | 216 | ||
217 | /** | ||
218 | * 下载网络数据 | ||
219 | * @param url | ||
220 | * @return 图片数据流 | ||
221 | * @throws Exception | ||
222 | */ | ||
188 | protected byte[] getTile(String url) throws Exception { | 223 | protected byte[] getTile(String url) throws Exception { |
189 | byte[] result = null; | 224 | byte[] result = null; |
190 | try { | 225 | try { | ... | ... |
1 | package utils; | 1 | package utils; |
2 | 2 | ||
3 | /** | ||
4 | * 范围计算后的Row与Col类 | ||
5 | */ | ||
3 | public class CalEnv { | 6 | public class CalEnv { |
4 | 7 | ||
5 | private int startX; | 8 | private int startCol; |
6 | 9 | ||
7 | private int startY; | 10 | private int startRow; |
8 | 11 | ||
9 | public CalEnv(int startX, int startY) { | 12 | public CalEnv(int startX, int startY) { |
10 | this.startX = startX; | 13 | this.startCol = startX; |
11 | this.startY = startY; | 14 | this.startRow = startY; |
12 | } | 15 | } |
13 | 16 | ||
14 | public int getStartX() { | 17 | public int getStartCol() { |
15 | return startX; | 18 | return startCol; |
16 | } | 19 | } |
17 | 20 | ||
18 | public void setStartX(int startX) { | 21 | public void setStartCol(int startCol) { |
19 | this.startX = startX; | 22 | this.startCol = startCol; |
20 | } | 23 | } |
21 | 24 | ||
22 | public int getStartY() { | 25 | public int getStartRow() { |
23 | return startY; | 26 | return startRow; |
24 | } | 27 | } |
25 | 28 | ||
26 | public void setStartY(int startY) { | 29 | public void setStartRow(int startRow) { |
27 | this.startY = startY; | 30 | this.startRow = startRow; |
28 | } | 31 | } |
29 | } | 32 | } | ... | ... |
-
Please register or sign in to post a comment