Java Code Examples for org.apache.lucene.geo.GeoEncodingUtils#decodeLatitude()

The following examples show how to use org.apache.lucene.geo.GeoEncodingUtils#decodeLatitude() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GeoPointColumnReference.java    From crate with Apache License 2.0 6 votes vote down vote up
@Override
public Point value() {
    try {
        if (values.advanceExact(docId)) {
            switch (values.docValueCount()) {
                case 1:
                    long encoded = values.nextValue();
                    return new PointImpl(
                        GeoEncodingUtils.decodeLongitude((int) encoded),
                        GeoEncodingUtils.decodeLatitude((int) (encoded >>> 32)),
                        JtsSpatialContext.GEO
                    );

                default:
                    throw new GroupByOnArrayUnsupportedException(columnName);
            }
        } else {
            return null;
        }
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}
 
Example 2
Source File: LatLonShapeQuery.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
protected Relation relateRangeBBoxToQuery(int minXOffset, int minYOffset, byte[] minTriangle,
                                          int maxXOffset, int maxYOffset, byte[] maxTriangle) {

  double minLat = GeoEncodingUtils.decodeLatitude(NumericUtils.sortableBytesToInt(minTriangle, minYOffset));
  double minLon = GeoEncodingUtils.decodeLongitude(NumericUtils.sortableBytesToInt(minTriangle, minXOffset));
  double maxLat = GeoEncodingUtils.decodeLatitude(NumericUtils.sortableBytesToInt(maxTriangle, maxYOffset));
  double maxLon = GeoEncodingUtils.decodeLongitude(NumericUtils.sortableBytesToInt(maxTriangle, maxXOffset));

  // check internal node against query
  return component2D.relate(minLon, maxLon, minLat, maxLat);
}
 
Example 3
Source File: LatLonPointDistanceFeatureQuery.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private double getDistanceKeyFromEncoded(long encoded) {
  int latitudeBits = (int)(encoded >> 32);
  int longitudeBits = (int)(encoded & 0xFFFFFFFF);
  double lat = GeoEncodingUtils.decodeLatitude(latitudeBits);
  double lon = GeoEncodingUtils.decodeLongitude(longitudeBits);
  return SloppyMath.haversinSortKey(originLat, originLon, lat, lon);
}
 
Example 4
Source File: TestLatLonShape.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testIndexAndQuerySamePolygon() throws Exception {
  Directory dir = newDirectory();
  RandomIndexWriter w = new RandomIndexWriter(random(), dir);
  Document doc = new Document();
  Polygon polygon;
  while(true) {
    try {
      polygon = GeoTestUtil.nextPolygon();
      // quantize the polygon
      double[] lats = new double[polygon.numPoints()];
      double[] lons = new double[polygon.numPoints()];
      for (int i = 0; i < polygon.numPoints(); i++) {
        lats[i] = GeoEncodingUtils.decodeLatitude(GeoEncodingUtils.encodeLatitude(polygon.getPolyLat(i)));
        lons[i] = GeoEncodingUtils.decodeLongitude(GeoEncodingUtils.encodeLongitude(polygon.getPolyLon(i)));
      }
      polygon = new Polygon(lats, lons);
      Tessellator.tessellate(polygon);
      break;
    } catch (Exception e) {
      // invalid polygon, try a new one
    }
  }
  addPolygonsToDoc(FIELDNAME, doc, polygon);
  w.addDocument(doc);
  w.forceMerge(1);

  ///// search //////
  IndexReader reader = w.getReader();
  w.close();
  IndexSearcher searcher = newSearcher(reader);

  Query q = LatLonShape.newPolygonQuery(FIELDNAME, QueryRelation.WITHIN, polygon);
  assertEquals(1, searcher.count(q));
  q = LatLonShape.newPolygonQuery(FIELDNAME, QueryRelation.INTERSECTS, polygon);
  assertEquals(1, searcher.count(q));
  q = LatLonShape.newPolygonQuery(FIELDNAME, QueryRelation.DISJOINT, polygon);
  assertEquals(0, searcher.count(q));

  IOUtils.close(w, reader, dir);
}
 
Example 5
Source File: LatLonPointSpatialField.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Decodes the docValues number into latitude and longitude components, formatting as "lat,lon".
 * The encoding is governed by {@code LatLonDocValuesField}.  The decimal output representation is reflective
 * of the available precision.
 * @param value Non-null; stored location field data
 * @return Non-null; "lat, lon"
 */
public static String decodeDocValueToString(long value) {
  final double latDouble = GeoEncodingUtils.decodeLatitude((int) (value >> 32));
  final double lonDouble = GeoEncodingUtils.decodeLongitude((int) (value & 0xFFFFFFFFL));
  // This # decimal places gets us close to our available precision to 1.40cm; we have a test for it.
  // CEILING round-trips (decode then re-encode then decode to get identical results). Others did not. It also
  //   reverses the "floor" that occurred when we encoded.
  final int DECIMAL_PLACES = 7;
  final RoundingMode ROUND_MODE = CEILING;
  BigDecimal latitudeDecoded = BigDecimal.valueOf(latDouble).setScale(DECIMAL_PLACES, ROUND_MODE);
  BigDecimal longitudeDecoded = BigDecimal.valueOf(lonDouble).setScale(DECIMAL_PLACES, ROUND_MODE);
  return latitudeDecoded.stripTrailingZeros().toPlainString() + ","
      + longitudeDecoded.stripTrailingZeros().toPlainString();
  // return ((float)latDouble) + "," + ((float)lonDouble);  crude but not quite as accurate
}
 
Example 6
Source File: TestNearest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
private double quantizeLat(double latRaw) {
  return GeoEncodingUtils.decodeLatitude(GeoEncodingUtils.encodeLatitude(latRaw));
}
 
Example 7
Source File: TestLatLonDocValuesQueries.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
protected double quantizeLat(double latRaw) {
  return GeoEncodingUtils.decodeLatitude(GeoEncodingUtils.encodeLatitude(latRaw));
}
 
Example 8
Source File: TestLatLonPointQueries.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
protected double quantizeLat(double latRaw) {
  return GeoEncodingUtils.decodeLatitude(GeoEncodingUtils.encodeLatitude(latRaw));
}
 
Example 9
Source File: TestLatLonShapeEncoding.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
protected double decodeY(int yEncoded) {
  return GeoEncodingUtils.decodeLatitude(yEncoded);
}
 
Example 10
Source File: InternalGeoPointClustering.java    From elasticsearch-aggregation-geoclustering with Apache License 2.0 4 votes vote down vote up
public static double decodeLatitude(long encodedLatLon) {
    return GeoEncodingUtils.decodeLatitude((int) (encodedLatLon >>> 32));
}