@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");
protected JtsGeometry jtsGeometry(Geometry geom) { //dateline180Check is false because ElasticSearch does it's own dateline wrapping JtsGeometry jtsGeometry = new JtsGeometry(geom, SPATIAL_CONTEXT, false, MULTI_POLYGON_MAY_OVERLAP); if (AUTO_VALIDATE_JTS_GEOMETRY) jtsGeometry.validate(); if (AUTO_INDEX_JTS_GEOMETRY) jtsGeometry.index(); return jtsGeometry; }
/** * INTERNAL * @see #makeShape(org.locationtech.jts.geom.Geometry) * * @param geom Non-null * @param dateline180Check if both this is true and {@link SpatialContext#isGeo()}, then JtsGeometry will check * for adjacent coordinates greater than 180 degrees longitude apart, and * it will do tricks to make that line segment (and the shape as a whole) * cross the dateline even though JTS doesn't have geodetic support. * @param allowMultiOverlap See {@link #isAllowMultiOverlap()}. */ public JtsGeometry makeShape(Geometry geom, boolean dateline180Check, boolean allowMultiOverlap) { JtsGeometry jtsGeom = new JtsGeometry(geom, (JtsSpatialContext) ctx, dateline180Check, allowMultiOverlap); if (isAutoIndex()) { jtsGeom.index(); } return jtsGeom; }
geom = narrowCollectionIfPossible((GeometryCollection)geom); if (geom == null) { throw new IllegalArgumentException("JtsGeometry does not support GeometryCollection but does support its subclasses."); geom = unwrapDateline(geom);//returns same or new geom geom = unionGeometryCollection(geom);//returns same or new geom geom = cutUnwrappedGeomInto360(geom);//returns same or new geom assert geom.getEnvelopeInternal().getWidth() <= 360; assert ! geom.getClass().equals(GeometryCollection.class) : "GeometryCollection unsupported";//double check bbox = computeGeoBBox(geom); } else {//not geo geom = unionGeometryCollection(geom);//returns same or new geom assert assertValidate();//kinda expensive but caches valid state
/** called via assertion */ private boolean assertValidate() { String assertValidate = System.getProperty(SYSPROP_ASSERT_VALIDATE); if (assertValidate == null || Boolean.parseBoolean(assertValidate)) validate(); return true; }
jtsGeom = makeShape(geom); if (getValidationRule() != ValidationRule.none) jtsGeom.validate(); } catch (RuntimeException e) {
protected JtsGeometry jtsGeometry(Geometry geom) { //dateline180Check is false because ElasticSearch does it's own dateline wrapping JtsGeometry jtsGeometry = new JtsGeometry(geom, SPATIAL_CONTEXT, false, MULTI_POLYGON_MAY_OVERLAP); if (AUTO_VALIDATE_JTS_GEOMETRY) jtsGeometry.validate(); if (AUTO_INDEX_JTS_GEOMETRY) jtsGeometry.index(); return jtsGeometry; }
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)); } }
protected JtsGeometry jtsGeometry(Geometry geom) { //dateline180Check is false because ElasticSearch does it's own dateline wrapping JtsGeometry jtsGeometry = new JtsGeometry(geom, SPATIAL_CONTEXT, false, multiPolygonMayOverlap); if (autoValidateJtsGeometry) { jtsGeometry.validate(); } if (autoIndexJtsGeometry) { jtsGeometry.index(); } return jtsGeometry; }
case MULTILINESTRING: case MULTIPOLYGON: Coordinate coordinate = ((JtsGeometry) shape).getGeom().getCoordinates()[position]; return new Geoshape.Point(coordinate.y, coordinate.x); case POINT:
protected JtsGeometry jtsGeometry(Geometry geom) { //dateline180Check is false because ElasticSearch does it's own dateline wrapping JtsGeometry jtsGeometry = new JtsGeometry(geom, SPATIAL_CONTEXT, false, MULTI_POLYGON_MAY_OVERLAP); if (AUTO_VALIDATE_JTS_GEOMETRY) jtsGeometry.validate(); if (AUTO_INDEX_JTS_GEOMETRY) jtsGeometry.index(); return jtsGeometry; }
@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); } }
protected JtsGeometry jtsGeometry(Geometry geom) { //dateline180Check is false because ElasticSearch does it's own dateline wrapping JtsGeometry jtsGeometry = new JtsGeometry(geom, SPATIAL_CONTEXT, false, MULTI_POLYGON_MAY_OVERLAP); if (AUTO_VALIDATE_JTS_GEOMETRY) jtsGeometry.validate(); if (AUTO_INDEX_JTS_GEOMETRY) jtsGeometry.index(); return jtsGeometry; }
@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");