use of org.apache.sis.internal.feature.GeometryWrapper in project sis by apache.
the class Factory method parseWKB.
/**
* Reads the given Well Known Binary (WKB).
* This implementation does not change the buffer position.
*
* @param data the sequence of bytes to parse.
* @return the geometry object for the given WKB.
* @throws ParseException if the WKB can not be parsed.
*/
@Override
public GeometryWrapper<Geometry> parseWKB(final ByteBuffer data) throws ParseException {
byte[] array;
if (data.hasArray()) {
/*
* Try to use the underlying array without copy if possible.
* Copy only if the position or length does not match.
*/
array = data.array();
int lower = data.arrayOffset();
int upper = data.limit() + lower;
lower += data.position();
if (lower != 0 || upper != array.length) {
array = Arrays.copyOfRange(array, lower, upper);
}
} else {
array = new byte[data.remaining()];
data.get(array);
}
// WKBReader(GeometryFactory) constructor is cheap.
return new Wrapper(new WKBReader(factory).read(array));
}
use of org.apache.sis.internal.feature.GeometryWrapper in project sis by apache.
the class Factory method parseWKT.
/**
* Parses the given Well Known Text (WKT).
*
* @param wkt the Well Known Text to parse.
* @return the geometry object for the given WKT.
* @throws ParseException if the WKT can not be parsed.
*/
@Override
public GeometryWrapper<Geometry> parseWKT(final String wkt) throws ParseException {
// WKTReader(GeometryFactory) constructor is cheap.
final WKTReader reader = new WKTReader(factory);
reader.setIsOldJtsCoordinateSyntaxAllowed(false);
return new Wrapper(reader.read(wkt));
}
use of org.apache.sis.internal.feature.GeometryWrapper in project sis by apache.
the class Factory method createMultiPolygon.
/**
* Creates a multi-polygon from an array of JTS {@link Polygon} or {@link LinearRing}.
* If some geometries are actually linear rings, they will be converted to polygons.
*
* @param geometries the polygons or linear rings to put in a multi-polygons.
* @throws ClassCastException if an element in the array is not a JTS geometry.
* @throws IllegalArgumentException if an element is a non-closed linear string.
*/
@Override
public GeometryWrapper<Geometry> createMultiPolygon(final Object[] geometries) {
final Polygon[] polygons = new Polygon[geometries.length];
boolean isFloat = true;
for (int i = 0; i < geometries.length; i++) {
final Object polyline = unwrap(geometries[i]);
final Polygon polygon;
if (polyline instanceof Polygon) {
polygon = (Polygon) polyline;
} else {
final boolean fs;
final CoordinateSequence cs;
if (polyline instanceof LinearRing) {
final LinearRing ring = (LinearRing) polyline;
cs = ring.getCoordinateSequence();
fs = isFloat(cs);
polygon = factory(fs).createPolygon(ring);
} else if (polyline instanceof LineString) {
// Let JTS throws an exception with its own error message if the ring is not valid.
cs = ((LineString) polyline).getCoordinateSequence();
fs = isFloat(cs);
polygon = factory(fs).createPolygon(cs);
} else {
throw new ClassCastException(Errors.format(Errors.Keys.IllegalArgumentClass_3, Strings.bracket("geometries", i), Polygon.class, Classes.getClass(polyline)));
}
JTS.copyMetadata((Geometry) polyline, polygon);
isFloat &= fs;
}
polygons[i] = polygon;
}
return new Wrapper(factory(isFloat).createMultiPolygon(polygons));
}
use of org.apache.sis.internal.feature.GeometryWrapper in project sis by apache.
the class Wrapper method operationSameCRS.
/**
* Applies a SQLMM operation on this geometry.
*
* @param operation the SQLMM operation to apply.
* @param other the other geometry, or {@code null} if the operation requires only one geometry.
* @param argument an operation-specific argument, or {@code null} if not applicable.
* @return result of the specified operation.
* @throws ClassCastException if the operation can only be executed on some specific argument types
* (for example geometries that are polylines) and one of the argument is not of that type.
*/
@Override
protected Object operationSameCRS(final SQLMM operation, final GeometryWrapper<Geometry> other, final Object argument) {
/*
* For all operation producing a geometry, the result is collected for post-processing.
* For all other kinds of value, the result is returned directly in the switch statement.
*/
final Geometry result;
switch(operation) {
case ST_IsMeasured:
return Boolean.FALSE;
case ST_Dimension:
return geometry.getDimension();
case ST_SRID:
return geometry.getSRID();
case ST_IsEmpty:
return geometry.isEmpty();
case ST_IsSimple:
return geometry.isSimple();
case ST_IsValid:
return geometry.isValid();
case ST_Envelope:
return getEnvelope();
case ST_Boundary:
result = geometry.getBoundary();
break;
case ST_ConvexHull:
result = geometry.convexHull();
break;
case ST_Buffer:
result = geometry.buffer(((Number) argument).doubleValue());
break;
case ST_Intersection:
result = geometry.intersection(((Wrapper) other).geometry);
break;
case ST_Union:
result = geometry.union(((Wrapper) other).geometry);
break;
case ST_Difference:
result = geometry.difference(((Wrapper) other).geometry);
break;
case ST_SymDifference:
result = geometry.symDifference(((Wrapper) other).geometry);
break;
case ST_Distance:
return geometry.distance(((Wrapper) other).geometry);
case ST_Equals:
return geometry.equalsTopo(((Wrapper) other).geometry);
case ST_Relate:
return geometry.relate(((Wrapper) other).geometry, argument.toString());
case ST_Disjoint:
return geometry.disjoint(((Wrapper) other).geometry);
case ST_Intersects:
return geometry.intersects(((Wrapper) other).geometry);
case ST_Touches:
return geometry.touches(((Wrapper) other).geometry);
case ST_Crosses:
return geometry.crosses(((Wrapper) other).geometry);
case ST_Within:
return geometry.within(((Wrapper) other).geometry);
case ST_Contains:
return geometry.contains(((Wrapper) other).geometry);
case ST_Overlaps:
return geometry.overlaps(((Wrapper) other).geometry);
// WKTWriter() constructor is cheap.
case ST_AsText:
return new WKTWriter().write(geometry);
case ST_AsBinary:
return FilteringContext.writeWKB(geometry);
case ST_X:
return ((Point) geometry).getX();
case ST_Y:
return ((Point) geometry).getY();
case ST_Z:
return ((Point) geometry).getCoordinate().getZ();
// JTS does not have curves.
case ST_ToLineString:
return geometry;
case ST_NumGeometries:
return geometry.getNumGeometries();
case ST_NumPoints:
return geometry.getNumPoints();
case ST_PointN:
result = ((LineString) geometry).getPointN(toIndex(argument));
break;
case ST_StartPoint:
result = ((LineString) geometry).getStartPoint();
break;
case ST_EndPoint:
result = ((LineString) geometry).getEndPoint();
break;
case ST_IsClosed:
return ((LineString) geometry).isClosed();
case ST_IsRing:
return ((LineString) geometry).isRing();
// Fallthrough: length is the perimeter for polygons.
case ST_Perimeter:
case ST_Length:
return geometry.getLength();
case ST_Area:
return geometry.getArea();
case ST_Centroid:
result = geometry.getCentroid();
break;
case ST_PointOnSurface:
result = geometry.getInteriorPoint();
break;
case ST_ExteriorRing:
result = ((Polygon) geometry).getExteriorRing();
break;
case ST_InteriorRingN:
result = ((Polygon) geometry).getInteriorRingN(toIndex(argument));
break;
case ST_NumInteriorRings:
return ((Polygon) geometry).getNumInteriorRing();
case ST_GeometryN:
result = geometry.getGeometryN(toIndex(argument));
break;
case ST_ToPoint:
case ST_ToPolygon:
case ST_ToMultiPoint:
case ST_ToMultiLine:
case ST_ToMultiPolygon:
case ST_ToGeomColl:
{
final GeometryType target = operation.getGeometryType().get();
final Class<?> type = factory().getGeometryClass(target);
if (type.isInstance(geometry)) {
return geometry;
}
result = convert(target);
break;
}
case ST_Is3D:
{
final Coordinate c = geometry.getCoordinate();
return (c != null) ? !Double.isNaN(c.z) : null;
}
case ST_CoordDim:
{
final Coordinate c = geometry.getCoordinate();
return (c != null) ? Double.isNaN(c.z) ? 2 : 3 : null;
}
case ST_GeometryType:
{
for (int i = 0; i < TYPES.length; i++) {
if (TYPES[i].isInstance(geometry)) {
return SQLMM_NAMES[i];
}
}
return null;
}
case ST_ExplicitPoint:
{
final Coordinate c = ((Point) geometry).getCoordinate();
if (c == null)
return ArraysExt.EMPTY_DOUBLE;
final double x = c.getX();
final double y = c.getY();
final double z = c.getZ();
return Double.isNaN(z) ? new double[] { x, y } : new double[] { x, y, z };
}
case ST_Simplify:
{
final double distance = ((Number) argument).doubleValue();
result = DouglasPeuckerSimplifier.simplify(geometry, distance);
break;
}
case ST_SimplifyPreserveTopology:
{
final double distance = ((Number) argument).doubleValue();
result = TopologyPreservingSimplifier.simplify(geometry, distance);
break;
}
default:
return super.operationSameCRS(operation, other, argument);
}
JTS.copyMetadata(geometry, result);
return result;
}
Aggregations