1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
| import com.google.common.geometry.*;
import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.stream.Collectors;
public class S2Utils {
private final S2RegionCoverer coverer;
private final int minLevel;
private final int maxLevel;
private final int maxCells;
public S2Utils(int minLevel, int maxLevel, int maxCells) { this.minLevel = minLevel; this.maxCells = maxCells; this.maxLevel = maxLevel; coverer = new S2RegionCoverer(); coverer.setMinLevel(minLevel); coverer.setMaxCells(maxCells); coverer.setMaxLevel(maxLevel); }
public S2CellId toCellId(double lon, double lat, int level) { return S2CellId.fromLatLng(S2LatLng.fromDegrees(lat, lon)).parent(level); }
public S2CellId toCellId(double lon, double lat) { return S2CellId.fromLatLng(S2LatLng.fromDegrees(lat, lon)).parent(maxLevel); }
public S2CellId tokenToCellId(String token) { return S2CellId.fromToken(token); }
public Point cellIdToPoint(S2CellId cellId) { S2LatLng s2LatLng = new S2CellId(cellId.id()).toLatLng(); return new Point(s2LatLng.lngDegrees(), s2LatLng.latDegrees()); }
public List<S2CellId> getCellIdInRegion(S2Region region) { ArrayList<S2CellId> s2CellIds = coverer.getCovering(region).cellIds(); return s2CellIds.stream().flatMap(s2CellId -> childrenCellId(s2CellId, minLevel).stream()).collect(Collectors.toList()); }
public static List<S2CellId> childrenCellId(S2CellId s2CellId, Integer desLevel) { return childrenCellId(s2CellId, s2CellId.level(), desLevel); }
private static List<S2CellId> childrenCellId(S2CellId s2CellId, Integer curLevel, Integer desLevel) { List<S2CellId> s2CellIds = new LinkedList<>(); if (curLevel < desLevel) { long interval = (s2CellId.childEnd().id() - s2CellId.childBegin().id()) / 4; for (int i = 0; i < 4; i++) { long id = s2CellId.childBegin().id() + interval * i; s2CellIds.addAll(childrenCellId(new S2CellId(id), curLevel + 1, desLevel)); } return s2CellIds; } else { s2CellIds.add(s2CellId); return s2CellIds; } }
public S2Region createCircleRegion(double lon, double lat, double radius) { double capHeight = (2 * S2.M_PI) * (radius / 40075017); return S2Cap.fromAxisHeight(S2LatLng.fromDegrees(lat, lon).toPoint(), capHeight * capHeight / 2); }
public static class Point { private double lon;
private double lat;
public Point(double lon, double lat) { this.lon = lon; this.lat = lat; }
public Point() { }
public double getLon() { return lon; }
public void setLon(double lon) { this.lon = lon; }
public double getLat() { return lat; }
public void setLat(double lat) { this.lat = lat; } } }
|