Skip to content

Commit

Permalink
add custom TilingScheme
Browse files Browse the repository at this point in the history
  • Loading branch information
cavencj committed Apr 11, 2024
1 parent 063ebe4 commit d15eebd
Show file tree
Hide file tree
Showing 7 changed files with 490 additions and 550 deletions.
77 changes: 34 additions & 43 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,59 +3,50 @@
@date : 2023-05-15
*/
import gulp from 'gulp'
import { rollup } from 'rollup'
import commonjs from '@rollup/plugin-commonjs'
import resolve from '@rollup/plugin-node-resolve'
import { babel } from '@rollup/plugin-babel'
import terser from '@rollup/plugin-terser'
import path from 'path'
import esbuild from 'esbuild'
import GlobalsPlugin from 'esbuild-plugin-globals'

async function buildMap(options) {
const bundle = await rollup({
input: 'src/index',
external: ['@cesium/engine'],
plugins: [
commonjs(),
resolve({ preferBuiltins: true }),
babel({
babelHelpers: 'runtime',
presets: [
[
'@babel/preset-env',
{
modules: false,
targets: {
browsers: ['> 1%', 'last 2 versions', 'ie >= 10'],
},
},
],
],
plugins: ['@babel/plugin-transform-runtime'],
}),
terser(),
],
})
const buildConfig = {
entryPoints: ['src/index.js'],
bundle: true,
color: true,
legalComments: `inline`,
logLimit: 0,
target: `es2019`,
minify: true,
sourcemap: false,
write: true,
logLevel: 'info',
plugins: [],
external: ['@cesium/engine'],
}

async function buildMap(options) {
// Build IIFE
if (options.iife) {
await bundle.write({
file: 'dist/cesium.map.min.js',
await esbuild.build({
...buildConfig,
format: 'iife',
name: 'window',
extend: true,
globals: {
'@cesium/engine': 'Cesium',
},
sourcemap: false,
globalName: '',
plugins: [
GlobalsPlugin({
'@cesium/engine': 'Cesium',
}),
],
outfile: path.join('dist', 'cesium.map.min.js'),
})
}

// Build Node
if (options.node) {
await bundle.write({
file: 'dist/index.cjs',
format: 'cjs',
sourcemap: false,
})
if (options.iife) {
await esbuild.build({
...buildConfig,
format: 'esm',
outfile: path.join('dist', 'index.js'),
})
}
}
}

Expand Down
17 changes: 7 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@dvgis/cesium-map",
"version": "3.1.0",
"version": "4.0.0",
"repository": "https://github.com/dvgis/cesium-map.git",
"homepage": "https://www.dvgis.cn",
"author": "Caven Chen <[email protected]>",
Expand All @@ -14,7 +14,7 @@
"tencent",
"tdt"
],
"main": "dist/index.cjs",
"main": "dist/index.js",
"type": "module",
"scripts": {
"build": "rimraf dist && gulp build",
Expand All @@ -28,11 +28,9 @@
"@babel/plugin-proposal-class-properties": "^7.18.6",
"@babel/plugin-transform-runtime": "^7.21.4",
"@babel/preset-env": "^7.21.5",
"@rollup/plugin-babel": "^6.0.3",
"@rollup/plugin-commonjs": "^25.0.0",
"@rollup/plugin-node-resolve": "^15.0.2",
"@rollup/plugin-terser": "^0.4.1",
"esbuild": "^0.20.2",
"esbuild-plugin-external-global": "^1.0.1",
"esbuild-plugin-globals": "^0.2.0",
"eslint": "^8.40.0",
"eslint-config-prettier": "^8.8.0",
"eslint-plugin-import": "^2.27.5",
Expand All @@ -41,11 +39,10 @@
"eslint-plugin-promise": "^6.1.1",
"gulp": "^4.0.2",
"prettier": "^2.8.8",
"rimraf": "^5.0.0",
"rollup": "^3.21.8"
"rimraf": "^5.0.0"
},
"dependencies": {
"@cesium/engine": "^2.4.0"
"peerDependencies": {
"@cesium/engine": "^9.0.0"
},
"files": [
"dist",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const TILE_URL = {
cva: '//webst{s}.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=8&x={x}&y={y}&z={z}',
}

class AmapImageryProvider extends UrlTemplateImageryProvider {
class AMapImageryProvider extends UrlTemplateImageryProvider {
constructor(options = {}) {
options['url'] =
options.url ||
Expand All @@ -29,4 +29,4 @@ class AmapImageryProvider extends UrlTemplateImageryProvider {
super(options)
}
}
export default AmapImageryProvider
export default AMapImageryProvider
68 changes: 68 additions & 0 deletions src/imagery/tiling-scheme/CustomGeographicTilingScheme.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* @Author: Caven
* @Date: 2024-04-06 20:40:25
*/
import {
GeographicTilingScheme,
Math as CesiumMath,
Cartesian2,
Rectangle,
defined,
} from '@cesium/engine'

class CustomGeographicTilingScheme extends GeographicTilingScheme {
constructor(options = {}) {
super(options)
this._origin = options.origin || [-180, 90]
this._zoomOffset = options.zoomOffset || 0
this._tileSize = options.tileSize || 256
this._resolutions = options.resolutions || []
}

get zoomOffset() {
return this._zoomOffset
}

tileXYToRectangle(x, y, level, result) {
if (!this._resolutions || !this._resolutions[level + this._zoomOffset]) {
return Rectangle.MAX_VALUE
}

const tileRes = this._resolutions[level + this._zoomOffset] * this._tileSize
const west = CesiumMath.toRadians(this._origin[0] + x * tileRes)
const south = CesiumMath.toRadians(this._origin[1] - (y + 1) * tileRes)
const east = CesiumMath.toRadians(this._origin[0] + (x + 1) * tileRes)
const north = CesiumMath.toRadians(this._origin[1] - y * tileRes)
if (!defined(result)) {
return new Rectangle(west, south, east, north)
}
result.west = west
result.south = south
result.east = east
result.north = north
return result
}

positionToTileXY(position, level, result) {
if (!this._resolutions || !this._resolutions[level + this._zoomOffset]) {
return new Cartesian2()
}
const tileRes = this._resolutions[level + this._zoomOffset] * this._tileSize
const longitude = CesiumMath.toDegrees(position.longitude)
const latitude = CesiumMath.toDegrees(position.latitude)
// Calculate the tile row and column numbers in the current coordinate system
const xTileCoordinate = Math.floor((longitude - this._origin[0]) / tileRes)
const yTileCoordinate = Math.floor((this._origin[1] - latitude) / tileRes)
if (!defined(result)) {
return new Cartesian2(
Math.max(0, xTileCoordinate),
Math.max(0, yTileCoordinate)
)
}
result.x = xTileCoordinate
result.y = yTileCoordinate
return result
}
}

export default CustomGeographicTilingScheme
82 changes: 82 additions & 0 deletions src/imagery/tiling-scheme/CustomMercatorTilingScheme.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/**
* @Author: Caven
* @Date: 2024-04-06 20:40:25
*/

import {
WebMercatorTilingScheme,
Cartesian2,
Rectangle,
defined,
} from '@cesium/engine'

class CustomMercatorTilingScheme extends WebMercatorTilingScheme {
constructor(options = {}) {
super(options)
this._origin = options.origin || [-20037508.3427892, 20037508.3427892]
this._zoomOffset = options.zoomOffset || 0
this._tileSize = options.tileSize || 256
this._resolutions = options.resolutions || []
}

get zoomOffset() {
return this._zoomOffset
}

tileXYToNativeRectangle(x, y, level, result) {
if (!this._resolutions || !this._resolutions[level + this._zoomOffset]) {
return Rectangle.MAX_VALUE
}

if (x < 0 || y < 0) {
return Rectangle.MAX_VALUE
}
const tileRes = this._resolutions[level + this._zoomOffset] * this._tileSize
let west = this._origin[0] + x * tileRes
let south = this._origin[1] - (y + 1) * tileRes
let east = this._origin[0] + (x + 1) * tileRes
let north = this._origin[1] - y * tileRes

if (!defined(result)) {
return new Rectangle(west, south, east, north)
}
result.west = west
result.south = south
result.east = east
result.north = north
return result
}

positionToTileXY(position, level, result) {
const rectangle = this._rectangle
if (!Rectangle.contains(rectangle, position)) {
return undefined
}
if (!this._resolutions || !this._resolutions[level + this._zoomOffset]) {
return new Cartesian2()
}
const tileRes = this._resolutions[level + this._zoomOffset] * this._tileSize
const projection = this._projection
const webMercatorPosition = projection.project(position)

// Calculate the tile row and column numbers in the current coordinate system
const xTileCoordinate = Math.floor(
(webMercatorPosition.x - this._origin[0]) / tileRes
)

const yTileCoordinate = Math.floor(
(this._origin[1] - webMercatorPosition.y) / tileRes
)
if (!defined(result)) {
return new Cartesian2(
Math.max(0, xTileCoordinate),
Math.max(0, yTileCoordinate)
)
}
result.x = xTileCoordinate
result.y = yTileCoordinate
return result
}
}

export default CustomMercatorTilingScheme
36 changes: 30 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,33 @@
@date : 2023-05-18
*/

export { default as BaiduImageryProvider } from './imagery/baidu/BaiduImageryProvider'
export { default as AmapImageryProvider } from './imagery/amap/AmapImageryProvider'
export { default as TencentImageryProvider } from './imagery/tencent/TencentImageryProvider'
export { default as TdtImageryProvider } from './imagery/tdt/TdtImageryProvider'
export { default as GoogleImageryProvider } from './imagery/google/GoogleImageryProvider'
export { default as GeoVisImageryProvider } from './imagery/geovis/GeoVisImageryProvider.js'
import BaiduImageryProvider from './imagery/baidu/BaiduImageryProvider'
import AMapImageryProvider from './imagery/amap/AMapImageryProvider.js'
import TencentImageryProvider from './imagery/tencent/TencentImageryProvider'
import TdtImageryProvider from './imagery/tdt/TdtImageryProvider'
import GoogleImageryProvider from './imagery/google/GoogleImageryProvider'
import GeoVisImageryProvider from './imagery/geovis/GeoVisImageryProvider.js'
import CustomGeographicTilingScheme from './imagery/tiling-scheme/CustomGeographicTilingScheme.js'
import CustomMercatorTilingScheme from './imagery/tiling-scheme/CustomMercatorTilingScheme.js'

if (Cesium) {
Cesium.BaiduImageryProvider = BaiduImageryProvider
Cesium.AMapImageryProvider = AMapImageryProvider
Cesium.TencentImageryProvider = TencentImageryProvider
Cesium.TdtImageryProvider = TdtImageryProvider
Cesium.GoogleImageryProvider = GoogleImageryProvider
Cesium.GeoVisImageryProvider = GeoVisImageryProvider
Cesium.CustomGeographicTilingScheme = CustomGeographicTilingScheme
Cesium.CustomMercatorTilingScheme = CustomMercatorTilingScheme
}

export {
BaiduImageryProvider,
AMapImageryProvider,
TencentImageryProvider,
TdtImageryProvider,
GoogleImageryProvider,
GeoVisImageryProvider,
CustomGeographicTilingScheme,
CustomMercatorTilingScheme,
}
Loading

0 comments on commit d15eebd

Please sign in to comment.