org.locationtech.spatial4j.context.SpatialContext Java Examples

The following examples show how to use org.locationtech.spatial4j.context.SpatialContext. 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: ElasticsearchSearchQueryBase.java    From vertexium with Apache License 2.0 6 votes vote down vote up
private ShapeBuilder getCircleBuilder(GeoCircle geoCircle) {
    // NOTE: as of ES7, storing circles is no longer supported so we need approximate the circle with a polygon
    double radius = geoCircle.getRadius();
    double maxSideLengthKm = getSearchIndex().getConfig().getGeocircleToPolygonSideLength();
    maxSideLengthKm = Math.min(radius, maxSideLengthKm);

    // calculate how many points we need to use given the length of a polygon side
    int numberOfPoints = (int) Math.ceil(Math.PI / Math.asin((maxSideLengthKm / (2 * radius))));
    numberOfPoints = Math.min(numberOfPoints, getSearchIndex().getConfig().getGeocircleToPolygonMaxNumSides());

    // Given the number of sides, loop through slices of 360 degrees and calculate the lat/lon at that radius and heading
    SpatialContext spatialContext = SpatialContext.GEO;
    DistanceCalculator distanceCalculator = spatialContext.getDistCalc();
    Point centerPoint = spatialContext.getShapeFactory().pointXY(DistanceUtils.normLonDEG(geoCircle.getLongitude()), DistanceUtils.normLatDEG(geoCircle.getLatitude()));
    ArrayList<GeoPoint> points = new ArrayList<>();
    for (float angle = 360; angle > 0; angle -= 360.0 / numberOfPoints) {
        Point point = distanceCalculator.pointOnBearing(centerPoint, geoCircle.getRadius() * KM_TO_DEG, angle, spatialContext, null);
        points.add(new GeoPoint(point.getY(), point.getX()));
    }

    // Polygons must start/end at the same point, so add the first point onto the end
    points.add(points.get(0));

    return getPolygonBuilder(new GeoPolygon(points, geoCircle.getDescription()));
}
 
Example #2
Source File: SpatialArgs.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example #3
Source File: GeometricUnaryFunction.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public Value evaluate(ValueFactory valueFactory, Value... args) throws ValueExprEvaluationException {
	if (args.length != 1) {
		throw new ValueExprEvaluationException(getURI() + " requires exactly 1 argument, got " + args.length);
	}

	SpatialContext geoContext = SpatialSupport.getSpatialContext();
	Shape geom = FunctionArguments.getShape(this, args[0], geoContext);

	String wkt;
	try {
		wkt = SpatialSupport.getWktWriter().toWkt(operation(geom));
	} catch (IOException | RuntimeException e) {
		throw new ValueExprEvaluationException(e);
	}

	return valueFactory.createLiteral(wkt, GEO.WKT_LITERAL);
}
 
Example #4
Source File: GeometricBinaryFunction.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public Value evaluate(ValueFactory valueFactory, Value... args) throws ValueExprEvaluationException {
	if (args.length != 2) {
		throw new ValueExprEvaluationException(getURI() + " requires exactly 2 arguments, got " + args.length);
	}

	SpatialContext geoContext = SpatialSupport.getSpatialContext();
	Shape geom1 = FunctionArguments.getShape(this, args[0], geoContext);
	Shape geom2 = FunctionArguments.getShape(this, args[1], geoContext);

	String wkt;
	try {
		Shape result = operation(geom1, geom2);
		wkt = SpatialSupport.getWktWriter().toWkt(result);
	} catch (IOException | RuntimeException e) {
		throw new ValueExprEvaluationException(e);
	}
	return valueFactory.createLiteral(wkt, GEO.WKT_LITERAL);
}
 
Example #5
Source File: WithinFunction.java    From crate with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private static Shape parseLeftShape(Object left) {
    Shape shape;
    if (left instanceof Point) {
        Point point = (Point) left;
        shape = SpatialContext.GEO.getShapeFactory().pointXY(point.getX(), point.getY());
    } else if (left instanceof Double[]) {
        Double[] values = (Double[]) left;
        shape = SpatialContext.GEO.getShapeFactory().pointXY(values[0], values[1]);
    } else if (left instanceof String) {
        shape = GeoJSONUtils.wkt2Shape((String) left);
    } else {
        shape = GeoJSONUtils.map2Shape((Map<String, Object>) left);
    }
    return shape;
}
 
