標籤:瀏覽器 pre contain sea 定義 console sid img cti
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‘); } };
:
一種改進後的turf.idw演算法