thanks again, Marco. it is under your instructive idea i solved my problem.
1) as to regular clip, the correct code is:
1
2File shapefile = new File("path/filename.shp");
3String productfile = "path/filename";
4
5//read product and feature
6Product product = ProductIO.readProduct(productfile);
7FeatureCollection<SimpleFeatureType, SimpleFeature> featcoll_all = FeatureUtils.loadFeatureCollectionFromShapefile(shapefile);
8
9//get the intersection between product bounds and feature bounds
10Geometry productBounds = FeatureUtils.createGeoBoundaryPolygon(product);
11FeatureCollection<SimpleFeatureType, SimpleFeature> featcoll_part = FeatureUtils.clipCollection(featcoll_all,
12 DefaultGeographicCRS.WGS84, productBounds, DefaultGeographicCRS.WGS84, null, null,new NullProgressMonitor());
13
14// get the bounds of the subset product
15ReferencedEnvelope bounds = featcoll_part.getBounds();
16Rectangle2D rect = new Envelope2D(bounds);
17
18//get image_x, image_y, image_width and image_height of the bounds
19double map_x = rect.getX();//map_x of the upper-left corner (projection coordinate system)
20double map_y = rect.getY() + rect.getHeight();//map_y of the upper-left corner
21DirectPosition map_coord = new DirectPosition2D(map_x, map_y);//create a map coordinate
22GeoCoding geocoding = product.getGeoCoding();
23DirectPosition image_coord =
24 geocoding.getImageToMapTransform().inverse().transform(map_coord, null);//transform map coord to image coord
25int x = (int) Math.floor( image_coord.getCoordinate()[0] );//image_x (image coordinate system)
26int y = (int) Math.floor( image_coord.getCoordinate()[1] );//image_y
27final int resolution=30;//resolution of the landsat8 image
28int w = (int) Math.rint( rect.getWidth()/resolution ) + 1;//image_width
29int h = (int) Math.rint( rect.getHeight()/resolution ) + 1;//image_height
30
31//create product subset (method 1)
32OperatorSpiRegistry registry = GPF.getDefaultInstance().getOperatorSpiRegistry();
33registry.loadOperatorSpis();
34HashMap<String, Object> parameters = new HashMap<>();
35Rectangle rectangle = new Rectangle(x, y, w, h);
36parameters.put("region", rectangle);
37Product subset = GPF.createProduct("Subset", parameters, product);
38
39//create product subset (method 2)
40// ProductSubsetDef subsetdef=new ProductSubsetDef();
41// subsetdef.setRegion(x, y, w, h);
42// Product subset=product.createSubset(subsetdef, null, null);
2) as to irregular clip, the following code can be append to the end of the above code:
1
2// create vector data node
3VectorDataNode VecNod = new VectorDataNode("Shape", featcoll_part);
4subset.getVectorDataGroup().add(VecNod);
5
6// create mask
7Mask maske = subset.getMaskGroup().get(VecNod.getName());
8maske.readRasterDataFully();
9
10// get band
11Band band = subset.getBands()[0];
12band.readRasterDataFully();
13
14// clip irregularly
15int width = band.getRasterWidth();
16int height = band.getRasterHeight();
17for (int j = 0; j < height; j++) {
18 for (int i = 0; i < width; i++) {
19 if (maske.getPixelInt(i, j) == 0) {
20 band.setPixelInt(i, j, -9999);//-9999 is the value of the background
21 }
22 }
23}
i'm new in java and beam, if my code is Inefficient, please tell me, thanks in advance!
best wishes!
Zhuyu