import lineclip from 'lineclip' ; import pc from 'polygon-clipping' ; import { bboxToPolygon } from '../bbox.js' ; import { BBox , MultiPolygon , Polygon } from '../types.js' ; export const { intersection , union } = pc ; function samePoint ( a , b ) { return a [ 0 ] === b [ 0 ] && a [ 1 ] === b [ 1 ] ; } function removeDegenerateEdges ( polygons , bbox ) { return intersection ( polygons , bboxToPolygon ( bbox ) as Polygon ) ; } param polygons param bbox export function clipMultipolygon ( polygons , bbox ) { const result = [ ] ; for ( const poly of polygons ) { const clipped = lineclip . polygon ( poly [ 0 ] , bbox ) ; if ( clipped . length !== 0 ) { if ( ! samePoint ( clipped [ 0 ] , clipped [ clipped . length - 1 ] ) ) clipped . push ( clipped [ 0 ] ) ; result . push ( [ clipped ] ) ; } } return removeDegenerateEdges ( result , bbox ) ; }