Search in sources :

Example 21 with SimpleFeatureCollection

use of org.geotools.data.simple.SimpleFeatureCollection in project spatial-portal by AtlasOfLivingAustralia.

the class AreaUploadShapefileWizardController method executeShapeImageRenderer.

private void executeShapeImageRenderer(Filter filter) {
    try {
        LOGGER.debug("Generating image");
        SimpleFeatureCollection features1;
        if (filter == null) {
            features1 = source.getFeatures();
        } else {
            features1 = source.getFeatures(filter);
        }
        // Create a map content and add our shapefile to it
        MapContent map = new MapContent();
        org.geotools.styling.Style style = SLD.createSimpleStyle(source.getSchema());
        Layer layer = new FeatureLayer(features1, style);
        map.addLayer(layer);
        GTRenderer renderer = new StreamingRenderer();
        renderer.setMapContent(map);
        int imageWidth = 800;
        int imageHeight = 300;
        Rectangle imageBounds;
        ReferencedEnvelope mapBounds;
        mapBounds = map.getMaxBounds();
        double heightToWidth = mapBounds.getSpan(1) / mapBounds.getSpan(0);
        if (heightToWidth * imageWidth > imageHeight) {
            imageBounds = new Rectangle(0, 0, (int) Math.round(imageHeight / heightToWidth), imageHeight);
        } else {
            imageBounds = new Rectangle(0, 0, imageWidth, (int) Math.round(imageWidth * heightToWidth));
        }
        BufferedImage image = new BufferedImage(imageBounds.width, imageBounds.height, BufferedImage.TYPE_INT_RGB);
        Graphics2D gr = image.createGraphics();
        gr.setPaint(Color.WHITE);
        gr.fill(imageBounds);
        renderer.paint(gr, imageBounds, mapBounds);
        img.setContent(image);
    } catch (Exception e) {
        LOGGER.debug("Unable to generate image for selected shapefile", e);
    }
}
Also used : MapContent(org.geotools.map.MapContent) FeatureLayer(org.geotools.map.FeatureLayer) Layer(org.geotools.map.Layer) MapLayer(au.org.emii.portal.menu.MapLayer) BufferedImage(java.awt.image.BufferedImage) ParseException(com.vividsolutions.jts.io.ParseException) IOException(java.io.IOException) SimpleFeatureCollection(org.geotools.data.simple.SimpleFeatureCollection) StreamingRenderer(org.geotools.renderer.lite.StreamingRenderer) FeatureLayer(org.geotools.map.FeatureLayer) ReferencedEnvelope(org.geotools.geometry.jts.ReferencedEnvelope) GTRenderer(org.geotools.renderer.GTRenderer)

Example 22 with SimpleFeatureCollection

use of org.geotools.data.simple.SimpleFeatureCollection in project GeoGig by boundlessgeo.

the class GeoGigDataStoreTest method testDiffFeatures.

private void testDiffFeatures(ObjectId oldRoot, ObjectId newRoot, int expectedAdded, int expectedRemoved, int expectedChanged) throws IOException {
    dataStore.setHead(newRoot.toString());
    List<String> fids;
    SimpleFeatureCollection features;
    ChangeType changeType = ChangeType.ADDED;
    features = dataStore.getDiffFeatureSource(pointsName, oldRoot.toString(), changeType).getFeatures();
    fids = toIdList(features);
    assertEquals(changeType + fids.toString(), expectedAdded, fids.size());
    assertEquals(changeType + fids.toString(), expectedAdded, features.size());
    changeType = ChangeType.REMOVED;
    features = dataStore.getDiffFeatureSource(pointsName, oldRoot.toString(), changeType).getFeatures();
    fids = toIdList(features);
    assertEquals(changeType + fids.toString(), expectedRemoved, fids.size());
    assertEquals(changeType + fids.toString(), expectedRemoved, features.size());
    changeType = ChangeType.CHANGED_NEW;
    features = dataStore.getDiffFeatureSource(pointsName, oldRoot.toString(), changeType).getFeatures();
    fids = toIdList(features);
    assertEquals(changeType + fids.toString(), expectedChanged, fids.size());
    assertEquals(changeType + fids.toString(), expectedChanged, features.size());
    changeType = ChangeType.CHANGED_OLD;
    features = dataStore.getDiffFeatureSource(pointsName, oldRoot.toString(), changeType).getFeatures();
    fids = toIdList(features);
    assertEquals(changeType + fids.toString(), expectedChanged, fids.size());
    assertEquals(changeType + fids.toString(), expectedChanged, features.size());
}
Also used : ChangeType(org.locationtech.geogig.geotools.data.GeoGigDataStore.ChangeType) LineString(com.vividsolutions.jts.geom.LineString) SimpleFeatureCollection(org.geotools.data.simple.SimpleFeatureCollection)

Example 23 with SimpleFeatureCollection

use of org.geotools.data.simple.SimpleFeatureCollection in project GeoGig by boundlessgeo.

