function pairwise(array) {
const result = [];
for (let i = 0; i < array.length; i += 2) {
result.push({ x: array[i], y: array[i + 1] });
}
return result;
}
const polydata = 'MULTIPOLYGON(((-61.2811499892424 -51.8650394644694,-61.3156095883879 -51.84003983505,-61.3451180293736 -51.836359409781,-61.345979 -51.8243193384298,-61.2230287013809 -51.8586600606699,-61.2380183615625 -51.8776,-61.2792595470359 -51.8659093831693,-61.2811499892424 -51.8650394644694)),((-59.6938009153582 -52.2130104476997,-59.6697 -52.2216295490501,-59.6888996395972 -52.2286288414282,-59.6929605766079 -52.2129007110094,-59.6938009153582 -52.2130104476997),(-59.7427025342573 -52.2157144418542,-59.7402989592584 -52.226169308217,-59.7414397191119 -52.2143487582055,-59.7427025342573 -52.2157144418542)),((-58.4328292075227 -52.102969,-58.4668596645304 -52.094089483711,-58.444380216144 -52.0871892532998,-58.4332186231022 -52.1024482278935,-58.4328292075227 -52.102969)))';
const polystr = polydata.replace(/MULTIPOLYGON|\(|\)|\s+|\.-/g, (ch) => {
if (ch === 'MULTIPOLYGON') return '';
else if (ch === '.-') return '.0,-';
else if (ch === '(') return '[';
else if (ch === ')') return ']';
else if (ch[0] === ' ') return ',';
});
const polyarray = JSON.parse(polystr).map(arr => arr.map(a => pairwise(a)));
const { shapes, holes } = polyarray.reduce((acc, val) => {
if (val.length === 1) {
acc.shapes.push(...val[0]);
}
else if (val.length > 1) {
acc.holes.push(...val[0]);
acc.shapes.push(...val.slice(1).flat());
}
return acc;
}, { shapes: [], holes: [] });
console.log('shapes =>', shapes);
console.log('holes =>', holes);