要素层正在使用从 RasterToVectorProcess 派生的要素集合获取空边界
Feature Layer is getting null bounds with featurecollection derived from RasterToVectorProcess
Null Pinter Exception 仅仅意味着它是因为值正在变得空。但是在使用 GeoTiff 等 API 的情况下,发现使用错误变得很烦人。
我的代码如下:
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);
错误
at org.geotools.map.FeatureLayer.getBounds(FeatureLayer.java:199)
我正在尝试将 Tiff 转换为矢量图像,然后我想将其存储在磁盘上。
很可能,您的 featureSource
为 null,因为 geotools FeatureLayer
class 方法 getBounds()
使用 featureSource
检索边界,但在构造函数中 FeatureLayer
不检查 featureSource
是否为 null
.
featureSource
是 FeatureLayer
的构造函数的第一个参数。在您的情况下,这是变量 SimpleFeatureCollection fc
.
很可能,方法 process.process
返回了 null
,因此 fc
为空。
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);
Null Pinter Exception 仅仅意味着它是因为值正在变得空。但是在使用 GeoTiff 等 API 的情况下,发现使用错误变得很烦人。
我的代码如下:
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);
错误
at org.geotools.map.FeatureLayer.getBounds(FeatureLayer.java:199)
我正在尝试将 Tiff 转换为矢量图像,然后我想将其存储在磁盘上。
很可能,您的 featureSource
为 null,因为 geotools FeatureLayer
class 方法 getBounds()
使用 featureSource
检索边界,但在构造函数中 FeatureLayer
不检查 featureSource
是否为 null
.
featureSource
是 FeatureLayer
的构造函数的第一个参数。在您的情况下,这是变量 SimpleFeatureCollection fc
.
很可能,方法 process.process
返回了 null
,因此 fc
为空。
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);