the class GeoGigFeatureSourceTest method testGetFeaturesFilter.

@Test
public void testGetFeaturesFilter() throws Exception {
    SimpleFeatureCollection collection;
    Set<List<Object>> actual;
    Set<List<Object>> expected;
    Filter filter;
    filter = ff.id(Collections.singleton(ff.featureId(RepositoryTestCase.idP2)));
    collection = pointsSource.getFeatures(new Query(pointsName, filter));
    actual = Sets.newHashSet();
    for (SimpleFeature f : toList(collection)) {
        actual.add(f.getAttributes());
    }
    expected = Collections.singleton(((SimpleFeature) points2).getAttributes());
    assertEquals(expected, actual);
    ReferencedEnvelope queryBounds = boundsOf(points1, points2);
    Polygon geometry = JTS.toGeometry(queryBounds);
    filter = ff.intersects(ff.property(pointsType.getGeometryDescriptor().getLocalName()), ff.literal(geometry));
    collection = pointsSource.getFeatures(new Query(pointsName, filter));
    actual = Sets.newHashSet();
    for (SimpleFeature f : toList(collection)) {
        actual.add(f.getAttributes());
    }
    expected = ImmutableSet.of(((SimpleFeature) points1).getAttributes(), ((SimpleFeature) points2).getAttributes());
    assertEquals(expected, actual);
    ReferencedEnvelope transformedQueryBounds;
    CoordinateReferenceSystem queryCrs = CRS.decode("EPSG:3857");
    transformedQueryBounds = queryBounds.transform(queryCrs, true);
    geometry = JTS.toGeometry(transformedQueryBounds);
    geometry.setUserData(queryCrs);
    filter = ff.intersects(ff.property(pointsType.getGeometryDescriptor().getLocalName()), ff.literal(geometry));
    collection = pointsSource.getFeatures(new Query(pointsName, filter));
    actual = Sets.newHashSet();
    for (SimpleFeature f : toList(collection)) {
        actual.add(f.getAttributes());
    }
    expected = ImmutableSet.of(((SimpleFeature) points1).getAttributes(), ((SimpleFeature) points2).getAttributes());
    assertEquals(expected.size(), actual.size());
    assertEquals(expected, actual);
    filter = ECQL.toFilter("sp = 'StringProp2_3' OR ip = 2000");
    collection = linesSource.getFeatures(new Query(linesName, filter));
    actual = Sets.newHashSet();
    for (SimpleFeature f : toList(collection)) {
        actual.add(f.getAttributes());
    }
    expected = ImmutableSet.of(((SimpleFeature) lines2).getAttributes(), ((SimpleFeature) lines3).getAttributes());
    assertEquals(expected, actual);
}
Also used : ReferencedEnvelope(org.geotools.geometry.jts.ReferencedEnvelope) Query(org.geotools.data.Query) Filter(org.opengis.filter.Filter) List(java.util.List) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) Polygon(com.vividsolutions.jts.geom.Polygon) SimpleFeature(org.opengis.feature.simple.SimpleFeature) SimpleFeatureCollection(org.geotools.data.simple.SimpleFeatureCollection) Test(org.junit.Test)

Example 24 with SimpleFeatureCollection

use of org.geotools.data.simple.SimpleFeatureCollection in project GeoGig by boundlessgeo.

the class ExportOpTest method testExportingUsingFunction.

