Search in sources :

Example 1 with ListFeatureCollection

use of org.geotools.data.collection.ListFeatureCollection in project sldeditor by robward-scisys.

the class InLineFeatureModel method removeFeature.

/**
 * Removes the feature.
 *
 * @param selectedRow the selected row
 */
public void removeFeature(int selectedRow) {
    if ((selectedRow < 0) || (selectedRow >= getRowCount())) {
        return;
    }
    SimpleFeatureType featureType = userLayer.getInlineFeatureType();
    String typeName = userLayer.getInlineFeatureType().getTypeName();
    try {
        SimpleFeatureSource featureSource = userLayer.getInlineFeatureDatastore().getFeatureSource(typeName);
        SimpleFeatureBuilder sfb = new SimpleFeatureBuilder(featureType);
        ArrayList<SimpleFeature> featureList = new ArrayList<SimpleFeature>();
        SimpleFeatureIterator it = featureSource.getFeatures().features();
        try {
            int index = 0;
            while (it.hasNext()) {
                SimpleFeature sf = it.next();
                if (index != selectedRow) {
                    List<Object> attributeValueList = sf.getAttributes();
                    sfb.addAll(attributeValueList);
                    featureList.add(sfb.buildFeature(null));
                }
                index++;
            }
        } finally {
            it.close();
        }
        SimpleFeatureCollection collection = new ListFeatureCollection(featureType, featureList);
        featureCollection = collection;
        cachedFeature = null;
        lastRow = -1;
        DataStore dataStore = DataUtilities.dataStore(collection);
        userLayer.setInlineFeatureDatastore(dataStore);
    } catch (IOException e) {
        ConsoleManager.getInstance().exception(this, e);
    }
    this.fireTableStructureChanged();
    this.fireTableDataChanged();
    if (parentObj != null) {
        parentObj.inlineFeatureUpdated();
    }
}
Also used : 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 2 with ListFeatureCollection

use of org.geotools.data.collection.ListFeatureCollection in project sldeditor by robward-scisys.

the class InLineFeatureModel method updateCRS.

/**
 * Update CRS.
 *
 * @param selectedValue the selected value
 */
