case MULTIPOLYGON: Coordinate coordinate = ((JtsGeometry) shape).getGeom().getCoordinates()[position]; return new Geoshape.Point(coordinate.y, coordinate.x); case POINT: case CIRCLE: case BOX: if (position == 0) return new Geoshape.Point(shape.getBoundingBox().getMinY(), shape.getBoundingBox().getMinX()); else return new Geoshape.Point(shape.getBoundingBox().getMaxY(), shape.getBoundingBox().getMaxX()); case GEOMETRYCOLLECTION: return ((ShapeCollection<Shape>) shape).getShapes().stream()
final 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 && (predicate == Geo.INTERSECT || map == Mapping.DEFAULT)) { final Geoshape.Point southwest = geo.getPoint(0); final Geoshape.Point northeast = geo.getPoint(1); return (key + ":[" + southwest.getLatitude() + "," + southwest.getLongitude() + " TO " + northeast.getLatitude() + "," + northeast.getLongitude() + "]"); } else if (map == Mapping.PREFIX_TREE) { return key + ":\"" + SPATIAL_PREDICATES.get(predicate) + "(" + geo + ")\" distErrPct=0";
case CIRCLE: final Geoshape.Point center = shape.getPoint(); query = compat.geoDistance(key, center.getLatitude(), center.getLongitude(), shape.getRadius()); break; case BOX: final Geoshape.Point southwest = shape.getPoint(0); final Geoshape.Point northeast = shape.getPoint(1); query = compat.geoBoundingBox(key, southwest.getLatitude(), southwest.getLongitude(), northeast.getLatitude(), northeast.getLongitude()); break; case POLYGON: final List<List<Double>> points = IntStream.range(0, shape.size()) .mapToObj(i -> ImmutableList.of(shape.getPoint(i).getLongitude(), shape.getPoint(i).getLatitude())) .collect(Collectors.toList()); query = compat.geoPolygon(key, points); final Geoshape.Point center = shape.getPoint(); geo = ImmutableMap.of(ES_TYPE_KEY, "circle", ES_GEO_COORDS_KEY, ImmutableList.of(center.getLongitude(), center.getLatitude()), "radius", shape.getRadius() + "km"); break; ES_GEO_COORDS_KEY, ImmutableList.of( ImmutableList.of(southwest.getLongitude(), northeast.getLatitude()), ImmutableList.of(northeast.getLongitude(), southwest.getLatitude()))); break; case LINE:
@SuppressWarnings("unchecked") private static Object convertGeoshape(Geoshape geoshape, Mapping mapping) { if (geoshape.getType() == Geoshape.Type.POINT && Mapping.PREFIX_TREE != mapping) { final Geoshape.Point p = geoshape.getPoint(); return new double[]{p.getLongitude(), p.getLatitude()}; } else if (geoshape.getType() == Geoshape.Type.BOX) { final Rectangle box = geoshape.getShape().getBoundingBox(); final Map<String,Object> map = new HashMap<>(); map.put("type", "envelope"); map.put("coordinates", new double[][] {{box.getMinX(),box.getMaxY()},{box.getMaxX(),box.getMinY()}}); return map; } else if (geoshape.getType() == Geoshape.Type.CIRCLE) { try { final Map<String,Object> map = geoshape.toMap(); map.put("radius", map.get("radius") + ((Map<String, String>) map.remove("properties")).get("radius_units")); return map; } catch (final IOException e) { throw new IllegalArgumentException("Invalid geoshape: " + geoshape, e); } } else { try { return geoshape.toMap(); } catch (final IOException e) { throw new IllegalArgumentException("Invalid geoshape: " + geoshape, e); } } }
private void testGeo(int i, int origNumV, int numV) { final double offset = (i * 50.0 / origNumV); final double bufferKm = 20; final double distance = Geoshape.point(0.0, 0.0).getPoint().distance(Geoshape.point(offset, offset).getPoint()) + bufferKm;
final double 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()); assertCount(5, tx.query().has("boundary", Geo.INTERSECT, Geoshape.circle(0.0, 0.0, distance)).has("text", Text.CONTAINS, words[0]).vertices());
@Override public void serialize(Geoshape value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException { switch(value.getType()) { case POINT: jgen.writeStartObject(); jgen.writeFieldName(FIELD_TYPE); jgen.writeString(Type.POINT.toString()); jgen.writeFieldName(FIELD_COORDINATES); jgen.writeStartArray(); jgen.writeNumber(value.getPoint().getLongitude()); jgen.writeNumber(value.getPoint().getLatitude()); jgen.writeEndArray(); jgen.writeEndObject(); break; default: jgen.writeRawValue(toGeoJson(value)); break; } }
/** * Returns the singleton point of this shape. Only applicable for point and circle shapes. * * @return */ public Point getPoint() { Preconditions.checkArgument(getType()==Type.POINT || getType()==Type.CIRCLE,"Shape does not have a single point"); return new Point(shape.getCenter().getY(), shape.getCenter().getX()); }
/** * Returns the distance to another point in kilometers * * @param other Point * @return */ public double distance(Point other) { return DistanceUtils.degrees2Dist(HELPER.getContext().getDistCalc().distance(getSpatial4jPoint(),other.getSpatial4jPoint()),DistanceUtils.EARTH_MEAN_RADIUS_KM); }