|
|
@@ -23,6 +23,10 @@ function addS3mLayers(scps, callback) {
|
|
|
|
|
|
// 添加场景
|
|
|
function addScene(url, options, callback) {
|
|
|
+ console.log('===== addScene 开始 =====');
|
|
|
+ console.log('URL:', url);
|
|
|
+ console.log('选项:', options);
|
|
|
+
|
|
|
if (options && options.SceneToken) {
|
|
|
Cesium.Credential.CREDENTIAL = new Cesium.Credential(options.SceneToken);
|
|
|
}
|
|
|
@@ -30,17 +34,130 @@ function addScene(url, options, callback) {
|
|
|
if (options && options.autoSetView !== undefined) {
|
|
|
flag = options.autoSetView
|
|
|
}
|
|
|
+ // 提取透明色配置
|
|
|
+ const transparentColor = options && options.transparentColor;
|
|
|
+ const transparentColorTolerance = options && options.transparentColorTolerance || 0.01;
|
|
|
+
|
|
|
+ console.log('transparentColor:', transparentColor);
|
|
|
+ console.log('transparentColorTolerance:', transparentColorTolerance);
|
|
|
+
|
|
|
if (checkURL(url)) {
|
|
|
try {
|
|
|
+ console.log('开始调用 viewer.scene.open');
|
|
|
let s = [viewer.scene.open(url, undefined, { 'autoSetView': flag })];
|
|
|
- promiseWhen(s, callback, 'SCENE');
|
|
|
+ promiseWhen(s, function(layers, type) {
|
|
|
+ console.log('promiseWhen 回调触发');
|
|
|
+ console.log('回调参数layers:', layers);
|
|
|
+ console.log('viewer.scene.layers:', viewer.scene.layers);
|
|
|
+
|
|
|
+ // 如果配置了透明色,在图层加载完成后设置
|
|
|
+ if (transparentColor) {
|
|
|
+ console.log('准备设置透明色');
|
|
|
+
|
|
|
+ // 尝试从多个来源获取图层
|
|
|
+ let targetLayers = [];
|
|
|
+
|
|
|
+ // 1. 尝试从回调参数获取(处理嵌套数组的情况)
|
|
|
+ if (layers && layers.length > 0) {
|
|
|
+ console.log('从回调参数获取图层');
|
|
|
+ // 处理嵌套数组:layers 可能是 [Array(1)],真正的图层在 layers[0] 中
|
|
|
+ // 影像图层可能有 transparentBackColor 属性
|
|
|
+ if (layers[0] && layers[0].length > 0) {
|
|
|
+ targetLayers = layers[0];
|
|
|
+ console.log('图层在嵌套数组中,已提取');
|
|
|
+ } else if (layers[0]) {
|
|
|
+ targetLayers = layers;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 2. 尝试从图层队列获取
|
|
|
+ else if (viewer.scene.layers && viewer.scene.layers.layerQueue && viewer.scene.layers.layerQueue.length > 0) {
|
|
|
+ console.log('从layerQueue获取图层');
|
|
|
+ targetLayers = viewer.scene.layers.layerQueue;
|
|
|
+ }
|
|
|
+ // 3. 尝试从_layers属性获取(可能是Map或Set)
|
|
|
+ else if (viewer.scene.layers && viewer.scene.layers._layers) {
|
|
|
+ console.log('从_layers属性获取图层');
|
|
|
+ const layersObj = viewer.scene.layers._layers;
|
|
|
+ if (layersObj && layersObj.length > 0) {
|
|
|
+ targetLayers = Array.from(layersObj);
|
|
|
+ } else if (layersObj && layersObj.values) {
|
|
|
+ targetLayers = Array.from(layersObj.values());
|
|
|
+ } else if (layersObj && typeof layersObj === 'object') {
|
|
|
+ targetLayers = Object.values(layersObj);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 4. 尝试从viewer.imageryLayers获取(影像图层可能在这里)
|
|
|
+ else if (viewer.imageryLayers && viewer.imageryLayers.length > 0) {
|
|
|
+ console.log('从viewer.imageryLayers获取图层');
|
|
|
+ targetLayers = [];
|
|
|
+ for (let i = 0; i < viewer.imageryLayers.length; i++) {
|
|
|
+ targetLayers.push(viewer.imageryLayers.get(i));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ console.log('最终目标图层数组:', targetLayers);
|
|
|
+
|
|
|
+ if (targetLayers.length > 0) {
|
|
|
+ console.log('找到图层,开始设置透明色');
|
|
|
+ targetLayers.forEach(layer => {
|
|
|
+ console.log('图层名称:', layer._name || layer.name);
|
|
|
+ console.log('图层类型:', layer.constructor.name);
|
|
|
+
|
|
|
+ const colorObj = Cesium.Color.fromCssColorString(transparentColor);
|
|
|
+
|
|
|
+ if (layer && layer.style3D) {
|
|
|
+ // S3M图层
|
|
|
+ layer.style3D.transparentColor = colorObj;
|
|
|
+ layer.style3D.transparentColorTolerance = transparentColorTolerance;
|
|
|
+ layer.style3D.enableTransparent = true;
|
|
|
+ layer.refresh();
|
|
|
+ console.log('S3M图层透明色设置成功:', transparentColor);
|
|
|
+ } else if (layer && layer.imageryLayer) {
|
|
|
+ // 包装的影像图层
|
|
|
+ layer.imageryLayer.transparentBackColor = colorObj;
|
|
|
+ layer.imageryLayer.transparentBackColorTolerance = transparentColorTolerance;
|
|
|
+ console.log('包装影像图层透明色设置成功:', transparentColor);
|
|
|
+ } else if (layer && 'transparentBackColor' in layer) {
|
|
|
+ // 直接是影像图层(使用transparentBackColor属性)
|
|
|
+ layer.transparentBackColor = colorObj;
|
|
|
+ layer.transparentBackColorTolerance = transparentColorTolerance;
|
|
|
+ console.log('影像图层透明色设置成功:', transparentColor);
|
|
|
+ console.log('设置的transparentBackColor:', layer.transparentBackColor);
|
|
|
+ console.log('设置的transparentBackColorTolerance:', layer.transparentBackColorTolerance);
|
|
|
+ } else if (layer && layer._imageryProvider) {
|
|
|
+ // 尝试在imageryProvider上设置
|
|
|
+ if (layer._imageryProvider && 'transparentColor' in layer._imageryProvider) {
|
|
|
+ layer._imageryProvider.transparentColor = colorObj;
|
|
|
+ layer._imageryProvider.transparentColorTolerance = transparentColorTolerance;
|
|
|
+ console.log('ImageryProvider透明色设置成功:', transparentColor);
|
|
|
+ } else {
|
|
|
+ console.log('图层有_imageryProvider但不支持透明色设置');
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ console.log('图层不支持透明色设置,尝试输出图层详情:', layer);
|
|
|
+ if (layer) {
|
|
|
+ console.log('图层所有属性:', Object.keys(layer));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ } else {
|
|
|
+ console.log('未找到任何图层');
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ console.log('没有配置透明色,跳过设置');
|
|
|
+ }
|
|
|
+ callback(layers, type);
|
|
|
+ }, 'SCENE');
|
|
|
} catch (e) {
|
|
|
+ console.error('addScene 错误:', e);
|
|
|
let widget = viewer.cesiumWidget;
|
|
|
if (widget._showRenderLoopErrors) {
|
|
|
let title = "渲染时发生错误,已停止渲染。";
|
|
|
widget.showErrorPanel(title, undefined, e);
|
|
|
}
|
|
|
}
|
|
|
+ } else {
|
|
|
+ console.log('URL检查失败');
|
|
|
}
|
|
|
};
|
|
|
|
|
|
@@ -198,6 +315,59 @@ function addMvtLayer(LayerURL, name, callback) {
|
|
|
}
|
|
|
};
|
|
|
|
|
|
+// 验证GeoJSON坐标数据
|
|
|
+function validateGeoJsonCoordinates(coordinates, geometryType) {
|
|
|
+ try {
|
|
|
+ if (!coordinates || !Array.isArray(coordinates)) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ let coordArrays = [];
|
|
|
+
|
|
|
+ if (geometryType === 'Polygon') {
|
|
|
+ // Polygon: [[[lon, lat], ...]]
|
|
|
+ if (Array.isArray(coordinates[0]) && Array.isArray(coordinates[0][0])) {
|
|
|
+ coordArrays = coordinates[0];
|
|
|
+ }
|
|
|
+ } else if (geometryType === 'MultiPolygon') {
|
|
|
+ // MultiPolygon: [[[[lon, lat], ...]]]
|
|
|
+ if (Array.isArray(coordinates[0]) && Array.isArray(coordinates[0][0]) && Array.isArray(coordinates[0][0][0])) {
|
|
|
+ coordArrays = coordinates[0][0];
|
|
|
+ }
|
|
|
+ } else if (geometryType === 'LineString') {
|
|
|
+ // LineString: [[lon, lat], ...]
|
|
|
+ if (Array.isArray(coordinates[0])) {
|
|
|
+ coordArrays = coordinates;
|
|
|
+ }
|
|
|
+ } else if (geometryType === 'Point') {
|
|
|
+ // Point: [lon, lat]
|
|
|
+ if (typeof coordinates[0] === 'number') {
|
|
|
+ return typeof coordinates[0] === 'number' && typeof coordinates[1] === 'number' &&
|
|
|
+ !isNaN(coordinates[0]) && !isNaN(coordinates[1]) &&
|
|
|
+ Math.abs(coordinates[0]) <= 180 && Math.abs(coordinates[1]) <= 90;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ for (const coord of coordArrays) {
|
|
|
+ if (!coord || !Array.isArray(coord) || coord.length < 2) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ const lon = coord[0];
|
|
|
+ const lat = coord[1];
|
|
|
+ if (typeof lon !== 'number' || typeof lat !== 'number' ||
|
|
|
+ isNaN(lon) || isNaN(lat) || !isFinite(lon) || !isFinite(lat) ||
|
|
|
+ Math.abs(lon) > 180 || Math.abs(lat) > 90) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return coordArrays.length >= 2; // 至少需要2个点(线)或3个点(面)
|
|
|
+ } catch (e) {
|
|
|
+ console.error('验证GeoJSON坐标失败:', e);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
// 加载GeoJson图层
|
|
|
function addGeoJsonLayer(url, name, callback) {
|
|
|
try {
|
|
|
@@ -215,16 +385,23 @@ function addGeoJsonLayer(url, name, callback) {
|
|
|
});
|
|
|
|
|
|
Cesium.when(loadPromise, function (dataSource) {
|
|
|
- viewer.dataSources.add(dataSource);
|
|
|
- processGeoJsonEntities(dataSource, name);
|
|
|
- viewer.flyTo(dataSource, { duration: 2 });
|
|
|
- actions.setChangeLayers();
|
|
|
- if (callback) callback(dataSource);
|
|
|
+ try {
|
|
|
+ viewer.dataSources.add(dataSource);
|
|
|
+ processGeoJsonEntities(dataSource, name);
|
|
|
+ viewer.flyTo(dataSource, { duration: 2 });
|
|
|
+ actions.setChangeLayers();
|
|
|
+ if (callback) callback(dataSource);
|
|
|
+ } catch (e) {
|
|
|
+ console.error("添加GeoJSON数据源失败:", e);
|
|
|
+ if (callback) callback(null, e);
|
|
|
+ }
|
|
|
}, function (error) {
|
|
|
console.error("加载GeoJSON失败:", error);
|
|
|
+ if (callback) callback(null, error);
|
|
|
});
|
|
|
} catch (e) {
|
|
|
console.error("加载GeoJSON异常:", e);
|
|
|
+ if (callback) callback(null, e);
|
|
|
}
|
|
|
};
|
|
|
|