原文地址
前言
在进行GoogleS2
开发的时候常常会用到通过希尔伯特曲线划分的S2Cell
作为区域隔离,但是比较常用的在线可视化网站目前是处于挂掉的状态,如果小伙伴有比较好的代替网站可以分享在评论区,这里介绍一种解决方案
实现
我尝试找了几个在线可视化的网站,对于googleS2
的支持都算不上特别好,要么需要下插件,要么一堆破事。靠人不行就只能靠自己了,我们需要找到一种中间数据结构,然后将我们的S2Cell
数据转换成这种中间数据结构,那么如果地图网站对于GoogleS2
支持不好,只要能支持这种中间数据结构的显示就可以了,这里我们选择GeoJson
,GeoJson可视化网站
转换方法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
的可视化图片了