surprise
2023-12-29 18377dc5d61caf3a6a0835e17015ac2601f8709d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
import CustomMercatorProjection from './CustomMercatorProjection.js';
/**
 * A tiling scheme for geometry referenced to a {@link WebMercatorProjection}, EPSG:3857.  This is
 * the tiling scheme used by Google Maps, Microsoft Bing Maps, and most of ESRI ArcGIS Online.
 *
 * @alias BJ2000TilingScheme2
 * @constructor
 *
 * @param {Object} [options] Object with the following properties:
 * @param {Ellipsoid} [options.ellipsoid=Ellipsoid.WGS84] The ellipsoid whose surface is being tiled. Defaults to
 * the WGS84 ellipsoid.
 * @param {Number} [options.numberOfLevelZeroTilesX=1] The number of tiles in the X direction at level zero of
 *        the tile tree.
 * @param {Number} [options.numberOfLevelZeroTilesY=1] The number of tiles in the Y direction at level zero of
 *        the tile tree.
 * @param {Cartesian2} [options.rectangleSouthwestInMeters] The southwest corner of the rectangle covered by the
 *        tiling scheme, in meters.  If this parameter or rectangleNortheastInMeters is not specified, the entire
 *        globe is covered in the longitude direction and an equal distance is covered in the latitude
 *        direction, resulting in a square projection.
 * @param {Cartesian2} [options.rectangleNortheastInMeters] The northeast corner of the rectangle covered by the
 *        tiling scheme, in meters.  If this parameter or rectangleSouthwestInMeters is not specified, the entire
 *        globe is covered in the longitude direction and an equal distance is covered in the latitude
 *        direction, resulting in a square projection.
 */
var Cesium = null;
function Bj2000TilingScheme(options) {
  Cesium = options.SmartEarth;
  options = Cesium.defaultValue(options, Cesium.defaultValue.EMPTY_OBJECT);
  this._ellipsoid = Cesium.defaultValue(options.ellipsoid, Cesium.Ellipsoid.WGS84);
  this._numberOfLevelZeroTilesX = Cesium.defaultValue(
    options.numberOfLevelZeroTilesX,
    1
  );
  this._numberOfLevelZeroTilesY = Cesium.defaultValue(
    options.numberOfLevelZeroTilesY,
    1
  );
  if(options.customEPSG){
    this._projection = new CustomMercatorProjection(options.customEPSG,this._ellipsoid,Cesium,options.proj);
  }else {
    this._projection = new Cesium.WebMercatorProjection(this._ellipsoid);
  }
  if (
    Cesium.defined(options.rectangleSouthwestInMeters) &&
    Cesium.defined(options.rectangleNortheastInMeters)
  ) {
    this._rectangleSouthwestInMeters = options.rectangleSouthwestInMeters;
    this._rectangleNortheastInMeters = options.rectangleNortheastInMeters;
  } else {
    const semimajorAxisTimesPi = this._ellipsoid.maximumRadius * Math.PI;
    this._rectangleSouthwestInMeters = new Cesium.Cartesian2(
      -semimajorAxisTimesPi,
      -semimajorAxisTimesPi
    );
    this._rectangleNortheastInMeters = new Cesium.Cartesian2(
      semimajorAxisTimesPi,
      semimajorAxisTimesPi
    );
  }
 
  const southwest = this._projection.unproject(
    this._rectangleSouthwestInMeters
  );
  const northeast = this._projection.unproject(
    this._rectangleNortheastInMeters
  );
  this._rectangle = new Cesium.Rectangle(
    southwest.longitude,
    southwest.latitude,
    northeast.longitude,
    northeast.latitude
  );
  this._resolutions = Cesium.defaultValue(options.resolutions,[0]);
  this._origin = Cesium.defaultValue(options.origin, [0,0]);
  this.tileWidth = Cesium.defaultValue(options.tileWidth, 256);
  this.tileHeight = Cesium.defaultValue(options.tileHeight, 256);
}
 
Object.defineProperties(Bj2000TilingScheme.prototype, {
  /**
   * Gets the ellipsoid that is tiled by this tiling scheme.
   * @memberof Bj2000TilingScheme.prototype
   * @type {Ellipsoid}
   */
  ellipsoid: {
    get: function () {
      return this._ellipsoid;
    },
  },
 
  /**
   * Gets the rectangle, in radians, covered by this tiling scheme.
   * @memberof Bj2000TilingScheme.prototype
   * @type {Rectangle}
   */
  rectangle: {
    get: function () {
      return this._rectangle;
    },
  },
 
  /**
   * Gets the map projection used by this tiling scheme.
   * @memberof Bj2000TilingScheme.prototype
   * @type {MapProjection}
   */
  projection: {
    get: function () {
      return this._projection;
    },
  },
  rectangleNortheastInMeters:{
    get: function() {
      return this._rectangleNortheastInMeters;
    }
  },
  rectangleSouthwestInMeters:{
    get: function() {
      return this._rectangleSouthwestInMeters;
    }
  },
});
 
