[Solved] Rename column schema issue with geotools

gizmogaurav Asks: Rename column schema issue with geotools
I am trying to rename the geometry column using the same code mentioned in the question GeoTools & Shapefile: How to rename column (attribute) name but when it is adding the outputfeature collection in the write step feature.addFeatures(outputFeatureCollection)” , it is still getting the old schema to_geom column.

     final SimpleFeature toWrite = writer.next(); 
     for (int i = 0; i < toWrite.getType().getAttributeCount(); i++) {
        String name = toWrite.getType().getDescriptor(i).getLocalName();
        toWrite.setAttribute(name, feature.getAttribute(name));

This is what the documentation saying in the addFeature,

// grab next feature and populate it 
// JD: worth a note on how we do this... we take a "pull" approach 
// because the raw schema we are inserting into may not match the 
// schema of the features we are inserting

Is there any way to fix this?

Below is the code for same

public class MergeFile2 {

    static Transaction mergedTransaction = null;
    static Transaction transaction = new DefaultTransaction("create");

    public static void main(String[] args) throws IOException {
        String SrcDir = "D:\Git\GIS spatial changes\Shoreline Files";
        String newShapeFilePath = "D:\\Git\\GIS spatial changes\\Shoreline Files\\county_spatial_erase_poc_new.shp";
        ArrayList<String> shapeFileList = new ArrayList<String>();
        try {
            init(shapeFileList, newShapeFilePath);
        } catch (IOException e) {


    private static List<String> getFilesPath(String srcDir) throws IOException {
        List<String> filePath = new ArrayList<>();
        DirectoryStream<Path> files = Files.newDirectoryStream(Paths.get(srcDir));
        for (Path file : files) {
            if (file.getFileName().toString().endsWith("county spatial erase.shp")) {
        return filePath;

    public static void init(ArrayList<String> shpArray, String newShapeFile) throws IOException {

        init(newShapeFile, shpArray.get(0), shpArray);

    private static void init(String newShapeFile, String shapeFile, ArrayList<String> shpArray) throws IOException {

        File oldFile = new File(shapeFile);
        Map<String, Serializable> params = new HashMap<>();
        params.put("url", oldFile.toURI().toURL());
        params.put("create spatial index", Boolean.TRUE);

        DataStore inputDataStore = DataStoreFinder.getDataStore(params);

        String inputTypeName = inputDataStore.getTypeNames()[0];
        SimpleFeatureType inputType = inputDataStore.getSchema(inputTypeName);
        FeatureSource<SimpleFeatureType, SimpleFeature> source = inputDataStore.getFeatureSource(inputTypeName);
        FeatureCollection<SimpleFeatureType, SimpleFeature> inputFeatureCollection = source.getFeatures();
        Map<String, String> changed = new HashMap<String, String>();
        SimpleFeatureType nSchema = changeDeafultGeomName(inputType, "GEOMETRY", changed);

        // Write
        File copyFile = new File(newShapeFile);

        Map<String, Serializable> copyFileParams = new HashMap<String, Serializable>();
        copyFileParams.put("url", copyFile.toURI().toURL());

        try {
            FeatureCollection<SimpleFeatureType, SimpleFeature> outputFeatureCollection = addFeatureandAttribute(
                    inputFeatureCollection, nSchema, changed); // this is not required because will be
                                                                // handled in merge
            // addFeatureandAttribute(shapefileData, featureStore);
            ShapefileDataStoreFactory dataStoreFactoryNew = new ShapefileDataStoreFactory();
            ShapefileDataStore newDataStore = (ShapefileDataStore) dataStoreFactoryNew

            String typeName = newDataStore.getTypeNames()[0];

            ContentFeatureStore featureStore = (ContentFeatureStore) newDataStore.getFeatureSource(typeName);

            for (int i = 1; i < shpArray.size(); i++) {
                copyFile = new File(shpArray.get(i));

                copyFileParams = new HashMap<String, Serializable>();
                copyFileParams.put("url", copyFile.toURI().toURL());
                copyFileParams.put("create spatial index", Boolean.TRUE);
                inputDataStore = DataStoreFinder.getDataStore(params);

                inputTypeName = inputDataStore.getTypeNames()[0];
                inputType = inputDataStore.getSchema(inputTypeName);
                source = inputDataStore.getFeatureSource(inputTypeName);
                inputFeatureCollection = source.getFeatures();
                outputFeatureCollection = addFeatureandAttribute(inputFeatureCollection, nSchema, changed);

        } catch (Exception problem) {
        } finally {


    public static SimpleFeature retype(SimpleFeature source, SimpleFeatureType schema, Map<String, String> changes)
            throws IllegalAttributeException {
        SimpleFeatureBuilder builder = new SimpleFeatureBuilder(schema);
        for (int i = 0; i < schema.getAttributeCount(); i++) {
            AttributeDescriptor attributeType = schema.getDescriptor(i);
            Object value = null;
            String name = changes.get(attributeType.getLocalName());
            // String name = attributeType.getLocalName();
            if (source.getFeatureType().getDescriptor(name) != null) {
                value = source.getAttribute(name);

            builder.set(attributeType.getLocalName(), value);
        FeatureId id = source.getIdentifier();
        SimpleFeature retyped = builder.buildFeature(id.getID());
        return retyped;

    private static FeatureCollection<SimpleFeatureType, SimpleFeature> addFeatureandAttribute(
            FeatureCollection<SimpleFeatureType, SimpleFeature> inputFeatureCollection, SimpleFeatureType schema,
            Map<String, String> changed) throws IOException {
        List<SimpleFeature> features = new ArrayList<>();
        try (SimpleFeatureIterator itr = (SimpleFeatureIterator) inputFeatureCollection.features()) {
            while (itr.hasNext()) {
                SimpleFeature f = itr.next();
                SimpleFeature f2 = retype(f, schema, changed);
        FeatureCollection<SimpleFeatureType, SimpleFeature> outputFeatureCollection = DataUtilities
        return outputFeatureCollection;

    public static SimpleFeatureType changeDeafultGeomName(SimpleFeatureType inputType, String name,
            Map<String, String> changed) {
        SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
        builder.setSuperType((SimpleFeatureType) inputType.getSuper());
        for (AttributeDescriptor att : inputType.getAttributeDescriptors()) {
            AttributeType type = att.getType();
            if (type instanceof GeometryType) {
                GeometryType geomType = (GeometryType) type;
                GeometryDescriptor geom = (GeometryDescriptor) att;
                GeometryDescriptor geomDesc = new GeometryDescriptorImpl(geomType, new NameImpl(name),
                        geom.getMinOccurs(), geom.getMaxOccurs(), geom.isNillable(), geom.getDefaultValue());
                changed.put(name, att.getLocalName());
            } else {
                changed.put(att.getLocalName(), att.getLocalName());

        return builder.buildFeatureType();


    private static void saveShapefile() {
        if (mergedTransaction == null)

        try {

        } catch (Exception e) {


Ten-tools.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your response here to help other visitors like you. Thank you, Ten-tools.