IntersectionFeatureCollection implements VectorProcess { @DescribeResult(description = "Output feature collection") public SimpleFeatureCollection execute( @DescribeParameter(name = "first feature collection", description = "First feature collection") SimpleFeatureCollection firstFeatures, @DescribeParameter(name = "second feature collection", description = "Second feature collection") SimpleFeatureCollection secondFeatures, @DescribeParameter(name = "first attributes to retain", collectionType = String.class, min = 0, description = "First feature collection attribute to include") List<String> firstAttributes, @DescribeParameter(name = "second attributes to retain", collectionType = String.class, min = 0, description = "Second feature collection attribute to include") List<String> sndAttributes, @DescribeParameter(name = "intersectionMode", min = 0, description = "Specifies geometry computed for intersecting features. INTERSECTION (default) computes the spatial intersection of the inputs. FIRST copies geometry A. SECOND copies geometry B.", defaultValue="INTERSECTION") IntersectionMode intersectionMode, @DescribeParameter(name = "percentagesEnabled", min = 0, description = "Indicates whether to output feature area percentages (attributes percentageA and percentageB)") Boolean percentagesEnabled, @DescribeParameter(name = "areasEnabled", min = 0, description = "Indicates whether to output feature areas (attributes areaA and areaB)") Boolean areasEnabled) { logger.fine("INTERSECTION FEATURE COLLECTION WPS STARTED"); if (percentagesEnabled == null) { percentagesEnabled = false; } if (areasEnabled == null) { areasEnabled = false; } if (intersectionMode == null) { intersectionMode = IntersectionMode.INTERSECTION; } Class firstGeomType = firstFeatures.getSchema().getGeometryDescriptor().getType() .getBinding(); Class secondGeomType = secondFeatures.getSchema().getGeometryDescriptor().getType() .getBinding(); if ((percentagesEnabled || areasEnabled) && (!isGeometryTypeIn(firstGeomType, MultiPolygon.class, Polygon.class) || !isGeometryTypeIn( secondGeomType, MultiPolygon.class, Polygon.class))) { throw new IllegalArgumentException( "In case of opMode or areaMode are true, the features in the first and second collection must be polygonal"); } if (!isGeometryTypeIn(firstGeomType, MultiPolygon.class, Polygon.class, MultiLineString.class, LineString.class)) { throw new IllegalArgumentException( "First feature collection must be polygonal or linear"); } return new IntersectedFeatureCollection(firstFeatures, firstAttributes, secondFeatures, sndAttributes, intersectionMode, percentagesEnabled, areasEnabled); }  @DescribeResult(description = "Output feature collection") SimpleFeatureCollection execute(
            @DescribeParameter(name = "first feature collection", description = "First feature collection") SimpleFeatureCollection firstFeatures,
            @DescribeParameter(name = "second feature collection", description = "Second feature collection") SimpleFeatureCollection secondFeatures,
            @DescribeParameter(name = "first attributes to retain", collectionType = String.class, min = 0, description = "First feature collection attribute to include") List<String> firstAttributes,
            @DescribeParameter(name = "second attributes to retain", collectionType = String.class, min = 0, description = "Second feature collection attribute to include") List<String> sndAttributes,
            @DescribeParameter(name = "intersectionMode", min = 0, description = "Specifies geometry computed for intersecting features.  INTERSECTION (default) computes the spatial intersection of the inputs. FIRST copies geometry A.  SECOND copies geometry B.", defaultValue="INTERSECTION") IntersectionMode intersectionMode,
            @DescribeParameter(name = "percentagesEnabled", min = 0, description = "Indicates whether to output feature area percentages (attributes percentageA and percentageB)") Boolean percentagesEnabled,
            @DescribeParameter(name = "areasEnabled", min = 0, description = "Indicates whether to output feature areas (attributes areaA and areaB)") Boolean areasEnabled);  }
@Test(expected=IllegalArgumentException.class) public void testExceptionOpModeFstCollection() throws Exception { logger.info("Running testExceptionOpModeFstCollection ..."); ArrayList<String> toRemoveFst = new ArrayList<String>(); toRemoveFst.add("cat2"); ArrayList<String> toRemoveSnd = new ArrayList<String>(); toRemoveSnd.add("cat"); SimpleFeatureCollection output = process.execute(polylineCollection, featuresCollection,toRemoveFst, toRemoveSnd, IntersectionMode.INTERSECTION, true, false); }
@Test(expected=IllegalArgumentException.class) public void testExceptionOpModeSndCollection() throws Exception { logger.info("Running testExceptionOpModeSndCollection ..."); ArrayList<String> toRetainFst = new ArrayList<String>(); toRetainFst.add("cat2"); ArrayList<String> toRetainSnd = new ArrayList<String>(); toRetainSnd.add("cat"); SimpleFeatureCollection output = process.execute(zonesCollection, polylineCollection,toRetainFst,toRetainSnd, IntersectionMode.INTERSECTION, true, false); }
@Test(expected = IllegalArgumentException.class) public void testProcessArguments1() throws IllegalArgumentException{ SimpleFeatureCollection output2 = process.execute(multipointCollection, featuresCollection,null,null, IntersectionMode.INTERSECTION, null, null); }
@Test(expected = IllegalArgumentException.class) public void testProcessArguments2() throws IllegalArgumentException{ SimpleFeatureCollection output2 = process.execute(featuresCollection, multipointCollection,null,null, IntersectionMode.INTERSECTION, true, false); }
@Test public void testReturnedAttributes() throws Exception { logger.info("Running testReturnedAttributes ..."); ArrayList<String> toRetainFst = new ArrayList<String>(); toRetainFst.add("str1"); ArrayList<String> toRetainSnd = new ArrayList<String>(); toRetainSnd.add("str2"); SimpleFeatureCollection output2 = process.execute(zonesCollection, featuresCollection,toRetainFst,toRetainSnd, IntersectionMode.INTERSECTION, true, true); System.out.println("count "+output2.getSchema().getAttributeCount()); assertNotNull(output2.getSchema().getDescriptor("the_geom")); assertNotNull(output2.getSchema().getDescriptor("zones_str1")); assertNotNull(output2.getSchema().getDescriptor("features_str2")); assertNotNull(output2.getSchema().getDescriptor("percentageA")); assertNotNull(output2.getSchema().getDescriptor("percentageB")); assertNotNull(output2.getSchema().getDescriptor("areaA")); assertNotNull(output2.getSchema().getDescriptor("areaB")); assertNotNull(output2.getSchema().getDescriptor("INTERSECTION_ID")); assertTrue(output2.getSchema().getAttributeCount()==8); SimpleFeatureIterator sfTemp2 = output2.features(); sfTemp2.hasNext(); SimpleFeature sf = sfTemp2.next(); assertNotNull(sf.getAttribute("the_geom")); assertNotNull(sf.getAttribute("zones_str1")); assertNotNull(sf.getAttribute("features_str2")); assertNotNull(sf.getAttribute("percentageA")); assertNotNull(sf.getAttribute("percentageB")); assertNotNull(sf.getAttribute("areaA")); assertNotNull(sf.getAttribute("areaB")); assertTrue(sf.getAttributeCount()==8); SimpleFeatureCollection output3 = process.execute(zonesCollection, featuresCollection,toRetainFst,toRetainSnd, IntersectionMode.INTERSECTION, false, false); SimpleFeatureIterator sfTemp3 = output3.features(); sfTemp3.hasNext(); SimpleFeature sf2 = sfTemp3.next(); assertNotNull(sf2.getAttribute("the_geom")); assertNotNull(sf2.getAttribute("zones_str1")); assertNotNull(sf2.getAttribute("features_str2")); assertTrue(sf2.getAttributeCount()==4); }
@Test public void testExecute() throws Exception { SimpleFeatureTypeBuilder tb = new SimpleFeatureTypeBuilder(); tb.setName("featureType"); tb.add("geometry", Polygon.class); tb.add("integer", Integer.class); GeometryFactory gf = new GeometryFactory(); SimpleFeatureBuilder b = new SimpleFeatureBuilder(tb.buildFeatureType()); DefaultFeatureCollection features = new DefaultFeatureCollection(null, b.getFeatureType()); DefaultFeatureCollection secondFeatures = new DefaultFeatureCollection(null, b .getFeatureType()); Polygon[] firstArrayGeometry = new Polygon[1]; Polygon[] secondArrayGeometry = new Polygon[1]; for (int numFeatures = 0; numFeatures < 1; numFeatures++) { Coordinate array[] = new Coordinate[5]; array[0] = new Coordinate(0, 0); array[1] = new Coordinate(1, 0); array[2] = new Coordinate(1, 1); array[3] = new Coordinate(0, 1); array[4] = new Coordinate(0, 0); LinearRing shell = new LinearRing(array, new PrecisionModel(), 0); b.add(gf.createPolygon(shell, null)); b.add(0); firstArrayGeometry[0] = gf.createPolygon(shell, null); features.add(b.buildFeature(numFeatures + "")); } for (int numFeatures = 0; numFeatures < 1; numFeatures++) { Coordinate array[] = new Coordinate[5]; Coordinate centre = ((Polygon) features.features().next().getDefaultGeometry()) .getCentroid().getCoordinate(); array[0] = new Coordinate(centre.x, centre.y); array[1] = new Coordinate(centre.x + 1, centre.y); array[2] = new Coordinate(centre.x + 1, centre.y + 1); array[3] = new Coordinate(centre.x, centre.y + 1); array[4] = new Coordinate(centre.x, centre.y); LinearRing shell = new LinearRing(array, new PrecisionModel(), 0); b.add(gf.createPolygon(shell, null)); b.add(0); secondArrayGeometry[0] = gf.createPolygon(shell, null); secondFeatures.add(b.buildFeature(numFeatures + "")); } SimpleFeatureCollection output3 = process.execute(features,secondFeatures, null, null, null, false, false); assertTrue(output3.size()==1); SimpleFeatureIterator iterator = output3.features(); GeometryCollection firstCollection = null; GeometryCollection secondCollection = null; firstCollection = new GeometryCollection(firstArrayGeometry, new GeometryFactory()); secondCollection = new GeometryCollection(secondArrayGeometry, new GeometryFactory()); for (int i = 0; i < firstCollection.getNumGeometries() && iterator.hasNext(); i++) { Geometry expected = (Geometry) firstCollection.getGeometryN(i).intersection( secondCollection.getGeometryN(i)); SimpleFeature sf = iterator.next(); assertTrue(expected.equals((Geometry) sf.getDefaultGeometry())); } }