/**
 * Gets the total number of tiles in the X direction at a specified level-of-detail.
 *
 * @param {Number} level The level-of-detail.
 * @returns {Number} The number of tiles in the X direction at the given level.
 */
Bj2000TilingScheme.prototype.getNumberOfXTilesAtLevel = function (level) {
  return this._numberOfLevelZeroTilesX << level;
};
 
/**
 * Gets the total number of tiles in the Y direction at a specified level-of-detail.
 *
 * @param {Number} level The level-of-detail.
 * @returns {Number} The number of tiles in the Y direction at the given level.
 */
Bj2000TilingScheme.prototype.getNumberOfYTilesAtLevel = function (level) {
  return this._numberOfLevelZeroTilesY << level;
};
 
/**
 * Transforms a rectangle specified in geodetic radians to the native coordinate system
 * of this tiling scheme.
 *
 * @param {Rectangle} rectangle The rectangle to transform.
 * @param {Rectangle} [result] The instance to which to copy the result, or undefined if a new instance
 *        should be created.
 * @returns {Rectangle} The specified 'result', or a new object containing the native rectangle if 'result'
 *          is undefined.
 */
Bj2000TilingScheme.prototype.rectangleToNativeRectangle = function (
  rectangle,
  result
) {
  const projection = this._projection;
  const southwest = projection.project(Cesium.Rectangle.southwest(rectangle));
  const northeast = projection.project(Cesium.Rectangle.northeast(rectangle));
 
  if (!Cesium.defined(result)) {
    return new Cesium.Rectangle(southwest.x, southwest.y, northeast.x, northeast.y);
  }
 
  result.west = southwest.x;
  result.south = southwest.y;
  result.east = northeast.x;
  result.north = northeast.y;
  return result;
};
 
/**
 * Converts tile x, y coordinates and level to a rectangle expressed in the native coordinates
 * of the tiling scheme.
 *
 * @param {Number} x The integer x coordinate of the tile.
 * @param {Number} y The integer y coordinate of the tile.
 * @param {Number} level The tile level-of-detail.  Zero is the least detailed.
 * @param {Object} [result] The instance to which to copy the result, or undefined if a new instance
 *        should be created.
 * @returns {Rectangle} The specified 'result', or a new object containing the rectangle
 *          if 'result' is undefined.
 */
Bj2000TilingScheme.prototype.tileXYToNativeRectangle = function (
  x,
  y,
  level,
  result
) {
  level +=6;
  const xTiles = this.getNumberOfXTilesAtLevel(level);
  const yTiles = this.getNumberOfYTilesAtLevel(level);
 
  const resolution = this._resolutions[level];
  const eastFromOrigin = this._rectangleNortheastInMeters.x - this._origin[0];
  const southFromOrigin = this._origin[1] - this._rectangleSouthwestInMeters.y;
  const xTileWidth = eastFromOrigin / xTiles;
  const yTileHeight = southFromOrigin / yTiles;
 
  // let west = this._origin[0] + x * xTileWidth;
  // let east = this._origin[0] + (x + 1) * xTileWidth;
  // if(west < this._rectangleSouthwestInMeters.x){
  //   west = this._rectangleSouthwestInMeters.x;
  // }
  // if(east >= this._rectangleNortheastInMeters.x) {
  //   east = this._rectangleNortheastInMeters.x;
  // }
  const west = this._origin[0] + x * this.tileWidth * resolution;
  const east = west + this.tileWidth * resolution;
 
  // let north = this._origin[1] - y * yTileHeight;
  // let south = this._origin[1] - (y + 1) * yTileHeight;
  // if(south < this._rectangleSouthwestInMeters.y) {
  //   south = this._rectangleSouthwestInMeters.y;
  // }
  // if(north > this._rectangleNortheastInMeters.y) {
  //   north = this._rectangleNortheastInMeters.y;
  // }
 
  const south = this._origin[1] - (y + 1) * this.tileHeight * resolution;
  const north = south + this.tileHeight * resolution;
 
  if (!Cesium.defined(result)) {
    return new Cesium.Rectangle(west, south, east, north);
  }
 
  result.west = west;
  result.south = south;
  result.east = east;
  result.north = north;
  return result;
};
 
/**
 * Converts tile x, y coordinates and level to a cartographic rectangle in radians.
 *
 * @param {Number} x The integer x coordinate of the tile.
 * @param {Number} y The integer y coordinate of the tile.
 * @param {Number} level The tile level-of-detail.  Zero is the least detailed.
 * @param {Object} [result] The instance to which to copy the result, or undefined if a new instance
 *        should be created.
 * @returns {Rectangle} The specified 'result', or a new object containing the rectangle
 *          if 'result' is undefined.
 */
