本文整理汇总了Java中com.spatial4j.core.context.SpatialContext类的典型用法代码示例。如果您正苦于以下问题:Java SpatialContext类的具体用法?Java SpatialContext怎么用?Java SpatialContext使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
SpatialContext类属于com.spatial4j.core.context包,在下文中一共展示了SpatialContext类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: makeShape
import com.spatial4j.core.context.SpatialContext; //导入依赖的package包/类
@Override
public Rectangle makeShape(OCompositeKey key, SpatialContext ctx) {
Point[] points = new Point[2];
int i = 0;
for (Object o : key.getKeys()) {
List<Number> numbers = (List<Number>) o;
double lat = ((Double) OType.convert(numbers.get(0), Double.class)).doubleValue();
double lng = ((Double) OType.convert(numbers.get(1), Double.class)).doubleValue();
points[i] = ctx.makePoint(lng, lat);
i++;
}
Point lowerLeft = points[0];
Point topRight = points[1];
if (lowerLeft.getX() > topRight.getX()) {
double x = lowerLeft.getX();
lowerLeft = ctx.makePoint(topRight.getX(), lowerLeft.getY());
topRight = ctx.makePoint(x, topRight.getY());
}
return ctx.makeRectangle(lowerLeft, topRight);
}
示例2: makeSpatialStrategy
import com.spatial4j.core.context.SpatialContext; //导入依赖的package包/类
/**
* Builds a SpatialStrategy from configuration options.
*/
protected SpatialStrategy makeSpatialStrategy(final Config config) {
//A Map view of Config that prefixes keys with "spatial."
Map<String, String> configMap = new AbstractMap<String, String>() {
@Override
public Set<Entry<String, String>> entrySet() {
throw new UnsupportedOperationException();
}
@Override
public String get(Object key) {
return config.get("spatial." + key, null);
}
};
SpatialContext ctx = SpatialContextFactory.makeSpatialContext(configMap, null);
//Some day the strategy might be initialized with a factory but such a factory
// is non-existent.
return makeSpatialStrategy(config, configMap, ctx);
}
示例3: makeSPT
import com.spatial4j.core.context.SpatialContext; //导入依赖的package包/类
/**
* The factory is looked up via "prefixTree" in args, expecting "geohash" or "quad".
* If its neither of these, then "geohash" is chosen for a geo context, otherwise "quad" is chosen.
*/
public static SpatialPrefixTree makeSPT(Map<String,String> args, ClassLoader classLoader, SpatialContext ctx) {
SpatialPrefixTreeFactory instance;
String cname = args.get(PREFIX_TREE);
if (cname == null)
cname = ctx.isGeo() ? "geohash" : "quad";
if ("geohash".equalsIgnoreCase(cname))
instance = new GeohashPrefixTree.Factory();
else if ("quad".equalsIgnoreCase(cname))
instance = new QuadPrefixTree.Factory();
else {
try {
Class c = classLoader.loadClass(cname);
instance = (SpatialPrefixTreeFactory) c.newInstance();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
instance.init(args,ctx);
return instance.newSPT();
}
示例4: calcDistanceFromErrPct
import com.spatial4j.core.context.SpatialContext; //导入依赖的package包/类
/**
* Computes the distance given a shape and the {@code distErrPct}. The
* algorithm is the fraction of the distance from the center of the query
* shape to its closest bounding box corner.
*
* @param shape Mandatory.
* @param distErrPct 0 to 0.5
* @param ctx Mandatory
* @return A distance (in degrees).
*/
public static double calcDistanceFromErrPct(Shape shape, double distErrPct, SpatialContext ctx) {
if (distErrPct < 0 || distErrPct > 0.5) {
throw new IllegalArgumentException("distErrPct " + distErrPct + " must be between [0 to 0.5]");
}
if (distErrPct == 0 || shape instanceof Point) {
return 0;
}
Rectangle bbox = shape.getBoundingBox();
//Compute the distance from the center to a corner. Because the distance
// to a bottom corner vs a top corner can vary in a geospatial scenario,
// take the closest one (greater precision).
Point ctr = bbox.getCenter();
double y = (ctr.getY() >= 0 ? bbox.getMaxY() : bbox.getMinY());
double diagonalDist = ctx.getDistCalc().distance(ctr, bbox.getMaxX(), y);
return diagonalDist * distErrPct;
}
示例5: testQueries
import com.spatial4j.core.context.SpatialContext; //导入依赖的package包/类
@Test
public void testQueries() throws IOException {
String name = StrategyTestCase.QTEST_Cities_Intersects_BBox;
InputStream in = getClass().getClassLoader().getResourceAsStream(name);
SpatialContext ctx = SpatialContext.GEO;
Iterator<SpatialTestQuery> iter = SpatialTestQuery.getTestQueries(
new SpatialArgsParser(), ctx, name, in );//closes the InputStream
List<SpatialTestQuery> tests = new ArrayList<>();
while( iter.hasNext() ) {
tests.add( iter.next() );
}
Assert.assertEquals( 3, tests.size() );
SpatialTestQuery sf = tests.get(0);
// assert
Assert.assertEquals( 1, sf.ids.size() );
Assert.assertTrue( sf.ids.get(0).equals( "G5391959" ) );
Assert.assertTrue( sf.args.getShape() instanceof Rectangle);
Assert.assertEquals(SpatialOperation.Intersects, sf.args.getOperation());
}
示例6: calcDistanceFromErrPct
import com.spatial4j.core.context.SpatialContext; //导入依赖的package包/类
@Test
public void calcDistanceFromErrPct() {
final SpatialContext ctx = SpatialContext.GEO;
final double DEP = 0.5;//distErrPct
//the result is the diagonal distance from the center to the closest corner,
// times distErrPct
Shape superwide = ctx.makeRectangle(-180, 180, 0, 0);
//0 distErrPct means 0 distance always
assertEquals(0, SpatialArgs.calcDistanceFromErrPct(superwide, 0, ctx), 0);
assertEquals(180 * DEP, SpatialArgs.calcDistanceFromErrPct(superwide, DEP, ctx), 0);
Shape supertall = ctx.makeRectangle(0, 0, -90, 90);
assertEquals(90 * DEP, SpatialArgs.calcDistanceFromErrPct(supertall, DEP, ctx), 0);
Shape upperhalf = ctx.makeRectangle(-180, 180, 0, 90);
assertEquals(45 * DEP, SpatialArgs.calcDistanceFromErrPct(upperhalf, DEP, ctx), 0.0001);
Shape midCircle = ctx.makeCircle(0, 0, 45);
assertEquals(60 * DEP, SpatialArgs.calcDistanceFromErrPct(midCircle, DEP, ctx), 0.0001);
}
示例7: createFields
import com.spatial4j.core.context.SpatialContext; //导入依赖的package包/类
@Override
public List<IndexableField> createFields(SchemaField field, Object value, float boost) {
String externalVal = value.toString();
//we could have 3 fields (two for the lat & lon, one for storage)
List<IndexableField> f = new ArrayList<>(3);
if (field.indexed()) {
Point point = SpatialUtils.parsePointSolrException(externalVal, SpatialContext.GEO);
//latitude
SchemaField subLatSF = subField(field, LAT, schema);
f.add(subLatSF.createField(String.valueOf(point.getY()), subLatSF.indexed() && !subLatSF.omitNorms() ? boost : 1f));
//longitude
SchemaField subLonSF = subField(field, LON, schema);
f.add(subLonSF.createField(String.valueOf(point.getX()), subLonSF.indexed() && !subLonSF.omitNorms() ? boost : 1f));
}
if (field.stored()) {
FieldType customType = new FieldType();
customType.setStored(true);
f.add(createField(field.getName(), externalVal, customType, 1f));
}
return f;
}
示例8: testQueries
import com.spatial4j.core.context.SpatialContext; //导入依赖的package包/类
@Test
public void testQueries() throws IOException {
String name = StrategyTestCase.QTEST_Cities_Intersects_BBox;
InputStream in = getClass().getClassLoader().getResourceAsStream(name);
SpatialContext ctx = SpatialContext.GEO;
Iterator<SpatialTestQuery> iter = SpatialTestQuery.getTestQueries(
new SpatialArgsParser(), ctx, name, in );
List<SpatialTestQuery> tests = new ArrayList<SpatialTestQuery>();
while( iter.hasNext() ) {
tests.add( iter.next() );
}
Assert.assertEquals( 3, tests.size() );
SpatialTestQuery sf = tests.get(0);
// assert
Assert.assertEquals( 1, sf.ids.size() );
Assert.assertTrue( sf.ids.get(0).equals( "G5391959" ) );
Assert.assertTrue( sf.args.getShape() instanceof Rectangle);
Assert.assertEquals( SpatialOperation.Intersects, sf.args.getOperation() );
}
示例9: configure
import com.spatial4j.core.context.SpatialContext; //导入依赖的package包/类
@Override
public void configure(String fieldNameForThisInstance, Map<String, String> properties, Configuration configuration) {
_ctx = SpatialContext.GEO;
_grid = getSpatialPrefixTree(fieldNameForThisInstance, properties);
boolean docValue = false;
if (properties.get(DOC_VALUE) != null) {
docValue = true;
}
_strategy = new RecursivePrefixTreeStrategy(_grid, fieldNameForThisInstance, docValue);
_shapeReadWriter = new ShapeReadWriter<SpatialContext>(_ctx);
addSupportedIndexedShapes(Shape.class);
addSupportedOperations(SpatialOperation.IsDisjointTo);
addSupportedOperations(SpatialOperation.Intersects);
addSupportedOperations(SpatialOperation.IsWithin);
addSupportedOperations(SpatialOperation.Contains);
}
开发者ID:apache,项目名称:incubator-blur,代码行数:17,代码来源:SpatialRecursivePrefixTreeStrategyFieldTypeDefinition.java
示例10: test28
import com.spatial4j.core.context.SpatialContext; //导入依赖的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);
}
示例11: createFields
import com.spatial4j.core.context.SpatialContext; //导入依赖的package包/类
@Override
public List<IndexableField> createFields(SchemaField field, Object value, float boost) {
String externalVal = value.toString();
//we could have 3 fields (two for the lat & lon, one for storage)
List<IndexableField> f = new ArrayList<IndexableField>(3);
if (field.indexed()) {
Point point = SpatialUtils.parsePointSolrException(externalVal, SpatialContext.GEO);
//latitude
SchemaField subLatSF = subField(field, LAT, schema);
f.add(subLatSF.createField(String.valueOf(point.getY()), subLatSF.indexed() && !subLatSF.omitNorms() ? boost : 1f));
//longitude
SchemaField subLonSF = subField(field, LON, schema);
f.add(subLonSF.createField(String.valueOf(point.getX()), subLonSF.indexed() && !subLonSF.omitNorms() ? boost : 1f));
}
if (field.stored()) {
FieldType customType = new FieldType();
customType.setStored(true);
f.add(createField(field.getName(), externalVal, customType, 1f));
}
return f;
}
示例12: GeoProperty
import com.spatial4j.core.context.SpatialContext; //导入依赖的package包/类
public GeoProperty(Property prop) {
this.prop = prop;
this.spatialctx = SpatialContext.GEO;
int maxlevels = 11; // FIXME: how to compute?
GeohashPrefixTree grid = new GeohashPrefixTree(spatialctx, maxlevels);
this.strategy = new RecursivePrefixTreeStrategy(grid, prop.getName());
}
示例13: getDistance
import com.spatial4j.core.context.SpatialContext; //导入依赖的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;
}
示例14: calcDistance
import com.spatial4j.core.context.SpatialContext; //导入依赖的package包/类
public static double calcDistance(Point point, BytesRef bytes, SpatialContext ctx) {
float[] floats = bytesToFloats(bytes);//x y pair order
double minDist = Double.MAX_VALUE;
for (int i = 0; i < floats.length; i += 2) {
float x = floats[i];
float y = floats[i + 1];
double dist = ctx.getDistCalc().distance(point, x, y);
minDist = Math.min(minDist, dist);
}
return minDist;
}
示例15: parse
import com.spatial4j.core.context.SpatialContext; //导入依赖的package包/类
@Override
public ValueSource parse(FunctionQParser fp) throws SyntaxError {
String fieldName = fp.parseId();
SchemaField field = fp.getReq().getSchema().getField(fieldName);
FieldType type = field.getType();
if (!(type instanceof MultiPointDocValuesField))
throw new SyntaxError("This function only supports fields of type "+
MultiPointDocValuesField.class.getName()+", not "+type.getClass().getName());
MultiPointDocValuesField mpdvFieldType = (MultiPointDocValuesField) type;
double[] parsedLatLong = null;
try {
parsedLatLong = ParseUtils.parseLatitudeLongitude(fp.parseArg());
} catch (InvalidShapeException e) {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, e);
}
double y = parsedLatLong[0];
double x = parsedLatLong[1];
SpatialContext ctx = mpdvFieldType.getCtx();
Point point = ctx.makePoint(x, y);
String score = fp.getLocalParams().get("score", "distance");
ValueSource valueSource = new MultiPointDistanceValueSource(fieldName, point, ctx);
if ("distance".equals(score)) {
return valueSource;
}
else if ("recipDistance".equals(score)) {
int shift = fp.getLocalParams().getInt("shift", 100);
int maxScore = fp.getLocalParams().getInt("maxScore", 10);
return new ReciprocalFloatFunction(valueSource, maxScore, shift, shift);
}
else {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "'score' local-param must be one of 'distance', or 'recipDistance'");
}
}