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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.apache.lucene.spatial.tier;
20 import java.math.BigDecimal;
21 import java.math.RoundingMode;
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;
33 * <p><font color="red"><b>NOTE:</b> This API is still in
34 * flux and might change in incompatible ways in the next
37 public class CartesianPolyFilterBuilder {
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;
45 private IProjector projector = new SinusoidalProjector();
46 private final String tierPrefix;
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
55 public CartesianPolyFilterBuilder( String tierPrefix, int minTierIndexed, int maxTierIndexed ) {
56 this.tierPrefix = tierPrefix;
57 this.minTier = minTierIndexed;
58 this.maxTier = maxTierIndexed;
61 public Shape getBoxShape(double latitude, double longitude, double miles)
63 if (miles < MILES_FLOOR) {
66 LLRect box1 = LLRect.createBox( new FloatLatLng( latitude, longitude ), miles, miles );
67 LatLng ll = box1.getLowerLeft();
68 LatLng ur = box1.getUpperRight();
70 double latY = ur.getLat();
71 double latX = ll.getLat();
72 double longY = ur.getLng();
73 double longX = ll.getLng();
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) {
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) {
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){
95 } else if (bestFit > maxTier){
99 ctp = new CartesianTierPlotter(bestFit, projector,tierPrefix);
100 Shape shape = new Shape(ctp.getTierFieldName());
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);
113 //handles the lower left longitude to the prime meridian
114 //shape = getShapeLoop(shape, ctp, latX, longX, latY, longY);
116 //this clause handles the lower left longitude up to the 180 meridian
120 shape = getShapeLoop(shape, ctp, latX, longX, latY, longY);
122 //System.err.println("getBoxShape2:"+latY+"," + longY);
123 //System.err.println("getBoxShape2:"+latX+"," + longX);
130 public Shape getShapeLoop(Shape shape, CartesianTierPlotter ctp, double latX, double longX, double latY, double longY)
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;
142 double tierVert = ctp.getTierVerticalPosDivider();
143 //System.err.println(" | "+ beginAt+" | "+ endAt);
145 double startX = beginAt - (beginAt %1);
146 double startY = beginAt - startX ; //should give a whole number
148 double endX = endAt - (endAt %1);
149 double endY = endAt -endX; //should give a whole number
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();
157 //System.err.println("go from startX:"+startX+" to:" + endX);
158 for (; startX <= endX; startX++){
161 //System.err.println("go from startY:"+startY+" to:" + endY);
165 double boxId = startX + itY ;
167 //System.err.println("----"+startX+" and "+itY);
168 //System.err.println("----"+boxId);
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();
179 public Filter getBoundingArea(double latitude, double longitude, double miles)
181 Shape shape = getBoxShape(latitude, longitude, miles);
182 return new CartesianShapeFilter(shape, shape.getTierId());