Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  
  package com.browseengine.bobo.geosearch.impl;
  
  import java.util.HashMap;
  import java.util.Map;
  
 
This is the GeoSearch POJO-in, POJO-out converter that handles bit interlacing and inverse bit interlacing.

Author(s):
Shane Detsch
Ken McCracken
Geoff Cooney
 
 public class GeoConverter implements IGeoConverter {
     private static final double PIRADIANS = 180.0;
     private static final int LONGLENGTH = 64;
     private static final int INTLENGTH = 32;
     private static final long ONE_AS_LONG = 1L;
     private static final int ONE_AS_INT = 1;
     
     Map<StringBytebitmasks = new HashMap<StringByte>();
     
     @Override
     public void addFieldBitMask(String fieldNamebyte bitMask) {
         .put(fieldNamebitMask);
     }
     
     @Override
         MappedFieldNameFilterConverter mappedFieldNameFilterConverter = new MappedFieldNameFilterConverter();
         for (Entry<StringBytebitmask : .entrySet()) {
             String fieldName = bitmask.getKey();
             Byte bitMask = bitmask.getValue();
             mappedFieldNameFilterConverter.addFieldBitMask(fieldNamebitMask);
         }
         return mappedFieldNameFilterConverter;
     }
    
    
    
 
     @Override
     public CartesianGeoRecord toCartesianGeoRecord(IFieldNameFilterConverter fieldNameFilterConverter
             String fieldNameLatitudeLongitudeDocId longitudeLatitudeDocId) {
         byte filterByte = fieldNameFilterConverter == null ? 
                 . : 
                 fieldNameFilterConverter.getFilterValue(new String[] {fieldName});
         
         return toCartesianGeoRecord(longitudeLatitudeDocIdfilterByte);
     }
     
     //This constant is calculated as the 1 - e^2 where 
     //e^2 = (a^2 - b^2) / a^2 with a = major-axis of the earth and
     //b = minor axis of the earth.  Calculated with values from WGS84
     //Geodetic datum.
     static final double ONE_MINUS_ECCENTRICITY_OF_EARTH = 0.99330562;
     
     @Override
     public int getXFromRadians(double latRadiansdouble longRadians)
     {
       return (int) (. * Math.cos(latRadians) * Math.cos(longRadians));
     }
 
     @Override
     public int getYFromRadians(double latRadiansdouble longRadians)
     {
       return (int) (. * Math.cos(latRadians) * Math.sin(longRadians));
     }
 
     @Override
     public int getZFromRadians(double latRadians)
     {
       return (int) (. *  * Math.sin(latRadians));
     }
 
     @Override
     public IDGeoRecord toIDGeoRecord(CartesianCoordinateUUID coordinate) {
         return toIDGeoRecord(coordinate.xcoordinate.ycoordinate.zcoordinate.uuid);
     }
     
     @Override
    public CartesianCoordinateUUID toCartesianCoordinate(double latitudedouble longitudebyte[] uuid) {
        double latRadians = Conversions.d2r(latitude);
        double longRadians =  Conversions.d2r(longitude);
        int x = getXFromRadians(latRadianslongRadians);
        int y = getYFromRadians(latRadianslongRadians);
        int z = getZFromRadians(latRadians);
        
        return new CartesianCoordinateUUID(xyzuuid);
    }
    
    @Override
    public IDGeoRecord toIDGeoRecord(double latitudedouble longitudebyte[] uuid) {
        double latRadians = Conversions.d2r(latitude);
        double longRadians =  Conversions.d2r(longitude);
        int x = getXFromRadians(latRadianslongRadians);
        int y = getYFromRadians(latRadianslongRadians);
        int z = getZFromRadians(latRadians);
        
        return toIDGeoRecord(xyzuuid);
    }
    
    protected IDGeoRecord toIDGeoRecord(int xint yint zbyte[] uuid) { 
        long highOrderBits = 0;
        int highOrderPosition =  - 2;
        
        //first divide on the sign bit
        if (x < 0) {
            x = x + .;
        } else {
            highOrderBits += ( << highOrderPosition);
        }
        highOrderPosition--;
        
        if (y < 0) {
            y = y + .;
        } else {
            highOrderBits += ( << highOrderPosition);
        }
        highOrderPosition--;
        
        if (z < 0) {
            z = z + .;
        } else {
            highOrderBits += ( << highOrderPosition);
        }
        highOrderPosition--;
        //now interlace the rest
        int xPos =  - 2;
        int yPos =  - 2;
        int zPos =  - 2;
        
        highOrderBits = interlaceToLong(x - 2, highOrderBitshighOrderPosition, 3);
        xPos -= (highOrderPosition / 3) + 1;
        highOrderBits = interlaceToLong(y - 2, highOrderBits, --highOrderPosition, 3);
        yPos -= (highOrderPosition / 3) + 1;
        highOrderBits = interlaceToLong(z - 2, highOrderBits, --highOrderPosition, 3);
        zPos -= (highOrderPosition / 3) + 1;
        
        int lowOrderBits = 0;
        int lowOrderPosition =  - 2;
        lowOrderBits = interlaceToInteger(xxPoslowOrderBitslowOrderPosition, 3);
        lowOrderBits = interlaceToInteger(yyPoslowOrderBits, --lowOrderPosition, 3);
        lowOrderBits = interlaceToInteger(zzPoslowOrderBits, --lowOrderPosition, 3);
        return new IDGeoRecord(highOrderBitslowOrderBitsuuid);
    }
    
    private long interlaceToLong(int inputValueint inputBitPositionlong longValueint longBitPosition
            int interlaceInterval) {
        while (longBitPosition > -1 && inputBitPosition > -1) {
            if ((inputValue & ( << inputBitPosition)) != 0) {
                longValue +=  << longBitPosition;
            }
            longBitPosition -= interlaceInterval;
            inputBitPosition--;
        }
        
        return longValue;
    }
    
    private int interlaceToInteger(int inputValueint inputBitPosition
            int integerValueint integerBitPositionint interlaceInterval) {
        while (integerBitPosition > -1 && inputBitPosition > -1) {
            if ((inputValue & ( << inputBitPosition)) != 0) {
                integerValue +=  << integerBitPosition;
            }
            integerBitPosition -= interlaceInterval;
            inputBitPosition--;
        }
        
        return integerValue;
    }   
    @Override
        int x = 0;
        int y = 0;
        int z = 0;
        int dimensions = 3;
        
        int highOrderPosition =  - 2 - dimensions;
        
        //now interlace the rest
        int xPos =  - 2;
        int yPos =  - 2;
        int zPos =  - 2;
        
        //uninterlace high order bits
        x = unInterlaceFromLong(xxPosgeoRecord.highOrderhighOrderPositiondimensions);
        xPos -= (highOrderPosition / dimensions) + 1;
        y = unInterlaceFromLong(yyPosgeoRecord.highOrder, --highOrderPositiondimensions);
        yPos -= (highOrderPosition / dimensions) + 1;
        z = unInterlaceFromLong(zzPosgeoRecord.highOrder, --highOrderPositiondimensions);
        zPos -= (highOrderPosition / dimensions) + 1;
        
        //uninterlace low order bits
        int lowOrderPosition =  - 2;
        x = unInterlaceFromInt(xxPosgeoRecord.lowOrderlowOrderPositiondimensions);
        y = unInterlaceFromInt(yyPosgeoRecord.lowOrder, --lowOrderPositiondimensions);
        z = unInterlaceFromInt(zzPosgeoRecord.lowOrder, --lowOrderPositiondimensions);
        
        highOrderPosition =  - 2;
        //uninterlace sign bit
        if ((geoRecord.highOrder & ( << highOrderPosition)) == 0) {
            x = x - .;
        } 
        highOrderPosition--;
        
        if ((geoRecord.highOrder & ( << highOrderPosition)) == 0) {
            y = y - .;
        } 
        highOrderPosition--;
        
        if ((geoRecord.highOrder & ( << highOrderPosition)) == 0) {
            z = z - .;
        } 
        highOrderPosition--;
        return new CartesianCoordinateUUID(xyzgeoRecord.id);
    }
    private int unInterlaceFromLong(int outputValueint outputBitPoslong longValueint longBitPosition
            int interlaceInterval) {
        while (longBitPosition > -1 && outputBitPos > -1) {
            if ((longValue & ( << longBitPosition)) != 0) {
                outputValue +=  << outputBitPos
            }
            longBitPosition -= interlaceInterval;
            outputBitPos--;
        }
        
        return outputValue;
    }
    
    private int unInterlaceFromInt(int outputValueint outputBitPosint intValueint intBitPosition
            int interlaceInterval) {
        while (intBitPosition > -1 && outputBitPos > -1) {
            if ((intValue & ( << intBitPosition)) != 0) {
                outputValue +=  << outputBitPos
            } 
            intBitPosition -= interlaceInterval;
            outputBitPos--;
        }
        
        return outputValue;
    }
    
    @Override
    public CartesianGeoRecord toCartesianGeoRecord(LatitudeLongitudeDocId latLongDocIDbyte filterByte) {
        double latRadians = Conversions.d2r(latLongDocID.latitude);
        double longRadians =  Conversions.d2r(latLongDocID.longitude);
        int x = getXFromRadians(latRadianslongRadians);
        int y = getYFromRadians(latRadianslongRadians);
        int z = getZFromRadians(latRadians);
        
        return toCartesianGeoRecord(xyzlatLongDocID.docidfilterByte);
    }
    
    
