ClipProcess implements VectorProcess { @DescribeResult(name = "result", description = "Clipped feature collection") public SimpleFeatureCollection execute( @DescribeParameter(name = "features", description = "Input feature collection") SimpleFeatureCollection features, @DescribeParameter(name = "clip", description = "Geometry to use for clipping (in same CRS as input features)") Geometry clip, @DescribeParameter(name = "preserveZ", min=0, description = "Attempt to preserve Z values from the original geometry (interpolate value for new points)") Boolean preserveZ) throws ProcessException { Envelope box = clip.getEnvelopeInternal(); String srs = null; if(features.getSchema().getCoordinateReferenceSystem() != null) { srs = CRS.toSRS(features.getSchema().getCoordinateReferenceSystem()); } BBOX bboxFilter = ff.bbox("", box.getMinX(), box.getMinY(), box.getMaxX(), box.getMaxY(), srs); if(preserveZ == null) { preserveZ = false; } return new ClippingFeatureCollection(features.subCollection(bboxFilter), clip, preserveZ); }  @DescribeResult(name = "result", description = "Clipped feature collection") SimpleFeatureCollection execute(
            @DescribeParameter(name = "features", description = "Input feature collection") SimpleFeatureCollection features,
            @DescribeParameter(name = "clip", description = "Geometry to use for clipping (in same CRS as input features)") Geometry clip,
            @DescribeParameter(name = "preserveZ", min=0, description = "Attempt to preserve Z values from the original geometry (interpolate value for new points)") Boolean preserveZ);  }
@Test public void testClipPoly3DIncluded() throws Exception { SimpleFeatureCollection features = fsMeters.getFeatures(); ClipProcess cp = new ClipProcess(); SimpleFeatureCollection result = cp.execute(features, new WKTReader().read("POLYGON((-10 -10, -10 10010, 10010 10010, 10010 -10, -10 -10))"), true); assertEquals(2, result.size()); assertSquaresMetersIdentical(result); }
@Test public void testClipPoly3DOnBorder() throws Exception { SimpleFeatureCollection features = fsMeters.getFeatures(); ClipProcess cp = new ClipProcess(); SimpleFeatureCollection result = cp.execute(features, new WKTReader().read("POLYGON((0 0, 0 10000, 10000 10000, 10000 0, 0 0))"), true); assertEquals(2, result.size()); assertSquaresMetersIdentical(result); }
@Test public void testClipPoly3DNewVertices() throws Exception { SimpleFeatureCollection features = fsMeters.getFeatures(); ClipProcess cp = new ClipProcess(); SimpleFeatureCollection result = cp.execute(features, new WKTReader().read("POLYGON((0 5000, 0 10000, 10000 10000, 10000 5000, 0 5000))"), true); assertEquals(1, result.size()); SimpleFeatureIterator fi = result.features(); SimpleFeature f = fi.next(); MultiPolygon mp = (MultiPolygon) f.getDefaultGeometry(); assertEquals(1, mp.getNumGeometries()); Polygon p = (Polygon) mp.getGeometryN(0); assertEquals(0, p.getNumInteriorRing()); LineString shell = p.getExteriorRing(); CoordinateSequence cs = shell.getCoordinateSequence(); assertEquals(5, cs.size()); assertOrdinates(0, 5000, 1, cs, 0); assertOrdinates(0, 10000, 2, cs, 1); assertOrdinates(10000, 10000, 2, cs, 2); assertOrdinates(10000, 5000, 1, cs, 3); assertOrdinates(0, 5000, 1, cs, 4); assertFalse(fi.hasNext()); fi.close(); }
@Test public void testClipPoly3DFullyInside() throws Exception { SimpleFeatureCollection features = fsMeters.getFeatures(); ClipProcess cp = new ClipProcess(); SimpleFeatureCollection result = cp.execute(features, new WKTReader().read("POLYGON((2500 2500, 2500 7500, 7500 7500, 7500 2500, 2500 2500))"), true); assertEquals(2, result.size()); SimpleFeatureIterator fi = result.features(); SimpleFeature f = fi.next(); MultiPolygon mp = (MultiPolygon) f.getDefaultGeometry(); assertEquals(1, mp.getNumGeometries()); Polygon p = (Polygon) mp.getGeometryN(0); assertEquals(0, p.getNumInteriorRing()); LineString shell = p.getExteriorRing(); CoordinateSequence cs = shell.getCoordinateSequence(); assertEquals(5, cs.size()); assertOrdinates(2500, 2500, 0.411765, cs, 0); assertOrdinates(2500, 7500, 1.588235, cs, 1); assertOrdinates(7500, 7500, 1.588235, cs, 2); assertOrdinates(7500, 2500, 0.411765, cs, 3); assertOrdinates(2500, 2500, 0.411765, cs, 4); fi.close(); }
@Test public void testClipLine3DIncluded() throws Exception { SimpleFeatureCollection features = fsLines.getFeatures(); ClipProcess cp = new ClipProcess(); SimpleFeatureCollection result = cp.execute(features, new WKTReader().read("POLYGON((-10 -10, -10 10010, 10010 10010, 10010 -10, -10 -10))"), true); assertEquals(1, result.size()); SimpleFeatureIterator fi = result.features(); SimpleFeature f = fi.next(); MultiLineString ml = (MultiLineString) f.getDefaultGeometry(); assertEquals(1, ml.getNumGeometries()); LineString ls = (LineString) ml.getGeometryN(0); CoordinateSequence cs = ls.getCoordinateSequence(); assertEquals(3, cs.size()); assertOrdinates(0, 0, 0, cs, 0); assertOrdinates(10000, 0, 1, cs, 1); assertOrdinates(10000, 10000, 2, cs, 2); fi.close(); }
@Test public void testClipLine3DMidFirstSegment() throws Exception { SimpleFeatureCollection features = fsLines.getFeatures(); ClipProcess cp = new ClipProcess(); SimpleFeatureCollection result = cp.execute(features, new WKTReader().read("POLYGON((-10 -10, -10 10, 5000 10, 5000 -10, -10 -10))"), true); assertEquals(1, result.size()); SimpleFeatureIterator fi = result.features(); SimpleFeature f = fi.next(); MultiLineString ml = (MultiLineString) f.getDefaultGeometry(); assertEquals(1, ml.getNumGeometries()); LineString ls = (LineString) ml.getGeometryN(0); CoordinateSequence cs = ls.getCoordinateSequence(); assertEquals(2, cs.size()); assertOrdinates(0, 0, 0, cs, 0); assertOrdinates(5000, 0, 0.5, cs, 1); fi.close(); }
@Test public void testService() throws Exception{ VectorProcessFactory factory = new VectorProcessFactory(); Set<Name> names = factory.getNames(); assertFalse(names.isEmpty()); assertTrue(names.contains(new NameImpl("vec", "Clip"))); SimpleFeatureCollection features = fsLines.getFeatures(); Map<String, Object> arguments= new HashMap<String, Object>(); arguments.put("features", features); arguments.put("clip", new WKTReader().read("POLYGON((-10 -10, -10 10, 5000 10, 5000 -10, -10 -10))")); Map<String, Object> output = factory.create(new NameImpl("vec", "Clip")).execute(arguments, null); SimpleFeatureCollection result=(SimpleFeatureCollection) output.get("result"); assertEquals(1, result.size()); }
@Test public void testClipCollinearLine3DMidFirstSegment() throws Exception { SimpleFeatureCollection features = fsCollinear.getFeatures(); ClipProcess cp = new ClipProcess(); SimpleFeatureCollection result = cp.execute(features, new WKTReader().read("POLYGON((-10 -10, -10 10, 5000 10, 5000 -10, -10 -10))"), true); assertEquals(1, result.size()); SimpleFeatureIterator fi = result.features(); SimpleFeature f = fi.next(); MultiLineString ml = (MultiLineString) f.getDefaultGeometry(); assertEquals(1, ml.getNumGeometries()); LineString ls = (LineString) ml.getGeometryN(0); CoordinateSequence cs = ls.getCoordinateSequence(); assertEquals(4, cs.size()); assertOrdinates(0, 0, 0, cs, 0); assertOrdinates(1000, 0, 0.1, cs, 1); assertOrdinates(4000, 0, 0.4, cs, 2); assertOrdinates(5000, 0, 0.5, cs, 3); fi.close(); }
@Test public void testClipLine3DMidSecondSegment() throws Exception { SimpleFeatureCollection features = fsLines.getFeatures(); ClipProcess cp = new ClipProcess(); SimpleFeatureCollection result = cp.execute(features, new WKTReader().read("POLYGON((9000 5000, 9000 10000, 11000 10000, 11000 5000, 9000 5000))"), true); assertEquals(1, result.size()); SimpleFeatureIterator fi = result.features(); SimpleFeature f = fi.next(); MultiLineString ml = (MultiLineString) f.getDefaultGeometry(); assertEquals(1, ml.getNumGeometries()); LineString ls = (LineString) ml.getGeometryN(0); CoordinateSequence cs = ls.getCoordinateSequence(); assertEquals(2, cs.size()); assertOrdinates(10000, 5000, 1.5, cs, 0); assertOrdinates(10000, 10000, 2, cs, 1); fi.close(); }
@Test public void testClipExtractBend() throws Exception { SimpleFeatureCollection features = fsLines.getFeatures(); ClipProcess cp = new ClipProcess(); SimpleFeatureCollection result = cp.execute(features, new WKTReader().read("POLYGON((5000 -10, 5000 5000, 11000 5000, 11000 -10, 5000 -10))"), true); assertEquals(1, result.size()); SimpleFeatureIterator fi = result.features(); SimpleFeature f = fi.next(); MultiLineString ml = (MultiLineString) f.getDefaultGeometry(); assertEquals(1, ml.getNumGeometries()); LineString ls = (LineString) ml.getGeometryN(0); CoordinateSequence cs = ls.getCoordinateSequence(); assertEquals(3, cs.size()); assertOrdinates(5000, 0, 0.5, cs, 0); assertOrdinates(10000, 0, 1, cs, 1); assertOrdinates(10000, 5000, 1.5, cs, 2); fi.close(); }
@Test public void testClipExtractSeparateBitsLowLine() throws Exception { SimpleFeatureCollection features = fsLines.getFeatures(); ClipProcess cp = new ClipProcess(); SimpleFeatureCollection result = cp.execute(features, new WKTReader().read("POLYGON((1000 -10, 1000 10, 9000 10, 9000 -10, 8000 -10, 8000 5, 2000 5, 2000 -10, 1000 -10))"), true); assertEquals(1, result.size()); SimpleFeatureIterator fi = result.features(); SimpleFeature f = fi.next(); fi.close(); MultiLineString ml = (MultiLineString) f.getDefaultGeometry(); assertEquals(2, ml.getNumGeometries()); LineString ls = (LineString) ml.getGeometryN(0); CoordinateSequence cs = ls.getCoordinateSequence(); assertEquals(2, cs.size()); assertOrdinates(1000, 0, 0.1, cs, 0); assertOrdinates(2000, 0, 0.2, cs, 1); ls = (LineString) ml.getGeometryN(1); cs = ls.getCoordinateSequence(); assertEquals(2, cs.size()); assertOrdinates(8000, 0, 0.8, cs, 0); assertOrdinates(9000, 0, 0.9, cs, 1); }
@Test public void testClipExtractSeparateBitsBothLines() throws Exception { SimpleFeatureCollection features = fsLines.getFeatures(); ClipProcess cp = new ClipProcess(); SimpleFeatureCollection result = cp.execute(features, new WKTReader().read("POLYGON((1000 -10, 1000 5000, 11000 5000, 11000 4000, 2000 4000, 2000 -10, 1000 -10))"), true); assertEquals(1, result.size()); SimpleFeatureIterator fi = result.features(); SimpleFeature f = fi.next(); fi.close(); MultiLineString ml = (MultiLineString) f.getDefaultGeometry(); assertEquals(2, ml.getNumGeometries()); LineString ls = (LineString) ml.getGeometryN(0); CoordinateSequence cs = ls.getCoordinateSequence(); assertEquals(2, cs.size()); assertOrdinates(1000, 0, 0.1, cs, 0); assertOrdinates(2000, 0, 0.2, cs, 1); ls = (LineString) ml.getGeometryN(1); cs = ls.getCoordinateSequence(); assertEquals(2, cs.size()); assertOrdinates(10000, 4000, 1.4, cs, 0); assertOrdinates(10000, 5000, 1.5, cs, 1); }
@Test public void testClipMultiline() throws Exception { SimpleFeatureCollection features = fsMultilines.getFeatures(); ClipProcess cp = new ClipProcess(); SimpleFeatureCollection result = cp.execute(features, new WKTReader().read("POLYGON((900 900, 900 2100, 2100 2100, 2100 900, 900 900))"), true); assertEquals(1, result.size()); SimpleFeatureIterator fi = result.features(); SimpleFeature f = fi.next(); fi.close(); MultiLineString ml = (MultiLineString) f.getDefaultGeometry(); assertEquals(2, ml.getNumGeometries()); LineString ls = (LineString) ml.getGeometryN(0); CoordinateSequence cs = ls.getCoordinateSequence(); assertEquals(4, cs.size()); assertOrdinates(1000, 900, 0.9, cs, 0); assertOrdinates(1000, 1000, 1.0, cs, 1); assertOrdinates(2000, 1000, 2.0, cs, 2); assertOrdinates(2000, 900, 2.1, cs, 3); ls = (LineString) ml.getGeometryN(1); cs = ls.getCoordinateSequence(); assertEquals(4, cs.size()); assertOrdinates(1000, 2100, 0.9, cs, 0); assertOrdinates(1000, 2000, 1.0, cs, 1); assertOrdinates(2000, 2000, 2.0, cs, 2); assertOrdinates(2000, 2100, 2.1, cs, 3); }
@Test public void testEmptyLines() throws Exception { SimpleFeatureCollection features = fsLines.getFeatures(); ClipProcess cp = new ClipProcess(); SimpleFeatureCollection result = cp.execute(features, new WKTReader().read("POLYGON((-8 -7, -8 3, 2 3, -8 -7))"), true); assertEquals(0, result.size()); SimpleFeatureIterator fi = result.features(); assertFalse(fi.hasNext()); fi.close(); }
@Test public void testEmptyMultiLines() throws Exception { SimpleFeatureCollection features = fsMultilines.getFeatures(); ClipProcess cp = new ClipProcess(); SimpleFeatureCollection result = cp.execute(features, new WKTReader().read("POLYGON((-10 -10, -10 -5, -5 -5, -5 -10, -10 -10))"), true); assertEquals(0, result.size()); SimpleFeatureIterator fi = result.features(); assertFalse(fi.hasNext()); fi.close(); }