gridLayer_obj.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. L.canvasLayer = L.GridLayer.extend({
  2. createTile: function(coords){
  3. // console.log("createTilea and z:"+z+"a:"+a+"b:"+b);
  4. var tile = L.DomUtil.create('canvas', 'leaflet-tile');
  5. var ctx = tile.getContext('2d');
  6. var size = this.getTileSize()
  7. tile.width = size.x
  8. tile.height = size.y
  9. // 将切片号乘以切片分辨率,默认为256pixel,得到切片左上角的绝对像素坐标
  10. var nwPoint = coords.scaleBy(size);
  11. // 根据绝对像素坐标,以及缩放层级,反投影得到其经纬度
  12. var nw = map2DViewer.map.unproject(nwPoint, z)
  13. // console.log(tile.width);
  14. ctx.lineWidth = 1;
  15. ctx.strokeStyle = 'red';
  16. var lstCoord = Math.pow(2, coords.z) - 1;
  17. if(coords.z - z <= 1 && coords.z - z >= -1){
  18. // console.log("coords.z: "+ coords.z);
  19. // console.log("z: "+ z);
  20. if(coords.z == z){ // 同级时直接绘制
  21. ctx.beginPath();
  22. ctx.moveTo(0, 0);
  23. ctx.lineTo(size.x-1, 0);
  24. ctx.lineTo(size.x-1, size.y-1);
  25. ctx.lineTo(0, size.y-1);
  26. ctx.closePath();
  27. ctx.fillStyle = 'rgba(225,225,225,0.5)';
  28. ctx.fillRect(0, 0, size.x, 70);
  29. ctx.font = "18px Arial";
  30. ctx.fillStyle = 'black';
  31. ctx.textAlign = 'left';
  32. ctx.fillText(a+'-'+b+'-'+z+'-'+coords.x+'-'+coords.y+'.ptp', 40, 20);
  33. ctx.font = "14px Arial";
  34. ctx.textAlign = 'left';
  35. ctx.fillText('lat: ' + nw.lat.toFixed(6), 40, 40); // 纬度
  36. ctx.fillText('lon: ' + nw.lng.toFixed(6), 40, 60); // 经度
  37. }
  38. else{ // 非同级时
  39. var lstCoord = Math.pow(2, coords.z) - 1;
  40. if(coords.z - z < 0 && coords.z - z > -2){
  41. var mul = Math.pow(2, (z-coords.z));
  42. for(var i=0;i<mul;i++){
  43. ctx.moveTo((size.x-1)/mul*i, 0);
  44. ctx.lineTo((size.x-1)/mul*i, size.y-1);
  45. ctx.moveTo(0, (size.y-1)/mul*i);
  46. ctx.lineTo((size.x-1), (size.y-1)/mul*i);
  47. for(var j=0;j<=mul;j++){
  48. ctx.fillStyle = 'rgba(225,225,225,0.5)';
  49. ctx.fillRect(size.x/mul*j, size.y/mul*i, size.x/mul, 70);
  50. ctx.font = "9px Arial";
  51. ctx.fillStyle = 'black';
  52. ctx.fillText(a+'-'+b+'-'+z+'-'+(coords.x*mul+j)+'-'+(coords.y*mul+i)+'.ptp', size.x/mul*j, size.y/mul*i+20);
  53. }
  54. nw = map2DViewer.map.unproject(nwPoint, coords.z);
  55. ctx.fillText('lat: ' + nw.lat.toFixed(6), 0, 40); // 纬度
  56. ctx.fillText('lon: ' + nw.lng.toFixed(6), 0, 60); // 经度
  57. }
  58. }
  59. else{
  60. var bei = coords.z - z;
  61. var mi = Math.pow(2, bei);
  62. if(coords.x % mi == 0 && coords.y % mi == 0){ // 需要记录的坐标
  63. ctx.fillStyle = 'rgba(225,225,225,0.5)';
  64. ctx.fillRect(0, 0, size.x, 70);
  65. ctx.font = "18px Arial";
  66. ctx.fillStyle = 'black';
  67. ctx.textAlign = 'left';
  68. ctx.fillText(a+'-'+b+'-'+z+'-'+coords.x/mi+'-'+coords.y/mi+'.ptp', 40, 20); // 仅适用于放大一级
  69. ctx.font = "14px Arial";
  70. ctx.textAlign = 'left';
  71. nw = map2DViewer.map.unproject(nwPoint, coords.z);
  72. ctx.fillText('lat: ' + nw.lat.toFixed(6), 40, 40); // 纬度
  73. ctx.fillText('lon: ' + nw.lng.toFixed(6), 40, 60); // 经度
  74. }
  75. if(coords.y % mi == 0){
  76. ctx.moveTo(0, 0);
  77. ctx.lineTo(size.x-1,0);
  78. }
  79. if(coords.x % mi == 0){
  80. ctx.moveTo(0, 0);
  81. ctx.lineTo(0,size.y-1);
  82. }
  83. if(coords.y == lstCoord){
  84. ctx.moveTo(0,size.y-1);
  85. ctx.lineTo(size.x-1,size.y-1);
  86. }
  87. }
  88. }
  89. }
  90. ctx.stroke();
  91. return tile;
  92. }
  93. });
  94. L.control.createTilePackage = function(options){
  95. map2DViewer.map.removeLayer(cl_start);
  96. map2DViewer.map.setZoom(z);
  97. // console.log(z);
  98. cl_start = new L.canvasLayer;
  99. cl_start.addTo(map2DViewer.map);
  100. }
  101. // 自定义空间范围
  102. L.control.fitBds = function(crds,z){
  103. // console.log("getz:"+z);
  104. var latlngs = [
  105. [crds[2], crds[0]],
  106. [crds[3], crds[0]],
  107. [crds[3], crds[1]],
  108. [crds[2], crds[1]]
  109. ];
  110. map2DViewer.map.removeLayer(polygon);
  111. polygon = L.polygon(latlngs, {color: 'blue'}).addTo(map2DViewer.map);
  112. // zoom the map to the polygon
  113. map2DViewer.map.fitBounds(polygon.getBounds(),{maxZoom:parseInt(z)}).setZoom(z);
  114. mCrd = [
  115. crds[0],crds[2],crds[1],crds[3]
  116. ];
  117. mz = z;
  118. }
  119. L.control.request = function(fileName, tiles){
  120. var lgPopUp_total = document.querySelector('.lgPopUp_total');
  121. var lgPopUp_none = document.querySelector('.lgPopUp_none');
  122. var blackBoard = document.querySelector('.blackBoard');
  123. var tileType = document.querySelector('.lgPopUp_total .tileType');
  124. var tileSize = document.querySelector('.lgPopUp_total .tileSize');
  125. var loading = document.querySelector('.lgPopUp_total img');
  126. var esc = document.querySelector('.lgPopUp_total .esc');
  127. var qd = document.querySelector('.qd');
  128. // esc.innerHTML = "取消";
  129. // loading.style.display = "block";
  130. console.log(tiles);
  131. console.log(tiles.length);
  132. var xhr1,xhr2;
  133. if(tiles.length == 1){
  134. xhr1 = ajax({
  135. url: config.countServer+"/tileSizeData/"+fileName+"/"+tiles, //请求地址
  136. type: "GET",
  137. dataType: "json",
  138. success: function (response, xml) {
  139. loading.style.display = "none";
  140. esc.innerHTML = "确定";
  141. var str = JSON.parse(response);
  142. console.log(str[0].data[0]);
  143. console.log(str[0].data[0].size);
  144. if(!str[0].data[0].size){
  145. lgPopUp_none.style.display = "block";
  146. lgPopUp_total.style.display = "none";
  147. blackBoard.style.display = "block";
  148. }
  149. else{
  150. lgPopUp_total.style.display = "block";
  151. blackBoard.style.display = "block";
  152. loading.style.display = "none";
  153. tileType.innerHTML = fileName;
  154. tileSize.innerHTML = str[0].data[0].size;
  155. // alert("当前数据包大小为:"+str[0].data[0].size);
  156. }
  157. },
  158. fail: function (status) {
  159. loading.style.display = "none";
  160. console.log("get data err");
  161. }
  162. });
  163. }
  164. if(tiles.length > 1){
  165. xhr2 = ajax({
  166. url: config.countServer+"/tileSizeData/"+fileName+"/", //请求地址
  167. type: "POST",
  168. data: { "ptplist": JSON.stringify(tiles)},
  169. dataType: "json",
  170. success: function (response, xml) {
  171. esc.innerHTML = "确定";
  172. loading.style.display = "none";
  173. var str = JSON.parse(response);
  174. // console.log(str);
  175. // console.log(str.total);
  176. if(str.total == -1){
  177. lgPopUp_none.style.display = "block";
  178. blackBoard.style.display = "block";
  179. lgPopUp_total.style.display = "none";
  180. }
  181. else{
  182. lgPopUp_total.style.display = "block";
  183. blackBoard.style.display = "block";
  184. // alert("当前数据包总大小为:"+str.total);
  185. tileType.innerHTML = fileName;
  186. tileSize.innerHTML = str.total;
  187. }
  188. },
  189. fail: function (status) {
  190. loading.style.display = "none";
  191. console.log("post data err");
  192. }
  193. });
  194. }
  195. esc.onclick = function(){
  196. if(esc.innerHTML == "取消"){
  197. // xhr1.abort();
  198. xhr2.abort();
  199. esc.innerHTML = "确定";
  200. }
  201. if(esc.innerHTML == "确定"){
  202. lgPopUp_total.style.display = "none";
  203. blackBoard.style.display = "none";
  204. }
  205. }
  206. qd.onclick = function(){
  207. lgPopUp_none.style.display = "none";
  208. blackBoard.style.display = "none";
  209. }
  210. }
  211. L.control.showNameList = function(mcrd,mz){
  212. console.log(mcrd)
  213. var merc = new SphericalMercator({
  214. size: 256
  215. });
  216. var minX = merc.xyz(mcrd, mz).minX;
  217. var maxX = merc.xyz(mcrd, mz).maxX;
  218. var minY = merc.xyz(mcrd, mz).minY;
  219. var maxY = merc.xyz(mcrd, mz).maxY;
  220. var nameList = new Array();
  221. var tileList = new Array();
  222. for(var i=minX;i<=maxX;i++){
  223. for(var j=minY;j<=maxY;j++){
  224. nameList.push(a+"-"+b+"-"+z+"-"+i+"-"+j+".ptp");
  225. tileList.push(a+"-"+b+"-"+z+"-"+i+"-"+j);
  226. }
  227. }
  228. var contents = "<h3>包名列表:</h3>" +'<div class="list"><p>'+ nameList.join('</p><p>'
  229. ) + '</p></div><div class="close">×</div>';
  230. var options = "<div class='calculation'><label>选择瓦片类型</label><select id='choose'>"+
  231. "<option>r_dtt_dem</option>"+
  232. "<option>r_google_hybrid</option>"+
  233. "<option>r_google_satellite</option>"+
  234. "<option>r_google_street</option>"+
  235. "<option>r_google_terrain</option>"+
  236. "</select><div><button title='计算包总大小'>计算</button></div></div>";
  237. document.querySelector('.lgPopUp').innerHTML = contents+options;
  238. document.querySelector('.lgPopUp').style.display = "block";
  239. document.querySelector('.close').onclick = function(){
  240. document.querySelector('.lgPopUp').style.display = "none";
  241. }
  242. // console.log("minX: "+merc.xyz(mcrd, mz).minX);
  243. // console.log("maxX: "+merc.xyz(mcrd, mz).maxX);
  244. // console.log("minY: "+merc.xyz(mcrd, mz).minY);
  245. // console.log("maxY: "+merc.xyz(mcrd, mz).maxY);
  246. var cal = document.querySelector('.calculation button');
  247. cal.onclick = function(){
  248. var myselect=document.getElementById("choose");
  249. var index=myselect.selectedIndex; // selectedIndex代表的是你所选中项的index
  250. // myselect.options[index].value;
  251. var fileName = myselect.options[index].text;
  252. L.control.request(fileName, tileList);
  253. var lgPopUp_total = document.querySelector('.lgPopUp_total');
  254. var blackBoard = document.querySelector('.blackBoard');
  255. var tileType = document.querySelector('.lgPopUp_total .tileType');
  256. var tileSize = document.querySelector('.lgPopUp_total .tileSize');
  257. var loading = document.querySelector('.lgPopUp_total img');
  258. var esc = document.querySelector('.lgPopUp_total .esc');
  259. lgPopUp_total.style.display = "block";
  260. blackBoard.style.display = "block";
  261. tileType.innerHTML = fileName;
  262. loading.style.display = "block";
  263. esc.innerHTML = "取消";
  264. tileSize.innerHTML = "";
  265. }
  266. }