s2.sidewalklabs挂掉后的替代GoogleS2可视化解决方案

原文地址

原文链接

前言

在进行GoogleS2开发的时候常常会用到通过希尔伯特曲线划分的S2Cell作为区域隔离,但是比较常用的在线可视化网站目前是处于挂掉的状态,如果小伙伴有比较好的代替网站可以分享在评论区,这里介绍一种解决方案

实现

我尝试找了几个在线可视化的网站,对于googleS2的支持都算不上特别好,要么需要下插件,要么一堆破事。靠人不行就只能靠自己了,我们需要找到一种中间数据结构,然后将我们的S2Cell数据转换成这种中间数据结构,那么如果地图网站对于GoogleS2支持不好,只要能支持这种中间数据结构的显示就可以了,这里我们选择GeoJsonGeoJson可视化网站

转换方法1

使用开源工具osmcoverer,我试了下,并不能满足我需求,小伙伴可以稍微尝试下,毕竟是6年没更新的东西有问题也很正常

转换方法2

自己写代码,源码位于geo-data-sebastian

public class GeoJsonUtils {

    public static String getGeoJsonFromCellList(List<S2CellId> cellList) {
        String json = null;
        if (null != cellList && 0 != cellList.size()) {
            GeoJson geoJson = new GeoJson();
            geoJson.setType(GeoJsonConstant.FC_STR);

            GeoJson feature = new GeoJson();
            feature.setType(GeoJsonConstant.F_STR);
            feature.setProperties(new GeoJsonProperty());

            GeoJson geometry = new GeoJson();
            geometry.setType(GeoJsonConstant.MP_STR);
            List<List<List<List<BigDecimal>>>> coordinates = new ArrayList<>();
            for (S2CellId s2Cell : cellList) {
                List<List<List<BigDecimal>>> pointList = new ArrayList<>();
                List<List<BigDecimal>> vertex = new ArrayList<>();
                for (int ver = 0; ver < 4; ++ver) {
                    List<BigDecimal> verPoint = new ArrayList<>();
                    S2Cell cell = new S2Cell(s2Cell);
                    S2Point point = cell.getVertex(ver);
                    org.postgis.Point gisPoint = PointUtils.s2PointToGisPointEarth(point);
                    verPoint.add(BigDecimal.valueOf(gisPoint.getX()));
                    verPoint.add(BigDecimal.valueOf(gisPoint.getY()));
                    vertex.add(verPoint);
                }
                if (!vertex.isEmpty()) {
                    vertex.add(vertex.get(0));
                }
                pointList.add(vertex);

                coordinates.add(pointList);
            }
            geometry.setCoordinates(coordinates);
            feature.setGeometry(geometry);
            geoJson.setFeatures(Collections.singletonList(feature));
            json = JSON.toJSONString(geoJson);
        }
        return json;
    }

    public static String getGeoJsonFromCellIdList(List<Long> cellIdList) {
        String json = null;
        if (null != cellIdList && 0 != cellIdList.size()) {
            json = getGeoJsonFromCellList(cellIdList.stream().map(S2CellId::new)
                    .collect(Collectors.toList()));
        }
        return json;
    }

}

可以得到

{"features":[{"geometry":{"coordinates":[[[[119.97405342347365,30.274008521115835],[120.15893030899788,30.22743291296648],[120.15893030899788,30.39475478881055],[119.97405342347365,30.441483570197626],[119.97405342347365,30.274008521115835]]],[[[120.02029643173766,30.22049725018854],[120.06652363285932,30.208870455693624],[120.06652363285932,30.250750685147413],[120.02029643173766,30.26238711253284],[120.02029643173766,30.22049725018854]]],[[[120.15893030899788,30.05991718993883],[120.52790957361556,29.966365936611684],[120.52790957361556,30.300579890777662],[120.15893030899788,30.39475478881055],[120.15893030899788,30.05991718993883]]],[[[120.15893030899788,30.39475478881055],[120.52790957361556,30.300579890777662],[120.52790957361556,30.634017643550617],[120.15893030899791,30.72880192993202],[120.15893030899788,30.39475478881055]]],[[[120.52790957361556,30.300579890777662],[120.89582422732902,30.20546363138297],[120.89582422732902,30.538282211230015],[120.52790957361556,30.634017643550617],[120.52790957361556,30.300579890777662]]]],"type":"MultiPolygon"},"properties":{"fill-opacity":0.2,"stroke-width":1},"type":"Feature"}],"type":"FeatureCollection"}

将生成的json拷贝到网站上,我们就能得到给到的5个S2Cell的可视化图片了

原文地址

原文链接

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值