All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jgrasstools.nww.shapes.IFeatureShape Maven / Gradle / Ivy

/*
 * This file is part of JGrasstools (http://www.jgrasstools.org)
 * (C) HydroloGIS - www.hydrologis.com 
 * 
 * JGrasstools is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see .
 */
package org.jgrasstools.nww.shapes;

import java.io.IOException;
import java.util.Collections;

import org.geotools.data.DefaultTransaction;
import org.geotools.data.Transaction;
import org.geotools.data.simple.SimpleFeatureStore;
import org.geotools.factory.CommonFactoryFinder;
import org.geotools.factory.GeoTools;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.filter.Filter;
import org.opengis.filter.FilterFactory;

/**
 * A NWW shape holding a {@link SimpleFeature} and if editable, also a store.
 * 
 * @author Andrea Antonello (www.hydrologis.com)
 *
 */
public interface IFeatureShape {

    public SimpleFeature getFeature();

    public FeatureStoreInfo getFeatureStoreInfo();
    
   
    /**
     * Modify the attributes of this feature.
     * 
     * @param names the names of the attributes to modify.
     * @param values the values to set.
     * @return the new feature if the editing has been successful.
     */
    default public SimpleFeature modifyFeatureAttribute( String[] names, Object[] values ) {
        FeatureStoreInfo featureStoreInfo = getFeatureStoreInfo();
        SimpleFeatureStore featureStore = featureStoreInfo.getFeatureStore();
        if (featureStore != null) {
            Transaction transaction = new DefaultTransaction("modify");
            featureStore.setTransaction(transaction);

            FilterFactory ff = CommonFactoryFinder.getFilterFactory(GeoTools.getDefaultHints());
            Filter filter = ff.id(Collections.singleton(ff.featureId(getFeature().getID())));

            try {
                featureStore.modifyFeatures(names, values, filter);
                transaction.commit();

                SimpleFeature modifiedFeature = featureStore.getFeatures(filter).features().next();
                return modifiedFeature;
            } catch (Exception eek) {
                try {
                    transaction.rollback();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    public void setFeature( SimpleFeature feature );

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy