对于 SimpaleFeatureCollection 类型,方法 add(SimpleFeature) 未定义
The method add(SimpleFeature) is undefined for the type SimpaleFeatureCollection
我正在尝试实施一个矢量化程序,但出现以下编译时错误。我正在使用 GeoTools 14.4。
private SimpleFeatureCollection assembleFeatures(GridCoverage2D grid, int band,
boolean insideEdges, SimpleFeatureType type, ProgressListener monitor) {
if (monitor == null) {
monitor = new NullProgressListener();
}
SimpleFeatureCollection features = FeatureCollections.newCollection();
SimpleFeatureBuilder builder = new SimpleFeatureBuilder(type);
Point2D p = new Point2D.Double();
double[] bandData = new double[grid.getNumSampleDimensions()];
polygonizer.add(lines);
Collection polygons = polygonizer.getPolygons();
final int size = polygons.size();
try {
float progressScale = 100.0f / size;
monitor.started();
int index = 0;
for (Iterator i = polygons.iterator(); i.hasNext(); index++) {
if (monitor.isCanceled()) {
throw new CancellationException();
}
monitor.progress(progressScale * index);
Polygon poly = (Polygon) i.next();
InteriorPointArea ipa = new InteriorPointArea(poly);
Coordinate c = ipa.getInteriorPoint();
Point insidePt = geomFactory.createPoint(c);
if (!poly.contains(insidePt)) {
// try another method to generate an interior point
boolean found = false;
for (Coordinate ringC : poly.getExteriorRing().getCoordinates()) {
c.x = ringC.x + cellWidthX / 2;
c.y = ringC.y;
insidePt = geomFactory.createPoint(c);
if (poly.contains(insidePt)) {
found = true;
break;
}
}
if (!found) {
throw new IllegalStateException("Can't locate interior point for polygon");
}
}
p.setLocation(c.x, c.y);
bandData = grid.evaluate(p, bandData);
if (!isOutside(bandData[band])) {
builder.add(poly);
if (insideEdges) {
builder.add(bandData[band]);
} else {
builder.add(INSIDE_FLAG_VALUE);
}
features.add(builder.buildFeature(null));
// here it gives error "The method add(SimpleFeature) is undefined for the type SimpaleFeatureCollection"
}
}
return features;
} finally {
monitor.complete();
}
}
完整的源代码是here
实际上我复制了 2 类 RasterToVectorFactory.java
和 RasterToVectorProcess.java
,其余的是我的 GeoTiff 14.4 代码,但我有以下错误。
The method add(SimpleFeature) is undefined for the type SimpaleFeatureCollection
我尝试使用以下代码作为解决方法。
但是在他的网站上提供代码的作者说代码无法运行!!
//features.add(builder.buildFeature(null));
//add
System.out.println("adding...");
SimpleFeature feature = builder.buildFeature(null);
((Collection<SimpleFeature>) features).add(feature);
将 SimpleFeatureCollection 的声明更改为 DefaultFeatureCollection class
DefaultFeatureCollection features = new DefaultFeatureCollection("your_id",type);
我正在尝试实施一个矢量化程序,但出现以下编译时错误。我正在使用 GeoTools 14.4。
private SimpleFeatureCollection assembleFeatures(GridCoverage2D grid, int band,
boolean insideEdges, SimpleFeatureType type, ProgressListener monitor) {
if (monitor == null) {
monitor = new NullProgressListener();
}
SimpleFeatureCollection features = FeatureCollections.newCollection();
SimpleFeatureBuilder builder = new SimpleFeatureBuilder(type);
Point2D p = new Point2D.Double();
double[] bandData = new double[grid.getNumSampleDimensions()];
polygonizer.add(lines);
Collection polygons = polygonizer.getPolygons();
final int size = polygons.size();
try {
float progressScale = 100.0f / size;
monitor.started();
int index = 0;
for (Iterator i = polygons.iterator(); i.hasNext(); index++) {
if (monitor.isCanceled()) {
throw new CancellationException();
}
monitor.progress(progressScale * index);
Polygon poly = (Polygon) i.next();
InteriorPointArea ipa = new InteriorPointArea(poly);
Coordinate c = ipa.getInteriorPoint();
Point insidePt = geomFactory.createPoint(c);
if (!poly.contains(insidePt)) {
// try another method to generate an interior point
boolean found = false;
for (Coordinate ringC : poly.getExteriorRing().getCoordinates()) {
c.x = ringC.x + cellWidthX / 2;
c.y = ringC.y;
insidePt = geomFactory.createPoint(c);
if (poly.contains(insidePt)) {
found = true;
break;
}
}
if (!found) {
throw new IllegalStateException("Can't locate interior point for polygon");
}
}
p.setLocation(c.x, c.y);
bandData = grid.evaluate(p, bandData);
if (!isOutside(bandData[band])) {
builder.add(poly);
if (insideEdges) {
builder.add(bandData[band]);
} else {
builder.add(INSIDE_FLAG_VALUE);
}
features.add(builder.buildFeature(null));
// here it gives error "The method add(SimpleFeature) is undefined for the type SimpaleFeatureCollection"
}
}
return features;
} finally {
monitor.complete();
}
}
完整的源代码是here
实际上我复制了 2 类 RasterToVectorFactory.java
和 RasterToVectorProcess.java
,其余的是我的 GeoTiff 14.4 代码,但我有以下错误。
The method add(SimpleFeature) is undefined for the type SimpaleFeatureCollection
我尝试使用以下代码作为解决方法。
但是在他的网站上提供代码的作者说代码无法运行!!
//features.add(builder.buildFeature(null));
//add
System.out.println("adding...");
SimpleFeature feature = builder.buildFeature(null);
((Collection<SimpleFeature>) features).add(feature);
将 SimpleFeatureCollection 的声明更改为 DefaultFeatureCollection class
DefaultFeatureCollection features = new DefaultFeatureCollection("your_id",type);