0

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.

prem30488
  • 2,828
  • 2
  • 25
  • 57
  • 1
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Pradeep Simha Aug 30 '16 at 06:55
  • change if(layer.getBounds()!=null) to if(layer != null && layer.getBounds()!=null) – Stultuske Aug 30 '16 at 06:56
  • No this is not orthodox, this is usage of GeoTIFF Api. As in my code I used Layer layer = new FeatureLayer(fc, style,"VectorLayer"); which should initialize it. – prem30488 Aug 30 '16 at 06:59
  • Actually problem is with getBounds is null and I have no setter from API. – prem30488 Aug 30 '16 at 07:00

1 Answers1

0

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);
prem30488
  • 2,828
  • 2
  • 25
  • 57
Erwin Bolwidt
  • 30,799
  • 15
  • 56
  • 79