@Test
public void testExportingUsingFunction() throws Exception {
    // Testing export of points feature type into a simplified feature type that
    // does not contain the integer attribute.
    String simplifiedPointsName = "simplifiedPoints";
    String simplifiedPointsTypeSpec = "sp:String,pp:Point:srid=4326";
    SimpleFeatureType simplifiedPointsType = DataUtilities.createType(pointsNs, simplifiedPointsName, simplifiedPointsTypeSpec);
    Feature simplifiedPoints1 = feature(simplifiedPointsType, ((SimpleFeature) points1).getID(), ((SimpleFeature) points1).getAttribute(0), ((SimpleFeature) points1).getAttribute(2));
    Feature simplifiedPoints2 = feature(simplifiedPointsType, ((SimpleFeature) points2).getID(), ((SimpleFeature) points2).getAttribute(0), ((SimpleFeature) points2).getAttribute(2));
    Feature simplifiedPoints3 = feature(simplifiedPointsType, ((SimpleFeature) points3).getID(), ((SimpleFeature) points3).getAttribute(0), ((SimpleFeature) points3).getAttribute(2));
    Feature[] simplifiedPoints = new Feature[] { simplifiedPoints1, simplifiedPoints2, simplifiedPoints3 };
    final SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(simplifiedPointsType);
    Function<Feature, Optional<Feature>> function = new Function<Feature, Optional<Feature>>() {

        @Override
        @Nullable
        public Optional<Feature> apply(@Nullable Feature feature) {
            SimpleFeature simpleFeature = (SimpleFeature) feature;
            featureBuilder.add(simpleFeature.getAttribute(0));
            featureBuilder.add(simpleFeature.getAttribute(2));
            return Optional.of((Feature) featureBuilder.buildFeature(null));
        }
    };
    Feature[] points = new Feature[] { points1, points2, points3 };
    for (Feature feature : points) {
        insert(feature);
    }
    MemoryDataStore dataStore = new MemoryDataStore(simplifiedPointsType);
    final String typeName = dataStore.getTypeNames()[0];
    SimpleFeatureSource featureSource = dataStore.getFeatureSource(typeName);
    SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
    geogig.command(ExportOp.class).setFeatureStore(featureStore).setPath(pointsName).setFeatureTypeConversionFunction(function).call();
    featureSource = dataStore.getFeatureSource(typeName);
    featureStore = (SimpleFeatureStore) featureSource;
    SimpleFeatureCollection featureCollection = featureStore.getFeatures();
    assertEquals(featureCollection.size(), points.length);
    SimpleFeatureIterator features = featureCollection.features();
    assertTrue(collectionsAreEqual(features, simplifiedPoints));
    // check for exceptions when using a function that returns features with a wrong featuretype
    try {
        String wrongFeaturesName = "wrongFeatures";
        String wrongFeaturesTypeSpec = "sp:String";
        SimpleFeatureType wrongFeaturesType = DataUtilities.createType(pointsNs, wrongFeaturesName, wrongFeaturesTypeSpec);
        final SimpleFeatureBuilder wrongFeatureBuilder = new SimpleFeatureBuilder(wrongFeaturesType);
        Function<Feature, Optional<Feature>> wrongFunction = new Function<Feature, Optional<Feature>>() {

            @Override
            @Nullable
            public Optional<Feature> apply(@Nullable Feature feature) {
                SimpleFeature simpleFeature = (SimpleFeature) feature;
                wrongFeatureBuilder.add(simpleFeature.getAttribute(0));
                return Optional.of((Feature) wrongFeatureBuilder.buildFeature(null));
            }
        };
        geogig.command(ExportOp.class).setFeatureStore(featureStore).setPath(pointsName).setFeatureTypeConversionFunction(wrongFunction).call();
        fail();
    } catch (GeoToolsOpException e) {
        assertEquals(e.statusCode, StatusCode.UNABLE_TO_ADD);
    }
}
Also used : Optional(com.google.common.base.Optional) SimpleFeatureSource(org.geotools.data.simple.SimpleFeatureSource) MemoryDataStore(org.geotools.data.memory.MemoryDataStore) Feature(org.opengis.feature.Feature) SimpleFeature(org.opengis.feature.simple.SimpleFeature) SimpleFeature(org.opengis.feature.simple.SimpleFeature) SimpleFeatureCollection(org.geotools.data.simple.SimpleFeatureCollection) Function(com.google.common.base.Function) SimpleFeatureIterator(org.geotools.data.simple.SimpleFeatureIterator) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) SimpleFeatureStore(org.geotools.data.simple.SimpleFeatureStore) Nullable(javax.annotation.Nullable) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder) Test(org.junit.Test)

Example 25 with SimpleFeatureCollection

use of org.geotools.data.simple.SimpleFeatureCollection in project GeoGig by boundlessgeo.

the class ExportOpTest method testExportWithAlterUsingFeatureTypeId.

@Test
public void testExportWithAlterUsingFeatureTypeId() throws Exception {
    Feature[] points = new Feature[] { points2, points1B, points3 };
    for (Feature feature : points) {
        insert(feature);
    }
    MemoryDataStore dataStore = new MemoryDataStore(modifiedPointsType);
    final String typeName = dataStore.getTypeNames()[0];
    SimpleFeatureSource featureSource = dataStore.getFeatureSource(typeName);
    SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
    geogig.command(ExportOp.class).setFeatureStore(featureStore).setPath(pointsName).setAlter(true).setFilterFeatureTypeId(RevFeatureTypeImpl.build(modifiedPointsType).getId()).call();
    featureSource = dataStore.getFeatureSource(typeName);
    featureStore = (SimpleFeatureStore) featureSource;
    SimpleFeatureCollection featureCollection = featureStore.getFeatures();
    assertEquals(featureCollection.size(), points.length);
    SimpleFeatureIterator features = featureCollection.features();
    while (features.hasNext()) {
        List<Object> attributes = features.next().getAttributes();
        assertEquals(4, attributes.size());
    }
}
Also used : SimpleFeatureIterator(org.geotools.data.simple.SimpleFeatureIterator) SimpleFeatureSource(org.geotools.data.simple.SimpleFeatureSource) SimpleFeatureStore(org.geotools.data.simple.SimpleFeatureStore) MemoryDataStore(org.geotools.data.memory.MemoryDataStore) Feature(org.opengis.feature.Feature) SimpleFeature(org.opengis.feature.simple.SimpleFeature) SimpleFeatureCollection(org.geotools.data.simple.SimpleFeatureCollection) Test(org.junit.Test)

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