| 256 | |
| 257 | //feature合并 |
| 258 | function unionFeature(featureCollection, isFirst) { |
| 259 | var results = []; |
| 260 | var features = featureCollection.features; |
| 261 | var featureLength = features.length; |
| 262 | var oneceTotal = 2; //两两合并 直到最后剩一个多面对象为止(分网格 合并) |
| 263 | var total = Math.round(featureLength / oneceTotal); |
| 264 | //数组顺序打乱 |
| 265 | if (!isFirst) this.random(features); |
| 266 | for (var i = 0; i <= total; i++) { |
| 267 | var start = i * oneceTotal; |
| 268 | var result = featureCollection.features[start]; |
| 269 | for (var j = 1; j < oneceTotal; j++) { |
| 270 | var index = start + j; |
| 271 | if (featureCollection.features[index]) { |
| 272 | try { |
| 273 | result = this.union(result, featureCollection.features[index]); |
| 274 | } catch (e) { |
| 275 | results.push(featureCollection.features[index]); |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | if (result) results.push(result); |
| 280 | } |
| 281 | |
| 282 | if (results && results.length > 1) { |
| 283 | //结果还是多个 继续合并 |
| 284 | return this.unionFeature(turf.featureCollection(results), false); |
| 285 | } else { |
| 286 | return results[0]; |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | //数组顺序打乱 |
| 291 | function random(arr) { |