Search in sources :

Example 41 with SimpleFeatureCollection

use of org.geotools.data.simple.SimpleFeatureCollection in project sldeditor by robward-scisys.

the class InLineFeatureModel method addNewColumn.

/**
 * Adds the new column.
 */
public void addNewColumn() {
    if (featureCollection != null) {
        String attributeName = getUniqueAttributeName();
        columnList.add(attributeName);
        // Populate field names
        SimpleFeatureTypeBuilder featureTypeBuilder = new SimpleFeatureTypeBuilder();
        featureTypeBuilder.init(featureCollection.getSchema());
        featureTypeBuilder.add(attributeName, String.class);
        SimpleFeatureType newFeatureType = featureTypeBuilder.buildFeatureType();
        String typeName = userLayer.getInlineFeatureType().getTypeName();
        try {
            SimpleFeatureSource featureSource = userLayer.getInlineFeatureDatastore().getFeatureSource(typeName);
            SimpleFeatureBuilder sfb = new SimpleFeatureBuilder(newFeatureType);
            ArrayList<SimpleFeature> featureList = new ArrayList<>();
            SimpleFeatureIterator it = featureSource.getFeatures().features();
            try {
                while (it.hasNext()) {
                    SimpleFeature sf = it.next();
                    sfb.addAll(sf.getAttributes());
                    sfb.add(new String(""));
                    featureList.add(sfb.buildFeature(null));
                }
            } finally {
                it.close();
            }
            SimpleFeatureCollection collection = new ListFeatureCollection(newFeatureType, featureList);
            featureCollection = collection;
            cachedFeature = null;
            lastRow = -1;
            DataStore dataStore = DataUtilities.dataStore(collection);
            userLayer.setInlineFeatureDatastore(dataStore);
            userLayer.setInlineFeatureType(newFeatureType);
        } catch (IOException e) {
            ConsoleManager.getInstance().exception(this, e);
        }
        this.fireTableStructureChanged();
        this.fireTableDataChanged();
        if (parentObj != null) {
            parentObj.inlineFeatureUpdated();
        }
    }
}
Also used : SimpleFeatureTypeBuilder(org.geotools.feature.simple.SimpleFeatureTypeBuilder) SimpleFeatureSource(org.geotools.data.simple.SimpleFeatureSource) ArrayList(java.util.ArrayList) IOException(java.io.IOException) SimpleFeature(org.opengis.feature.simple.SimpleFeature) SimpleFeatureCollection(org.geotools.data.simple.SimpleFeatureCollection) SimpleFeatureIterator(org.geotools.data.simple.SimpleFeatureIterator) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) DataStore(org.geotools.data.DataStore) ListFeatureCollection(org.geotools.data.collection.ListFeatureCollection) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder)

Example 42 with SimpleFeatureCollection

use of org.geotools.data.simple.SimpleFeatureCollection in project sldeditor by robward-scisys.

the class InLineFeatureModel method removeColumn.

/**
 * Removes the column.
 *
 * @param columnName the column name
 */
public void removeColumn(String columnName) {
    if (featureCollection != null) {
        if (columnList.contains(columnName)) {
            columnList.remove(columnName);
            // Find field name to remote
            SimpleFeatureTypeBuilder featureTypeBuilder = new SimpleFeatureTypeBuilder();
            featureTypeBuilder.init(featureCollection.getSchema());
            featureTypeBuilder.remove(columnName);
            SimpleFeatureType newFeatureType = featureTypeBuilder.buildFeatureType();
            int attributeToRemoveIndex = 0;
            for (AttributeDescriptor descriptor : newFeatureType.getAttributeDescriptors()) {
                if (descriptor.getLocalName().compareTo(columnName) == 0) {
                    break;
                }
                attributeToRemoveIndex++;
            }
            String typeName = userLayer.getInlineFeatureType().getTypeName();
            try {
                SimpleFeatureSource featureSource = userLayer.getInlineFeatureDatastore().getFeatureSource(typeName);
                SimpleFeatureBuilder sfb = new SimpleFeatureBuilder(newFeatureType);
                ArrayList<SimpleFeature> featureList = new ArrayList<>();
                SimpleFeatureIterator it = featureSource.getFeatures().features();
                try {
                    while (it.hasNext()) {
                        SimpleFeature sf = it.next();
                        List<Object> attributes = sf.getAttributes();
                        attributes.remove(attributeToRemoveIndex);
                        sfb.addAll(attributes);
                        featureList.add(sfb.buildFeature(null));
                    }
                } finally {
                    it.close();
                }
                SimpleFeatureCollection collection = new ListFeatureCollection(newFeatureType, featureList);
                featureCollection = collection;
                cachedFeature = null;
                lastRow = -1;
                DataStore dataStore = DataUtilities.dataStore(collection);
                userLayer.setInlineFeatureDatastore(dataStore);
                userLayer.setInlineFeatureType(newFeatureType);
            } catch (IOException e) {
                ConsoleManager.getInstance().exception(this, e);
            }
            this.fireTableStructureChanged();
            this.fireTableDataChanged();
            if (parentObj != null) {
                parentObj.inlineFeatureUpdated();
            }
        }
    }
}
Also used : SimpleFeatureTypeBuilder(org.geotools.feature.simple.SimpleFeatureTypeBuilder) SimpleFeatureSource(org.geotools.data.simple.SimpleFeatureSource) ArrayList(java.util.ArrayList) AttributeDescriptor(org.opengis.feature.type.AttributeDescriptor) IOException(java.io.IOException) SimpleFeature(org.opengis.feature.simple.SimpleFeature) SimpleFeatureCollection(org.geotools.data.simple.SimpleFeatureCollection) SimpleFeatureIterator(org.geotools.data.simple.SimpleFeatureIterator) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) DataStore(org.geotools.data.DataStore) ListFeatureCollection(org.geotools.data.collection.ListFeatureCollection) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder)

