zoukankan      html  css  js  c++  java
  • 一种改进后的turf.idw算法

    turf 是Advanced geospatial analysis geojson data in javascript.

    官网:http://turfjs.org/

    针对github 中的源码。

    记得在以前使用arcgis 的反距离插值的时候有搜索半径和剪切范围定义。

    于是就增加了这两个参数。

    可在浏览器端使用的IDW算法就优化到相对适合使用了。

      /**
         * idwPolygon - 反距离插值生成格网面集
         *
         * @param {type} controlPoints   离散点
         * @param {type} valueField      计算属性值z
         * @param {type} b               反距离幂值
         * @param {type} cellWidth       单元格宽
         * @param {type} SearchR         搜索半径
         * @param {type} units           单位km
         * @param {type} boundaryPolygon 剪裁范围
         *
         * @return {type} Description
         */
        var idwPolygon = function(controlPoints, valueField, b, cellWidth, SearchR, units, boundaryPolygon) {
            var distance = turf.distance;
            var squareGrid = turf.squareGrid;
            var centroid = turf.centroid;
            var bbox = turf.bbox;
            var inside = turf.inside;
            var featureCollection = turf.featureCollection;
            // check if field containing data exists..
            var filtered = controlPoints.features.filter(function(feature) {
                return feature.properties &&
                    feature.properties.hasOwnProperty(valueField);
            });
            if (filtered.length !== 0) {
                // create a sample square grid
                // compared to a point grid helps visualizing the output (like a raster..)
                var resultGrid = [];
                var bbbox = boundaryPolygon ? bbox(boundaryPolygon) : bbox(controlPoints);//剪切范围增加
                var samplingGrid = squareGrid(bbbox, cellWidth, units);
                var N = samplingGrid.features.length;
                for (var i = 0; i < N; i++) {
                    var cpointi = centroid(samplingGrid.features[i]);
                    if (!inside(cpointi, boundaryPolygon)) { //如果在面外,不参与计算
                        continue;
                    }
                    var zw = 0;
                    var sw = 0;
                    // calculate the distance from each control point to cell's centroid 
                    for (var j = 0; j < controlPoints.features.length; j++) {
                        var d = distance(cpointi, controlPoints.features[j], units);
                        if (d > SearchR) {
                            continue;
                        }
                        if (d === 0) {
                            zw = controlPoints.features[j].properties[valueField];
                        }
                        var w = 1.0 / Math.pow(d, b);
                        sw += w;
                        zw += w * controlPoints.features[j].properties[valueField];
                    }
                    // write IDW value for each grid cell
                    var zvalue = zw / sw; //如果都在影响半径外,那么可能为非数字,赋值为0
                    samplingGrid.features[i].properties.z = zvalue ? zvalue : 0;
    
                    resultGrid.push(samplingGrid.features[i]);
                }
                return featureCollection(resultGrid);
            } else {
                console.log('Specified Data Field is Missing');
            }
        };
    

      

    效果图:

  • 相关阅读:
    How to get the IIS root path in other application.
    Web.UI.Controls与页面事件的冲突问题。
    分析在服务器上设置计时器的问题。
    首次感觉我的电脑过时了。。。。。。。。郁闷。
    Google Logos
    2005年的最后一天
    TreeView的几个使用小技
    浅淡反射问题
    The restricted headers are:
    在服务器上用Timer遇到的小问题。。。。
  • 原文地址:https://www.cnblogs.com/hillgisman/p/6230186.html
Copyright © 2011-2022 走看看