Search in sources :

Example 6 with SimpleFeatureTypeBuilder

use of org.geotools.feature.simple.SimpleFeatureTypeBuilder in project GeoGig by boundlessgeo.

the class ImportOp method overrideGeometryName.

private SimpleFeatureType overrideGeometryName(SimpleFeatureType featureType) {
    if (geomName == null) {
        return featureType;
    }
    SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
    List<AttributeDescriptor> newAttributes = Lists.newArrayList();
    String oldGeomName = featureType.getGeometryDescriptor().getName().getLocalPart();
    Collection<AttributeDescriptor> descriptors = featureType.getAttributeDescriptors();
    for (AttributeDescriptor descriptor : descriptors) {
        String name = descriptor.getName().getLocalPart();
        Preconditions.checkArgument(!name.equals(geomName), "The provided geom name is already in use by another attribute");
        if (name.equals(oldGeomName)) {
            AttributeDescriptorImpl newDescriptor = new AttributeDescriptorImpl(descriptor.getType(), new NameImpl(geomName), descriptor.getMinOccurs(), descriptor.getMaxOccurs(), descriptor.isNillable(), descriptor.getDefaultValue());
            newAttributes.add(newDescriptor);
        } else {
            newAttributes.add(descriptor);
        }
    }
    builder.setAttributes(newAttributes);
    builder.setName(featureType.getName());
    builder.setCRS(featureType.getCoordinateReferenceSystem());
    featureType = builder.buildFeatureType();
    return featureType;
}
Also used : NameImpl(org.geotools.feature.NameImpl) SimpleFeatureTypeBuilder(org.geotools.feature.simple.SimpleFeatureTypeBuilder) AttributeDescriptor(org.opengis.feature.type.AttributeDescriptor) AttributeDescriptorImpl(org.geotools.feature.type.AttributeDescriptorImpl)

Example 7 with SimpleFeatureTypeBuilder

use of org.geotools.feature.simple.SimpleFeatureTypeBuilder in project GeoGig by boundlessgeo.

the class GeogigFeatureSource method buildFeatureType.

@Override
protected SimpleFeatureType buildFeatureType() throws IOException {
    SimpleFeatureType featureType = getNativeType();
    final Name name = featureType.getName();
    final Name assignedName = getEntry().getName();
    if (assignedName.getNamespaceURI() != null && !assignedName.equals(name)) {
        SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
        builder.init(featureType);
        builder.setName(assignedName);
        featureType = builder.buildFeatureType();
    }
    return featureType;
}
Also used : SimpleFeatureTypeBuilder(org.geotools.feature.simple.SimpleFeatureTypeBuilder) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) Name(org.opengis.feature.type.Name)

Example 8 with SimpleFeatureTypeBuilder

use of org.geotools.feature.simple.SimpleFeatureTypeBuilder in project GeoGig by boundlessgeo.

the class ExportDiffOpTest method testExportDiffUsingOldVersion.

@Test
public void testExportDiffUsingOldVersion() throws Exception {
    insertAndAdd(points1);
    final RevCommit insertCommit = geogig.command(CommitOp.class).setAll(true).call();
    final String featureId = points1.getIdentifier().getID();
    final Feature modifiedFeature = feature((SimpleFeatureType) points1.getType(), featureId, "changedProp", new Integer(1500));
    insertAndAdd(modifiedFeature, points2);
    final RevCommit changeCommit = geogig.command(CommitOp.class).setAll(true).call();
    Feature[] points = new Feature[] { points1 };
    SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
    builder.add("geogig_fid", String.class);
    for (AttributeDescriptor descriptor : pointsType.getAttributeDescriptors()) {
        builder.add(descriptor);
    }
    builder.setName(pointsType.getName());
    builder.setCRS(pointsType.getCoordinateReferenceSystem());
    SimpleFeatureType outputFeatureType = builder.buildFeatureType();
    MemoryDataStore dataStore = new MemoryDataStore(outputFeatureType);
    final String typeName = dataStore.getTypeNames()[0];
    SimpleFeatureSource featureSource = dataStore.getFeatureSource(typeName);
    SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
    geogig.command(ExportDiffOp.class).setFeatureStore(featureStore).setPath(pointsName).setNewRef(changeCommit.getId().toString()).setOldRef(insertCommit.getId().toString()).setUseOld(true).call();
    featureSource = dataStore.getFeatureSource(typeName);
    featureStore = (SimpleFeatureStore) featureSource;
    SimpleFeatureCollection featureCollection = featureStore.getFeatures();
    assertEquals(featureCollection.size(), points.length);
    SimpleFeatureIterator features = featureCollection.features();
    assertTrue(collectionsAreEqual(features, points));
}
Also used : SimpleFeatureTypeBuilder(org.geotools.feature.simple.SimpleFeatureTypeBuilder) SimpleFeatureSource(org.geotools.data.simple.SimpleFeatureSource) MemoryDataStore(org.geotools.data.memory.MemoryDataStore) AttributeDescriptor(org.opengis.feature.type.AttributeDescriptor) Feature(org.opengis.feature.Feature) SimpleFeature(org.opengis.feature.simple.SimpleFeature) SimpleFeatureCollection(org.geotools.data.simple.SimpleFeatureCollection) SimpleFeatureIterator(org.geotools.data.simple.SimpleFeatureIterator) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) SimpleFeatureStore(org.geotools.data.simple.SimpleFeatureStore) RevCommit(org.locationtech.geogig.api.RevCommit) Test(org.junit.Test)

Example 9 with SimpleFeatureTypeBuilder

use of org.geotools.feature.simple.SimpleFeatureTypeBuilder in project GeoGig by boundlessgeo.

the class ExportDiffOpTest method testExportDiff.

@Test
public void testExportDiff() throws Exception {
    insertAndAdd(points1);
    final RevCommit insertCommit = geogig.command(CommitOp.class).setAll(true).call();
    final String featureId = points1.getIdentifier().getID();
    final Feature modifiedFeature = feature((SimpleFeatureType) points1.getType(), featureId, "changedProp", new Integer(1500));
    insertAndAdd(modifiedFeature, points2);
    final RevCommit changeCommit = geogig.command(CommitOp.class).setAll(true).call();
    Feature[] points = new Feature[] { modifiedFeature, points2 };
    SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
    builder.add("geogig_fid", String.class);
    for (AttributeDescriptor descriptor : pointsType.getAttributeDescriptors()) {
        builder.add(descriptor);
    }
    builder.setName(pointsType.getName());
    builder.setCRS(pointsType.getCoordinateReferenceSystem());
    SimpleFeatureType outputFeatureType = builder.buildFeatureType();
    MemoryDataStore dataStore = new MemoryDataStore(outputFeatureType);
    final String typeName = dataStore.getTypeNames()[0];
    SimpleFeatureSource featureSource = dataStore.getFeatureSource(typeName);
    SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
    geogig.command(ExportDiffOp.class).setFeatureStore(featureStore).setPath(pointsName).setNewRef(changeCommit.getId().toString()).setOldRef(insertCommit.getId().toString()).call();
    featureSource = dataStore.getFeatureSource(typeName);
    featureStore = (SimpleFeatureStore) featureSource;
    SimpleFeatureCollection featureCollection = featureStore.getFeatures();
    assertEquals(featureCollection.size(), points.length);
    SimpleFeatureIterator features = featureCollection.features();
    assertTrue(collectionsAreEqual(features, points));
}
Also used : SimpleFeatureTypeBuilder(org.geotools.feature.simple.SimpleFeatureTypeBuilder) SimpleFeatureSource(org.geotools.data.simple.SimpleFeatureSource) MemoryDataStore(org.geotools.data.memory.MemoryDataStore) AttributeDescriptor(org.opengis.feature.type.AttributeDescriptor) Feature(org.opengis.feature.Feature) SimpleFeature(org.opengis.feature.simple.SimpleFeature) SimpleFeatureCollection(org.geotools.data.simple.SimpleFeatureCollection) SimpleFeatureIterator(org.geotools.data.simple.SimpleFeatureIterator) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) SimpleFeatureStore(org.geotools.data.simple.SimpleFeatureStore) RevCommit(org.locationtech.geogig.api.RevCommit) Test(org.junit.Test)

