mvt.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. window.createMVTWithStyle = function createMVTWithStyle(Cesium, ol, createMapboxStreetsV6Style, options) {
  2. function MVTProvider(options) {
  3. options = Cesium.defaultValue(options, Cesium.defaultValue.EMPTY_OBJECT);
  4. this._tilingScheme = Cesium.defined(options.tilingScheme) ? options.tilingScheme : new Cesium.WebMercatorTilingScheme({ ellipsoid: options.ellipsoid });
  5. this._tileWidth = Cesium.defaultValue(options.tileWidth, 512);
  6. this._tileHeight = Cesium.defaultValue(options.tileHeight, 512);
  7. this._readyPromise = Cesium.when.resolve(true);
  8. this._ol = ol;
  9. this._mvtParser = new this._ol.format.MVT();
  10. this._styleFun = createMapboxStreetsV6Style;
  11. this._key = Cesium.defaultValue(options.key, "");
  12. this._url = Cesium.defaultValue(options.url, "https://a.tiles.mapbox.com/v4/mapbox.mapbox-streets-v6/{z}/{x}/{y}.vector.pbf?access_token={k}");
  13. var sw = this._tilingScheme._rectangleSouthwestInMeters;
  14. var ne = this._tilingScheme._rectangleNortheastInMeters;
  15. var mapExtent = [sw.x, sw.y, ne.x, ne.y];
  16. this._resolutions = ol.tilegrid.resolutionsFromExtent(
  17. mapExtent, 22, this._tileWidth);
  18. this._pixelRatio = 1;
  19. this._transform = [0.125, 0, 0, 0.125, 0, 0];
  20. this._replays = ["Default", "Image", "Polygon", "LineString", "Text"];
  21. this._tileQueue = new Cesium.TileReplacementQueue();
  22. this._cacheSize = 1000;
  23. }
  24. Cesium.defineProperties(MVTProvider.prototype, {
  25. proxy: {
  26. get: function() {
  27. return undefined;
  28. }
  29. },
  30. tileWidth: {
  31. get: function() {
  32. return this._tileWidth;
  33. }
  34. },
  35. tileHeight: {
  36. get: function() {
  37. return this._tileHeight;
  38. }
  39. },
  40. maximumLevel: {
  41. get: function() {
  42. return undefined;
  43. }
  44. },
  45. minimumLevel: {
  46. get: function() {
  47. return undefined;
  48. }
  49. },
  50. tilingScheme: {
  51. get: function() {
  52. return this._tilingScheme;
  53. }
  54. },
  55. rectangle: {
  56. get: function() {
  57. return this._tilingScheme.rectangle;
  58. }
  59. },
  60. tileDiscardPolicy: {
  61. get: function() {
  62. return undefined;
  63. }
  64. },
  65. errorEvent: {
  66. get: function() {
  67. return this._errorEvent;
  68. }
  69. },
  70. ready: {
  71. get: function() {
  72. return true;
  73. }
  74. },
  75. readyPromise: {
  76. get: function() {
  77. return this._readyPromise;
  78. }
  79. },
  80. credit: {
  81. get: function() {
  82. return undefined;
  83. }
  84. },
  85. hasAlphaChannel: {
  86. get: function() {
  87. return true;
  88. }
  89. }
  90. });
  91. MVTProvider.prototype.getTileCredits = function(x, y, level) {
  92. return undefined;
  93. };
  94. function findTileInQueue(x, y, level, tileQueue) {
  95. var item = tileQueue.head;
  96. while (item != undefined && !(item.xMvt == x && item.yMvt == y && item.zMvt == level)) {
  97. item = item.replacementNext;
  98. }
  99. return item;
  100. };
  101. function remove(tileReplacementQueue, item) {
  102. var previous = item.replacementPrevious;
  103. var next = item.replacementNext;
  104. if (item === tileReplacementQueue._lastBeforeStartOfFrame) {
  105. tileReplacementQueue._lastBeforeStartOfFrame = next;
  106. }
  107. if (item === tileReplacementQueue.head) {
  108. tileReplacementQueue.head = next;
  109. } else {
  110. previous.replacementNext = next;
  111. }
  112. if (item === tileReplacementQueue.tail) {
  113. tileReplacementQueue.tail = previous;
  114. } else {
  115. next.replacementPrevious = previous;
  116. }
  117. item.replacementPrevious = undefined;
  118. item.replacementNext = undefined;
  119. --tileReplacementQueue.count;
  120. }
  121. function trimTiles(tileQueue, maximumTiles) {
  122. var tileToTrim = tileQueue.tail;
  123. while (tileQueue.count > maximumTiles &&
  124. Cesium.defined(tileToTrim)) {
  125. var previous = tileToTrim.replacementPrevious;
  126. remove(tileQueue, tileToTrim);
  127. delete tileToTrim;
  128. tileToTrim = null;
  129. tileToTrim = previous;
  130. }
  131. };
  132. MVTProvider.prototype.requestImage = function(x, y, level, request) {
  133. var cacheTile = findTileInQueue(x, y, level, this._tileQueue);
  134. if (cacheTile != undefined) {
  135. console.log('cache')
  136. return cacheTile;
  137. }
  138. else {
  139. var that = this;
  140. var url = this._url;
  141. url = url.replace('{x}', x).replace('{y}', y).replace('{z}', level).replace('{k}', this._key);
  142. var tilerequest = async function(x, y, z) {
  143. var resource = Cesium.Resource.createIfNeeded(url);
  144. var arrayBuffer = await resource.fetchArrayBuffer()
  145. console.log(arrayBuffer)
  146. var canvas = document.createElement('canvas');
  147. canvas.width = 512;
  148. canvas.height = 512;
  149. if (arrayBuffer.byteLength == 0) {
  150. canvas.xMvt = x;
  151. canvas.yMvt = y;
  152. canvas.zMvt = z;
  153. that._tileQueue.markTileRendered(canvas);
  154. return canvas;
  155. }
  156. var vectorContext = canvas.getContext('2d');
  157. var features = that._mvtParser.readFeatures(arrayBuffer);
  158. // console.log(features)
  159. var styleFun = that._styleFun();
  160. var extent = [0, 0, 4096, 4096];
  161. var _replayGroup = new ol.render.canvas.ReplayGroup(0, extent,
  162. 8, true, 100);
  163. for (var i = 0; i < features.length; i++) {
  164. var feature = features[i];
  165. // if(!feature.layer){
  166. // feature.properties_.layer = 'road'
  167. // feature.properties_.class = 'path'
  168. // }
  169. var styles = styleFun(features[i], that._resolutions[level]);
  170. for (var j = 0; j < styles.length; j++)
  171. {
  172. // console.log( styles[j])
  173. ol.renderer.vector.renderFeature_(_replayGroup, feature, styles[j], 16);
  174. }
  175. }
  176. _replayGroup.finish();
  177. _replayGroup.replay(vectorContext, that._pixelRatio, that._transform, 0, {}, that._replays, true);
  178. if (that._tileQueue.count > that._cacheSize) {
  179. trimTiles(that._tileQueue, that._cacheSize / 2);
  180. }
  181. canvas.xMvt = x;
  182. canvas.yMvt = y;
  183. canvas.zMvt = z;
  184. // console.log(arrayBuffer.byteLength,canvas.toDataURL("image/png"));
  185. that._tileQueue.markTileRendered(canvas);
  186. delete _replayGroup;
  187. _replayGroup = null;
  188. return canvas;
  189. }(x, y, level);
  190. }
  191. };
  192. MVTProvider.prototype.pickFeatures = function(x, y, level, longitude, latitude) {
  193. return undefined;
  194. };
  195. return new MVTProvider(options);
  196. }