Search in sources :

Example 16 with SimpleFeatureBuilder

use of org.geotools.feature.simple.SimpleFeatureBuilder 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 17 with SimpleFeatureBuilder

use of org.geotools.feature.simple.SimpleFeatureBuilder in project OpenTripPlanner by opentripplanner.

the class LIsochrone method makeContourFeatures.

/**
 * Create a geotools feature collection from a list of isochrones in the OTPA internal format.
 * Once in a FeatureCollection, they can for example be exported as GeoJSON.
 */
public static SimpleFeatureCollection makeContourFeatures(List<IsochroneData> isochrones) {
    DefaultFeatureCollection featureCollection = new DefaultFeatureCollection(null, contourSchema);
    SimpleFeatureBuilder fbuilder = new SimpleFeatureBuilder(contourSchema);
    for (IsochroneData isochrone : isochrones) {
        fbuilder.add(isochrone.geometry);
        fbuilder.add(isochrone.cutoffSec);
        featureCollection.add(fbuilder.buildFeature(null));
    }
    return featureCollection;
}
Also used : IsochroneData(org.opentripplanner.analyst.core.IsochroneData) DefaultFeatureCollection(org.geotools.feature.DefaultFeatureCollection) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder)

Example 18 with SimpleFeatureBuilder

use of org.geotools.feature.simple.SimpleFeatureBuilder in project OpenTripPlanner by opentripplanner.

the class SimpleIsochrone method makePointFeatures.

private SimpleFeatureCollection makePointFeatures() throws Exception {
    Map<Vertex, Double> points = makePoints();
    /* Stage the point features in memory */
    DefaultFeatureCollection featureCollection = new DefaultFeatureCollection(null, pointSchema);
    SimpleFeatureBuilder fbuilder = new SimpleFeatureBuilder(pointSchema);
    GeometryFactory gf = new GeometryFactory();
    for (Map.Entry<Vertex, Double> entry : points.entrySet()) {
        Vertex vertex = entry.getKey();
        Double travelTime = entry.getValue();
        fbuilder.add(gf.createPoint(vertex.getCoordinate()));
        fbuilder.add(travelTime);
        featureCollection.add(fbuilder.buildFeature(null));
    }
    return featureCollection;
}
Also used : StreetVertex(org.opentripplanner.routing.vertextype.StreetVertex) Vertex(org.opentripplanner.routing.graph.Vertex) IntersectionVertex(org.opentripplanner.routing.vertextype.IntersectionVertex) GeometryFactory(com.vividsolutions.jts.geom.GeometryFactory) Map(java.util.Map) HashMap(java.util.HashMap) DefaultFeatureCollection(org.geotools.feature.DefaultFeatureCollection) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder)

Example 19 with SimpleFeatureBuilder

use of org.geotools.feature.simple.SimpleFeatureBuilder in project OpenTripPlanner by opentripplanner.

the class SimpleIsochrone method makeContourFeatures.

private SimpleFeatureCollection makeContourFeatures() throws Exception {
    Map<Integer, Geometry> contours = makeContours();
    /* Stage the features in memory, in order from bottom to top, biggest to smallest */
    DefaultFeatureCollection featureCollection = new DefaultFeatureCollection(null, contourSchema);
    SimpleFeatureBuilder fbuilder = new SimpleFeatureBuilder(contourSchema);
    List<Integer> thresholds = new ArrayList<Integer>(contours.keySet());
    Collections.sort(thresholds);
    // Collections.reverse(thresholds);
    for (Integer threshold : thresholds) {
        Geometry contour = contours.get(threshold);
        fbuilder.add(contour);
        fbuilder.add(threshold);
        featureCollection.add(fbuilder.buildFeature(null));
    }
    return featureCollection;
}
Also used : Geometry(com.vividsolutions.jts.geom.Geometry) ArrayList(java.util.ArrayList) DefaultFeatureCollection(org.geotools.feature.DefaultFeatureCollection) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder)

Example 20 with SimpleFeatureBuilder

use of org.geotools.feature.simple.SimpleFeatureBuilder 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)

Aggregations

SimpleFeatureBuilder (org.geotools.feature.simple.SimpleFeatureBuilder)56 SimpleFeature (org.opengis.feature.simple.SimpleFeature)34 SimpleFeatureType (org.opengis.feature.simple.SimpleFeatureType)25 Optional (com.google.common.base.Optional)17 ArrayList (java.util.ArrayList)15 RevFeature (org.locationtech.geogig.api.RevFeature)14 RevFeatureType (org.locationtech.geogig.api.RevFeatureType)13 Test (org.junit.Test)11 SimpleFeatureCollection (org.geotools.data.simple.SimpleFeatureCollection)10 SimpleFeatureIterator (org.geotools.data.simple.SimpleFeatureIterator)10 File (java.io.File)9 IOException (java.io.IOException)9 HashMap (java.util.HashMap)9 SimpleFeatureSource (org.geotools.data.simple.SimpleFeatureSource)9 Coordinate (com.vividsolutions.jts.geom.Coordinate)8 GeometryFactory (com.vividsolutions.jts.geom.GeometryFactory)8 Point (com.vividsolutions.jts.geom.Point)8 SimpleFeatureStore (org.geotools.data.simple.SimpleFeatureStore)8 Geometry (com.vividsolutions.jts.geom.Geometry)7 List (java.util.List)7