public static String stripSpatialFieldSuffix(String fieldName) { if ( !isSpatialField( fieldName ) ) { throw new AssertionFailure( "The field " + fieldName + " is not a spatial field." ); } return fieldName.substring( 0, fieldName.indexOf( SPATIAL_FIELD_SUFFIX ) ); }
/** * Returns a Lucene query to match documents by distance to a center, * relying only on spatial hashes. * * @param center center of the search discus * @param radius distance max to center in km * @param fieldName name of the Lucene Field implementing Coordinates * @return Lucene Query to be used in a search * * @see org.apache.lucene.search.Query * @see org.hibernate.search.spatial.Coordinates */ public static Query buildSpatialHashQuery(Coordinates center, double radius, String fieldName) { int bestSpatialHashLevel = SpatialHelper.findBestSpatialHashLevelForSearchRange( 2.0d * radius ); if ( bestSpatialHashLevel > SpatialFieldBridgeByHash.DEFAULT_BOTTOM_SPATIAL_HASH_LEVEL ) { bestSpatialHashLevel = SpatialFieldBridgeByHash.DEFAULT_BOTTOM_SPATIAL_HASH_LEVEL; } List<String> spatialHashCellsIds = SpatialHelper.getSpatialHashCellsIds( center, radius, bestSpatialHashLevel ); return new SpatialHashQuery( spatialHashCellsIds, SpatialHelper.formatFieldName( bestSpatialHashLevel, fieldName ) ); }
/** * Generate a spatial hash cell id (with both cell index on both dimension in it) for a position * * @param point position to compute the spatial hash cell id for * @param spatialHashLevel Hox many time the dimensions have been split in two * @return the cell id for the point at the given spatial hash level */ public static String getSpatialHashCellId(Point point, int spatialHashLevel) { double[] indexablesCoordinates = projectToIndexSpace( point ); int longitudeCellIndex = getCellIndex( indexablesCoordinates[0], GeometricConstants.PROJECTED_LONGITUDE_RANGE, spatialHashLevel ); int latitudeCellIndex = getCellIndex( indexablesCoordinates[1], GeometricConstants.PROJECTED_LATITUDE_RANGE, spatialHashLevel ); return formatSpatialHashCellId( longitudeCellIndex, latitudeCellIndex ); }
public DistanceComparator(Point center, int numHits, String fieldName) { this.center = center; this.distances = new double[numHits]; this.latitudeField = SpatialHelper.formatLatitude( fieldName ); this.longitudeField = SpatialHelper.formatLongitude( fieldName ); }
if ( SpatialHelper.isSpatialField( fieldPath ) ) { if ( SpatialHelper.isSpatialFieldLatitude( fieldPath ) ) { Number value = field.numericValue(); String spatialPropertyPath = SpatialHelper.stripSpatialFieldSuffix( fieldPath ); accessorBuilder.buildForPath( spatialPropertyPath + ".lat" ) .add( root, value != null ? new JsonPrimitive( value ) : null ); else if ( SpatialHelper.isSpatialFieldLongitude( fieldPath ) ) { Number value = field.numericValue(); String spatialPropertyPath = SpatialHelper.stripSpatialFieldSuffix( fieldPath ); accessorBuilder.buildForPath( spatialPropertyPath + ".lon" ) .add( root, value != null ? new JsonPrimitive( value ) : null );
String fieldName = bridgeDefinedField.getName(); String simpleFieldName = FieldHelper.getEmbeddedFieldPropertyName( fieldName ); if ( !SpatialHelper.isSpatialField( simpleFieldName ) ) { JsonObject field = new JsonObject(); if ( SpatialHelper.isSpatialFieldLongitude( simpleFieldName ) ) { else if ( SpatialHelper.isSpatialFieldLatitude( simpleFieldName ) ) { getOrCreateProperties( payload, fieldName ).add( SpatialHelper.getSpatialFieldRootName( simpleFieldName ), field );
public boolean projectedBoundingBoxCellsIdsInclusionTest( Point center, Double radius) { Integer spatialHashLevel = SpatialHelper.findBestSpatialHashLevelForSearchRange( radius ); List<String> cellsIds = SpatialHelper.getSpatialHashCellsIds( center, radius, spatialHashLevel ); Point edge = null; boolean validated = true; for ( int heading = 0; heading < 360; heading++ ) { edge = center.computeDestination( radius, heading ); String cellId = SpatialHelper.getSpatialHashCellId( edge, spatialHashLevel ); validated &= cellsIds.contains( cellId ); } return validated; }
String propertyPath = bridgeDefinedField.getAbsoluteName(); if ( SpatialHelper.isSpatialFieldLongitude( propertyPath ) ) { else if ( SpatialHelper.isSpatialFieldLatitude( propertyPath ) ) { mappingBuilder.setPropertyAbsolute( SpatialHelper.stripSpatialFieldSuffix( propertyPath ), propertyMapping );
@Test public void getSpatialHashCellIdTest() { Point point = Point.fromDegrees( 45, 4 ); String cellId = SpatialHelper.getSpatialHashCellId( point, 5 ); Assert.assertEquals( "0|8", cellId ); String cellId2 = SpatialHelper.getSpatialHashCellId( point, 7 ); Assert.assertEquals( "1|32", cellId2 ); String cellId3 = SpatialHelper.getSpatialHashCellId( point, 14 ); Assert.assertEquals( "128|4096", cellId3 ); Point point2 = Point.fromDegrees( -12, -179 ); String cellId4 = SpatialHelper.getSpatialHashCellId( point2, 5 ); Assert.assertEquals( "-16|-3", cellId4 ); String cellId5 = SpatialHelper.getSpatialHashCellId( point2, 7 ); Assert.assertEquals( "-63|-9", cellId5 ); String cellId6 = SpatialHelper.getSpatialHashCellId( point2, 14 ); Assert.assertEquals( "-7969|-1093", cellId6 ); }
public String getCoordinatesField() { if ( coordinatesField != null ) { return coordinatesField; } else { return SpatialHelper.stripSpatialFieldSuffix( latitudeField ); } }
spatialHashCellsIds = getSpatialHashCellsIds( Point.fromDegreesInclusive( lowerLeftLatitude, lowerLeftLongitude ), Point.fromDegreesInclusive( upperRightLatitude, GeometricConstants.LONGITUDE_DEGREE_RANGE / 2 ), ); spatialHashCellsIds.addAll( getSpatialHashCellsIds( Point.fromDegreesInclusive( lowerLeftLatitude, return getSpatialHashCellsIds( Point.fromDegreesInclusive( lowerLeftLatitude, lowerLeftLongitude ), Point.fromDegreesInclusive( upperRightLatitude, upperRightLongitude ),
@Test public void getCellIndexTest() { int cellIndex = SpatialHelper.getCellIndex( 0.1, 0.3, 1 ); Assert.assertEquals( 0, cellIndex ); int cellIndex2 = SpatialHelper.getCellIndex( 0.2, 0.3, 1 ); Assert.assertEquals( 1, cellIndex2 ); int cellIndex3 = SpatialHelper.getCellIndex( 3, 10, 4 ); Assert.assertEquals( 4, cellIndex3 ); int cellIndex4 = SpatialHelper.getCellIndex( 6, 10, 4 ); Assert.assertEquals( 9, cellIndex4 ); }
@Test public void findBestSpatialHashLevelForSearchRangeTest() { int bestSpatialHashLevel = SpatialHelper.findBestSpatialHashLevelForSearchRange( 50 ); Assert.assertEquals( 9, bestSpatialHashLevel ); int bestSpatialHashLevel2 = SpatialHelper.findBestSpatialHashLevelForSearchRange( 1 ); Assert.assertEquals( 15, bestSpatialHashLevel2 ); }
private String getLongitudeField() { if ( longitudeField != null ) { return longitudeField; } else { return SpatialHelper.formatLongitude( coordinatesField ); } }
private String getLatitudeField() { if ( latitudeField != null ) { return latitudeField; } else { return SpatialHelper.formatLatitude( coordinatesField ); } }
@Override public void configureFieldMetadata(String name, FieldMetadataBuilder builder) { super.configureFieldMetadata( name, builder ); hashIndexedFieldNames = new String[bottomSpatialHashLevel + 1]; for ( int i = topSpatialHashLevel; i <= bottomSpatialHashLevel; i++ ) { String fieldName = SpatialHelper.formatFieldName( i, name ); hashIndexedFieldNames[i] = fieldName; builder.field( fieldName, FieldType.STRING ); } builder.field( latitudeIndexedFieldName, FieldType.DOUBLE ); builder.field( longitudeIndexedFieldName, FieldType.DOUBLE ); }
if ( SpatialHelper.isSpatialField( fieldPath ) ) { if ( SpatialHelper.isSpatialFieldLatitude( fieldPath ) ) { Number value = field.numericValue(); String spatialPropertyPath = SpatialHelper.stripSpatialFieldSuffix( fieldPath ); accessorBuilder.buildForPath( spatialPropertyPath + ".lat" ) .add( root, value != null ? new JsonPrimitive( value ) : null ); else if ( SpatialHelper.isSpatialFieldLongitude( fieldPath ) ) { Number value = field.numericValue(); String spatialPropertyPath = SpatialHelper.stripSpatialFieldSuffix( fieldPath ); accessorBuilder.buildForPath( spatialPropertyPath + ".lon" ) .add( root, value != null ? new JsonPrimitive( value ) : null );
if ( SpatialHelper.isSpatialField( jsonPropertyName ) ) { if ( isNumeric( field ) && ( SpatialHelper.isSpatialFieldLatitude( jsonPropertyName ) || SpatialHelper.isSpatialFieldLongitude( jsonPropertyName ) ) ) { String spatialJsonPropertyName = SpatialHelper.getSpatialFieldRootName( jsonPropertyName ); JsonObject spatialParent; if ( SpatialHelper.isSpatialFieldLatitude( jsonPropertyName ) ) { addPropertyOfPotentiallyMultipleCardinality( spatialParent, "lat", value != null ? new JsonPrimitive( value ) : null ); else if ( SpatialHelper.isSpatialFieldLongitude( jsonPropertyName ) ) { addPropertyOfPotentiallyMultipleCardinality( spatialParent, "lon", value != null ? new JsonPrimitive( value ) : null );
String propertyPath = bridgeDefinedField.getAbsoluteName(); if ( SpatialHelper.isSpatialFieldLongitude( propertyPath ) ) { else if ( SpatialHelper.isSpatialFieldLatitude( propertyPath ) ) { mappingBuilder.setPropertyAbsolute( SpatialHelper.stripSpatialFieldSuffix( propertyPath ), propertyMapping );
public DistanceComparator(Point center, int numHits, String fieldName) { this.center = center; this.distances = new double[numHits]; this.latitudeField = SpatialHelper.formatLatitude( fieldName ); this.longitudeField = SpatialHelper.formatLongitude( fieldName ); }