I am trying to create my own WPS process in Java about overlaying two polygon shapefiles, but I can't figure out how to do it. Here is a code about reading Shapefile and creating buffer around point.
public String overlay(String pointString, double distance) {
String names = "Object:Area of overlay";
try {
//read SHP
ShapefileDataStore sfdt = new ShapefileDataStore(new URL("file:///_ING/geoserver-2.8.0/data_dir/data/sf/restricted.shp"));
SimpleFeatureSource sfs = sfdt.getFeatureSource("restricted");
//double distance 1000.0d;
GeometryFactory gf = new GeometryFactory ();
String xy [] = pointString.split(" ");
Point point = gf.createPoint(new Coordinate(Double.parseDouble(xy[0]), Double.parseDouble(xy[1])));
Polygon p1 = (Polygon) point.buffer(distance);
SimpleFeatureIterator sfi = sfs.getFeatures().features();
while (sfi.hasNext()) {
SimpleFeature sf = sfi.next();
MultiPolygon mp2 = (MultiPolygon) sf.getDefaultGeometry();
Polygon p2 = (Polygon) mp2.getGeometryN(0);
Polygon p3 = (Polygon) p2.intersection(p1);
names = names + "\n" + sf.getAttribute("cat") + ": " + p3.getArea();
}
} catch (MalformedURLException ex) {
Logger.getLogger(Process.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex2) {
Logger.getLogger(Process.class.getName()).log(Level.SEVERE, null, ex2);
}
return "Objects found: " + names;
}
Here is implementation into GeoServer
@DescribeProcess (title="overlayWPS", description="Creates buffer around point and overlays it with polygon layer. Returns area of overlay.")
public class OverlayWPS implements GeoServerProcess {
@DescribeResult(name="result", description="output result")
public String execute(@DescribeParameter(name="point", description="point") String point, @DescribeParameter(name="distance", description="distance to search") double distance) {
Process p = new Process();
return p.overlay(point, distance);
I would like to change it get overlay of two separate shapefiles and get overlaid area as third multi-polygon layer.