public void updateCRS(ValueComboBoxData selectedValue) {
    if (selectedValue != null) {
        String crsCode = selectedValue.getKey();
        CoordinateReferenceSystem newCRS = CoordManager.getInstance().getCRS(crsCode);
        SimpleFeatureType newFeatureType = SimpleFeatureTypeBuilder.retype(featureCollection.getSchema(), newCRS);
        String typeName = userLayer.getInlineFeatureType().getTypeName();
        try {
            SimpleFeatureSource featureSource = userLayer.getInlineFeatureDatastore().getFeatureSource(typeName);
            SimpleFeatureBuilder sfb = new SimpleFeatureBuilder(newFeatureType);
            ArrayList<SimpleFeature> featureList = new ArrayList<SimpleFeature>();
            SimpleFeatureIterator it = featureSource.getFeatures().features();
            try {
                while (it.hasNext()) {
                    SimpleFeature sf = it.next();
                    List<Object> attributeValueList = sf.getAttributes();
                    sfb.addAll(attributeValueList);
                    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 : 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) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder)

Example 3 with ListFeatureCollection

use of org.geotools.data.collection.ListFeatureCollection in project traffic-engine by opentraffic.

the class OSMUtils method toShapefile.

public static void toShapefile(List<SpatialDataItem> segs, String filename) throws SchemaException, IOException {
    final SimpleFeatureType TYPE = DataUtilities.createType("Location", "the_geom:LineString:srid=4326," + "name:String");
    System.out.println("TYPE:" + TYPE);
    List<SimpleFeature> features = new ArrayList<SimpleFeature>();
    /*
         * GeometryFactory will be used to create the geometry attribute of each feature,
         * using a Point object for the location.
         */
    GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();
    SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(TYPE);
    for (SpatialDataItem seg : segs) {
        featureBuilder.add(seg.getGeometry());
        featureBuilder.add(seg.id);
        SimpleFeature feature = featureBuilder.buildFeature(null);
        features.add(feature);
    }
    File newFile = new File(filename);
    ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
    Map<String, Serializable> params = new HashMap<String, Serializable>();
    params.put("url", newFile.toURI().toURL());
    params.put("create spatial index", Boolean.TRUE);
    ShapefileDataStore newDataStore = (ShapefileDataStore) dataStoreFactory.createNewDataStore(params);
    /*
         * TYPE is used as a template to describe the file contents
         */
    newDataStore.createSchema(TYPE);
    ContentFeatureSource cfs = newDataStore.getFeatureSource();
    if (cfs instanceof SimpleFeatureStore) {
        SimpleFeatureStore featureStore = (SimpleFeatureStore) cfs;
        SimpleFeatureCollection collection = new ListFeatureCollection(TYPE, features);
        try {
            featureStore.addFeatures(collection);
        } catch (Exception problem) {
            problem.printStackTrace();
        } finally {
        }
    }
}
Also used : Serializable(java.io.Serializable) GeometryFactory(com.vividsolutions.jts.geom.GeometryFactory) ShapefileDataStore(org.geotools.data.shapefile.ShapefileDataStore) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SpatialDataItem(io.opentraffic.engine.data.SpatialDataItem) LineString(com.vividsolutions.jts.geom.LineString) SimpleFeature(org.opengis.feature.simple.SimpleFeature) SchemaException(org.geotools.feature.SchemaException) IOException(java.io.IOException) SimpleFeatureCollection(org.geotools.data.simple.SimpleFeatureCollection) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) SimpleFeatureStore(org.geotools.data.simple.SimpleFeatureStore) ListFeatureCollection(org.geotools.data.collection.ListFeatureCollection) ShapefileDataStoreFactory(org.geotools.data.shapefile.ShapefileDataStoreFactory) File(java.io.File) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder) ContentFeatureSource(org.geotools.data.store.ContentFeatureSource)

Example 4 with ListFeatureCollection

use of org.geotools.data.collection.ListFeatureCollection 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<SimpleFeature>();
                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 5 with ListFeatureCollection

use of org.geotools.data.collection.ListFeatureCollection in project sldeditor by robward-scisys.

the class InLineFeatureModel method addNewFeature.

/**
 * Adds the new feature.
 */
public void addNewFeature() {
    SimpleFeatureType featureType = userLayer.getInlineFeatureType();
    String typeName = userLayer.getInlineFeatureType().getTypeName();
    try {
        SimpleFeatureSource featureSource = userLayer.getInlineFeatureDatastore().getFeatureSource(typeName);
        SimpleFeatureBuilder sfb = new SimpleFeatureBuilder(featureType);
        ArrayList<SimpleFeature> featureList = new ArrayList<SimpleFeature>();
        SimpleFeatureIterator it = featureSource.getFeatures().features();
        try {
            while (it.hasNext()) {
                SimpleFeature sf = it.next();
                List<Object> attributeValueList = sf.getAttributes();
                sfb.addAll(attributeValueList);
                featureList.add(sfb.buildFeature(null));
            }
            // Add new feature
            String wktString = "wkt://POINT(0 0)";
            Geometry geometry = WKTConversion.convertToGeometry(wktString, getSelectedCRSCode());
            sfb.add(geometry);
            featureList.add(sfb.buildFeature(null));
        } finally {
            it.close();
        }
        SimpleFeatureCollection collection = new ListFeatureCollection(featureType, featureList);
        featureCollection = collection;
        cachedFeature = null;
        lastRow = -1;
        DataStore dataStore = DataUtilities.dataStore(collection);
        userLayer.setInlineFeatureDatastore(dataStore);
    } catch (IOException e) {
        ConsoleManager.getInstance().exception(this, e);
    }
    this.fireTableStructureChanged();
    this.fireTableDataChanged();
    if (parentObj != null) {
        parentObj.inlineFeatureUpdated();
    }
}
Also used : 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) Geometry(com.vividsolutions.jts.geom.Geometry) 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)

Aggregations

IOException (java.io.IOException)7 ListFeatureCollection (org.geotools.data.collection.ListFeatureCollection)7 SimpleFeatureCollection (org.geotools.data.simple.SimpleFeatureCollection)7 ArrayList (java.util.ArrayList)6 SimpleFeatureSource (org.geotools.data.simple.SimpleFeatureSource)6 SimpleFeatureBuilder (org.geotools.feature.simple.SimpleFeatureBuilder)6 SimpleFeature (org.opengis.feature.simple.SimpleFeature)6 SimpleFeatureType (org.opengis.feature.simple.SimpleFeatureType)6 DataStore (org.geotools.data.DataStore)5 SimpleFeatureIterator (org.geotools.data.simple.SimpleFeatureIterator)5 HashMap (java.util.HashMap)2 ShapefileDataStore (org.geotools.data.shapefile.ShapefileDataStore)2 SimpleFeatureStore (org.geotools.data.simple.SimpleFeatureStore)2 SimpleFeatureTypeBuilder (org.geotools.feature.simple.SimpleFeatureTypeBuilder)2 Geometry (com.vividsolutions.jts.geom.Geometry)1 GeometryFactory (com.vividsolutions.jts.geom.GeometryFactory)1 LineString (com.vividsolutions.jts.geom.LineString)1 SpatialDataItem (io.opentraffic.engine.data.SpatialDataItem)1 File (java.io.File)1 Serializable (java.io.Serializable)1