I have a FeatureLayer (point) to which I would like to digitize some points and add them to this layer. The FeatureLayer can have some attributes, e.g fid (int, primary key), Name (string) and the_geom (point geometry)...
I've tried to read the original features and add them to a FeatureCollection (array list). Then I'm trying to add my new digitized points to this array, and create a new FeatureLayer with this featureCollection. I can't get it to work. New features are added but the point-geometry is stored in the Name attribute as a WKT-String (POINT (15.026579830870883 58.37316178562563)) and has no geometry, the primary key for the new points are correct.
This is my current faulty function for adding new points:
static void addNewPoints(Coordinate[] coordinates) {
SimpleFeatureType sft=(SimpleFeatureType) layer.getFeatureSource().getSchema();
SimpleFeatureBuilder builder = new SimpleFeatureBuilder((SimpleFeatureType) sft);
GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();
Point point;
Style style = null;
List<SimpleFeature> featureCollection = new ArrayList<>();
if (layer!=null && layer.getFeatureSource()!=null) {
style = layer.getStyle();
//Add existing features to the feature collection
try {
FeatureIterator<SimpleFeature> fi= (FeatureIterator<SimpleFeature>)
layer.getFeatureSource().getFeatures().features();
while(fi.hasNext()) {
featureCollection.add((SimpleFeature)fi.next());
}
} catch (IOException e) {
e.printStackTrace();
}
}
// Add the new points to the existing points
for (int i = 0; i<coordinates.length; i++) {
// ??? what shall the argument be ???
SimpleFeature feature = builder.buildFeature(null);
if (CRS.getAxisOrder(MapView.map.getCoordinateReferenceSystem())
.equals(AxisOrder.EAST_NORTH)) {
point = geometryFactory.createPoint(new Coordinate(coordinates[i].x,
coordinates[i].y));
} else {
point = geometryFactory.createPoint(new Coordinate(coordinates[i].y,
coordinates[i].x));
}
builder.add(point);
featureCollection.add(feature);
}
layer = new FeatureLayer(DataUtilities.collection(featureCollection), style);
layer.setTitle("Tmp_"+ sft.getName().toString());
addLayerToTheMap(layer);
}