Skip to content

坡向分析

坡向分析是通过创建色彩渐变,使地形的坡向以色彩渐变的形式展示出来

地形加载

requestVertexNormals 设置为 true 时会计算法线,用于坡度坡向

Javascript
async loadTerrain() {
  let terrainPath = '';
  try{
    //构建地形数据,参数requestVertexNormals为true时会计算法线,用于坡度坡向
    let terrainLayer = await LSGlobe.LSTerrainProvider.fromUrl(terrainPath, {
      requestVertexNormals: true,
    });
    this.viewer.terrainLayers.addTerrainProvider(terrainLayer);
    //单个地形时
    let rect = terrainLayer.rectangle; //地形范围
    this.viewer.scene.globe.materialRectangle = rect; //设置坡度坡向分析的范围

      //有多个地形时,合并多个地形
      /*
    let rect = this.viewer.terrainLayers.get(0).rectangle;
    for (let i = 0; i < this.viewer.terrainLayers.length; i++) {
      this.viewer.terrainLayers.get(i)._requestVertexNormals = true;
      LSGlobe.Rectangle.union(rect, this.viewer.terrainLayers.get(i).rectangle, rect);
    }
    */
    this.#flyToRectBoundingSphere(rect);
  } catch (error) {
      console.log(`Error loading terrain: ${error}`);
  }
}

创建色带

创建一个 canvas 容器,填充渐变色

javascript
getColorRamp() {
  let ramp = document.createElement('canvas');
  ramp.width = 100;
  ramp.height = 1;
  let ctx = ramp.getContext('2d');
  let values;
  let colors;
  values = aspectAnalysis.colorLegends.map(
    (item) => Math.floor((item.value / 360) * 100) / 100
  );
  colors = aspectAnalysis.colorLegends.map((item) => item.color);
  var grd = ctx.createLinearGradient(0, 0, 100, 1);
  values?.forEach((item, i) => {
    grd.addColorStop(values[i], colors[i]);
  });
  ctx.fillStyle = grd;
  ctx.fillRect(0, 0, 100, 1);
  return ramp;
}

启用坡向分析

javascript
enableAnalysis() {
  //创建材质
  let material = LSGlobe.Material.fromType('AspectRamp');
  //创建加载色带
  let shadingUniforms = material.uniforms;
  shadingUniforms.image = this.#getColorRamp();
  let globe = this.viewer.scene.globe;
  //启用灯光
  globe.enableLighting = true;
  //应用材质
  globe.material = material;
}

完整代码

Javascript
class wish3dEarth {
  viewer = null;
  constructor() {
    if (!LSGlobe?.viewer) {
      this.#initEarth();
    } else {
      this.viewer = LSGlobe.viewer;
    }
    //加载地形
    this.#loadTerrain();
  }
  //初始化地球
  #initEarth() {
    let licenseCode = '';
    let wish3DEarthLicenseUrl = '';
    this.viewer = new LSGlobe.Viewer('lsGlobe', {
      guid: licenseCode,
      licenseUrl: wish3DEarthLicenseUrl,
    });
  }
  //加载地形
  #loadTerrain() {
    let terrainPath = '';
    try{
      //构建地形数据,参数requestVertexNormals为true时会计算法线,用于坡度坡向
      let terrainLayer = await LSGlobe.LSTerrainProvider.fromUrl(terrainPath, {
        requestVertexNormals: true,
      });
      this.viewer.terrainLayers.addTerrainProvider(terrainLayer);
      //单个地形时
      let rect = terrainLayer.rectangle; //地形范围
      this.viewer.scene.globe.materialRectangle = rect; //设置坡度坡向分析的范围

      //有多个地形时,合并多个地形
      /*
      let rect = this.viewer.terrainLayers.get(0).rectangle;
      for (let i = 0; i < this.viewer.terrainLayers.length; i++) {
        this.viewer.terrainLayers.get(i)._requestVertexNormals = true;
        LSGlobe.Rectangle.union(rect, this.viewer.terrainLayers.get(i).rectangle, rect);
      }
      */
        this.#flyToRectBoundingSphere(rect);
    } catch (error) {
        console.log(`Error loading terrain: ${error}`);
    }
  }
  //飞行至rectangle
  #flyToRectBoundingSphere(rect) {
    let boundingSphere = LSGlobe.BoundingSphere.fromRectangle3D(rect);
    const heading = LSGlobe.Math.toRadians(30.0);
    const pitch = LSGlobe.Math.toRadians(-30.0);
    const range = 1.4e4;
    this.viewer.camera.flyToBoundingSphere(boundingSphere, {
      offset: new LSGlobe.HeadingPitchRange(heading, pitch, range),
      maximumHeight: 2e6,
      pitchAdjustHeight: 2e6,
      duration: 4,
    });
  }
}
class aspectAnalysis extends wish3dEarth {
  //色带分级
  static colorLegends = [
    {
      value: 0,
      lable: '北',
      color: '#95f20445',
    },
    {
      value: 45,
      lable: '西北',
      color: '#caf98245',
    },
    {
      value: 90,
      lable: '西',
      color: '#ffff8045',
    },
    {
      value: 135,
      lable: '西南',
      color: '#ffff0045',
    },
    {
      value: 180,
      lable: '南',
      color: '#facd9145',
    },
    {
      value: 225,
      lable: '东南',
      color: '#f59a2345',
    },
    {
      value: 270,
      lable: '东',
      color: '#ec808d45',
    },
    {
      value: 315,
      lable: '东北',
      color: '#d9001b45',
    },
  ];
  constructor() {
    super();
  }
  enableAnalysis() {
    let material = LSGlobe.Material.fromType('AspectRamp');
    let shadingUniforms = material.uniforms;
    shadingUniforms.image = this.#getColorRamp();
    let globe = this.viewer.scene.globe;
    globe.enableLighting = true;
    globe.material = material;
  }
  clearAnalysis() {
    this.viewer.scene.globe.enableLighting = false;
    try {
      if (this.viewer.scene.globe.material !== undefined) {
        this.viewer.scene.globe.material = undefined;
      }
    } catch (error) {
      console.error(error);
    }
  }
  #getColorRamp() {
    let ramp = document.createElement('canvas');
    ramp.width = 100;
    ramp.height = 1;
    let ctx = ramp.getContext('2d');
    let values;
    let colors;
    values = aspectAnalysis.colorLegends.map(
      (item) => Math.floor((item.value / 360) * 100) / 100
    );
    colors = aspectAnalysis.colorLegends.map((item) => item.color);
    var grd = ctx.createLinearGradient(0, 0, 100, 1);
    values?.forEach((item, i) => {
      grd.addColorStop(values[i], colors[i]);
    });
    ctx.fillStyle = grd;
    ctx.fillRect(0, 0, 100, 1);
    return ramp;
  }
}
let analysis = new aspectAnalysis();
analysis.enableAnalysis();
//删除分析
//analysis.clearAnalysis()

示例代码 code

code示例地址

Released under the MIT License.