Search code examples
javageotiff

Feature Layer is getting null bounds with featurecollection derived from RasterToVectorProcess


Null Pinter Exception simply means that it is because of value is getting null. But In case of using APis such as GeoTiff it becomes annoying to find out the error in usage.

My code is as follows:

System.out.println("vectorization starts");

GridCoverage2D srcCoverage = new GeoTiffReader(new File("E:/output/ll_processed.TIFF")).read(new GeneralParameterValue[]{policy, gridsize, useJaiRead});

SimpleFeatureCollection fc = RasterToVectorProcess.process(srcCoverage, 3, cov.getEnvelope(), Collections.singletonList(0.0d), true, null);

System.out.println("process ends");
System.out.println("vectorization ends");
//MapContext map = new DefaultMapContext();
//map.setTitle("raster to vector conversion");
Style style = SLD.createPolygonStyle(Color.BLUE, Color.CYAN, 1.0f);
//map.addLayer(fc, style);
//map.getLayerBounds();
//JMapFrame.showMap(map);

MapContent mapContent= new MapContent();
mapContent.setTitle("Illegal Mining");
Layer layer = new FeatureLayer(fc, style,"VectorLayer");

//int boundary = 10;
// ReferencedEnvelope env2 = new  ReferencedEnvelope(srcCoverage.getEnvelope().getMinimum(0) - boundary, srcCoverage.getEnvelope().getMaximum(0) + boundary,
    //srcCoverage.getEnvelope().getMinimum(1) - boundary, srcCoverage.getEnvelope().getMaximum(1) + boundary, srcCoverage.getCoordinateReferenceSystem());

//mapContent.getViewport().setBounds(fc.getBounds());
Line 199 : if(layer.getBounds()!=null) // here the error is coming also tried with if(layer != null && layer.getBounds()!=null) 
 {
    mapContent.addLayer(layer);
}else{
    System.out.println("Layer bounds are null");
}
mapContent.getViewport().setCoordinateReferenceSystem(
        DefaultGeographicCRS.WGS84);

Error

at org.geotools.map.FeatureLayer.getBounds(FeatureLayer.java:199)

I am trying to convert Tiff to Vector image and Then I want to store it on disk.


Solution

  • Most likely, your featureSource is null, since the geotools FeatureLayer class method getBounds() uses the featureSource to retrieve the bounds, but in the constructor FeatureLayer doesn't check whether the featureSource is null.

    The featureSource is the first argument to the constructor of FeatureLayer. In your case, that's variable SimpleFeatureCollection fc.

    Most likely, the method process.process returned null so fc is null.

    Minor Change in RasterToVectorProcess.java

           //features.add(builder.buildFeature(null));
                    //System.out.println("ignored");
                    //add
                    System.out.println("adding...");
                    SimpleFeature feature = builder.buildFeature(null);
                    ((Collection<SimpleFeature>) features).add(feature);