Skip to content

Intersection of polygon borders #364

@iskandari

Description

@iskandari

Given a polygon from an s2 cell, and a polygon from an s2 cell union, what methods could be used to check whether the borders of these cells intersect? In the below example, the both red and green cells are contained by the yellow union, but only the green cell's borders intersect the union's borders.

Screenshot 2024-05-03 at 3 26 31 PM

I have tried using S2ShapeIndex but this does not work. I would be grateful for any suggestions!

bool checkIntersection(const std::vector<uint64_t>& cell_union_ids, uint64_t cell_id) {
    std::vector<S2CellId> cell_ids;
    for (auto id : cell_union_ids) {
        cell_ids.push_back(S2CellId(id));
    }
    S2CellUnion cell_union;
    cell_union.Init(cell_ids);

    S2Cell cell(S2CellId(cell_id));
    S2Polygon cell_polygon(S2Cell(cell));

    S2Polygon cell_union_polygon;
    cell_union_polygon.InitToCellUnionBorder(cell_union);

    S2BooleanOperation::Options options;
    S2PolygonLayer::Options poly_options;
    S2PolygonLayer result_layer(&cell_polygon, poly_options);

    S2ShapeIndex cell_index;
    cell_index.Add(absl::make_unique<S2Polygon::Shape>(&cell_polygon));
    S2ShapeIndex union_index;
    union_index.Add(absl::make_unique<S2Polygon::Shape>(&cell_union_polygon));

    S2BooleanOperation op(S2BooleanOperation::OpType::INTERSECT, &result_layer, options);
    bool intersected = op.Build(&cell_index, &union_index);

    return intersected && !cell_polygon.is_empty();
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions