javageotoolsjts

Why does the ForceCoordinateSystemIterator class next() method retype the SimpleFeature object?


English is not my native language; please excuse typing errors.

I have two questions

Q1: ForceCoordinateSystemIterator.class next() method why not return the original SimpleFeature but retype the SimpleFeature?

Q2: when use ForceCoordinateSystemIterator I cannot retrieve the original object to update this default geometry. what would be the best way to do that?

FeatureJSON featureJSON = new FeatureJSON(new GeometryJSON(15));
InputStream is = Files.newInputStream(Paths.get("/arjen_data/rust/2.geojson"));
SimpleFeatureIterator simpleFeatureCollection2 = (SimpleFeatureCollection) featureJSON.readFeatureCollection(is);
try (SimpleFeatureIterator fit = simpleFeatureCollection2.features()) {
            while (fit.hasNext()) {
                // fit is ForceCoordinateSystemIterator.class
                SimpleFeature next = fit.next(); // is retype object
                Geometry defaultGeometry = (Geometry) next.getDefaultGeometry();
                Geometry intersection = defaultGeometry.difference(union);
                // update default Geometry
                next.setDefaultGeometry(intersection);
            }
}

i read ForceCoordinateSystemIterator.class next() method source code. I have the two questions mentioned above now.

    @Override
    public SimpleFeature next() throws NoSuchElementException {
        if (reader == null) {
            throw new IllegalStateException("Reader has already been closed");
        }

        SimpleFeature next = reader.next();
        if (builder == null) return next;

        try {
            // why retype? original object is bad?
            return SimpleFeatureBuilder.retype(next, builder);
        } catch (IllegalAttributeException eep) {
            throw (IllegalStateException)
                    new IllegalStateException(eep.getMessage()).initCause(eep);
        }
    }

Solution

  • I suspect the answer is to do with mutable and immutable objects with in the library, a FeatureType (schema) is immutable and the schema of an Feature includes information about the CRS of the Geometry objects so if you change the schema you must change the feature.

    I suspect your life would be easier if you use the gt-geojson-store to get a "proper" GeoJSON datastore rather than using gt-geojson which is (also) unsupported but even less well supported. However, that won't let you modify the default geometry to update the collection either (I think). You probably want to checkout this tutorial.