本文整理汇总了Java中com.spatial4j.core.distance.DistanceUtils类的典型用法代码示例。如果您正苦于以下问题:Java DistanceUtils类的具体用法?Java DistanceUtils怎么用?Java DistanceUtils使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
DistanceUtils类属于com.spatial4j.core.distance包,在下文中一共展示了DistanceUtils类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: parseSpatial
import com.spatial4j.core.distance.DistanceUtils; //导入依赖的package包/类
private LindenHit parseSpatial(int doc, LindenHit lindenHit) throws IOException {
if (request.isSetSpatialParam()) {
Double lat = LindenUtil.getFieldDoubleValue(leaves, doc, LindenSchemaConf.LATITUDE);
Double lon = LindenUtil.getFieldDoubleValue(leaves, doc, LindenSchemaConf.LONGITUDE);
if (lat != 0.0 && lon != 0.0) {
double distance = DistanceUtils.distLawOfCosinesRAD(
DistanceUtils.toRadians(lat),
DistanceUtils.toRadians(lon),
DistanceUtils.toRadians(request.getSpatialParam().getCoordinate().getLatitude()),
DistanceUtils.toRadians(request.getSpatialParam().getCoordinate().getLongitude()));
distance = DistanceUtils.radians2Dist(distance, DistanceUtils.EARTH_MEAN_RADIUS_KM);
lindenHit.setDistance(distance);
}
}
return lindenHit;
}
示例2: searchNearby
import com.spatial4j.core.distance.DistanceUtils; //导入依赖的package包/类
/**
* Returns a list of location near a certain coordinate.
* @param latitude, @param longitude - Center of search area
* @param distanceInMiles - Search Radius in miles
* @param indexerPath - Path to Lucene index
* @param count - Upper bound to number of results
* @return - List of locations sorted by population
* @throws IOException
*/
public List<Location> searchNearby(Double latitude, Double longitude, Double distanceInMiles, String indexerPath, int count) throws IOException {
double distanceInDeg = DistanceUtils.dist2Degrees(distanceInMiles,DistanceUtils.EARTH_EQUATORIAL_RADIUS_MI);
SpatialArgs spatialArgs = new SpatialArgs(SpatialOperation.IsWithin,
ctx.makeCircle(longitude,latitude, distanceInDeg));
String key = latitude+"-"+longitude;
Filter filter = strategy.makeFilter(spatialArgs);
IndexSearcher searcher = new IndexSearcher(createIndexReader(indexerPath));
Sort sort = new Sort(populationSort);
TopDocs topDocs = searcher.search(new MatchAllDocsQuery(), filter, count, sort);
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
HashMap<String, List<Location>> allCandidates = new HashMap<String, List<Location>>();
getMatchingCandidates(searcher, allCandidates, key, scoreDocs);
List<Location> results = allCandidates.get(key);
return results;
}
示例3: searchIntersect
import com.spatial4j.core.distance.DistanceUtils; //导入依赖的package包/类
public Object searchIntersect(OCompositeKey key, double distance, OCommandContext context) throws IOException {
double lat = ((Double) OType.convert(((OCompositeKey) key).getKeys().get(0), Double.class)).doubleValue();
double lng = ((Double) OType.convert(((OCompositeKey) key).getKeys().get(1), Double.class)).doubleValue();
SpatialOperation operation = SpatialOperation.Intersects;
Point p = ctx.makePoint(lng, lat);
SpatialArgs args = new SpatialArgs(operation, ctx.makeCircle(lng, lat,
DistanceUtils.dist2Degrees(distance, DistanceUtils.EARTH_MEAN_RADIUS_KM)));
Filter filter = strategy.makeFilter(args);
IndexSearcher searcher = searcher();
ValueSource valueSource = strategy.makeDistanceValueSource(p);
Sort distSort = new Sort(valueSource.getSortField(false)).rewrite(searcher);
return new LuceneResultSet(this,
new SpatialQueryContext(context, searcher, new MatchAllDocsQuery(), filter, distSort).setSpatialArgs(args));
}
示例4: build
import com.spatial4j.core.distance.DistanceUtils; //导入依赖的package包/类
@Override
public SpatialQueryContext build(Map<String, Object> query) throws Exception {
Shape shape = parseShape(query);
double distance = 0;
Number n = (Number) query.get(MAX_DISTANCE);
if (n != null) {
distance = n.doubleValue();
}
Point p = (Point) shape;
SpatialArgs args = new SpatialArgs(SpatialOperation.Intersects, factory.context().makeCircle(p.getX(), p.getY(),
DistanceUtils.dist2Degrees(distance, DistanceUtils.EARTH_MEAN_RADIUS_KM)));
Filter filter = manager.strategy().makeFilter(args);
ValueSource valueSource = manager.strategy().makeDistanceValueSource(p);
IndexSearcher searcher = manager.searcher();
Sort distSort = new Sort(valueSource.getSortField(false)).rewrite(searcher);
return new SpatialQueryContext(null, searcher, new MatchAllDocsQuery(), filter, distSort).setSpatialArgs(args);
}
示例5: initMaxLevels
import com.spatial4j.core.distance.DistanceUtils; //导入依赖的package包/类
protected void initMaxLevels() {
String mlStr = args.get(MAX_LEVELS);
if (mlStr != null) {
maxLevels = Integer.valueOf(mlStr);
return;
}
double degrees;
String maxDetailDistStr = args.get(MAX_DIST_ERR);
if (maxDetailDistStr == null) {
if (!ctx.isGeo()) {
return;//let default to max
}
degrees = DistanceUtils.dist2Degrees(DEFAULT_GEO_MAX_DETAIL_KM, DistanceUtils.EARTH_MEAN_RADIUS_KM);
} else {
degrees = Double.parseDouble(maxDetailDistStr);
}
maxLevels = getLevelForDistance(degrees);
}
示例6: distance
import com.spatial4j.core.distance.DistanceUtils; //导入依赖的package包/类
/**
* @param doc The doc to score
* @return The haversine distance formula
*/
protected double distance(int doc, FunctionValues p1DV, FunctionValues p2DV) {
double[] p1D = new double[2];
double[] p2D = new double[2];
p1DV.doubleVal(doc, p1D);
p2DV.doubleVal(doc, p2D);
double y1;
double x1;
double y2;
double x2;
if (convertToRadians) {
y1 = p1D[0] * DistanceUtils.DEGREES_TO_RADIANS;
x1 = p1D[1] * DistanceUtils.DEGREES_TO_RADIANS;
y2 = p2D[0] * DistanceUtils.DEGREES_TO_RADIANS;
x2 = p2D[1] * DistanceUtils.DEGREES_TO_RADIANS;
} else {
y1 = p1D[0];
x1 = p1D[1];
y2 = p2D[0];
x2 = p2D[1];
}
return DistanceUtils.distHaversineRAD(y1,x1,y2,x2)*radius;
}
示例7: SpatialScorer
import com.spatial4j.core.distance.DistanceUtils; //导入依赖的package包/类
public SpatialScorer(AtomicReaderContext readerContext, Bits acceptDocs, SpatialWeight w, float qWeight) throws IOException {
super(w);
this.weight = w;
this.qWeight = qWeight;
this.reader = readerContext.reader();
this.maxDoc = reader.maxDoc();
this.acceptDocs = acceptDocs;
latVals = latSource.getValues(weight.latContext, readerContext);
lonVals = lonSource.getValues(weight.lonContext, readerContext);
this.lonMin = SpatialDistanceQuery.this.lonMin;
this.lonMax = SpatialDistanceQuery.this.lonMax;
this.lon2Min = SpatialDistanceQuery.this.lon2Min;
this.lon2Max = SpatialDistanceQuery.this.lon2Max;
this.latMin = SpatialDistanceQuery.this.latMin;
this.latMax = SpatialDistanceQuery.this.latMax;
this.lon2 = SpatialDistanceQuery.this.lon2;
this.calcDist = SpatialDistanceQuery.this.calcDist;
this.latCenterRad = SpatialDistanceQuery.this.latCenter * DistanceUtils.DEGREES_TO_RADIANS;
this.lonCenterRad = SpatialDistanceQuery.this.lonCenter * DistanceUtils.DEGREES_TO_RADIANS;
this.latCenterRad_cos = this.calcDist ? Math.cos(latCenterRad) : 0;
this.dist = SpatialDistanceQuery.this.dist;
this.planetRadius = SpatialDistanceQuery.this.planetRadius;
}
示例8: dist
import com.spatial4j.core.distance.DistanceUtils; //导入依赖的package包/类
double dist(double lat, double lon) {
double latRad = lat * DistanceUtils.DEGREES_TO_RADIANS;
double lonRad = lon * DistanceUtils.DEGREES_TO_RADIANS;
// haversine, specialized to avoid a cos() call on latCenterRad
double diffX = latCenterRad - latRad;
double diffY = lonCenterRad - lonRad;
double hsinX = Math.sin(diffX * 0.5);
double hsinY = Math.sin(diffY * 0.5);
double h = hsinX * hsinX +
(latCenterRad_cos * Math.cos(latRad) * hsinY * hsinY);
double result = (planetRadius * 2 * Math.atan2(Math.sqrt(h), Math.sqrt(1 - h)));
// save the results of this calculation
lastDistDoc = doc;
lastDist = result;
return result;
}
示例9: radiusQuery
import com.spatial4j.core.distance.DistanceUtils; //导入依赖的package包/类
private String radiusQuery(double lat, double lon, double dDEG, String score, String filter) {
//Choose between the Solr/Geofilt syntax, and the Lucene spatial module syntax
if (fieldName.equals("bbox") || random().nextBoolean()) {
//we cheat for bbox strategy which doesn't do radius, only rect.
final String qparser = fieldName.equals("bbox") ? "bbox" : "geofilt";
return "{!" + qparser + " " +
"sfield=" + fieldName + " "
+ (score != null ? "score="+score : "") + " "
+ (filter != null ? "filter="+filter : "") + " "
+ "pt=" + lat + "," + lon + " d=" + (dDEG * DistanceUtils.DEG_TO_KM) + "}";
} else {
return "{! "
+ (score != null ? "score="+score : "") + " "
+ (filter != null ? "filter="+filter : "") + " "
+ "}" + fieldName + ":\"Intersects(BUFFER(POINT(" + lon + " " + lat + ")," + dDEG + "))\"";
}
}
示例10: searchIntersect
import com.spatial4j.core.distance.DistanceUtils; //导入依赖的package包/类
public Object searchIntersect(OCompositeKey key, double distance, OCommandContext context) throws IOException {
double lat = ((Double) OType.convert(((OCompositeKey) key).getKeys().get(0), Double.class)).doubleValue();
double lng = ((Double) OType.convert(((OCompositeKey) key).getKeys().get(1), Double.class)).doubleValue();
SpatialOperation operation = SpatialOperation.Intersects;
Point p = ctx.makePoint(lng, lat);
SpatialArgs args = new SpatialArgs(operation, ctx.makeCircle(lng, lat,
DistanceUtils.dist2Degrees(distance, DistanceUtils.EARTH_MEAN_RADIUS_KM)));
Filter filter = strategy.makeFilter(args);
IndexSearcher searcher = getSearcher();
ValueSource valueSource = strategy.makeDistanceValueSource(p);
Sort distSort = new Sort(valueSource.getSortField(false)).rewrite(searcher);
return new LuceneResultSet(this,
new SpatialQueryContext(context, searcher, new MatchAllDocsQuery(), filter, distSort).setSpatialArgs(args));
}
示例11: createSpatialQuery
import com.spatial4j.core.distance.DistanceUtils; //导入依赖的package包/类
/**
* Implemented for compatibility with Solr 3 spatial geofilt & bbox query parsers:
* {@link SpatialQueryable}.
*/
@Override
public Query createSpatialQuery(QParser parser, SpatialOptions options) {
//--WARNING: the code from here to the next marker is identical to LatLonType's impl.
double[] point = null;
try {
point = ParseUtils.parseLatitudeLongitude(options.pointStr);
} catch (InvalidShapeException e) {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, e);
}
// lat & lon in degrees
double latCenter = point[0];
double lonCenter = point[1];
double distDeg = DistanceUtils.dist2Degrees(options.distance, options.radius);
//--END-WARNING
Shape shape = ctx.makeCircle(lonCenter, latCenter, distDeg);
if (options.bbox)
shape = shape.getBoundingBox();
SpatialArgs spatialArgs = new SpatialArgs(SpatialOperation.Intersects, shape);
return getQueryFromSpatialArgs(parser, options.field, spatialArgs);
}
示例12: test28
import com.spatial4j.core.distance.DistanceUtils; //导入依赖的package包/类
@Test
public void test28() throws ParseException {
SpatialContext ctx = SpatialContext.GEO;
ShapeReadWriter<SpatialContext> shapeReadWriter = new ShapeReadWriter<SpatialContext>(ctx);
int maxLevels = 11;
SpatialPrefixTree grid = new GeohashPrefixTree(ctx, maxLevels);
RecursivePrefixTreeStrategy strategy = new RecursivePrefixTreeStrategy(grid, "a.id_gis", false);
Circle circle = ctx.makeCircle(-80.0, 33.0, DistanceUtils.dist2Degrees(10, DistanceUtils.EARTH_MEAN_RADIUS_KM));
SpatialArgs args = new SpatialArgs(SpatialOperation.Intersects, circle);
String writeSpatialArgs = SpatialArgsParser.writeSpatialArgs(args, shapeReadWriter);
// This has to be done because of rounding.
SpatialArgs spatialArgs = SpatialArgsParser.parse(writeSpatialArgs, shapeReadWriter);
Query q1 = sq(strategy.makeQuery(spatialArgs));
Query q = parseSq("a.id_gis:\"" + writeSpatialArgs + "\"");
boolean equals = q1.equals(q);
assertTrue(equals);
}
示例13: construct
import com.spatial4j.core.distance.DistanceUtils; //导入依赖的package包/类
@Override
protected Filter construct(LindenFilter lindenFilter, LindenConfig config) throws IOException {
LindenSpatialFilter spatialFilter = lindenFilter.getSpatialFilter();
SpatialArgs spatialArgs = new SpatialArgs(
SpatialOperation.Intersects,
spatialContext.makeCircle(
spatialFilter.getSpatialParam().coordinate.getLongitude(),
spatialFilter.getSpatialParam().coordinate.getLatitude(),
DistanceUtils
.dist2Degrees(spatialFilter.getSpatialParam().getDistanceRange(), DistanceUtils.EARTH_MEAN_RADIUS_KM)));
return spatialStrategy.makeFilter(spatialArgs);
}
示例14: geoSearch
import com.spatial4j.core.distance.DistanceUtils; //导入依赖的package包/类
/**
* Returns a geoquery.
*/
public Filter geoSearch(String value) {
GeopositionComparator comp = (GeopositionComparator) prop.getComparator();
double dist = comp.getMaxDistance();
double degrees = DistanceUtils.dist2Degrees(dist, DistanceUtils.EARTH_MEAN_RADIUS_KM * 1000.0);
Shape circle = spatialctx.makeCircle(parsePoint(value), degrees);
SpatialArgs args = new SpatialArgs(SpatialOperation.Intersects, circle);
return strategy.makeFilter(args);
}
示例15: getDistance
import com.spatial4j.core.distance.DistanceUtils; //导入依赖的package包/类
/**
* Returns distance (in meters) between 2 points.
*
* @param point1 Must not be null
* @param point2 Must not be null
* @return distance in meters
*/
public static double getDistance(Point point1, Point point2) {
Assert.notNull(point1, "point1 must not be null");
Assert.notNull(point2, "point2 must not be null");
final SpatialContext ctx = SpatialContext.GEO;
com.spatial4j.core.shape.Point p1 = ctx.makePoint(point1.getLongitude(), point1.getLatitude());
com.spatial4j.core.shape.Point p2 = ctx.makePoint(point2.getLongitude(), point2.getLatitude());
return DistanceUtils.degrees2Dist(ctx.getDistCalc().distance(p1, p2), DistanceUtils.EARTH_MEAN_RADIUS_KM) * 1000;
}