Example 43 with SimpleFeatureCollection

use of org.geotools.data.simple.SimpleFeatureCollection in project sldeditor by robward-scisys.

the class DataSourceImpl method readAttributes.

/**
 * Read attributes.
 *
 * @param attributeData the attribute data
 */
/* (non-Javadoc)
     * @see com.sldeditor.datasource.DataSourceInterface#updateAttributes(com.sldeditor.render.iface.RenderAttributeDataInterface)
     */
@Override
public void readAttributes(DataSourceAttributeListInterface attributeData) {
    if (attributeData == null) {
        return;
    }
    List<DataSourceAttributeData> valueMap = new ArrayList<DataSourceAttributeData>();
    SimpleFeatureCollection featureCollection = dataSourceInfo.getFeatureCollection();
    if (featureCollection != null) {
        SimpleFeatureIterator iterator = featureCollection.features();
        if (iterator.hasNext()) {
            SimpleFeature feature = iterator.next();
            List<Object> attributes = feature.getAttributes();
            for (int i = 0; i < attributes.size(); i++) {
                Name fieldName = fieldNameMap.get(i);
                DataSourceAttributeData data = new DataSourceAttributeData(fieldName, fieldTypeMap.get(i), attributes.get(i));
                valueMap.add(data);
            }
        }
    }
    attributeData.setData(valueMap);
}
Also used : DataSourceAttributeData(com.sldeditor.common.datasource.attribute.DataSourceAttributeData) SimpleFeatureIterator(org.geotools.data.simple.SimpleFeatureIterator) ArrayList(java.util.ArrayList) SimpleFeature(org.opengis.feature.simple.SimpleFeature) SimpleFeatureCollection(org.geotools.data.simple.SimpleFeatureCollection) Name(org.opengis.feature.type.Name)

Aggregations

SimpleFeatureCollection (org.geotools.data.simple.SimpleFeatureCollection)43 SimpleFeature (org.opengis.feature.simple.SimpleFeature)27 SimpleFeatureIterator (org.geotools.data.simple.SimpleFeatureIterator)24 SimpleFeatureSource (org.geotools.data.simple.SimpleFeatureSource)21 IOException (java.io.IOException)15 SimpleFeatureStore (org.geotools.data.simple.SimpleFeatureStore)15 Test (org.junit.Test)15 SimpleFeatureType (org.opengis.feature.simple.SimpleFeatureType)12 Feature (org.opengis.feature.Feature)10 ArrayList (java.util.ArrayList)9 MemoryDataStore (org.geotools.data.memory.MemoryDataStore)9 SimpleFeatureBuilder (org.geotools.feature.simple.SimpleFeatureBuilder)9 ListFeatureCollection (org.geotools.data.collection.ListFeatureCollection)7 File (java.io.File)6 HashMap (java.util.HashMap)6 ShapefileDataStore (org.geotools.data.shapefile.ShapefileDataStore)6 AttributeDescriptor (org.opengis.feature.type.AttributeDescriptor)6 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)6 DataStore (org.geotools.data.DataStore)5 Query (org.geotools.data.Query)5