public static SpatialContext getSpatialContext() { return HELPER.getContext(); }
/** * Returns the point at the given position. The position must be smaller than {@link #size()}. * * @param position * @return */ public Point getPoint(int position) { return HELPER.getPoint(this, position); }
/** * Returns the {@link Type} of this geoshape. * * @return */ public Type getType() { return HELPER.getType(shape); }
public Geoshape.Point getPoint(Geoshape geoshape, int position) { Shape shape = geoshape.getShape(); if (position < 0 || position >= size(shape)) throw new ArrayIndexOutOfBoundsException("Invalid position: " + position); switch(getType(shape)) { case LINE: case POLYGON: case GEOMETRYCOLLECTION: return ((ShapeCollection<Shape>) shape).getShapes().stream() .flatMap(internShape -> IntStream.range(0, size(internShape)) .mapToObj(i -> new AbstractMap.SimpleImmutableEntry<>(internShape, i))) .skip(position) .findFirst() .map(entry -> getPoint(new Geoshape(entry.getKey()), entry.getValue())) .orElse(null); default: throw new IllegalStateException("getPoint(int) not supported for type: " + getType(shape));
public int size(Shape shape) { switch(getType(shape)) { case LINE: case POLYGON: case MULTIPOINT: case MULTILINESTRING: case MULTIPOLYGON: return ((JtsGeometry) shape).getGeom().getCoordinates().length; case POINT: return 1; case CIRCLE: return 1; case BOX: return 2; case GEOMETRYCOLLECTION: return ((ShapeCollection<?>) shape).getShapes().stream().map(s -> (Shape) s).mapToInt(s -> size(s)).sum(); default: throw new IllegalStateException("size() not supported for type: " + getType(shape)); } }
@Override public Geoshape createObject(Map<String, Object> data) { final Geoshape shape; if (data.containsKey(FIELD_COORDINATES) && data.get(FIELD_COORDINATES) instanceof List) { final List<Number> coordinates = (List<Number>) data.get(FIELD_COORDINATES); if (coordinates.size() < 2) throw new RuntimeException("Expecting two coordinates when reading point"); shape = Geoshape.point(coordinates.get(1).doubleValue(), coordinates.get(0).doubleValue()); } else { try { final String json = mapWriter.writeValueAsString(data.get("geometry")); shape = new Geoshape(HELPER.getGeojsonReader().read(new StringReader(json))); } catch (IOException | ParseException e) { throw new RuntimeException("I/O exception reading geoshape", e); } } return shape; } }
public static String toGeoJson(Geoshape geoshape) { return HELPER.getGeojsonWriter().toString(geoshape.shape); }
/** * Constructs a polygon from list of coordinates * @param coordinates Coordinate (lon,lat) pairs * @return */ public static Geoshape polygon(List<double[]> coordinates) { return HELPER.polygon(coordinates); }
/** * Returns the WKT representation of the shape. * @return */ @Override public String toString() { return HELPER.getWktWriter().toString(shape); }
/** * Deserialize a geoshape. * @param inputStream * @return * @throws IOException */ public static Geoshape read(InputStream inputStream) throws IOException { try (DataInputStream dataInput = new DataInputStream(inputStream)) { return new Geoshape(HELPER.readShape(dataInput)); } } }
/** * Create Geoshape from WKT representation. * @param wkt * @return * @throws ParseException */ public static Geoshape fromWkt(String wkt) throws ParseException { return new Geoshape(HELPER.getWktReader().parse(wkt)); }
public void write(DataOutputStream dataOutput, Geoshape attribute) throws IOException { if (isJts(attribute.getShape())) { dataOutput.writeByte(0); // note geometries written with writeJtsGeom cannot be deserialized with readShape so to maintain backwards // compatibility with previously written geometries must maintain read/writeJtsGeom for the JtsGeometry case binaryCodec.writeJtsGeom(dataOutput, attribute.getShape()); } else { dataOutput.writeByte(1); binaryCodec.writeShape(dataOutput, attribute.getShape()); } }
@Override public Geoshape deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException { jsonParser.nextToken(); if (jsonParser.getCurrentName().equals(FIELD_COORDINATES)) { double[] f = jsonParser.readValueAs(double[].class); jsonParser.nextToken(); return Geoshape.point(f[1], f[0]); } else { try { HashMap map = jsonParser.readValueAs(LinkedHashMap.class); jsonParser.nextToken(); String json = mapWriter.writeValueAsString(map); return new Geoshape(HELPER.getGeojsonReader().read(new StringReader(json))); } catch (ParseException e) { throw new IOException("Unable to read and parse geojson", e); } } } }
public static String toGeoJson(Geoshape geoshape) { return HELPER.getGeojsonWriter().toString(geoshape.shape); }
public Geoshape polygon(List<double[]> coordinates) { Preconditions.checkArgument(coordinates.size() >= 4, "Too few coordinate pairs provided"); Preconditions.checkArgument(Arrays.equals(coordinates.get(0), coordinates.get(coordinates.size()-1)), "Polygon is not closed"); final PolygonBuilder builder = this.getContext().getShapeFactory().polygon(); for (double[] coordinate : coordinates) { Preconditions.checkArgument(coordinate.length==2 && Geoshape.isValidCoordinate(coordinate[1], coordinate[0]), "Invalid coordinate provided"); builder.pointXY(coordinate[0], coordinate[1]); } return new Geoshape(builder.build()); }
return new Geoshape(HELPER.getGeojsonReader().read(new StringReader(json)));
/** * 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); }