Example #6
Source File: Buffer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public Value evaluate(ValueFactory valueFactory, Value... args) throws ValueExprEvaluationException {
	if (args.length != 3) {
		throw new ValueExprEvaluationException(getURI() + " requires exactly 3 arguments, got " + args.length);
	}

	SpatialContext geoContext = SpatialSupport.getSpatialContext();
	Shape geom = FunctionArguments.getShape(this, args[0], geoContext);
	double radiusUom = FunctionArguments.getDouble(this, args[1]);
	IRI units = FunctionArguments.getUnits(this, args[2]);
	double radiusDegs = FunctionArguments.convertToDegrees(radiusUom, units);

	Shape buffered = SpatialSupport.getSpatialAlgebra().buffer(geom, radiusDegs);

	String wkt;
	try {
		wkt = SpatialSupport.getWktWriter().toWkt(buffered);
	} catch (IOException ioe) {
		throw new ValueExprEvaluationException(ioe);
	}
	return valueFactory.createLiteral(wkt, GEO.WKT_LITERAL);
}
 
Example #7
Source File: S2PrefixTreeTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
@Repeat(iterations = 10)
public void testPrecision() {
  int arity = random().nextInt(3) +1;
  SpatialContext context = new Geo3dSpatialContextFactory().newSpatialContext();
  S2PrefixTree tree = new S2PrefixTree(context, S2PrefixTree.getMaxLevels(arity), arity);
  double precision = random().nextDouble();
  int level = tree.getLevelForDistance(precision);
  Point point = context.getShapeFactory().pointXY(0, 0);
  CellIterator iterator = tree.getTreeCellIterator(point, level);
  S2PrefixTreeCell cell = null;
  while (iterator.hasNext()) {
    cell = (S2PrefixTreeCell)iterator.next();
  }
  assertTrue(cell.getLevel() == level);
  double precisionCell = S2Projections.MAX_WIDTH.getValue(cell.cellId.level());
  assertTrue(precision > precisionCell);
}
 
Example #8
Source File: SpatialDocMaker.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 * 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);

  return makeSpatialStrategy(config, configMap, ctx);
}
 
Example #9
Source File: Geo3dDistanceCalculator.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
public Point pointOnBearing(Point from, double distDEG, double bearingDEG, SpatialContext ctx, Point reuse) {
  Geo3dPointShape geoFrom = (Geo3dPointShape) from;
  GeoPoint point = (GeoPoint) geoFrom.shape;
  double dist = DistanceUtils.DEGREES_TO_RADIANS * distDEG;
  double bearing = DistanceUtils.DEGREES_TO_RADIANS * bearingDEG;
  GeoPoint newPoint = planetModel.surfacePointOnBearing(point, dist, bearing);
  double newLat = newPoint.getLatitude() * DistanceUtils.RADIANS_TO_DEGREES;
  double newLon = newPoint.getLongitude() * DistanceUtils.RADIANS_TO_DEGREES;
  if (reuse != null) {
    reuse.reset(newLon, newLat);
    return reuse;
  }
  else {
    return ctx.getShapeFactory().pointXY(newLon, newLat);
  }
}
 
Example #10
Source File: SpatialArgsTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void calcDistanceFromErrPct() {
  final SpatialContext ctx = usually() ? SpatialContext.GEO : new Geo3dSpatialContextFactory().newSpatialContext();
  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);
}
 
Example #11
Source File: SpatialPrefixTreeFactory.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 * The factory is looked up via "prefixTree" in args, expecting "geohash" or "quad".
 * If it's neither of these, then "geohash" is chosen for a geo context, otherwise "quad" is chosen.
 * The "version" arg, if present, is parsed with {@link Version} and the prefix tree might be sensitive to it.
 */
public static SpatialPrefixTree makeSPT(Map<String,String> args, ClassLoader classLoader, SpatialContext ctx) {
  //TODO refactor to use Java SPI like how Lucene already does for codecs/postingsFormats, etc
  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 if ("packedQuad".equalsIgnoreCase(cname))
    instance = new PackedQuadPrefixTree.Factory();
  else if ("s2".equalsIgnoreCase(cname))
    instance = new S2PrefixTree.Factory();
  else {
    try {
      Class<?> c = classLoader.loadClass(cname);
      instance = (SpatialPrefixTreeFactory) c.getConstructor().newInstance();
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }
  instance.init(args, ctx);
  return instance.newSPT();
}
 
Example #12
Source File: SpatialUtils.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/** Calls {@link #parsePoint(String, org.locationtech.spatial4j.context.SpatialContext)} and wraps
 * the exception with {@link org.apache.solr.common.SolrException} with a helpful message. */
public static Point parsePointSolrException(String externalVal, SpatialContext ctx) throws SolrException {
  try {
    return parsePoint(externalVal, ctx);
  } catch (InvalidShapeException e) {
    String message = e.getMessage();
    if (!message.contains(externalVal))
      message = "Can't parse point '" + externalVal + "' because: " + message;
    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, message, e);
  }
}
 
