1 /*
2  * Copyright (c) 2019 Martin Davis.
3  *
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
7  * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v20.html
8  * and the Eclipse Distribution License is available at
9  *
10  * http://www.eclipse.org/org/documents/edl-v10.php.
11  */
12 package org.locationtech.jts.index.hprtree;
13 
14 import org.locationtech.jts.geom.Envelope;
15 import org.locationtech.jts.shape.fractal.HilbertCode;
16 
17 public class HilbertEncoder {
18   private int level;
19   private double minx;
20   private double miny;
21   private double strideX;
22   private double strideY;
23 
HilbertEncoder(int level, Envelope extent)24   public HilbertEncoder(int level, Envelope extent) {
25     this.level = level;
26     int hside = (int) Math.pow(2, level) - 1;
27 
28     minx = extent.getMinX();
29     double extentX = extent.getWidth();
30     strideX = extentX / hside;
31 
32     miny = extent.getMinX();
33     double extentY = extent.getHeight();
34     strideY = extentY / hside;
35   }
36 
encode(Envelope env)37   public int encode(Envelope env) {
38     double midx = env.getWidth()/2 + env.getMinX();
39     int x = (int) ((midx - minx) / strideX);
40 
41     double midy = env.getHeight()/2 + env.getMinY();
42     int y = (int) ((midy - miny) / strideY);
43 
44     return HilbertCode.encode(level, x, y);
45   }
46 
47 }
48