private List<Geoshape.Point> getPolygonPoints(Geoshape polygon) { List<Geoshape.Point> locations = new ArrayList<Geoshape.Point>(); int index = 0; boolean hasCoordinates = true; while (hasCoordinates) { try { locations.add(polygon.getPoint(index)); } catch (ArrayIndexOutOfBoundsException ignore) { //just means we asked for a point past the size of the list //of known coordinates hasCoordinates = false; } } return locations; }
/** * Returns the singleton point of this shape. Only applicable for point and circle shapes. * * @return */ public Point getPoint() { Preconditions.checkArgument(size()==1,"Shape does not have a single point"); return getPoint(0); }
/** * Converts this shape into its equivalent Spatial4j {@link Shape}. * @return */ public Shape convert2Spatial4j() { switch(getType()) { case POINT: return getPoint().getSpatial4jPoint(); case CIRCLE: return CTX.makeCircle(getPoint(0).getSpatial4jPoint(), DistanceUtils.dist2Degrees(getRadius(), DistanceUtils.EARTH_MEAN_RADIUS_KM)); case BOX: return CTX.makeRectangle(getPoint(0).getSpatial4jPoint(),getPoint(1).getSpatial4jPoint()); case POLYGON: throw new UnsupportedOperationException("Not yet supported"); default: throw new IllegalStateException("Unrecognized type: " + getType()); } }
@Override public String toString() { Type type = getType(); StringBuilder s = new StringBuilder(); s.append(type.toString().toLowerCase()); switch (type) { case POINT: s.append(getPoint().toString()); break; case CIRCLE: s.append(getPoint().toString()).append(":").append(getRadius()); break; default: s.append("["); for (int i=0;i<size();i++) { if (i>0) s.append(","); s.append(getPoint(i)); } s.append("]"); } return s.toString(); }
private static Object convertToEsType(Object value) { if (value instanceof Number) { if (AttributeUtil.isWholeNumber((Number) value)) { return ((Number) value).longValue(); } else { //double or float return ((Number) value).doubleValue(); } } else if (AttributeUtil.isString(value)) { return value; } else if (value instanceof Geoshape) { Geoshape shape = (Geoshape) value; if (shape.getType() == Geoshape.Type.POINT) { Geoshape.Point p = shape.getPoint(); return new double[]{p.getLongitude(), p.getLatitude()}; } else throw new UnsupportedOperationException("Geo type is not supported: " + shape.getType()); } else if (value instanceof Date || value instanceof Instant) { return value; } else if (value instanceof Boolean) { return value; } else if (value instanceof UUID) { return value.toString(); } else throw new IllegalArgumentException("Unsupported type: " + value.getClass() + " (value: " + value + ")"); }
/** * {@link com.thinkaurelius.titan.core.attribute.Geoshape} stores Points in the String format: point[X.0,Y.0]. * Solr needs it to be in Well-Known Text format: POINT(X.0 Y.0) */ public static String convertToWktString(Geoshape fieldValue) throws BackendException { if (fieldValue.getType() == Geoshape.Type.POINT) { Geoshape.Point point = fieldValue.getPoint(); return "POINT(" + point.getLongitude() + " " + point.getLatitude() + ")"; } else { throw new PermanentBackendException("Cannot index " + fieldValue.getType()); } } }
Geoshape geo = (Geoshape)value; if (geo.getType() == Geoshape.Type.CIRCLE) { Geoshape.Point center = geo.getPoint(); return ("{!geofilt sfield=" + key + " pt=" + center.getLatitude() + "," + center.getLongitude() + " d=" + geo.getRadius() + "} distErrPct=0"); //distance in kilometers } else if (geo.getType() == Geoshape.Type.BOX) { Geoshape.Point southwest = geo.getPoint(0); Geoshape.Point northeast = geo.getPoint(1); return (key + ":[" + southwest.getLatitude() + "," + southwest.getLongitude() + " TO " + northeast.getLatitude() + "," + northeast.getLongitude() + "]");
Geoshape shape = (Geoshape) value; if (shape.getType() == Geoshape.Type.CIRCLE) { Geoshape.Point center = shape.getPoint(); return FilterBuilders.geoDistanceFilter(key).lat(center.getLatitude()).lon(center.getLongitude()).distance(shape.getRadius(), DistanceUnit.KILOMETERS); } else if (shape.getType() == Geoshape.Type.BOX) { Geoshape.Point southwest = shape.getPoint(0); Geoshape.Point northeast = shape.getPoint(1); return FilterBuilders.geoBoundingBoxFilter(key).bottomRight(southwest.getLatitude(), northeast.getLongitude()).topLeft(northeast.getLatitude(), southwest.getLongitude()); } else
distance = Geoshape.point(0.0, 0.0).getPoint().distance(Geoshape.point(offset, offset).getPoint()) + 20; assertCount(i + 1, tx.query().has("location", Geo.WITHIN, Geoshape.circle(0.0, 0.0, distance)).vertices()); assertCount(i + 1, tx.query().has("location", Geo.WITHIN, Geoshape.circle(0.0, 0.0, distance)).edges()); distance = Geoshape.point(0.0, 0.0).getPoint().distance(Geoshape.point(offset, offset).getPoint()) + 20; assertCount(5, tx.query().has("location", Geo.INTERSECT, Geoshape.circle(0.0, 0.0, distance)).has("text", Text.CONTAINS, words[0]).vertices()); distance = Geoshape.point(0.0, 0.0).getPoint().distance(Geoshape.point(offset, offset).getPoint()) + 20; assertCount(i + 1, tx.query().has("location", Geo.WITHIN, Geoshape.circle(0.0, 0.0, distance)).vertices()); assertCount(i + 1, tx.query().has("location", Geo.WITHIN, Geoshape.circle(0.0, 0.0, distance)).edges()); distance = Geoshape.point(0.0, 0.0).getPoint().distance(Geoshape.point(offset, offset).getPoint()) + 20; assertCount(5, tx.query().has("location", Geo.INTERSECT, Geoshape.circle(0.0, 0.0, distance)).has("text", Text.CONTAINS, words[0]).vertices()); distance = Geoshape.point(0.0, 0.0).getPoint().distance(Geoshape.point(offset, offset).getPoint()) + 20; assertCount(i + 1, tx.query().has("location", Geo.WITHIN, Geoshape.circle(0.0, 0.0, distance)).vertices()); assertCount(i + 1, tx.query().has("location", Geo.WITHIN, Geoshape.circle(0.0, 0.0, distance)).edges()); distance = Geoshape.point(0.0, 0.0).getPoint().distance(Geoshape.point(offset, offset).getPoint()) + 20; assertCount(5, tx.query().has("location", Geo.INTERSECT, Geoshape.circle(0.0, 0.0, distance)).has("text", Text.CONTAINS, words[0]).vertices());
private List<Geoshape.Point> getPolygonPoints(Geoshape polygon) { List<Geoshape.Point> locations = new ArrayList<Geoshape.Point>(); int index = 0; boolean hasCoordinates = true; while (hasCoordinates) { try { locations.add(polygon.getPoint(index)); } catch (ArrayIndexOutOfBoundsException ignore) { //just means we asked for a point past the size of the list //of known coordinates hasCoordinates = false; } } return locations; }
private List<Geoshape.Point> getPolygonPoints(Geoshape polygon) { List<Geoshape.Point> locations = new ArrayList<>(); int index = 0; boolean hasCoordinates = true; while (hasCoordinates) { try { locations.add(polygon.getPoint(index)); } catch (ArrayIndexOutOfBoundsException ignore) { //just means we asked for a point past the size of the list //of known coordinates hasCoordinates = false; } } return locations; }
/** * Returns the singleton point of this shape. Only applicable for point and circle shapes. * * @return */ public Point getPoint() { Preconditions.checkArgument(size()==1,"Shape does not have a single point"); return getPoint(0); }
/** * Returns the singleton point of this shape. Only applicable for point and circle shapes. * * @return */ public Point getPoint() { Preconditions.checkArgument(size()==1,"Shape does not have a single point"); return getPoint(0); }
/** * Converts this shape into its equivalent Spatial4j {@link Shape}. * @return */ public Shape convert2Spatial4j() { switch(getType()) { case POINT: return getPoint().getSpatial4jPoint(); case CIRCLE: return CTX.makeCircle(getPoint(0).getSpatial4jPoint(), DistanceUtils.dist2Degrees(getRadius(), DistanceUtils.EARTH_MEAN_RADIUS_KM)); case BOX: return CTX.makeRectangle(getPoint(0).getSpatial4jPoint(),getPoint(1).getSpatial4jPoint()); case POLYGON: throw new UnsupportedOperationException("Not yet supported"); default: throw new IllegalStateException("Unrecognized type: " + getType()); } }
/** * Converts this shape into its equivalent Spatial4j {@link Shape}. * @return */ public Shape convert2Spatial4j() { switch(getType()) { case POINT: return getPoint().getSpatial4jPoint(); case CIRCLE: return CTX.makeCircle(getPoint(0).getSpatial4jPoint(), DistanceUtils.dist2Degrees(getRadius(), DistanceUtils.EARTH_MEAN_RADIUS_KM)); case BOX: return CTX.makeRectangle(getPoint(0).getSpatial4jPoint(),getPoint(1).getSpatial4jPoint()); case POLYGON: throw new UnsupportedOperationException("Not yet supported"); default: throw new IllegalStateException("Unrecognized type: " + getType()); } }
@Override public String toString() { Type type = getType(); StringBuilder s = new StringBuilder(); s.append(type.toString().toLowerCase()); switch (type) { case POINT: s.append(getPoint().toString()); break; case CIRCLE: s.append(getPoint().toString()).append(":").append(getRadius()); break; default: s.append("["); for (int i=0;i<size();i++) { if (i>0) s.append(","); s.append(getPoint(i)); } s.append("]"); } return s.toString(); }
@Override public String toString() { Type type = getType(); StringBuilder s = new StringBuilder(); s.append(type.toString().toLowerCase()); switch (type) { case POINT: s.append(getPoint().toString()); break; case CIRCLE: s.append(getPoint().toString()).append(":").append(getRadius()); break; default: s.append("["); for (int i=0;i<size();i++) { if (i>0) s.append(","); s.append(getPoint(i)); } s.append("]"); } return s.toString(); }
/** * {@link com.thinkaurelius.titan.core.attribute.Geoshape} stores Points in the String format: point[X.0,Y.0]. * Solr needs it to be in Well-Known Text format: POINT(X.0 Y.0) */ static String convertToWktString(Geoshape fieldValue) throws BackendException { if (fieldValue.getType() == Geoshape.Type.POINT) { Geoshape.Point point = fieldValue.getPoint(); return "POINT(" + point.getLongitude() + " " + point.getLatitude() + ")"; } else { throw new PermanentBackendException("Cannot index " + fieldValue.getType()); } } }
/** * {@link com.thinkaurelius.titan.core.attribute.Geoshape} stores Points in the String format: point[X.0,Y.0]. * Solr needs it to be in Well-Known Text format: POINT(X.0 Y.0) */ static String convertToWktString(Geoshape fieldValue) throws BackendException { if (fieldValue.getType() == Geoshape.Type.POINT) { Geoshape.Point point = fieldValue.getPoint(); return "POINT(" + point.getLongitude() + " " + point.getLatitude() + ")"; } else { throw new PermanentBackendException("Cannot index " + fieldValue.getType()); } } }
Geoshape shape = (Geoshape) add.value; if (shape.getType() == Geoshape.Type.POINT) { Geoshape.Point p = shape.getPoint(); builder.field(add.key, new double[]{p.getLongitude(), p.getLatitude()}); } else throw new UnsupportedOperationException("Geo type is not supported: " + shape.getType());