pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / spatial / src / java / org / apache / lucene / spatial / tier / projections / CartesianTierPlotter.java
1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 package org.apache.lucene.spatial.tier.projections;
19
20 /**
21  * <p><font color="red"><b>NOTE:</b> This API is still in
22  * flux and might change in incompatible ways in the next
23  * release.</font>
24  */
25 public class CartesianTierPlotter {
26   public static final String DEFALT_FIELD_PREFIX = "_tier_";
27   
28   final int tierLevel;
29   int tierLength;
30   int tierBoxes;
31   int tierVerticalPosDivider;
32   final IProjector projector;
33   final String fieldPrefix;
34   Double idd = Double.valueOf(180);
35   
36   public CartesianTierPlotter (int tierLevel, IProjector projector, String fieldPrefix) {
37   
38     this.tierLevel  = tierLevel;
39     this.projector = projector;
40     this.fieldPrefix = fieldPrefix;
41     
42     setTierLength();
43     setTierBoxes();
44     setTierVerticalPosDivider();
45   }
46   
47   private void setTierLength (){
48     this.tierLength = (int) Math.pow(2 , this.tierLevel);
49   }
50   
51   private void setTierBoxes () {
52     this.tierBoxes = (int)Math.pow(this.tierLength, 2);
53   }
54   
55   /**
56    * Get nearest max power of 10 greater than
57    * the tierlen
58    * e.g
59    * tierId of 13 has tierLen 8192
60    * nearest max power of 10 greater than tierLen 
61    * would be 10,000
62    */
63   
64   private void setTierVerticalPosDivider() {
65     
66     // ceiling of log base 10 of tierLen
67     
68     tierVerticalPosDivider = Double.valueOf(Math.ceil(
69           Math.log10(Integer.valueOf(this.tierLength).doubleValue()))).intValue();
70     
71     // 
72     tierVerticalPosDivider = (int)Math.pow(10, tierVerticalPosDivider );
73     
74   }
75   
76   public double getTierVerticalPosDivider(){
77     return tierVerticalPosDivider;
78   }
79   
80   /**
81    * TierBoxId is latitude box id + longitude box id
82    * where latitude box id, and longitude box id are transposed in to position
83    * coordinates.
84    * 
85    * @param latitude
86    * @param longitude
87    */
88   public double getTierBoxId (double latitude, double longitude) {
89     
90     double[] coords = projector.coords(latitude, longitude);
91     
92     double id = getBoxId(coords[0]) + (getBoxId(coords[1]) / tierVerticalPosDivider);
93     return id ;
94   }
95   
96   
97   private double getBoxId (double coord){
98     
99     
100     return Math.floor(coord / (idd / this.tierLength));
101   }
102   
103   @SuppressWarnings("unused")
104   private double getBoxId (double coord, int tierLen){
105     return Math.floor(coord / (idd / tierLen) );
106   }
107   /**
108    * get the string name representing current tier
109    * _localTier&lt;tiedId&gt;
110    */
111   public String getTierFieldName (){
112     
113     return fieldPrefix + this.tierLevel;
114   }
115   
116   /**
117    * get the string name representing tierId
118    * _localTier&lt;tierId&gt;
119    * @param tierId
120    */
121   public String getTierFieldName (int tierId){
122     
123     return fieldPrefix + tierId;
124   }
125   
126   /**
127    * Find the tier with the best fit for a bounding box
128    * Best fit is defined as the ceiling of
129    *  log2 (circumference of earth / distance) 
130    *  distance is defined as the smallest box fitting
131    *  the corner between a radius and a bounding box.
132    *  
133    *  Distances less than a mile return 15, finer granularity is
134    *  in accurate
135    */
136   public int bestFit(double miles){
137     
138     //28,892 a rough circumference of the earth
139     int circ = 28892;
140     
141     double r = miles / 2.0;
142     
143     double corner = r - Math.sqrt(Math.pow(r, 2) / 2.0d);
144     double times = circ / corner;
145     int bestFit =  (int)Math.ceil(log2(times)) + 1;
146     
147     if (bestFit > 15) {
148       // 15 is the granularity of about 1 mile
149       // finer granularity isn't accurate with standard java math
150       return 15;
151     }
152     return bestFit;
153   }
154   
155   /**
156    * a log to the base 2 formula
157    * <code>Math.log(value) / Math.log(2)</code>
158    * @param value
159    */
160   public double log2(double value) {
161     
162     return Math.log(value) / Math.log(2);
163   }
164 }