Search in sources :

Example 6 with InvalidShapeException

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);
    }
}
Also used : InvalidShapeException(org.locationtech.spatial4j.exception.InvalidShapeException) Point(org.locationtech.spatial4j.shape.Point) SolrException(org.apache.solr.common.SolrException) InvalidShapeException(org.locationtech.spatial4j.exception.InvalidShapeException) ParseException(java.text.ParseException)

Aggregations

InvalidShapeException (org.locationtech.spatial4j.exception.InvalidShapeException)6 Point (org.locationtech.spatial4j.shape.Point)3 ParseException (java.text.ParseException)2 SolrException (org.apache.solr.common.SolrException)2 CoordinatesBuilder (org.elasticsearch.common.geo.builders.CoordinatesBuilder)2 LineStringBuilder (org.elasticsearch.common.geo.builders.LineStringBuilder)2 PolygonBuilder (org.elasticsearch.common.geo.builders.PolygonBuilder)2 ConvexHull (com.vividsolutions.jts.algorithm.ConvexHull)1 Coordinate (com.vividsolutions.jts.geom.Coordinate)1 Geometry (com.vividsolutions.jts.geom.Geometry)1 ElasticsearchException (org.elasticsearch.ElasticsearchException)1 CoordinateCollection (org.elasticsearch.common.geo.builders.CoordinateCollection)1 MultiLineStringBuilder (org.elasticsearch.common.geo.builders.MultiLineStringBuilder)1 MultiPointBuilder (org.elasticsearch.common.geo.builders.MultiPointBuilder)1 MultiPolygonBuilder (org.elasticsearch.common.geo.builders.MultiPolygonBuilder)1 PointBuilder (org.elasticsearch.common.geo.builders.PointBuilder)1 ShapeBuilder (org.elasticsearch.common.geo.builders.ShapeBuilder)1