1 year ago
#318507
Chris Davenport
Difficulty manipulating polygons using Turfjs and TypeScript
I've searched all over and haven't found an answer to this issue using Turfjs and Typescript. I'm a beginner, so please forgive me if the answer is obvious. I'm trying to use turf.intersect to help me redraw a polygon so there is no longer an overlap. I probably have several mistakes in here, but the error message I get is on intersectionArray[i], saying:
Argument of type 'Position[] | Position[][]' is not assignable to parameter of type 'Position[]'. Type 'Position[][]' is not assignable to type 'Position[]'. Type 'Position[]' is not assignable to type 'Position'. Type 'Position' is not assignable to type 'number'.ts(2345)
Can anyone point me in the right direction? It looks like turf.intersect creates a polygon OR a multipolygon, but I cannot figure out how to work with the intersection after defining it.
function adjustedBoundaries(newAreaBoundaries:turf.Polygon,
oldAreaBoundaries:turf.Polygon){
const intersection = turf.intersect(newAreaBoundaries, oldAreaBoundaries);
const intersectionArray = intersection?.geometry.coordinates;
if(intersectionArray){
let oldAreaSpliceIndex = 0;
for(let i = 0; i < intersectionArray.length; i++){
if(oldAreaBoundaries.coordinates.includes(intersectionArray[i])){
intersectionArray.splice(i,1);
oldAreaSpliceIndex = oldAreaBoundaries.coordinates.indexOf(intersectionArray[i]);
oldAreaBoundaries.coordinates.splice(oldAreaSpliceIndex,1);
}}
for(let i = 0; i < intersectionArray.length; i++){
oldAreaBoundaries.coordinates.splice(oldAreaSpliceIndex,0,intersectionArray[i])
}
return oldAreaBoundaries;
}}
javascript
typescript
polygon
turfjs
0 Answers
Your Answer