当前位置:网站首页>JS to realize the calculation of discrete aggregation points

JS to realize the calculation of discrete aggregation points

2022-06-25 11:57:00 GIS roast lamb leg is delicious

explain

This example uses Turf.js Inside clustersDbscan class , He aggregates according to the distance between adjacent points , When the distance is less than the specified distance, it will converge , Generally, the introduction of front-end map rendering components does not require self calculation , This method combines echarts Better rendering .
advantage : Simple , Aggregate directly through distance .
shortcoming : After thousands of quantity sets, it will lead to Caton . Optimization plan , Batch polymerizable , Per aggregation 300 strip , Finally, the results of each aggregation are aggregated twice , This can withstand tens of thousands of points . But the error will be large .

turf.js download

npm install @turf/turf

Code block

import * as turf from '@turf/turf'
var points={
     type: "FeatureCollection",
     features:[
        {type: "Feature",geometry:{ type: "Point",coordinates:[123.4,33.9] } },
        {type: "Feature",geometry:{ type: "Point",coordinates:[123.5,33.9] } },
        {type: "Feature",geometry:{ type: "Point",coordinates:[96.4,20.9] } },
        {type: "Feature",geometry:{ type: "Point",coordinates:[120.4,38.9] } },
        {type: "Feature",geometry:{ type: "Point",coordinates:[123.4,33.9] } },
        {type: "Feature",geometry:{ type: "Point",coordinates:[123.4,33.9] } },
	]
}

 var clustered=  turf.clustersDbscan(points, 100);// The default unit is kilometers 
 var arr =  clustered.features;  // The returned data is marked with characteristics .
var ary={};
 /* Remove the weight and count the quantity */
 for(let i=0;i<arr.length;i++){
   var x=arr[i].properties.centroid[0];
   var y=arr[i].properties.centroid[1];
    if(arr[i].properties.cluster!=undefined){
     if(arr[i].properties.cluster in ary){
         ary[arr[i].properties.cluster]=[x,y,ary[arr[i].properties.cluster][2]+1];// Duplicate quantity +1
      }else{
         ary[arr[i].properties.cluster]=[x,y,1];// The default quantity without repetition is one 
      }
     }else{
       ary[arr[i].properties.centroid]=[x,y,1];// The default quantity without repetition is one 
      }
    }
	// The final results pointResult
   var pointResult=[]
   for( var key in ary){
       pointResult.push({
           name:ary[key][2],
           value:ary[key]
       })
    }
    console.log(pointResult)
原网站

版权声明
本文为[GIS roast lamb leg is delicious]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202200535316103.html