Google S2 算法 Java 操作

获取指定子级的全部Cell

/**
     * 获取指定级别的全部子cell
     *
     * @param root     当前cellId
     * @param desLevel 目标level, > root.level()
     */
    public List<S2CellId> children(S2CellId root, int desLevel) {
        if (root.level() < desLevel) {
            long interval = (root.childEnd().id() - root.childBegin().id()) / 4;
            List<S2CellId> list = new ArrayList<>();
            for (int i = 0; i < 4; i++) {
                long id = root.childBegin().id() + interval * i;
                S2CellId cellId = new S2CellId(id);
                List<S2CellId> childrenCellId = children(cellId, desLevel);
                list.addAll(childrenCellId);
            }
            return list;
        } else if (root.level() == desLevel) {
            return Collections.singletonList(root);
        } else {
            return Collections.emptyList();
        }
    }

获取指定子级的边界Cell

/**
  * 获取指定子级的边界Cell(当前cell内部)
  * 
  * @param s2CellId 当前cellId
  * @param desLevel 目标cellId的level 需大于当前cellId
  */
public List<S2CellId> childrenCellId(S2CellId root, S2CellId s2CellId, int desLevel) throws IOException {
        if (s2CellId.level() < desLevel - 1) {
            long interval = (s2CellId.childEnd().id() - s2CellId.childBegin().id()) / 4;
            List<S2CellId> list = new ArrayList<>();
            for (int i = 0; i < 4; i++) {
                long id = s2CellId.childBegin().id() + interval * i;
                S2CellId cellId = new S2CellId(id);

                S2CellId[] neighbors = new S2CellId[4];
                cellId.getEdgeNeighbors(neighbors);

                boolean edge = Arrays.stream(neighbors).anyMatch(neighbor -> !root.contains(neighbor));
                if (edge) {
                    List<S2CellId> childrenCellId = childrenCellId(root, cellId, desLevel);
                    list.addAll(childrenCellId);
                }
            }
            return list;
        } else if (s2CellId.level() == desLevel - 1) {
            long interval = (s2CellId.childEnd().id() - s2CellId.childBegin().id()) / 4;
            List<S2CellId> list = new ArrayList<>();
            for (int i = 0; i < 4; i++) {
                long id = s2CellId.childBegin().id() + interval * i;
                S2CellId cellId = new S2CellId(id);

                S2CellId[] neighbors = new S2CellId[4];
                cellId.getEdgeNeighbors(neighbors);

                boolean edge = Arrays.stream(neighbors).anyMatch(neighbor -> !root.contains(neighbor));
                if (edge) {
                    list.add(cellId);
                }
            }
            return list;
        }
        return Collections.emptyList();
    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值