/** * Constructs a Geoshape from a spatial4j {@link Shape}. * @param shape * @return */ public static Geoshape geoshape(Shape shape) { return new Geoshape(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)); } } }
@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 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()); }
/** * 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)); }
/** * Constructs a point from its latitude and longitude information * @param latitude * @param longitude * @return */ public static Geoshape point(final double latitude, final double longitude) { Preconditions.checkArgument(isValidCoordinate(latitude, longitude), "Invalid coordinate provided"); return new Geoshape(getShapeFactory().pointXY(longitude, latitude)); }
public Geoshape geoshape(org.locationtech.jts.geom.Geometry geometry) { return new Geoshape(context.getShapeFactory().makeShapeFromGeometry(geometry)); }
/** * Constructs a circle from a given center point and a radius in kilometer * @param latitude * @param longitude * @param radiusInKM * @return */ public static Geoshape circle(final double latitude, final double longitude, final double radiusInKM) { Preconditions.checkArgument(isValidCoordinate(latitude, longitude), "Invalid coordinate provided"); Preconditions.checkArgument(radiusInKM > 0, "Invalid radius provided [%s]", radiusInKM); return new Geoshape(getShapeFactory().circle(longitude, latitude, DistanceUtils.dist2Degrees(radiusInKM, DistanceUtils.EARTH_MEAN_RADIUS_KM))); }
/** * Constructs a new box shape which is identified by its south-west and north-east corner points * @param southWestLatitude * @param southWestLongitude * @param northEastLatitude * @param northEastLongitude * @return */ public static Geoshape box(final double southWestLatitude, final double southWestLongitude, final double northEastLatitude, final double northEastLongitude) { Preconditions.checkArgument(isValidCoordinate(southWestLatitude, southWestLongitude), "Invalid south-west coordinate provided"); Preconditions.checkArgument(isValidCoordinate(northEastLatitude, northEastLongitude), "Invalid north-east coordinate provided"); return new Geoshape(getShapeFactory().rect(southWestLongitude, northEastLongitude, southWestLatitude, northEastLatitude)); }
/** * Constructs a line from list of coordinates * @param coordinates Coordinate (lon,lat) pairs * @return */ public static Geoshape line(List<double[]> coordinates) { Preconditions.checkArgument(coordinates.size() >= 2, "Too few coordinate pairs provided"); final LineStringBuilder builder = getShapeFactory().lineString(); for (double[] coordinate : coordinates) { Preconditions.checkArgument(isValidCoordinate(coordinate[1], coordinate[0]), "Invalid coordinate provided"); builder.pointXY(coordinate[0], coordinate[1]); } return new Geoshape(builder.build()); }
@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); } } } }
return new Geoshape(HELPER.getGeojsonReader().read(new StringReader(json)));
.skip(position) .findFirst() .map(entry -> getPoint(new Geoshape(entry.getKey()), entry.getValue())) .orElse(null); default: