Skip to content

Commit c7a50d1

Browse files
committed
Merge pull request #13 from akihiro0228/fixed-get-zone-by-long-xy
Fixed getZoneByXY by round xy.
2 parents d4abce3 + 847cf51 commit c7a50d1

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

src/main/java/org/geohex/geohex4j/GeoHex.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,8 @@ private static XY getXYByCode(String code) {
178178

179179
public static final Zone getZoneByXY(double x, double y, int level) {
180180
double h_size = calcHexSize(level);
181-
long h_x = (long) x;
182-
long h_y = (long) y;
181+
long h_x = Math.round(x);
182+
long h_y = Math.round(y);
183183
double unit_x = 6 * h_size;
184184
double unit_y = 6 * h_size * h_k;
185185
double h_lat = (h_k * h_x * unit_x + h_y * unit_y) / 2;
@@ -204,25 +204,26 @@ public static final Zone getZoneByXY(double x, double y, int level) {
204204
List<Integer> code3_y = new ArrayList<Integer>();
205205
StringBuffer code3 = new StringBuffer();
206206
StringBuffer code9 = new StringBuffer();
207-
long mod_x = (long) h_x;
208-
long mod_y = (long) h_y;
207+
long mod_x = h_x;
208+
long mod_y = h_y;
209209

210210
for (int i = 0; i <= level + 2; i++) {
211-
double h_pow = Math.pow(3, level + 2 - i);
212-
if (mod_x >= Math.ceil(h_pow / 2)) {
211+
long h_pow = Math.round(Math.pow(3, level + 2 - i));
212+
double h_pow_half = Math.ceil((double) h_pow / 2);
213+
if (mod_x >= h_pow_half) {
213214
code3_x.add(2);
214215
mod_x -= h_pow;
215-
} else if (mod_x <= -Math.ceil(h_pow / 2)) {
216+
} else if (mod_x <= -h_pow_half) {
216217
code3_x.add(0);
217218
mod_x += h_pow;
218219
} else {
219220
code3_x.add(1);
220221
}
221222

222-
if (mod_y >= Math.ceil(h_pow / 2)) {
223+
if (mod_y >= h_pow_half) {
223224
code3_y.add(2);
224225
mod_y -= h_pow;
225-
} else if (mod_y <= -Math.ceil(h_pow / 2)) {
226+
} else if (mod_y <= -h_pow_half) {
226227
code3_y.add(0);
227228
mod_y += h_pow;
228229
} else {

0 commit comments

Comments
 (0)