Bitinterlaces docid, x, y, z into long high and low order bits with the lose of a sign bit on docid which does not exist anyways and the least significant bit on z which is sub meter precision.

Parameters:
x
y
z
docid
filterByte
Returns:
    CartesianGeoRecord toCartesianGeoRecord(int xint yint zint docidbyte filterByte) {
        long highOrderBits = 0;
        int highOrderPosition =  - 2;
        
        //first divide on the sign bit
        if (x < 0) {
            x = x + .;
        } else {
            highOrderBits += ( << highOrderPosition);
        }
        highOrderPosition--;
        
        if (y < 0) {
            y = y + .;
        } else {
            highOrderBits += ( << highOrderPosition);
        }
        highOrderPosition--;
        
        if (z < 0) {
            z = z + .;
        } else {
            highOrderBits += ( << highOrderPosition);
        }
        highOrderPosition--;
        
        
        //now interlace the rest
        int xPos =  - 2;
        int yPos =  - 2;
        int zPos =  - 2;
        int docIdPos =  - 2;
        
        highOrderBits = interlaceToLong(docid - 2, highOrderBitshighOrderPosition, 4);
        docIdPos -= (highOrderPosition / 4) + 1;
        highOrderBits = interlaceToLong(x - 2, highOrderBits, --highOrderPosition, 4);
        xPos -= (highOrderPosition / 4) + 1;
        highOrderBits = interlaceToLong(y - 2, highOrderBits, --highOrderPosition, 4);
        yPos -= (highOrderPosition / 4) + 1;
        highOrderBits = interlaceToLong(z - 2, highOrderBits, --highOrderPosition, 4);
        zPos -= (highOrderPosition / 4) + 1;
        
        
        long lowOrderBits = 0;
        int lowOrderPosition =  - 2;
        lowOrderBits = interlaceToLong(dociddocIdPoslowOrderBitslowOrderPosition, 4);
        lowOrderBits = interlaceToLong(xxPoslowOrderBits, --lowOrderPosition, 4);
        lowOrderBits = interlaceToLong(yyPoslowOrderBits, --lowOrderPosition, 4);
        lowOrderBits = interlaceToLong(zzPoslowOrderBits, --lowOrderPosition, 4);
        
        return new CartesianGeoRecord(highOrderBitslowOrderBitsfilterByte);
    }
    
    @Override
        int x = 0;
        int y = 0;
        int z = 0;
        int docid = 0;
        int dimensions = 4;
        
        int highOrderPosition =  - 2 - dimensions + 1;
        
        //now interlace the rest
        int xPos =  - 2;
        int yPos =  - 2;
        int zPos =  - 2;
        int docidPos =  - 2;
        
        //uninterlace high order bits
        docid = unInterlaceFromLong(dociddocidPosgeoRecord.highOrderhighOrderPositiondimensions);
        docidPos -= (highOrderPosition / dimensions) + 1;
        x = unInterlaceFromLong(xxPosgeoRecord.highOrder, --highOrderPositiondimensions);
        xPos -= (highOrderPosition / dimensions) + 1;
        y = unInterlaceFromLong(yyPosgeoRecord.highOrder, --highOrderPositiondimensions);
        yPos -= (highOrderPosition / dimensions) + 1;
        z = unInterlaceFromLong(zzPosgeoRecord.highOrder, --highOrderPositiondimensions);
        zPos -= (highOrderPosition / dimensions) + 1;
        
        //uninterlace low order bits
        int lowOrderPosition =  - 2;
        docid = unInterlaceFromLong(dociddocidPosgeoRecord.lowOrderlowOrderPositiondimensions);
        x = unInterlaceFromLong(xxPosgeoRecord.lowOrder, --lowOrderPositiondimensions);
        y = unInterlaceFromLong(yyPosgeoRecord.lowOrder, --lowOrderPositiondimensions);
        z = unInterlaceFromLong(zzPosgeoRecord.lowOrder, --lowOrderPositiondimensions);
        
        
        highOrderPosition =  - 2;
        //uninterlace sign bit
        if ((geoRecord.highOrder & ( << highOrderPosition)) == 0) {
            x = x - .;
        } 
        highOrderPosition--;
        
        if ((geoRecord.highOrder & ( << highOrderPosition)) == 0) {
            y = y - .;
        } 
        highOrderPosition--;
        
        if ((geoRecord.highOrder & ( << highOrderPosition)) == 0) {
            z = z - .;
        } 
        highOrderPosition--;
        return new CartesianCoordinateDocId(xyzdocid);
    }
    @Override
    public CartesianGeoRecord toCartesianGeoRecord(CartesianCoordinateDocId coordbyte filterByte) {
        return toCartesianGeoRecord(coord.xcoord.ycoord.zcoord.docidfilterByte);
    }
New to GrepCode? Check out our FAQ X