Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

map support config crs MaxNativeZoom #2470

Merged
merged 3 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/GlobalConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ const GlobalConfig = {
//每个Worker Message中封装的task message数量
messagePostRatioPerWorker: 0.3,
//当前运行环境的最大FPS,用户可以手动配置,否则将自动检测并赋值,为地图锁帧渲染准备
maxFPS: 0
maxFPS: 0,
//投影最大层级,即地图最大能放大到多大层级
crsMaxNativeZoom: 22
};
export default GlobalConfig;
245 changes: 127 additions & 118 deletions src/map/spatial-reference/SpatialReference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Transformation from '../../geo/transformation/Transformation';
import { Measurer, DEFAULT } from '../../geo/measurer';
import loadWMTS from './SpatialReference.WMTS'
import loadArcgis from './SpatialReference.Arc'
const MAX_ZOOM = 23;
import GlobalConfig from '../../GlobalConfig';

export type FullExtent = {
top: number
Expand All @@ -22,118 +22,127 @@ export type SpatialReferenceType = {
fullExtent?: FullExtent | JsonExtent;
}

const DefaultSpatialReference: Record<string, SpatialReferenceType> = {
'EPSG:3857': {
'projection': 'EPSG:3857',
'resolutions': (function () {
const resolutions = [];
const d = 2 * 6378137 * Math.PI;
for (let i = 0; i < MAX_ZOOM; i++) {
resolutions[i] = d / (256 * Math.pow(2, i));
}
return resolutions;
})(),
'fullExtent': {
'top': 6378137 * Math.PI,
'left': -6378137 * Math.PI,
'bottom': -6378137 * Math.PI,
'right': 6378137 * Math.PI
}
},
'EPSG:4326': {
'projection': 'EPSG:4326',
'fullExtent': {
'top': 90,
'left': -180,
'bottom': -90,
'right': 180
},
'resolutions': (function () {
const resolutions = [];
for (let i = 0; i < MAX_ZOOM; i++) {
resolutions[i] = 180 / (Math.pow(2, i) * 128);
}
return resolutions;
})()
},
'BAIDU': {
'projection': 'baidu',
'resolutions': (function () {
let res = Math.pow(2, 18);
const resolutions = [];
for (let i = 0; i < MAX_ZOOM; i++) {
resolutions[i] = res;
res *= 0.5;
}
return resolutions;
})(),
'fullExtent': {
'top': 33554432,
'left': -33554432,
'bottom': -33554432,
'right': 33554432
}
},
'IDENTITY': {
'projection': 'identity',
'resolutions': (function () {
let res = Math.pow(2, 8);
const resolutions = [];
for (let i = 0; i < MAX_ZOOM; i++) {
resolutions[i] = res;
res *= 0.5;
}
return resolutions;
})(),
'fullExtent': {
'top': 200000,
'left': -200000,
'bottom': -200000,
'right': 200000
}
},

// TileSystem: [1, -1, -6378137 * Math.PI, 6378137 * Math.PI]
'PRESET-VT-3857': {
'projection': 'EPSG:3857',
'resolutions': (function () {
const resolutions = [];
const d = 6378137 * Math.PI;
for (let i = 0; i < MAX_ZOOM; i++) {
resolutions[i] = d / (256 * Math.pow(2, i));
}
return resolutions;
})(),
'fullExtent': {
'top': 6378137 * Math.PI,
'left': -6378137 * Math.PI,
'bottom': -6378137 * Math.PI,
'right': 6378137 * Math.PI
}
},

'PRESET-VT-4326': {
'projection': 'EPSG:4326',
'fullExtent': {
'top': 90,
'left': -180,
'bottom': -90,
'right': 180
},
'resolutions': (function () {
const resolutions = [];
for (let i = 0; i < MAX_ZOOM; i++) {
resolutions[i] = 180 / 4 / (Math.pow(2, i) * 128);
let DEFAULT_CRS: Record<string, SpatialReferenceType>;



function _getDefaultSpatialReference(): Record<string, SpatialReferenceType> {
if (!DEFAULT_CRS) {
const crsMaxNativeZoom = Math.round(GlobalConfig.crsMaxNativeZoom || 22);
DEFAULT_CRS = {
'EPSG:3857': {
'projection': 'EPSG:3857',
'resolutions': (function () {
const resolutions = [];
const d = 2 * 6378137 * Math.PI;
for (let i = 0; i <= crsMaxNativeZoom; i++) {
resolutions[i] = d / (256 * Math.pow(2, i));
}
return resolutions;
})(),
'fullExtent': {
'top': 6378137 * Math.PI,
'left': -6378137 * Math.PI,
'bottom': -6378137 * Math.PI,
'right': 6378137 * Math.PI
}
},
'EPSG:4326': {
'projection': 'EPSG:4326',
'fullExtent': {
'top': 90,
'left': -180,
'bottom': -90,
'right': 180
},
'resolutions': (function () {
const resolutions = [];
for (let i = 0; i <= crsMaxNativeZoom; i++) {
resolutions[i] = 180 / (Math.pow(2, i) * 128);
}
return resolutions;
})()
},
'BAIDU': {
'projection': 'baidu',
'resolutions': (function () {
let res = Math.pow(2, 18);
const resolutions = [];
for (let i = 0; i <= crsMaxNativeZoom; i++) {
resolutions[i] = res;
res *= 0.5;
}
return resolutions;
})(),
'fullExtent': {
'top': 33554432,
'left': -33554432,
'bottom': -33554432,
'right': 33554432
}
},
'IDENTITY': {
'projection': 'identity',
'resolutions': (function () {
let res = Math.pow(2, 8);
const resolutions = [];
for (let i = 0; i <= crsMaxNativeZoom; i++) {
resolutions[i] = res;
res *= 0.5;
}
return resolutions;
})(),
'fullExtent': {
'top': 200000,
'left': -200000,
'bottom': -200000,
'right': 200000
}
},

// TileSystem: [1, -1, -6378137 * Math.PI, 6378137 * Math.PI]
'PRESET-VT-3857': {
'projection': 'EPSG:3857',
'resolutions': (function () {
const resolutions = [];
const d = 6378137 * Math.PI;
for (let i = 0; i <= crsMaxNativeZoom; i++) {
resolutions[i] = d / (256 * Math.pow(2, i));
}
return resolutions;
})(),
'fullExtent': {
'top': 6378137 * Math.PI,
'left': -6378137 * Math.PI,
'bottom': -6378137 * Math.PI,
'right': 6378137 * Math.PI
}
},

'PRESET-VT-4326': {
'projection': 'EPSG:4326',
'fullExtent': {
'top': 90,
'left': -180,
'bottom': -90,
'right': 180
},
'resolutions': (function () {
const resolutions = [];
for (let i = 0; i <= crsMaxNativeZoom; i++) {
resolutions[i] = 180 / 4 / (Math.pow(2, i) * 128);
}
return resolutions;
})()
}
return resolutions;
})()
};
DEFAULT_CRS['EPSG:4490'] = DEFAULT_CRS['EPSG:4326'];
DEFAULT_CRS['PRESET-3857-512'] = DEFAULT_CRS['PRESET-VT-3857'];
DEFAULT_CRS['PRESET-4326-512'] = DEFAULT_CRS['PRESET-VT-4326'];
DEFAULT_CRS['PRESET-4490-512'] = DEFAULT_CRS['PRESET-VT-4326'];
}
};

DefaultSpatialReference['EPSG:4490'] = DefaultSpatialReference['EPSG:4326'];
DefaultSpatialReference['PRESET-3857-512'] = DefaultSpatialReference['PRESET-VT-3857'];
DefaultSpatialReference['PRESET-4326-512'] = DefaultSpatialReference['PRESET-VT-4326'];
DefaultSpatialReference['PRESET-4490-512'] = DefaultSpatialReference['PRESET-VT-4326'];
return DEFAULT_CRS;
}

/**
* 空间参考类
Expand Down Expand Up @@ -162,18 +171,18 @@ export default class SpatialReference {

static registerPreset(name: string, value: SpatialReferenceType) {
name = name && name.toUpperCase();
if (DefaultSpatialReference[name]) {
if (_getDefaultSpatialReference()[name]) {
console.warn(`Spatial reference ${name} already registered.`);
}
DefaultSpatialReference[name] = value;
_getDefaultSpatialReference()[name] = value;
}

static getPreset(preset: string) {
return DefaultSpatialReference[preset.toUpperCase()];
return _getDefaultSpatialReference()[preset.toUpperCase()];
}

static getAllPresets() {
return Object.keys(DefaultSpatialReference);
return Object.keys(_getDefaultSpatialReference());
}

static loadArcgis(url: string, cb: (_, spatialRef?) => void, options: any) {
Expand Down Expand Up @@ -306,7 +315,7 @@ export default class SpatialReference {
resolutions = this.options['resolutions'];
if (!resolutions) {
if (projection['code']) {
defaultSpatialRef = DefaultSpatialReference[projection['code'].toUpperCase()];
defaultSpatialRef = _getDefaultSpatialReference()[projection['code'].toUpperCase()];
if (defaultSpatialRef) {
resolutions = defaultSpatialRef['resolutions'];
this.isEPSG = projection['code'] !== 'IDENTITY';
Expand All @@ -332,7 +341,7 @@ export default class SpatialReference {
let fullExtent = this.options['fullExtent'];
if (!fullExtent) {
if (projection['code']) {
defaultSpatialRef = DefaultSpatialReference[projection['code'].toUpperCase()];
defaultSpatialRef = _getDefaultSpatialReference()[projection['code'].toUpperCase()];
if (defaultSpatialRef) {
fullExtent = defaultSpatialRef['fullExtent'];
}
Expand Down Expand Up @@ -444,5 +453,5 @@ export default class SpatialReference {


export function getDefaultSpatialReference(): Record<string, SpatialReferenceType> {
return JSON.parse(JSON.stringify(DefaultSpatialReference));
return JSON.parse(JSON.stringify(_getDefaultSpatialReference()));
}
1 change: 1 addition & 0 deletions test/SpecCommon.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ if (maptalks.Browser.ie) {
'markerHeight': 20
};
}
maptalks.GlobalConfig.crsMaxNativeZoom = 25;


/*eslint-disable no-unused-vars */
Expand Down
15 changes: 13 additions & 2 deletions test/map/MapSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ describe('Map.Spec', function () {
var center = new maptalks.Coordinate(118.846825, 32.046534);

beforeEach(function () {

container = document.createElement('div');
container.style.width = '4px';
container.style.height = '3px';
Expand All @@ -27,6 +28,16 @@ describe('Map.Spec', function () {
map.remove();
REMOVE_CONTAINER(container);
});
it('custom crsMaxNativeZoom', function (done) {
const maxZoom = map.getMaxZoom();
expect(maxZoom).to.be.eql(maptalks.GlobalConfig.crsMaxNativeZoom);
const crs = maptalks.getDefaultSpatialReference();
for (const epsg in crs) {
const resolutions = crs[epsg].resolutions;
expect(resolutions.length).to.be.eql(maptalks.GlobalConfig.crsMaxNativeZoom + 1);
}
done();
});

describe('status', function () {
it('has id and is readonly', function () {
Expand Down Expand Up @@ -913,14 +924,14 @@ describe('Map.Spec', function () {
it('#2128 clear glRes cache when SpatialReference change', function (done) {
//clear all layers
const glRes = map.getGLRes();
map.setSpatialReference({ projection:'EPSG:4326' });
map.setSpatialReference({ projection: 'EPSG:4326' });
setTimeout(() => {
expect(map.getGLRes()).not.to.be(glRes);
done();
}, 100);
});

it('map\'s center has altitude', function() {
it('map\'s center has altitude', function () {
const center = map.getCenter();
center.z = 100;
map.setCenter(center);
Expand Down
2 changes: 1 addition & 1 deletion test/map/spatial-reference/SpatialRefUpdateSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe('SpatialReference.Update', function () {
projection: 'baidu',
resolutions: resolutions
};
expect(map.getMaxZoom()).to.be.eql(22);
expect(map.getMaxZoom()).to.be.eql(25);
map.setSpatialReference(spatialReference);
expect(map.getMaxZoom()).to.be.eql(24);
});
Expand Down