运用的场景是关于多地块和单地块的多次分割,以及地块的多重分割的场景运用,地块的分割以及地块编号排序,面积整合等功能!
例如:分割线可携带自身的长度,以及相关的参数未分析做准备:
polygonCut(dataList,npoly, nline, tolerance, toleranceType, percent) {
let _this = this;
const { per1, per2 } = percent;
let areaData = [];
let changeData =[]
let polygonData= [];
npoly.forEach(itemData=>{
let totalArea = turf.area(itemData).toFixed(0);
areaData.push(totalArea);
});
polygonData =npoly;
for (let i = 0; i < nline.length; i++) {
const item = nline[i];
let pieces = [];
for (let j = 0; j < polygonData.length; j++) {
const poly = polygonData[j];
let isCrosses = turf.booleanCrosses(item, poly);
if (isCrosses) {
let lineIntersect = turf.lineIntersect(item, poly);
const lineExp = turf.explode(item);
for (let k = 0; k < lineExp.features.length - 1; k++) {
lineIntersect.features.push(
turf.point(lineExp.features[k].geometry.coordinates),
);
}
const lineBuffer = turf.buffer(item, tolerance, {
units: toleranceType,
});
const _body = turf.difference(poly, lineBuffer);
if (_body.geometry.type === 'Polygon') {
pieces.push(turf.polygon(_body.geometry.coordinates));
} else {
_body.geometry.coordinates.forEach(function (a) {
pieces.push(turf.polygon(a));
});
}
for (let p in pieces) {
const piece = pieces[p];
for (let c in piece.geometry.coordinates[0]) {
const coord = piece.geometry.coordinates[0][c];
const p = turf.point(coord);
for (let lp in lineIntersect.features) {
const lpoint = lineIntersect.features[lp];
if (turf.distance(lpoint, p, toleranceType) <= tolerance * 2) {
piece.geometry.coordinates[0][c] = lpoint.geometry.coordinates;
}
}
}
}
pieces.forEach((a, index) => {
a.properties = Object.assign({}, poly.properties);
a.properties.id += `-${index}`;
});
} else {
changeData.push(poly);
}
}
if (changeData.length>0){
polygonData = pieces.concat(...changeData);
}else{
polygonData = pieces
}
changeData=[]
}
return _this.polygonColor(polygonData,per1,per2,areaData,dataList)
}
polygonColor(newData,per1,per2,areaData,dataList){
let _this = this;
let color = [
'rgba(255,30,0,0.4)',
'rgba(255,255,0,0.4)',
'rgba(0,255,149,0.4)',
'rgba(0,21,255,0.4)',
'rgba(131,26,165,0.4)',
'rgba(15,244,188,0.4)',
'rgba(15,225,59,0.4)',
'rgba(183,224,15,0.4)',
];
let labelArr = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I','J','K','L','M','N','O','P'];
let splitInfo = [];
let changeData =areaData.reduce((accumulator, currentValue) => {
return accumulator + Number(currentValue);
}, 0);
let changeList = dataList.reduce((accumulator, currentValue) => {
return accumulator + currentValue.areaData;
}, 0);
newData.map((pl, index) => {
let arr = []
let polygonData = [];
pl.geometry.coordinates[0].map((pos) => {
arr.push(pos[0]);
arr.push(pos[1]);
});
polygonData.push(pl.geometry.coordinates[0])
// let polygon = {
// hierarchy: new PolygonHierarchy(Cartesian3.fromDegreesArray(arr)),
// material: Color.fromCssColorString(
// color[index] ? color[index] : 'rgba(255,255,255,0.5)',
// ),
// heightReference: HeightReference.CLAMP_TO_GROUND,
// };
let center = turf.centroid(pl);
let nameData ='';
let numberData ='';
if (dataList.length>0){
dataList.forEach(item=>{
let listData= _this.isSmallPolygonInLargePolygon(center,turf.polygon(item.polygon))
if (listData){
nameData=item.info.jibunAddr;
let pattern = /[0-9\-]/g;
numberData = item.info.jibunAddr.match(pattern).join("");
}
})
}
// let pyPositions = polygon.hierarchy.positions;
// let pcenter = Cesium.BoundingSphere.fromPoints(pyPositions).center;
let ar = turf.area(pl).toFixed(0);
let areaArr =((Number(ar)/changeData)*changeList).toFixed(0);
let addEntity = new Entity({
polygon: {
hierarchy: new PolygonHierarchy(Cartesian3.fromDegreesArray(arr)),
material: Color.fromCssColorString(
color[index] ? color[index] : 'rgba(255,255,255,0.5)',
),
heightReference: HeightReference.CLAMP_TO_GROUND,
},
position: Cartesian3.fromDegreesArray(center.geometry.coordinates)[0],
label: {
text: labelArr[index] + '=' + this.formatterNumber(areaArr) + '㎡',
scale: 1.0,
font: '500 30px Helvetica', // 15pt monospace
scale: 0.7,
style: LabelStyle.FILL,
fillColor: Color.WHITE,
heightReference: HeightReference.CLAMP_TO_GROUND,
scaleByDistance: new NearFarScalar(1.5e2, 1, 30000, 0.0),
},
});
_this.polygonSource.entities.add(addEntity);
let obj = {
index: index + 1,
nameData:nameData,
numberData:numberData,
differentiate: labelArr[index],
area: Number((Number(areaArr) * per1) / 100).toFixed(0),
area1: Number((Number(areaArr) * per2) / 100).toFixed(0),
color: color[index] ? color[index] : 'rgba(255,255,255,0.5)',
totalArea: areaArr,
totalArea1: areaData[index],
entity: addEntity,
polygonArr:polygonData,
};
splitInfo.push(obj);
});
return splitInfo;
}
isSmallPolygonInLargePolygon(smallFeature, largeFeature) {
return turf.booleanPointInPolygon(smallFeature, largeFeature)
}