Example #13
Source File: SpatialDocMaker.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
protected SpatialStrategy makeSpatialStrategy(final Config config, Map<String, String> configMap,
                                              SpatialContext ctx) {
  //TODO once strategies have factories, we could use them here.
  final String strategyName = config.get("spatial.strategy", "rpt");
  switch (strategyName) {
    case "rpt": return makeRPTStrategy(SPATIAL_FIELD, config, configMap, ctx);
    case "composite": return makeCompositeStrategy(config, configMap, ctx);
    //TODO add more as-needed
    default: throw new IllegalStateException("Unknown spatial.strategy: " + strategyName);
  }
}
 
Example #14
Source File: RandomSpatialOpFuzzyPrefixTreeTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private void setupCtx2D(SpatialContext ctx) {
  if (!ctx.isGeo())
    ctx2D = ctx;
  //A non-geo version of ctx.
  SpatialContextFactory ctxFactory = new SpatialContextFactory();
  ctxFactory.geo = false;
  ctxFactory.worldBounds = ctx.getWorldBounds();
  ctx2D = ctxFactory.newSpatialContext();
}
 
Example #15
Source File: ElasticsearchDocument.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public ElasticsearchDocument(String id, String type, String index, String resourceId, String context,
		Function<? super String, ? extends SpatialContext> geoContextMapper) {
	this(id, type, index, Versions.MATCH_ANY, new HashMap<>(), geoContextMapper);
	fields.put(SearchFields.URI_FIELD_NAME, resourceId);
	if (context != null) {
		fields.put(SearchFields.CONTEXT_FIELD_NAME, context);
	}
}
 
Example #16
Source File: LatLonPointSpatialField.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public LatLonPointSpatialStrategy(SpatialContext ctx, String fieldName, boolean indexed, boolean docValues) {
  super(ctx, fieldName);
  if (!ctx.isGeo()) {
    throw new IllegalArgumentException("ctx must be geo=true: " + ctx);
  }
  this.indexed = indexed;
  this.docValues = docValues;
}
 
Example #17
Source File: SpatialUtils.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Calls {@link #parseRectangle(String, org.locationtech.spatial4j.context.SpatialContext)} and wraps the exception with
 * {@link org.apache.solr.common.SolrException} with a helpful message.
 */
public static Rectangle parseRectangeSolrException(String externalVal, SpatialContext ctx) throws SolrException {
  try {
    return parseRectangle(externalVal, ctx);
  } catch (InvalidShapeException e) {
    String message = e.getMessage();
    if (!message.contains(externalVal))
      message = "Can't parse rectangle '" + externalVal + "' because: " + message;
    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, message, e);
  }
}
 
Example #18
Source File: ElasticsearchIndex.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private boolean supportsShapes(String field) {
	SpatialContext geoContext = geoContextMapper.apply(field);
	try {
		geoContext.readShapeFromWkt("POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0))");
		return true;
	} catch (ParseException e) {
		return false;
	}
}
 
Example #19
Source File: ShapeFieldCacheDistanceValueSource.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public ShapeFieldCacheDistanceValueSource(SpatialContext ctx,
    ShapeFieldCacheProvider<Point> provider, Point from, double multiplier) {
  this.ctx = ctx;
  this.from = from;
  this.provider = provider;
  this.multiplier = multiplier;
}
 
Example #20
Source File: SpatialDocMaker.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
protected SpatialStrategy makeCompositeStrategy(Config config, Map<String, String> configMap, SpatialContext ctx) {
  final CompositeSpatialStrategy strategy = new CompositeSpatialStrategy(
      SPATIAL_FIELD, makeRPTStrategy(SPATIAL_FIELD + "_rpt", config, configMap, ctx),
      makeSerializedDVStrategy(SPATIAL_FIELD + "_sdv", config, configMap, ctx)
  );
  strategy.setOptimizePredicates(config.get("query.spatial.composite.optimizePredicates", true));
  return strategy;
}
 
Example #21
Source File: GeohashPrefixTree.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public GeohashPrefixTree(SpatialContext ctx, int maxLevels) {
  super(ctx, maxLevels);
  Rectangle bounds = ctx.getWorldBounds();
  if (bounds.getMinX() != -180)
    throw new IllegalArgumentException("Geohash only supports lat-lon world bounds. Got "+bounds);
  int MAXP = getMaxLevelsPossible();
  if (maxLevels <= 0 || maxLevels > MAXP)
    throw new IllegalArgumentException("maxLevels must be [1-"+MAXP+"] but got "+ maxLevels);
}
 
