Search in sources :

Example 71 with SimpleFeature

use of org.opengis.feature.simple.SimpleFeature 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 72 with SimpleFeature

use of org.opengis.feature.simple.SimpleFeature in project GeoGig by boundlessgeo.

the class GeoGigFeatureStoreTest method testTransactionCommitAuthorAndEmail.

@Test
public void testTransactionCommitAuthorAndEmail() throws Exception {
    FeatureCollection<SimpleFeatureType, SimpleFeature> collection;
    collection = DataUtilities.collection(Arrays.asList((SimpleFeature) points1, (SimpleFeature) points2, (SimpleFeature) points3));
    DefaultTransaction tx = new DefaultTransaction();
    points.setTransaction(tx);
    assertSame(tx, points.getTransaction());
    try {
        points.addFeatures(collection);
        tx.putProperty(GeogigTransactionState.VERSIONING_COMMIT_AUTHOR, "john");
        tx.putProperty(GeogigTransactionState.VERSIONING_COMMIT_MESSAGE, "test message");
        tx.putProperty("fullname", "John Doe");
        tx.putProperty("email", "jd@example.com");
        tx.commit();
        assertEquals(3, dataStore.getFeatureSource(pointsTypeName).getFeatures().size());
    } catch (Exception e) {
        tx.rollback();
        throw e;
    } finally {
        tx.close();
    }
    List<RevCommit> commits = toList(geogig.command(LogOp.class).call());
    assertFalse(commits.isEmpty());
    assertTrue(commits.get(0).getAuthor().getName().isPresent());
    assertEquals("John Doe", commits.get(0).getAuthor().getName().orNull());
    assertEquals("jd@example.com", commits.get(0).getAuthor().getEmail().orNull());
}
Also used : SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) SimpleFeature(org.opengis.feature.simple.SimpleFeature) DefaultTransaction(org.geotools.data.DefaultTransaction) RevCommit(org.locationtech.geogig.api.RevCommit) Test(org.junit.Test)

Example 73 with SimpleFeature

use of org.opengis.feature.simple.SimpleFeature in project GeoGig by boundlessgeo.

the class GeoGigFeatureStoreTest method testAddFeaturesWhileNotOnABranch.

@Test
public void testAddFeaturesWhileNotOnABranch() throws Exception {
    boolean gotIllegalStateException = false;
    final ObjectId head = geogig.command(RevParse.class).setRefSpec("HEAD").call().get();
    dataStore.setHead(head.toString());
    FeatureCollection<SimpleFeatureType, SimpleFeature> collection;
    collection = DataUtilities.collection(Arrays.asList((SimpleFeature) points1, (SimpleFeature) points2, (SimpleFeature) points3));
    Transaction tx = new DefaultTransaction();
    points.setTransaction(tx);
    assertSame(tx, points.getTransaction());
    try {
        List<FeatureId> addedFeatures = points.addFeatures(collection);
        assertNotNull(addedFeatures);
        assertEquals(3, addedFeatures.size());
        // assert transaction isolation
        assertEquals(3, points.getFeatures().size());
        assertEquals(0, dataStore.getFeatureSource(pointsTypeName).getFeatures().size());
        tx.commit();
        assertEquals(3, dataStore.getFeatureSource(pointsTypeName).getFeatures().size());
    } catch (IllegalStateException e) {
        tx.rollback();
        gotIllegalStateException = true;
    } catch (Exception e) {
        tx.rollback();
        throw e;
    } finally {
        tx.close();
    }
    assertTrue("Should throw IllegalStateException when trying to modify data in geogig datastore when it is not configured with a branch.", gotIllegalStateException);
}
Also used : FeatureId(org.opengis.filter.identity.FeatureId) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) Transaction(org.geotools.data.Transaction) DefaultTransaction(org.geotools.data.DefaultTransaction) ObjectId(org.locationtech.geogig.api.ObjectId) RevParse(org.locationtech.geogig.api.plumbing.RevParse) SimpleFeature(org.opengis.feature.simple.SimpleFeature) DefaultTransaction(org.geotools.data.DefaultTransaction) Test(org.junit.Test)

Example 74 with SimpleFeature

use of org.opengis.feature.simple.SimpleFeature 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 75 with SimpleFeature

use of org.opengis.feature.simple.SimpleFeature in project GeoGig by boundlessgeo.

the class TestHelper method createTestFactory.

public static AbstractDataStoreFactory createTestFactory() throws Exception {
    SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
    builder.setCRS(CRS.decode("EPSG:4326"));
    builder.add("geom", Point.class);
    builder.add("label", String.class);
    builder.setName("table1");
    SimpleFeatureType type = builder.buildFeatureType();
    SimpleFeatureTypeBuilder builder2 = new SimpleFeatureTypeBuilder();
    builder2.setCRS(CRS.decode("EPSG:4326"));
    builder2.add("geom", Point.class);
    builder2.add("name", String.class);
    builder2.setName("table2");
    SimpleFeatureType type2 = builder2.buildFeatureType();
    SimpleFeatureTypeBuilder builder3 = new SimpleFeatureTypeBuilder();
    builder3.setCRS(CRS.decode("EPSG:4326"));
    builder3.add("geom", Point.class);
    builder3.add("name", String.class);
    builder3.add("number", Long.class);
    builder3.setName("table3");
    SimpleFeatureTypeBuilder builder4 = new SimpleFeatureTypeBuilder();
    builder4.setCRS(CRS.decode("EPSG:4326"));
    builder4.add("geom", Point.class);
    builder4.add("number", Double.class);
    builder4.setName("table4");
    // A table with a shp-like structure
    SimpleFeatureTypeBuilder builderShp = new SimpleFeatureTypeBuilder();
    builderShp.setCRS(CRS.decode("EPSG:4326"));
    builderShp.add("the_geom", Point.class);
    builderShp.add("number", Double.class);
    builderShp.add("number2", Double.class);
    builderShp.setName("shpLikeTable");
    SimpleFeatureTypeBuilder builderShp2 = new SimpleFeatureTypeBuilder();
    builderShp2.setCRS(CRS.decode("EPSG:4326"));
    builderShp2.add("the_geom", Point.class);
    builderShp2.add("number", Double.class);
    builderShp2.add("number2", Integer.class);
    builderShp2.setName("shpLikeTable2");
    // A table with a geojson-like structure
    SimpleFeatureTypeBuilder builderGeoJson = new SimpleFeatureTypeBuilder();
    builderGeoJson.setCRS(CRS.decode("EPSG:4326"));
    builderGeoJson.add("number", Double.class);
    builderGeoJson.add("number2", Double.class);
    builderGeoJson.add("geom", Point.class);
    builderGeoJson.setName("GeoJsonLikeTable");
    SimpleFeatureTypeBuilder builderGeoJson2 = new SimpleFeatureTypeBuilder();
    builderGeoJson2.setCRS(CRS.decode("EPSG:23030"));
    builderGeoJson2.add("number", Double.class);
    builderGeoJson2.add("number2", Double.class);
    builderGeoJson2.add("geom", Point.class);
    builderGeoJson2.setName("GeoJsonLikeTable2");
    SimpleFeatureType type3 = builder3.buildFeatureType();
    SimpleFeatureType typeShp = builderShp.buildFeatureType();
    SimpleFeatureType typeShp2 = builderShp2.buildFeatureType();
    SimpleFeatureType typeGeoJson = builderGeoJson.buildFeatureType();
    SimpleFeatureType typeGeoJson2 = builderGeoJson2.buildFeatureType();
    GeometryFactory gf = new GeometryFactory();
    SimpleFeature f1 = SimpleFeatureBuilder.build(type, new Object[] { gf.createPoint(new Coordinate(5, 8)), "feature1" }, "table1.feature1");
    SimpleFeature f2 = SimpleFeatureBuilder.build(type, new Object[] { gf.createPoint(new Coordinate(5, 4)), "feature2" }, "table1.feature2");
    SimpleFeature f3 = SimpleFeatureBuilder.build(type2, new Object[] { gf.createPoint(new Coordinate(3, 2)), "feature3" }, "table2.feature3");
    SimpleFeature f4 = SimpleFeatureBuilder.build(type3, new Object[] { gf.createPoint(new Coordinate(0, 5)), "feature4", 1000 }, "table2.feature4");
    SimpleFeature f5 = SimpleFeatureBuilder.build(typeShp, new Object[] { gf.createPoint(new Coordinate(0, 6)), 2.2, 1000 }, "feature1");
    SimpleFeature f6 = SimpleFeatureBuilder.build(typeShp2, new Object[] { gf.createPoint(new Coordinate(0, 7)), 3.2, 1100.0 }, "feature1");
    SimpleFeature f7 = SimpleFeatureBuilder.build(typeGeoJson, new Object[] { 4.2, 1200, gf.createPoint(new Coordinate(0, 8)) }, "feature1");
    SimpleFeature f8 = SimpleFeatureBuilder.build(typeGeoJson2, new Object[] { 4.2, 1200, gf.createPoint(new Coordinate(0, 9)) }, "feature1");
    MemoryDataStore testDataStore = new MemoryDataStore();
    testDataStore.addFeature(f1);
    testDataStore.addFeature(f2);
    testDataStore.addFeature(f3);
    testDataStore.addFeature(f4);
    testDataStore.addFeature(f5);
    testDataStore.addFeature(f6);
    testDataStore.addFeature(f7);
    testDataStore.addFeature(f8);
    testDataStore.createSchema(builder4.buildFeatureType());
    final AbstractDataStoreFactory factory = mock(AbstractDataStoreFactory.class);
    when(factory.createDataStore(anyMapOf(String.class, Serializable.class))).thenReturn(testDataStore);
    when(factory.canProcess(anyMapOf(String.class, Serializable.class))).thenReturn(true);
    return factory;
}
Also used : AbstractDataStoreFactory(org.geotools.data.AbstractDataStoreFactory) Serializable(java.io.Serializable) SimpleFeatureTypeBuilder(org.geotools.feature.simple.SimpleFeatureTypeBuilder) GeometryFactory(com.vividsolutions.jts.geom.GeometryFactory) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) Coordinate(com.vividsolutions.jts.geom.Coordinate) MemoryDataStore(org.geotools.data.memory.MemoryDataStore) Matchers.anyString(org.mockito.Matchers.anyString) SimpleFeature(org.opengis.feature.simple.SimpleFeature)

Aggregations

SimpleFeature (org.opengis.feature.simple.SimpleFeature)147 SimpleFeatureType (org.opengis.feature.simple.SimpleFeatureType)73 Test (org.junit.Test)45 SimpleFeatureBuilder (org.geotools.feature.simple.SimpleFeatureBuilder)35 IOException (java.io.IOException)34 ArrayList (java.util.ArrayList)26 SimpleFeatureIterator (org.geotools.data.simple.SimpleFeatureIterator)25 Geometry (com.vividsolutions.jts.geom.Geometry)23 Coordinate (com.vividsolutions.jts.geom.Coordinate)22 SimpleFeatureCollection (org.geotools.data.simple.SimpleFeatureCollection)20 File (java.io.File)19 HashMap (java.util.HashMap)18 Query (org.geotools.data.Query)18 RevFeature (org.locationtech.geogig.api.RevFeature)17 DefaultTransaction (org.geotools.data.DefaultTransaction)16 RevFeatureType (org.locationtech.geogig.api.RevFeatureType)16 Optional (com.google.common.base.Optional)15 SimpleFeatureSource (org.geotools.data.simple.SimpleFeatureSource)15 Filter (org.opengis.filter.Filter)15 Transaction (org.geotools.data.Transaction)14