use of org.locationtech.spatial4j.exception.InvalidShapeException in project lucene-solr by apache.
the class SpatialUtils method parsePoint.
/** Parses either "lat, lon" (spaces optional on either comma side) or "x y" style formats. Spaces can be basically
* anywhere. And not any whitespace, just the space char.
*
* @param str Non-null; may have leading or trailing spaces
* @param ctx Non-null
* @return Non-null
* @throws InvalidShapeException If for any reason there was a problem parsing the string or creating the point.
*/
public static Point parsePoint(String str, SpatialContext ctx) throws InvalidShapeException {
//note we don't do generic whitespace, just a literal space char detection
try {
double x, y;
//TODO use findIndexNotSpace instead?
str = str.trim();
int commaIdx = str.indexOf(',');
if (commaIdx == -1) {
// "x y" format
int spaceIdx = str.indexOf(' ');
if (spaceIdx == -1)
throw new InvalidShapeException("Point must be in 'lat, lon' or 'x y' format: " + str);
int middleEndIdx = findIndexNotSpace(str, spaceIdx + 1, +1);
x = Double.parseDouble(str.substring(0, spaceIdx));
y = Double.parseDouble(str.substring(middleEndIdx));
} else {
// "lat, lon" format
int middleStartIdx = findIndexNotSpace(str, commaIdx - 1, -1);
int middleEndIdx = findIndexNotSpace(str, commaIdx + 1, +1);
y = Double.parseDouble(str.substring(0, middleStartIdx + 1));
x = Double.parseDouble(str.substring(middleEndIdx));
}
//by default norm* methods do nothing but perhaps it's been customized
x = ctx.normX(x);
y = ctx.normY(y);
//will verify x & y fit in boundary
return ctx.makePoint(x, y);
} catch (InvalidShapeException e) {
throw e;
} catch (Exception e) {
throw new InvalidShapeException(e.toString(), e);
}
}
Aggregations