Example 10 with SimpleFeatureTypeBuilder

use of org.geotools.feature.simple.SimpleFeatureTypeBuilder in project GeoGig by boundlessgeo.

the class ImportOpTest method testImportAllWithDifferentFeatureTypesAndDestPathAndAdd.

@Test
public void testImportAllWithDifferentFeatureTypesAndDestPathAndAdd() throws Exception {
    SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
    builder.setCRS(CRS.decode("EPSG:4326"));
    builder.add("geom", Point.class);
    builder.add("label", String.class);
    builder.setName("dest");
    SimpleFeatureType type = builder.buildFeatureType();
    GeometryFactory gf = new GeometryFactory();
    SimpleFeature feature = SimpleFeatureBuilder.build(type, new Object[] { gf.createPoint(new Coordinate(0, 0)), "feature0" }, "feature");
    geogig.getRepository().workingTree().insert("dest", feature);
    ImportOp importOp = geogig.command(ImportOp.class);
    importOp.setDataStore(TestHelper.createTestFactory().createDataStore(null));
    importOp.setAll(true);
    importOp.setOverwrite(false);
    importOp.setDestinationPath("dest");
    importOp.setAdaptToDefaultFeatureType(false);
    importOp.call();
    Iterator<NodeRef> features = geogig.command(LsTreeOp.class).setStrategy(Strategy.DEPTHFIRST_ONLY_FEATURES).call();
    ArrayList<NodeRef> list = Lists.newArrayList(features);
    assertEquals(5, list.size());
    TreeSet<ObjectId> set = Sets.newTreeSet();
    ArrayList<RevFeatureType> ftlist = new ArrayList<RevFeatureType>();
    for (NodeRef node : list) {
        Optional<RevFeatureType> ft = geogig.command(RevObjectParse.class).setObjectId(node.getMetadataId()).call(RevFeatureType.class);
        assertTrue(ft.isPresent());
        ftlist.add(ft.get());
        set.add(node.getMetadataId());
    }
    assertEquals(4, set.size());
}
Also used : SimpleFeatureTypeBuilder(org.geotools.feature.simple.SimpleFeatureTypeBuilder) GeometryFactory(com.vividsolutions.jts.geom.GeometryFactory) ObjectId(org.locationtech.geogig.api.ObjectId) ArrayList(java.util.ArrayList) SimpleFeature(org.opengis.feature.simple.SimpleFeature) NodeRef(org.locationtech.geogig.api.NodeRef) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) Coordinate(com.vividsolutions.jts.geom.Coordinate) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) Test(org.junit.Test)

Aggregations

SimpleFeatureTypeBuilder (org.geotools.feature.simple.SimpleFeatureTypeBuilder)17 SimpleFeatureType (org.opengis.feature.simple.SimpleFeatureType)12 SimpleFeature (org.opengis.feature.simple.SimpleFeature)7 AttributeDescriptor (org.opengis.feature.type.AttributeDescriptor)6 Test (org.junit.Test)5 Coordinate (com.vividsolutions.jts.geom.Coordinate)4 MemoryDataStore (org.geotools.data.memory.MemoryDataStore)4 GeometryFactory (com.vividsolutions.jts.geom.GeometryFactory)3 Serializable (java.io.Serializable)3 SimpleFeatureSource (org.geotools.data.simple.SimpleFeatureSource)3 SimpleFeatureStore (org.geotools.data.simple.SimpleFeatureStore)3 SimpleFeatureBuilder (org.geotools.feature.simple.SimpleFeatureBuilder)3 Feature (org.opengis.feature.Feature)3 Point (com.vividsolutions.jts.geom.Point)2 AbstractDataStoreFactory (org.geotools.data.AbstractDataStoreFactory)2 SimpleFeatureCollection (org.geotools.data.simple.SimpleFeatureCollection)2 SimpleFeatureIterator (org.geotools.data.simple.SimpleFeatureIterator)2 NameImpl (org.geotools.feature.NameImpl)2 RevCommit (org.locationtech.geogig.api.RevCommit)2 RevFeatureType (org.locationtech.geogig.api.RevFeatureType)2