Bj2000TilingScheme.prototype.tileXYToRectangle = function (
  x,
  y,
  level,
  result
) {
  const nativeRectangle = this.tileXYToNativeRectangle(x, y, level, result);
  const projection = this._projection;
  const southwest = projection.unproject(
    new Cesium.Cartesian2(nativeRectangle.west, nativeRectangle.south)
  );
  const northeast = projection.unproject(
    new Cesium.Cartesian2(nativeRectangle.east, nativeRectangle.north)
  );
 
  nativeRectangle.west = southwest.longitude;
  nativeRectangle.south = southwest.latitude;
  nativeRectangle.east = northeast.longitude;
  nativeRectangle.north = northeast.latitude;
  return nativeRectangle;
};
 
/**
 * Calculates the tile x, y coordinates of the tile containing
 * a given cartographic position.
 *
 * @param {Cartographic} position The position.
 * @param {Number} level The tile level-of-detail.  Zero is the least detailed.
 * @param {Cartesian2} [result] The instance to which to copy the result, or undefined if a new instance
 *        should be created.
 * @returns {Cartesian2} The specified 'result', or a new object containing the tile x, y coordinates
 *          if 'result' is undefined.
 */
Bj2000TilingScheme.prototype.positionToTileXY = function (
  position,
  level,
  result
) {
  const rectangle = this._rectangle;
  if (!Cesium.Rectangle.contains(rectangle, position)) {
    // outside the bounds of the tiling scheme
    return undefined;
  }
  level +=6;
 
  const xTiles = this.getNumberOfXTilesAtLevel(level);
  const yTiles = this.getNumberOfYTilesAtLevel(level);
  //
  // const overallWidth =
  //   this._rectangleNortheastInMeters.x - this._rectangleSouthwestInMeters.x;
  // const xTileWidth = overallWidth / xTiles;
  // const overallHeight =
  //   this._rectangleNortheastInMeters.y - this._rectangleSouthwestInMeters.y;
  // const yTileHeight = overallHeight / yTiles;
 
  const projection = this._projection;
 
  const webMercatorPosition = projection.project(position);
  const resolution = this._resolutions[level];
  const realTileWidth = resolution * this.tileWidth;
  const realTileHeight = resolution * this.tileHeight;
 
  let xTileCoordinate = (webMercatorPosition.x - this._origin[0]) / resolution / this.tileWidth;
  let yTileCoordinate = (this._origin[1] - webMercatorPosition.y) / resolution / this.tileHeight;
  xTileCoordinate = Math.floor(xTileCoordinate);
  yTileCoordinate = Math.floor(yTileCoordinate);
 
  // const westFromOrigin = this._rectangleSouthwestInMeters.x - this._origin[0];
  // const eastFromOrigin = this._rectangleNortheastInMeters.x - this._origin[0];
  // const northFromOrigin = this._origin[1] - this._rectangleNortheastInMeters.y;
  // const southFromOrigin = this._origin[1] - this._rectangleSouthwestInMeters.y;
  // const xTileWidth = eastFromOrigin / xTiles;
  // const yTileHeight = southFromOrigin / yTiles;
  // let xTileMin = Math.floor(westFromOrigin / realTileWidth) | 0;
  // if (xTileMin >= xTiles) {
  //   xTileMin = xTiles - 1;
  // }
  // let xTileMax = Math.floor(eastFromOrigin / realTileWidth) | 0;
  // if ( xTileMax >= xTiles) {
  //   xTileMax = xTiles - 1;
  // }
  // let yTileMin = Math.floor(northFromOrigin / realTileHeight) | 0;
  // if (yTileMin >= yTiles) {
  //   yTileMin = yTiles - 1;
  // }
  // let yTileMax = Math.floor(southFromOrigin / realTileHeight) | 0;
  // if(yTileMax >= yTiles) {
  //   yTileMax = yTiles - 1;
  // }
 
  // const distanceFromWest = webMercatorPosition.x - this._origin[0];
  // const distanceFromNorth = this._origin[1] - webMercatorPosition.y;
  // let xTileCoordinate = (distanceFromWest / xTileWidth) | 0;
  // if (xTileCoordinate < xTileMin) {
  //   xTileCoordinate = xTileMin;
  //   // return undefined;
  // }
  // if ( xTileCoordinate > xTileMax ) {
  //   xTileCoordinate = xTileMax;
  //   // return undefined;
  // }
  // if (xTileCoordinate >= xTiles) {
  //   xTileCoordinate = xTiles - 1;
  // }
 
  // let yTileCoordinate = (distanceFromNorth / yTileHeight) | 0;
  // if (yTileCoordinate < yTileMin) {
  //   yTileCoordinate = yTileMin;
  //   // return undefined;
  // }
  // if (yTileCoordinate > yTileMax) {
  //   yTileCoordinate = yTileMax;
  //   // return undefined;
  // }
  // if (yTileCoordinate >= yTiles) {
  //   yTileCoordinate = yTiles - 1;
  // }
 
  if (!Cesium.defined(result)) {
    return new Cesium.Cartesian2(xTileCoordinate, yTileCoordinate);
  }
 
  result.x = xTileCoordinate;
  result.y = yTileCoordinate;
  return result;
};
export default Bj2000TilingScheme;