_selectPolygonObjects()

in modules/layers/src/layers/selection-layer.ts [91:139]


  _selectPolygonObjects(coordinates: any) {
    const { layerIds, onSelect } = this.props;
    const mousePoints = coordinates[0].map((c) => this.context.viewport.project(c));

    const allX = mousePoints.map((mousePoint) => mousePoint[0]);
    const allY = mousePoints.map((mousePoint) => mousePoint[1]);
    const x = Math.min(...allX);
    const y = Math.min(...allY);
    const maxX = Math.max(...allX);
    const maxY = Math.max(...allY);

    // Use a polygon to hide the outside, because pickObjects()
    // does not support polygons
    const landPointsPoly = polygon(coordinates);
    const bigBuffer = turfBuffer(landPointsPoly, EXPANSION_KM);
    let bigPolygon;
    try {
      // turfDifference throws an exception if the polygon
      // intersects with itself (TODO: check if true in all versions)
      bigPolygon = turfDifference(bigBuffer, landPointsPoly);
    } catch (e) {
      // invalid selection polygon
      console.log('turfDifference() error', e); // eslint-disable-line
      return;
    }

    this.setState({
      pendingPolygonSelection: {
        bigPolygon,
      },
    });

    const blockerId = `${this.props.id}-${LAYER_ID_BLOCKER}`;

    // HACK, find a better way
    setTimeout(() => {
      const pickingInfos = this.context.deck.pickObjects({
        x,
        y,
        width: maxX - x,
        height: maxY - y,
        layerIds: [blockerId, ...layerIds],
      });

      onSelect({
        pickingInfos: pickingInfos.filter((item) => item.layer.id !== this.props.id),
      });
    }, 250);
  }