@SuppressWarnings("unchecked") public Geoshape.Type getType(Shape shape) { final Geoshape.Type type; if (JtsGeometry.class.isAssignableFrom(shape.getClass()) && "LineString".equals(((JtsGeometry) shape).getGeom().getGeometryType())) { type = Geoshape.Type.LINE; } else if (JtsGeometry.class.isAssignableFrom(shape.getClass())) { try { type = Geoshape.Type.fromGson((((JtsGeometry) shape).getGeom().getGeometryType())); } catch (IllegalArgumentException e) { throw new IllegalStateException("Unrecognized shape type");
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)); } }
case MULTILINESTRING: case MULTIPOLYGON: Coordinate coordinate = ((JtsGeometry) shape).getGeom().getCoordinates()[position]; return new Geoshape.Point(coordinate.y, coordinate.x); case POINT:
@Override public void write(Encoder enc, Shape shape) throws IOException { if (shape == null) { throw new NullPointerException("Shape can not be null"); } if (shape instanceof JtsGeometry) { write(enc, ((JtsGeometry) shape).getGeom()); return; } super.write(enc, shape); } }
@Override public void write(Writer output, Shape shape) throws IOException { if (shape == null) { throw new NullPointerException("Shape can not be null"); } if (shape instanceof JtsGeometry) { write(output, ((JtsGeometry) shape).getGeom()); return; } super.write(output, shape); } }
@Override public String toString(Shape shape) { if (shape instanceof JtsGeometry) { return ((JtsGeometry) shape).getGeom().toText(); } return super.toString(shape); } }
@Override public void serialize(Shape shape, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException { // Use the specialized class if (shape instanceof JtsGeometry) { forJTS.serialize(((JtsGeometry) shape).getGeom(), gen, serializers); return; } write(gen, shape); } }
@Override public Shape build() { Class<?> last = null; List<Geometry> geoms = new ArrayList<>(shapes.size()); for(Shape s : shapes) { if (last != null && last != s.getClass()) { return super.build(); } if (s instanceof JtsGeometry) { geoms.add(((JtsGeometry)s).getGeom()); } else if (s instanceof JtsPoint) { geoms.add(((JtsPoint)s).getGeom()); } else { return super.build(); } last = s.getClass(); } return makeShapeFromGeometry(geometryFactory.buildGeometry(geoms)); } }
private boolean isLineString(Shape shape) { if (shape instanceof JtsGeometry) { Geometry geom = ((JtsGeometry) shape).getGeom(); return geom instanceof LineString; } return false; }
protected boolean isPolygon(Shape shape) { if (shape instanceof JtsGeometry) { Geometry geom = ((JtsGeometry) shape).getGeom(); return geom instanceof Polygon; } if (shape instanceof Rectangle) { return true; } return false; } }
} else if (shape instanceof Point == false) { throw new MapperParsingException("[{" + fieldType().name() + "}] is configured for points only but a " + ((shape instanceof JtsGeometry) ? ((JtsGeometry)shape).getGeom().getGeometryType() : shape.getClass()) + " was found");
return ((JtsGeometry)shape).getGeom();
protected JtsGeometry createMultiLine(ShapeCollection<JtsGeometry> geometries) { LineString[] multiLineString = new LineString[geometries.size()]; int i = 0; for (JtsGeometry geometry : geometries) { multiLineString[i] = (LineString) geometry.getGeom(); i++; } MultiLineString multiPoints = GEOMETRY_FACTORY.createMultiLineString(multiLineString); return SPATIAL_CONTEXT.makeShape(multiPoints); }
@Override public ODocument toDoc(Shape shape) { // TODO REFACTOR ODocument doc = null; if (Point.class.isAssignableFrom(shape.getClass())) { doc = factories.get(OPointShapeBuilder.NAME).toDoc(shape); } else if (Rectangle.class.isAssignableFrom(shape.getClass())) { doc = factories.get(ORectangleShapeBuilder.NAME).toDoc(shape); } else if (JtsGeometry.class.isAssignableFrom(shape.getClass())) { JtsGeometry geometry = (JtsGeometry) shape; Geometry geom = geometry.getGeom(); doc = factories.get("O" + geom.getClass().getSimpleName()).toDoc(shape); } else if (ShapeCollection.class.isAssignableFrom(shape.getClass())) { ShapeCollection collection = (ShapeCollection) shape; if (isMultiPolygon(collection)) { doc = factories.get("OMultiPolygon").toDoc(createMultiPolygon(collection)); } else if (isMultiPoint(collection)) { doc = factories.get("OMultiPoint").toDoc(createMultiPoint(collection)); } else if (isMultiLine(collection)) { doc = factories.get("OMultiLineString").toDoc(createMultiLine(collection)); } else { doc = factories.get("OGeometryCollection").toDoc(shape); } } return doc; }
@Override public ODocument toDoc(JtsGeometry shape) { ODocument doc = new ODocument(getName()); MultiPolygon multiPolygon = (MultiPolygon) shape.getGeom(); List<List<List<List<Double>>>> polyCoordinates = new ArrayList<List<List<List<Double>>>>(); int n = multiPolygon.getNumGeometries(); for (int i = 0; i < n; i++) { Geometry geom = multiPolygon.getGeometryN(i); polyCoordinates.add(coordinatesFromPolygon((Polygon) geom)); } doc.field(COORDINATES, polyCoordinates); return doc; }
@Override public ODocument toDoc(JtsGeometry shape) { final MultiLineString geom = (MultiLineString) shape.getGeom(); List<List<List<Double>>> coordinates = new ArrayList<List<List<Double>>>(); ODocument doc = new ODocument(getName()); for (int i = 0; i < geom.getNumGeometries(); i++) { final LineString lineString = (LineString) geom.getGeometryN(i); coordinates.add(coordinatesFromLineString(lineString)); } doc.field(COORDINATES, coordinates); return doc; } }
@Override public ODocument toDoc(JtsGeometry shape) { ODocument doc = new ODocument(getName()); Polygon polygon = (Polygon) shape.getGeom(); List<List<List<Double>>> polyCoordinates = coordinatesFromPolygon(polygon); doc.field(COORDINATES, polyCoordinates); return doc; }
@Override public ODocument toDoc(JtsGeometry shape) { ODocument doc = new ODocument(getName()); LineString lineString = (LineString) shape.getGeom(); doc.field(COORDINATES, coordinatesFromLineString(lineString)); return doc; } }
@Override public ODocument toDoc(final JtsGeometry shape) { final MultiPoint geom = (MultiPoint) shape.getGeom(); ODocument doc = new ODocument(getName()); doc.field(COORDINATES, new ArrayList<List<Double>>() { { Coordinate[] coordinates = geom.getCoordinates(); for (Coordinate coordinate : coordinates) { add(Arrays.asList(coordinate.x, coordinate.y)); } } }); return doc; } }
protected JtsGeometry createMultiPolygon(ShapeCollection<Shape> geometries) { Polygon[] polygons = new Polygon[geometries.size()]; int i = 0; for (Shape geometry : geometries) { if (geometry instanceof JtsGeometry) { polygons[i] = (Polygon) ((JtsGeometry) geometry).getGeom(); } else { Rectangle rectangle = (Rectangle) geometry; Geometry geometryFrom = SPATIAL_CONTEXT.getGeometryFrom(rectangle); polygons[i] = (Polygon) geometryFrom; } i++; } MultiPolygon multiPolygon = GEOMETRY_FACTORY.createMultiPolygon(polygons); return SPATIAL_CONTEXT.makeShape(multiPolygon); }