Java Code Examples for org.locationtech.spatial4j.context.SpatialContext#readShapeFromWkt()

The following examples show how to use org.locationtech.spatial4j.context.SpatialContext#readShapeFromWkt() . 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: SpatialUtils.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 * Parses a 'geom' parameter (might also be used to parse shapes for indexing). {@code geomStr} can either be WKT or
 * a rectangle-range syntax (see {@link #parseRectangle(String, org.locationtech.spatial4j.context.SpatialContext)}.
 */
public static Shape parseGeomSolrException(String geomStr, SpatialContext ctx) {
  if (geomStr.length() == 0) {
    throw new IllegalArgumentException("0-length geometry string");
  }
  char c = geomStr.charAt(0);
  if (c == '[' || c == '{') {
    return parseRectangeSolrException(geomStr, ctx);
  }
  //TODO parse a raw point?
  try {
    return ctx.readShapeFromWkt(geomStr);
  } catch (ParseException e) {
    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
        "Expecting WKT or '[minPoint TO maxPoint]': " + e, e);
  }

}
 
Example 2
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 3
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 4
Source File: SpatialArgsParser.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
protected Shape parseShape(String str, SpatialContext ctx) throws ParseException {
  //return ctx.readShape(str);//still in Spatial4j 0.4 but will be deleted
  return ctx.readShapeFromWkt(str);
}