Search in sources :

Example 31 with SimpleFeatureIterator

use of org.geotools.data.simple.SimpleFeatureIterator 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 32 with SimpleFeatureIterator

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

the class InlineFeatureUtils method determineGeometryType.

/**
 * Determine geometry type.
 *
 * @param geometryDescriptor the geometry descriptor
 * @param simpleFeatureCollection the simple feature collection
 * @return the geometry type enum
 */
public static GeometryTypeEnum determineGeometryType(GeometryDescriptor geometryDescriptor, SimpleFeatureCollection simpleFeatureCollection) {
    if (geometryDescriptor == null) {
        return GeometryTypeEnum.UNKNOWN;
    }
    if (simpleFeatureCollection == null) {
        return GeometryTypeEnum.UNKNOWN;
    }
    Class<?> bindingType = geometryDescriptor.getType().getBinding();
    if (bindingType == Geometry.class) {
        Name geometryName = geometryDescriptor.getName();
        SimpleFeatureIterator iterator = simpleFeatureCollection.features();
        List<GeometryTypeEnum> geometryFeatures = new ArrayList<>();
        while (iterator.hasNext()) {
            SimpleFeature feature = iterator.next();
            Object value = feature.getAttribute(geometryName);
            if (value != null) {
                GeometryTypeEnum geometryType = GeometryTypeMapping.getGeometryType(value.getClass());
                if (!geometryFeatures.contains(geometryType)) {
                    geometryFeatures.add(geometryType);
                }
            }
        }
        return (combineGeometryType(geometryFeatures));
    } else {
        return GeometryTypeMapping.getGeometryType(bindingType);
    }
}
Also used : SimpleFeatureIterator(org.geotools.data.simple.SimpleFeatureIterator) ArrayList(java.util.ArrayList) GeometryTypeEnum(com.sldeditor.datasource.impl.GeometryTypeEnum) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Name(org.opengis.feature.type.Name)

Example 33 with SimpleFeatureIterator

use of org.geotools.data.simple.SimpleFeatureIterator 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

SimpleFeatureIterator (org.geotools.data.simple.SimpleFeatureIterator)33 SimpleFeature (org.opengis.feature.simple.SimpleFeature)33 SimpleFeatureCollection (org.geotools.data.simple.SimpleFeatureCollection)24 SimpleFeatureSource (org.geotools.data.simple.SimpleFeatureSource)19 SimpleFeatureType (org.opengis.feature.simple.SimpleFeatureType)15 SimpleFeatureStore (org.geotools.data.simple.SimpleFeatureStore)13 Test (org.junit.Test)13 SimpleFeatureBuilder (org.geotools.feature.simple.SimpleFeatureBuilder)10 IOException (java.io.IOException)9 ArrayList (java.util.ArrayList)9 MemoryDataStore (org.geotools.data.memory.MemoryDataStore)9 Feature (org.opengis.feature.Feature)9 AttributeDescriptor (org.opengis.feature.type.AttributeDescriptor)8 HashMap (java.util.HashMap)6 Query (org.geotools.data.Query)6 Geometry (com.vividsolutions.jts.geom.Geometry)5 Map (java.util.Map)5 DataStore (org.geotools.data.DataStore)5 ListFeatureCollection (org.geotools.data.collection.ListFeatureCollection)5 File (java.io.File)4