react + openlayer实现克里金插值效果

主要用了turf + openlayer 实现
克里金插值组件:

import React, { useContext, useEffect, useRef } from 'react';
import { ContentlayersContext, OpenlayersContext } from '&/core/context';
import moment from 'moment';
import VectorLayer from 'ol/layer/Vector';
import VectorSource from 'ol/source/Vector';
import * as turf from '@turf/turf';
import Feature from 'ol/Feature';
import Point from 'ol/geom/Point';
import { Circle, Style, Icon, Fill } from 'ol/style';
import GeoJSON from 'ol/format/GeoJSON';
import { wgs84togcj02 } from '&/commonjs/coordinateUtils';

export default (props) => {
  const { time, mode, currentStd } = useContext(ContentlayersContext);
  const { map } = useContext(OpenlayersContext);

  let canvasLayer = useRef(null);
  useEffect(() => {
    if (!props.visible || !currentStd.wgs84_lat) {
      map.removeLayer(canvasLayer.current);
      return;
    }
    if (canvasLayer.current !== null) {
      map.removeLayer(canvasLayer.current);
    }
    let params = {
      mapCenter: wgs84togcj02(currentStd.wgs84_lng, currentStd.wgs84_lat),
      maxValue: 100,
      krigingModel: 'exponential', //model还可选'gaussian','spherical'
      krigingSigma2: 0,
      krigingAlpha: 100,
      canvasAlpha: 0.7, //canvas图层透明度
      colors: [
        'rgb(165,0,38)',
        'rgb(215,48,39)',
        'rgb(244,109,67)',
        'rgb(253,174,97)',
        'rgb(254,224,139)',
        'rgb(255,255,191)',
        'rgb(217,239,139)',
        'rgb(166,217,106)',
        'rgb(102,189,99)',
        'rgb(26,152,80)',
        'rgb(0,104,55)',
      ],
    };
    let WFSVectorSource = new VectorSource(); //获取selectFeature
    //创建10个位置随机、属性值随机的特征点
    for (let i = 0; i < 100; i++) {
      let feature = new Feature({
        geometry: new Point([
          params.mapCenter[0] + Math.random() * 0.01 - 0.005,
          params.mapCenter[1] + Math.random() * 0.01 - 0.005,
        ]),
        value: Math.round(Math.random() * params.maxValue),
      });

      WFSVectorSource.addFeature(feature);
    }

    //利用网格计算点集
    const gridFeatureCollection = function (grid, xlim, ylim) {
      var range = grid.zlim[1] - grid.zlim[0];
      var i, j, x, y, z;
      var n = grid.length; //列数
      var m = grid[0].length; //行数
      var pointArray = [];
      for (i = 0; i < n; i++)
        for (j = 0; j < m; j++) {
          x = i * grid.width + grid.xlim[0];
          y = j * grid.width + grid.ylim[0];
          z = (grid[i][j] - grid.zlim[0]) / range;
          if (z < 0.0) z = 0.0;
          if (z > 1.0) z = 1.0;
          pointArray.push(turf.point([x, y], { value: z }));
        }
      return pointArray;
    };

    //绘制kriging插值图
    const drawKriging = (extent) => {
      let values = [],
        lngs = [],
        lats = [];
      selectedFeatures.forEach((feature) => {
        values.push(feature.values_.value);
        lngs.push(feature.values_.geometry.flatCoordinates[0]);
        lats.push(feature.values_.geometry.flatCoordinates[1]);
      });
      if (values.length > 3) {
        // eslint-disable-next-line
        let variogram = kriging.train(
          values,
          lngs,
          lats,
          params.krigingModel,
          params.krigingSigma2,
          params.krigingAlpha
        );
        let polygons = [];
        polygons.push([
          [extent[0], extent[1]],
          [extent[0], extent[3]],
          [extent[2], extent[3]],
          [extent[2], extent[1]],
        ]);
        // eslint-disable-next-line
        let grid = kriging.grid(
          polygons,
          variogram,
          (extent[2] - extent[0]) / 200
        );

        let vectorSource = new VectorSource();
        canvasLayer.current = new VectorLayer({
          source: vectorSource,
          opacity: 0.7,
          style: function (feature) {
            var style = new Style({
              fill: new Fill({
                color:
                  params.colors[
                    parseFloat(feature.get('value').split('-')[1]) * 10
                  ],
              }),
            });
            return style;
          },
        });
        //使用turf渲染等值面/线
        let fc = gridFeatureCollection(
          grid,
          [extent[0], extent[2]],
          [extent[1], extent[3]]
        );
        var collection = turf.featureCollection(fc);
        var breaks = [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0];
        var isobands = turf.isobands(collection, breaks, {
          zProperty: 'value',
        });
        function sortArea(a, b) {
          return turf.area(b) - turf.area(a);
        }
        //按照面积对图层进行排序,规避turf的一个bug
        isobands.features.sort(sortArea);
        var polyFeatures = new GeoJSON().readFeatures(isobands, {
          featureProjection: 'EPSG:3857',
        });
        vectorSource.addFeatures(polyFeatures);

        map.addLayer(canvasLayer.current);
      } else {
        alert('有效样点个数不足,无法插值');
      }
    };

    //首次加载,自动渲染一次差值图
    let selectedFeatures = [];
    let extent = [
      params.mapCenter[0] - 0.02,
      params.mapCenter[1] - 0.02,
      params.mapCenter[0] + 0.02,
      params.mapCenter[1] + 0.02,
    ];
    WFSVectorSource.forEachFeatureIntersectingExtent(extent, (feature) => {
      selectedFeatures.push(feature);
    });
    drawKriging(extent);
  }, [props.visible, mode, time, currentStd]);

  return null;
};

实现效果:
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
React TS是一个用于构建用户界面的JavaScript库,而VR看房可以通过使用WebVR技术来实现。在React TS中实现VR看房需要以下步骤: 1. 了解WebVR技术:WebVR是一种使Web应用程序能够在虚拟现实设备上运行的技术,它提供了一种在虚拟现实设备上渲染内容的方式。 2. 导入WebVR库:在React TS项目中,需要导入与WebVR相关的库,例如A-Frame或React 360等,这些库提供了一些用于构建VR界面的组件和功能。 3. 创建VR场景:使用React TS的组件化开发方式,可以通过在项目中创建VR场景组件来构建VR看房的界面。可以使用库中提供的组件来构建3D场景、添加虚拟现实设备的交互等。 4. 加载房屋模型:在VR场景中加载房屋模型,可以使用库中提供的加载器将3D房屋模型导入到场景中,并设置适当的位置和缩放。 5. 添加交互功能:为了实现VR看房的功能,需要添加一些交互功能,例如移动、旋转和缩放房屋模型,点击房间以获取更多信息等。可以使用库中提供的交互组件或自定义事件处理程序来实现这些功能。 6. 兼容不同的设备:考虑到不同的虚拟现实设备,需要在React TS项目中进行一些适配工作,以确保VR看房界面在不同设备上的兼容性。 总的来说,使用React TS实现VR看房需要对WebVR技术有一定的了解,并结合具体的库和组件来构建VR场景、加载房屋模型,并添加交互功能。不同设备的兼容性也是一个需要考虑的因素。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值