shadow.js 5.98 KB
import objectManage from './maputils';
export default class shadow {
    constructor() {
        this.points = [];
        this.shadowQuery = new Cesium.ShadowQueryPoints(objectManage.viewer.scene);
        this.shadowQuery.build();
        this.setCurrentTime();
        this.init();
    }
    setCurrentTime() {
        let d = new Date();
        d.setHours(20)
        objectManage.viewer.clock.currentTime = Cesium.JulianDate.fromDate(d);
        objectManage.viewer.clock.multiplier = 1;
        objectManage.viewer.clock.shouldAnimate = true;
    }
    init() {
        this.tooltip = this.createTooltip(document.body);
        this.handlerPolygon = new Cesium.DrawHandler(objectManage.viewer, Cesium.DrawMode.Polygon, 0);
        this.handlerPolygon.activeEvt.addEventListener(a => {
            let body = $('body').removeClass('measureCur');
            a && body.addClass('measureCur');
            (objectManage.viewer.enableCursorStyle = !a) || (objectManage.viewer._element.style.cursor = '');
        });
        this.handlerPolygon.movingEvt.addEventListener(windowPosition => {
            this.handlerPolygon.isDrawing && this.tooltip.showAt(windowPosition, '<p>绘制阴影分析区域(右键结束绘制)</p>');
        });
        this.handlerPolygon.drawEvt.addEventListener(result => {
            this.tooltip.setVisible(false);
            this.points.length = 0;
            var polygon = result.object;
            if(!polygon) return;
            polygon.show = false;
            this.handlerPolygon.polyline.show = false;
            var positions = [].concat(polygon.positions);
            positions = Cesium.arrayRemoveDuplicates(positions, Cesium.Cartesian3.equalsEpsilon);
            //遍历多边形,取出所有点
            for(var i = 0, len = positions.length; i < len; i++) {
                //转化为经纬度,并加入至临时数组
                var cartographic = Cesium.Cartographic.fromCartesian(polygon.positions[i]);
                var longitude = Cesium.Math.toDegrees(cartographic.longitude);
                var latitude = Cesium.Math.toDegrees(cartographic.latitude);
                this.points.push(longitude);
                this.points.push(latitude);
            }
            //设置分析对象的开始结束时间
            // var dateValue = $("#selDate").val();
            var startTime = new Date();
            startTime.setHours(8);
            this.shadowQuery.startTime = Cesium.JulianDate.fromDate(startTime);

            var endTime = new Date();
            endTime.setHours(22);
            this.shadowQuery.endTime = Cesium.JulianDate.fromDate(endTime);

            this.setCurrentTime();
            this.shadowQuery.spacing = 10;
            this.shadowQuery.timeInterval = 60;

            //设置分析区域、底部高程和拉伸高度
            var bh = 20, eh = 20;
            this.shadowQuery.qureyRegion({
                position : this.points,
                bottom : bh,
                extend : eh
            });
        });
    }
    createTooltip(frameDiv) {
        var Tooltip = function(frameDiv) {
            var div = document.createElement('DIV');
            div.className = "twipsy right";
            var arrow = document.createElement('DIV');
            arrow.className = "twipsy-arrow";
            div.appendChild(arrow);
            var title = document.createElement('DIV');
            title.className = "twipsy-inner";
            div.appendChild(title);
            this._div = div;
            this._title = title;
            this.message = '';
            frameDiv.appendChild(div);
            var that = this;
            div.onmousemove = function(evt){
                that.showAt({
                    x : evt.clientX, 
                    y : evt.clientY
                }, that.message);
            };
        };
        Tooltip.prototype.setVisible = function(visible) {
            this._div.style.display = visible ? 'block' : 'none';
        };
        Tooltip.prototype.showAt = function(position, message) {
            if(position && message) {
                this.setVisible(true);
                this._title.innerHTML = message;
                this._div.style.left = position.x + 10 + "px";
                this._div.style.top = (position.y - this._div.clientHeight / 2) + "px";
                this.message = message;
            }
        };
        return new Tooltip(frameDiv);
    }
    shadowAnalysis() {
        this.handlerPolygon.deactivate();
        this.handlerPolygon.activate();
    }
    sunlight() {
        var startTime = new Date(), endTime = new Date(), shour = 8, ehour = 22, nTimer = 0.0;
        if(shour > ehour) return;
        this.shadowQuery.qureyRegion({
            position : [0, 0],
            bottom : 0,
            extend : 0
        });
        var nIntervId = setInterval(function() {
            if(shour < ehour) {
                startTime.setHours(shour);
                startTime.setMinutes(nTimer);
                objectManage.viewer.clock.currentTime = Cesium.JulianDate.fromDate(startTime);
                nTimer += 10.0;
                if(nTimer > 60.0){
                    shour += 1.0;
                    nTimer = 0.0;
                }
            }else {
                clearInterval(nIntervId);
                this.shadowQuery.qureyRegion({
                    position : points,
                    bottom : 20,
                    extend : 20
                });
            }
        }, 500);
    }
    clear() {
        this.handlerPolygon.deactivate();
        this.handlerPolygon.polygon.show = false;
        this.handlerPolygon.polyline.show = false;
        // $('#shadowRadioBox').css('display', 'none');
        objectManage.viewer.entities.removeAll();
        this.shadowQuery.qureyRegion({
            position : [0, 0],
            bottom : 0,
            extend : 0
        });
    }
    remove() {
        this.points = [];
        this.shadowQuery.destroy();
        this.shadowQuery = new Cesium.ShadowQueryPoints(objectManage.viewer.scene);
        this.shadowQuery.build();
    }
}