5. 分析

优质
小牛编辑
130浏览
2023-12-01

5.1. 光照分析

//是否开启光照
viewer.shadows=true;//光照分析时候一定要设置true

//光照阴影位置和当前的时间有关
//设置当前时间可以改变阴影的位置
var julianDay =computes(new Date());
//当天已经过去的时间单位s
var julianSecound=hour*60*60+minute*60+secound;//hour当前时,minute当前分,secound当前秒
viewer.clock.currentTime= new LSGlobe.JulianDate(julianDay,julianSecound);

//当前时间转换为朱莉安时间
function computes(now){
    var month = now.getUTCMonth() + 1;
    var day = now.getUTCDate();
    var year = now.getUTCFullYear()
    var y = eval(year);
    var m = eval(month);
    var d = eval(day);
    var extra = 100.0*y + m - 190002.5;
    var rjd = 367.0*y;
    rjd -= Math.floor(7.0*(y+Math.floor((m+9.0)/12.0))/4.0);
    rjd += Math.floor(275.0*m/9.0);
    rjd += d;
    rjd += 1721013.5;
    rjd -= 0.5*extra/Math.abs(extra);
    rjd += 0.5;
    return rjd;
}

5.2. 可视域分析

//1).初始化可视域分析
var viewshed3ding = false; //用于是否正在绘制可视域分析
var viewshed3d; //当前可视域对象
var aViewshed3d = []; //保存可视域分析的数组
//初始化可视域对象
viewshed3d = new LSGlobe.Viewshed3D(viewer.scene);
//将可视域分析对象加入到可是与分析数组中
aViewshed3d.push(viewshed3d);

//2).绘制可视域分析
//初始化鼠标事件
var ViewAnalysisHandler = new LSGlobe.ScreenSpaceEventHandler(viewer.scene.canvas);
ViewAnalysisHandler.setInputAction(function(movement) {
    var cartesian = scene.pickGlobe(movement.position);
    var cartographic = LSGlobe.Cartographic.fromCartesian(cartesian);
    var currentClickLon = LSGlobe.Math.toDegrees(cartographic.longitude);
    var currentClickLat = LSGlobe.Math.toDegrees(cartographic.latitude);
    var CurrentTileSet = whichTileset(currentClickLon, currentClickLat);
    //判断点是否在模型上函数和压平获取压平对象相同
    //可视域分析点必须在模型上
    if (CurrentTileSet.length < 1) {
        //如果点没有点在模型上不在向下走
        return;
    }
    if (cartesian != undefined && !viewshed3ding) {
        viewshed3d.viewerPosition = cartesian;
        //初始添加一个点
        viewshed3ding = true;
    } else {
        viewshed3d.setPoseByTargetPoint(cartesian);
        //点击第二个点绘制结束
        viewshed3ding = false;
    }
},
LSGlobe.ScreenSpaceEventType.LEFT_CLICK);

ViewAnalysisHandler.setInputAction(function(movement) {
    if (viewshed3daction && viewshed3ding) {
        var cartesian = viewer.scene.pickGlobe(movement.endPosition);
        viewshed3d.setPoseByTargetPoint(cartesian);
    }
},
LSGlobe.ScreenSpaceEventType.MOUSE_MOVE);

//3).删除可视域分析
for (var i = 0; i < aViewshed3d.length; i++) {
    try {
        aViewshed3d[i].destroy();
    } catch(e) {}
}
aViewshed3d = [];
viewshed3ding = false;

5.3. 方量分析

//1).初始化方量分析
var cutfill = new LSGlobe.LSCutFill(viewer);
var nodeMesh, tileset; //实景三维对象
var poinsTemp = []; //用于保存绘制的点
//分析对象
var tempDataSource;
//用于存储方量分析辅助面
var promise = viewer.dataSources.add(new LSGlobe.GeoJsonDataSource("tempDataSource"));
promise.then(function(dataSource) {
    tempDataSource = dataSource;
}).otherwise(function(error) {

});

//2).点击收集构成放量分析的面
//注意:绘制方量分析时点必须点在模型上
//初始化鼠标事件
var oCutFillHandler = new LSGlobe.ScreenSpaceEventHandler(viewer.scene.canvas);
oCutFillHandler.setInputAction(function(movement) {
    var cartesian = scene.pickGlobe(movement.position);
    poinsTemp.push(cartesian);

    var CurrentTileSet = whichTileset(currentClickLon, currentClickLat);
    //判断点是否在模型上函数和压平获取压平对象相同
    //可视域分析点必须在模型上
    if (CurrentTileSet.length < 1) {
        alert("请在模型上点击");
        //如果点没有点在模型上不在向下走
        return;
    } else {

        tempDataSource.entities.add({
            position: cartesian,
            clampToGround: true,
            point: {
                pixelSize: 3,
                color: LSGlobe.Color.YELLOW,
                perPositionHeight: false,
                disableDepthTestDistance: 1000000000 //优先级
            }
        });
        if (poinsTemp.length >= 2) {
            tempDataSource.entities.removeById("20191225");
            tempDataSource.entities.add({
                id: "20191225",
                name: 'line on the surface',
                polyline: {
                    positions: poinsTemp,
                    width: 2,
                    material: LSGlobe.Color.fromCssColorString("rgba(0,186,255,0.5)"),
                    depthFailMaterial: new LSGlobe.PolylineDashMaterialProperty({
                        color: LSGlobe.Color.fromCssColorString("rgba(0,186,255,0.5)")
                    }),
                    disableDepthTestDistance: 1000000000 //优先级
                }
            });
        }

    }

},
LSGlobe.ScreenSpaceEventType.LEFT_CLICK);

oCutFillHandler.setInputAction(function(movement) {
    //生成面
    var oValuePoint = ChangePolygonPosition(poinsTemp);
    //点集合设置为统一高度
    for (var i = 2; i < oValuePoint.length; i = i + 3) {
        oValuePoint[i] = oHeight;
    }
    tempDataSource.entities.removeAll();
    //该面是辅助方量分析结果的面,最后方量分析的高度和范围是该面显示的高度和范围
    tempDataSource.entities.add({
        name: '方量分析辅助面',
        id: new Date().getTime(),
        polygon: {
            hierarchy: LSGlobe.Cartesian3.fromDegreesArrayHeights(oValuePoint),
            material: new LSGlobe.Color(0.5, 1.0, 1.0, 0.7),
            fill: true,
            outline: true,
            outlineColor: LSGlobe.Color.YELLOW,
            perPositionHeight: true
        }
    });
},
LSGlobe.ScreenSpaceEventType.RIGHT_CLICK);

//3).开始分析
function getProgress() {
    nodeMesh = tileset.NodeMesh;
}
modelAnalysis(oHeight);
function modelAnalysis(oHeight) {
    cutfill.clear();
    cutfill.completeEvent.addEventListener(progress);
    var cartographic = LSGlobe.Cartographic.fromCartesian(poinsTemp[0]);
    cutfill.vertice = nodeMesh;
    cutfill.matLocal = tileset.m_matLocal;
    LSGlobe.Matrix4.inverseTransformation(tileset.m_matLocal, cutfill.matLocalInvert);
    if (oHeight) {
        cutfill.zFactor = height;
    } else {
        cutfill.zFactor = cartographic.height;
    }
    //分析的面对象
    var Mypolygon = new LSGlobe.PolygonGeometry({
        polygonHierarchy: new LSGlobe.PolygonHierarchy(poinsTemp),
        perPositionHeight: true
    });
    cutfill.polygon = Mypolygon;
    //采样间隔
    cutfill.sampleGap = interval;
    //执行分析
    cutfill.apply();
}