Example #22
Source File: RandomSpatialOpFuzzyPrefixTreeTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void testShapePair() {
  ctx = SpatialContext.GEO;
  setupCtx2D(ctx);

  Shape leftShape = new ShapePair(ctx.makeRectangle(-74, -56, -8, 1), ctx.makeRectangle(-180, 134, -90, 90), true);
  Shape queryShape = ctx.makeRectangle(-180, 180, -90, 90);
  assertEquals(SpatialRelation.WITHIN, leftShape.relate(queryShape));
}
 
Example #23
Source File: S2PrefixTree.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a S2 spatial tree with provided arity.
 *
 * @param ctx The provided spatial context. The shape factor of the spatial context
 *           must implement {@link S2ShapeFactory}
 * @param maxLevels The provided maximum level for this tree.
 * @param arity The arity of the tree.
 */
public S2PrefixTree(SpatialContext ctx, int maxLevels, int arity) {
    super(ctx, maxLevels);
    if (!(ctx.getShapeFactory() instanceof S2ShapeFactory)) {
        throw new IllegalArgumentException("Spatial context does not support S2 spatial index.");
    }
    this.s2ShapeFactory = (S2ShapeFactory) ctx.getShapeFactory();
    if (arity <1 || arity > 3) {
        throw new IllegalArgumentException("Invalid value for S2 tree arity. Possible values are 1, 2 or 3. Provided value is " + arity  + ".");
    }
    this.arity = arity;
}
 
Example #24
Source File: Geo3dSpatialContextFactory.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public SpatialContext newSpatialContext() {
  if (planetModel == null) {
    planetModel = DEFAULT_PLANET_MODEL;
  }
  if (distCalc == null) {
    this.distCalc = new Geo3dDistanceCalculator(planetModel);
  }
  return new SpatialContext(this);
}
 
Example #25
Source File: TestSolr4Spatial2.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test @Repeat(iterations = 10)
public void testLLPDecodeIsStableAndPrecise() throws Exception {
  // test that LatLonPointSpatialField decode of docValue will round-trip (re-index then re-decode) to the same value
  @SuppressWarnings({"resource", "IOResourceOpenedButNotSafelyClosed"})
  SolrClient client = new EmbeddedSolrServer(h.getCore());// do NOT close it; it will close Solr

  final String fld = "llp_1_dv_dvasst";
  String ptOrig = GeoTestUtil.nextLatitude() + "," + GeoTestUtil.nextLongitude();
  assertU(adoc("id", "0", fld, ptOrig));
  assertU(commit());
  // retrieve it (probably less precision)
  String ptDecoded1 = (String) client.query(params("q", "id:0")).getResults().get(0).get(fld);
  // now write it back
  assertU(adoc("id", "0", fld, ptDecoded1));
  assertU(commit());
  // retrieve it; assert that it's the same as written
  String ptDecoded2 = (String) client.query(params("q", "id:0")).getResults().get(0).get(fld);
  assertEquals("orig:" + ptOrig, ptDecoded1, ptDecoded2);

  // test that the representation is pretty accurate
  final Point ptOrigObj = SpatialUtils.parsePoint(ptOrig, SpatialContext.GEO);
  final Point ptDecodedObj = SpatialUtils.parsePoint(ptDecoded1, SpatialContext.GEO);
  double deltaCentimeters = SpatialContext.GEO.calcDistance(ptOrigObj, ptDecodedObj) * DistanceUtils.DEG_TO_KM * 1000.0 * 100.0;
  //See javadocs of LatLonDocValuesField for these constants
  final Point absErrorPt = SpatialContext.GEO.getShapeFactory().pointXY(8.381903171539307E-8, 4.190951585769653E-8);
  double deltaCentimetersMax
      = SpatialContext.GEO.calcDistance(absErrorPt, 0,0) * DistanceUtils.DEG_TO_KM * 1000.0 * 100.0;
  assertEquals(1.0420371840922256, deltaCentimetersMax, 0.0);// just so that we see it in black & white in the test

  //max found by trial & error.  If we used 8 decimal places then we could get down to 1.04cm accuracy but then we
  // lose the ability to round-trip -- 40 would become 39.99999997  (ugh).
  assertTrue("deltaCm too high: " + deltaCentimeters, deltaCentimeters < 1.41);
  // Pt(x=105.29894270124083,y=-0.4371673760042398) to  Pt(x=105.2989428,y=-0.4371673) is 1.38568
}
 
