add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / spatial / src / java / org / apache / lucene / spatial / tier / CartesianPolyFilterBuilder.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;
19
20 import java.math.BigDecimal;
21 import java.math.RoundingMode;
22
23 import org.apache.lucene.search.Filter;
24 import org.apache.lucene.spatial.tier.projections.CartesianTierPlotter;
25 import org.apache.lucene.spatial.tier.projections.IProjector;
26 import org.apache.lucene.spatial.tier.projections.SinusoidalProjector;
27 import org.apache.lucene.spatial.geometry.LatLng;
28 import org.apache.lucene.spatial.geometry.FloatLatLng;
29 import org.apache.lucene.spatial.geometry.shape.LLRect;
30
31
32 /**
33  * <p><font color="red"><b>NOTE:</b> This API is still in
34  * flux and might change in incompatible ways in the next
35  * release.</font>
36  */
37 public class CartesianPolyFilterBuilder {
38
39   // Finer granularity than 1 mile isn't accurate with
40   // standard java math.  Also, there's already a 2nd
41   // precise filter, if needed, in DistanceQueryBuilder,
42   // that will make the filtering exact.
43   public static final double MILES_FLOOR = 1.0;
44
45   private IProjector projector = new SinusoidalProjector();
46   private final String tierPrefix;
47         private int minTier;
48         private int maxTier;
49   /**
50    * 
51    * @param tierPrefix The prefix for the name of the fields containing the tier info
52    * @param minTierIndexed The minimum tier level indexed
53    * @param maxTierIndexed The maximum tier level indexed
54    */
55   public CartesianPolyFilterBuilder( String tierPrefix, int minTierIndexed, int maxTierIndexed ) {
56     this.tierPrefix = tierPrefix;
57         this.minTier = minTierIndexed;
58         this.maxTier = maxTierIndexed;
59   }
60   
61   public Shape getBoxShape(double latitude, double longitude, double miles)
62   {  
63     if (miles < MILES_FLOOR) {
64       miles = MILES_FLOOR;
65     }
66     LLRect box1 = LLRect.createBox( new FloatLatLng( latitude, longitude ), miles, miles );
67     LatLng ll = box1.getLowerLeft();
68     LatLng ur = box1.getUpperRight();
69
70     double latY = ur.getLat();
71     double latX = ll.getLat();
72     double longY = ur.getLng();
73     double longX = ll.getLng();
74     double longX2 = 0.0;
75         //These two if checks setup us up to deal with issues around the prime meridian and the 180th meridian
76         //In these two cases, we need to get tiles (tiers) from the lower left up to the meridian and then 
77         //from the meridan to the upper right
78         //Are we crossing the 180 deg. longitude, if so, we need to do some special things
79     if (ur.getLng() < 0.0 && ll.getLng() > 0.0) {
80         longX2 = ll.getLng();
81         longX = -180.0; 
82     }
83         //are we crossing the prime meridian (0 degrees)?  If so, we need to account for it and boxes on both sides
84     if (ur.getLng() > 0.0 && ll.getLng() < 0.0) {
85         longX2 = ll.getLng();
86         longX = 0.0;    
87     }
88     
89     //System.err.println("getBoxShape:"+latY+"," + longY);
90     //System.err.println("getBoxShape:"+latX+"," + longX);
91     CartesianTierPlotter ctp = new CartesianTierPlotter(2, projector,tierPrefix);
92     int bestFit = ctp.bestFit(miles);
93         if (bestFit < minTier){
94                 bestFit = minTier;
95         } else if (bestFit > maxTier){
96                 bestFit = maxTier;
97         }
98     
99     ctp = new CartesianTierPlotter(bestFit, projector,tierPrefix);
100     Shape shape = new Shape(ctp.getTierFieldName());
101     
102     // generate shape
103     // iterate from startX->endX
104     //     iterate from startY -> endY
105     //      shape.add(currentLat.currentLong);
106         //for the edge cases (prime meridian and the 180th meridian), this call handles all tiles East of the meridian
107     //for all other cases, it handles the whole set of tiles
108     shape = getShapeLoop(shape,ctp,latX,longX,latY,longY);
109         if (longX2 != 0.0) {
110               if (longX == 0.0) {
111                 longX = longX2;
112                 longY = 0.0;
113                 //handles the lower left longitude to the prime meridian
114                 //shape = getShapeLoop(shape, ctp, latX, longX, latY, longY);
115               } else {
116                 //this clause handles the lower left longitude up to the 180 meridian
117                 longX = longX2;
118                 longY = 180.0;
119               }
120               shape = getShapeLoop(shape, ctp, latX, longX, latY, longY);
121
122               //System.err.println("getBoxShape2:"+latY+"," + longY);
123                 //System.err.println("getBoxShape2:"+latX+"," + longX);
124             }
125         
126  
127     return shape; 
128   } 
129   
130   public Shape getShapeLoop(Shape shape, CartesianTierPlotter ctp, double latX, double longX, double latY, double longY)
131   {  
132  
133     //System.err.println("getShapeLoop:"+latY+"," + longY);
134     //System.err.println("getShapeLoop:"+latX+"," + longX);
135     double beginAt = ctp.getTierBoxId(latX, longX);
136     double endAt = ctp.getTierBoxId(latY, longY);
137     if (beginAt > endAt){
138               double tmp = beginAt;
139               beginAt = endAt;
140               endAt = tmp;
141         }
142     double tierVert = ctp.getTierVerticalPosDivider();
143     //System.err.println(" | "+ beginAt+" | "+ endAt);
144     
145     double startX = beginAt - (beginAt %1);
146     double startY = beginAt - startX ; //should give a whole number
147     
148     double endX = endAt - (endAt %1);
149     double endY = endAt -endX; //should give a whole number
150     
151     int scale = (int)Math.log10(tierVert);
152     endY = new BigDecimal(endY).setScale(scale, RoundingMode.HALF_EVEN).doubleValue();
153     startY = new BigDecimal(startY).setScale(scale, RoundingMode.HALF_EVEN).doubleValue();
154     double xInc = 1.0d / tierVert;
155     xInc = new BigDecimal(xInc).setScale(scale, RoundingMode.HALF_EVEN).doubleValue();
156     
157     //System.err.println("go from startX:"+startX+" to:" + endX);
158     for (; startX <= endX; startX++){
159       
160       double itY = startY;
161       //System.err.println("go from startY:"+startY+" to:" + endY);
162       while (itY <= endY){
163         //create a boxId
164         // startX.startY
165         double boxId = startX + itY ;
166         shape.addBox(boxId);
167         //System.err.println("----"+startX+" and "+itY);
168         //System.err.println("----"+boxId);
169         itY += xInc;
170         
171         // java keeps 0.0001 as 1.0E-1
172         // which ends up as 0.00011111
173         itY = new BigDecimal(itY).setScale(scale, RoundingMode.HALF_EVEN).doubleValue();
174       }
175     }
176     return shape;
177   }
178   
179   public Filter getBoundingArea(double latitude, double longitude, double miles) 
180   {
181     Shape shape = getBoxShape(latitude, longitude, miles);
182     return new CartesianShapeFilter(shape, shape.getTierId());
183   }
184 }