0

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);
}

1 Answer 1

1

As you know SimpleFeatureBuilder is used to build new features. It has two relevant methods for setting attributes for that feature add(value) and set(name, value).

This builder builds a feature by maintaining state. Each call to add(Object) creates a new attribute for the feature and stores it locally. When using the add method to add attributes to the feature, values added must be added in the same order as the attributes as defined by the feature type. The methods set(String, Object) and set(int, Object) are used to add attributes out of order.

So you need to either set the name of your feature before you add the geometry or make use of the set methods to add them in a different order.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.