Example #26
Source File: TestBBoxStrategy.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void testOperations() throws IOException {
  //setup
  if (random().nextInt(4) > 0) {//75% of the time choose geo (more interesting to test)
    this.ctx = SpatialContext.GEO;
  } else {
    SpatialContextFactory factory = new SpatialContextFactory();
    factory.geo = false;
    factory.worldBounds = new RectangleImpl(-300, 300, -100, 100, null);
    this.ctx = factory.newSpatialContext();
  }
  this.strategy = BBoxStrategy.newInstance(ctx, "bbox");
  //test we can disable docValues for predicate tests
  if (random().nextBoolean()) {
    FieldType fieldType = new FieldType(((BBoxStrategy)strategy).getFieldType());
    fieldType.setDocValuesType(DocValuesType.NONE);
    strategy = new BBoxStrategy(ctx, strategy.getFieldName(), fieldType);
  }
  for (SpatialOperation operation : SpatialOperation.values()) {
    if (operation == SpatialOperation.Overlaps)
      continue;//unsupported
    testOperationRandomShapes(operation);

    deleteAll();
    commit();
  }
}
 
Example #27
Source File: AbstractSpatialFieldType.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
protected SpatialArgsParser newSpatialArgsParser() {
  return new SpatialArgsParser() {
    @Override
    protected Shape parseShape(String str, SpatialContext ctx) throws ParseException {
      return AbstractSpatialFieldType.this.parseShape(str);
    }
  };
}
 
Example #28
Source File: SpatialArgsParser.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Parses a string such as "Intersects(ENVELOPE(-10,-8,22,20)) distErrPct=0.025".
 *
 * @param v   The string to parse. Mandatory.
 * @param ctx The spatial context. Mandatory.
 * @return Not null.
 * @throws IllegalArgumentException if the parameters don't make sense or an add-on parameter is unknown
 * @throws ParseException If there is a problem parsing the string
 * @throws InvalidShapeException When the coordinates are invalid for the shape
 */
public SpatialArgs parse(String v, SpatialContext ctx) throws ParseException, InvalidShapeException {
  int idx = v.indexOf('(');
  int edx = v.lastIndexOf(')');

  if (idx < 0 || idx > edx) {
    throw new ParseException("missing parens: " + v, -1);
  }

  SpatialOperation op = SpatialOperation.get(v.substring(0, idx).trim());

  String body = v.substring(idx + 1, edx).trim();
  if (body.length() < 1) {
    throw new ParseException("missing body : " + v, idx + 1);
  }

  Shape shape = parseShape(body, ctx);
  SpatialArgs args = newSpatialArgs(op, shape);

  if (v.length() > (edx + 1)) {
    body = v.substring(edx + 1).trim();
    if (body.length() > 0) {
      Map<String, String> aa = parseMap(body);
      readNameValuePairs(args, aa);
      if (!aa.isEmpty()) {
        throw new IllegalArgumentException("unused parameters: " + aa);
      }
    }
  }
  args.validate();
  return args;
}
 
Example #29
Source File: SpatialTestData.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/** Reads the stream, consuming a format that is a tab-separated values of 3 columns:
 * an "id", a "name" and the "shape".  Empty lines and lines starting with a '#' are skipped.
 * The stream is closed.
 */
public static Iterator<SpatialTestData> getTestData(InputStream in, SpatialContext ctx) throws IOException {
  List<SpatialTestData> results = new ArrayList<>();
  BufferedReader bufInput = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8));
  try {
    String line;
    while ((line = bufInput.readLine()) != null) {
      if (line.length() == 0 || line.charAt(0) == '#')
        continue;

      SpatialTestData data = new SpatialTestData();
      String[] vals = line.split("\t");
      if (vals.length != 3)
        throw new RuntimeException("bad format; expecting 3 tab-separated values for line: "+line);
      data.id = vals[0];
      data.name = vals[1];
      try {
        data.shape = ctx.readShapeFromWkt(vals[2]);
      } catch (ParseException e) {
        throw new RuntimeException(e);
      }
      results.add(data);
    }
  } finally {
    bufInput.close();
  }
  return results.iterator();
}
 
Example #30
Source File: SpatialStrategy.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs the spatial strategy with its mandatory arguments.
 */
public SpatialStrategy(SpatialContext ctx, String fieldName) {
  if (ctx == null)
    throw new IllegalArgumentException("ctx is required");
  this.ctx = ctx;
  if (fieldName == null || fieldName.length() == 0)
    throw new IllegalArgumentException("fieldName is required");
  this.fieldName = fieldName;
}