Search in sources :

Example 6 with AttributeDescriptor

use of org.opengis.feature.type.AttributeDescriptor 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 7 with AttributeDescriptor

use of org.opengis.feature.type.AttributeDescriptor in project GeoGig by boundlessgeo.

the class ApplyPatchOp method getFeatureType.

private RevFeatureType getFeatureType(FeatureDiff diff, RevFeature oldFeature, RevFeatureType oldRevFeatureType) {
    List<String> removed = Lists.newArrayList();
    List<AttributeDescriptor> added = Lists.newArrayList();
    Set<Entry<PropertyDescriptor, AttributeDiff>> featureDiffs = diff.getDiffs().entrySet();
    for (Iterator<Entry<PropertyDescriptor, AttributeDiff>> iterator = featureDiffs.iterator(); iterator.hasNext(); ) {
        Entry<PropertyDescriptor, AttributeDiff> entry = iterator.next();
        if (entry.getValue().getType() == TYPE.REMOVED) {
            removed.add(entry.getKey().getName().getLocalPart());
        } else if (entry.getValue().getType() == TYPE.ADDED) {
            PropertyDescriptor pd = entry.getKey();
            added.add((AttributeDescriptor) pd);
        }
    }
    SimpleFeatureType sft = (SimpleFeatureType) oldRevFeatureType.type();
    List<AttributeDescriptor> descriptors = (sft).getAttributeDescriptors();
    SimpleFeatureTypeBuilder featureTypeBuilder = new SimpleFeatureTypeBuilder();
    featureTypeBuilder.setCRS(sft.getCoordinateReferenceSystem());
    featureTypeBuilder.setDefaultGeometry(sft.getGeometryDescriptor().getLocalName());
    featureTypeBuilder.setName(sft.getName());
    for (int i = 0; i < descriptors.size(); i++) {
        AttributeDescriptor descriptor = descriptors.get(i);
        if (!removed.contains(descriptor.getName().getLocalPart())) {
            featureTypeBuilder.add(descriptor);
        }
    }
    for (AttributeDescriptor descriptor : added) {
        featureTypeBuilder.add(descriptor);
    }
    SimpleFeatureType featureType = featureTypeBuilder.buildFeatureType();
    return RevFeatureTypeImpl.build(featureType);
}
Also used : SimpleFeatureTypeBuilder(org.geotools.feature.simple.SimpleFeatureTypeBuilder) PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) AttributeDescriptor(org.opengis.feature.type.AttributeDescriptor) Entry(java.util.Map.Entry) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) AttributeDiff(org.locationtech.geogig.api.plumbing.diff.AttributeDiff)

Example 8 with AttributeDescriptor

use of org.opengis.feature.type.AttributeDescriptor in project GeoGig by boundlessgeo.

the class ShpExportDiff method runInternal.

/**
     * Executes the export command using the provided options.
     */
@Override
protected void runInternal(GeogigCLI cli) throws IOException {
    if (args.size() != 4) {
        printUsage(cli);
        throw new CommandFailedException();
    }
    String commitOld = args.get(0);
    String commitNew = args.get(1);
    String path = args.get(2);
    String shapefile = args.get(3);
    ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
    File file = new File(shapefile);
    if (file.exists() && !overwrite) {
        throw new CommandFailedException("The selected shapefile already exists. Use -o to overwrite");
    }
    Map<String, Serializable> params = new HashMap<String, Serializable>();
    params.put(ShapefileDataStoreFactory.URLP.key, file.toURI().toURL());
    params.put(ShapefileDataStoreFactory.CREATE_SPATIAL_INDEX.key, Boolean.FALSE);
    params.put(ShapefileDataStoreFactory.ENABLE_SPATIAL_INDEX.key, Boolean.FALSE);
    ShapefileDataStore dataStore = (ShapefileDataStore) dataStoreFactory.createNewDataStore(params);
    SimpleFeatureType outputFeatureType;
    try {
        outputFeatureType = getFeatureType(path, cli);
    } catch (GeoToolsOpException e) {
        cli.getConsole().println("No features to export.");
        return;
    }
    SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
    builder.add("geogig_fid", String.class);
    for (AttributeDescriptor descriptor : outputFeatureType.getAttributeDescriptors()) {
        builder.add(descriptor);
    }
    builder.setName(outputFeatureType.getName());
    builder.setCRS(outputFeatureType.getCoordinateReferenceSystem());
    outputFeatureType = builder.buildFeatureType();
    dataStore.createSchema(outputFeatureType);
    final String typeName = dataStore.getTypeNames()[0];
    final SimpleFeatureSource featureSource = dataStore.getFeatureSource(typeName);
    if (!(featureSource instanceof SimpleFeatureStore)) {
        throw new CommandFailedException("Could not create feature store.");
    }
    final SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
    Function<Feature, Optional<Feature>> function = getTransformingFunction(dataStore.getSchema());
    ExportDiffOp op = cli.getGeogig().command(ExportDiffOp.class).setFeatureStore(featureStore).setPath(path).setOldRef(commitOld).setNewRef(commitNew).setUseOld(old).setTransactional(false).setFeatureTypeConversionFunction(function);
    try {
        op.setProgressListener(cli.getProgressListener()).call();
    } catch (IllegalArgumentException iae) {
        throw new org.locationtech.geogig.cli.InvalidParameterException(iae.getMessage(), iae);
    } catch (GeoToolsOpException e) {
        file.delete();
        switch(e.statusCode) {
            case MIXED_FEATURE_TYPES:
                throw new CommandFailedException("Error: The selected tree contains mixed feature types.", e);
            default:
                throw new CommandFailedException("Could not export. Error:" + e.statusCode.name(), e);
        }
    }
    cli.getConsole().println(path + " exported successfully to " + shapefile);
}
Also used : Serializable(java.io.Serializable) ShapefileDataStore(org.geotools.data.shapefile.ShapefileDataStore) SimpleFeatureTypeBuilder(org.geotools.feature.simple.SimpleFeatureTypeBuilder) Optional(com.google.common.base.Optional) HashMap(java.util.HashMap) SimpleFeatureSource(org.geotools.data.simple.SimpleFeatureSource) ExportDiffOp(org.locationtech.geogig.geotools.plumbing.ExportDiffOp) AttributeDescriptor(org.opengis.feature.type.AttributeDescriptor) InvalidParameterException(org.locationtech.geogig.cli.InvalidParameterException) Feature(org.opengis.feature.Feature) CommandFailedException(org.locationtech.geogig.cli.CommandFailedException) GeoToolsOpException(org.locationtech.geogig.geotools.plumbing.GeoToolsOpException) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) SimpleFeatureStore(org.geotools.data.simple.SimpleFeatureStore) ShapefileDataStoreFactory(org.geotools.data.shapefile.ShapefileDataStoreFactory) File(java.io.File)

Example 9 with AttributeDescriptor

use of org.opengis.feature.type.AttributeDescriptor in project GeoGig by boundlessgeo.

the class ShpImport method runInternal.

/**
     * Executes the import command using the provided options.
     */
@Override
protected void runInternal(GeogigCLI cli) throws IOException {
    checkParameter(shapeFile != null && !shapeFile.isEmpty(), "No shapefile specified");
    for (String shp : shapeFile) {
        DataStore dataStore = null;
        try {
            dataStore = getDataStore(shp);
        } catch (InvalidParameterException e) {
            cli.getConsole().println("The shapefile '" + shp + "' could not be found, skipping...");
            continue;
        }
        if (fidAttribute != null) {
            AttributeDescriptor attrib = dataStore.getSchema(dataStore.getNames().get(0)).getDescriptor(fidAttribute);
            if (attrib == null) {
                throw new InvalidParameterException("The specified attribute does not exist in the selected shapefile");
            }
        }
        try {
            cli.getConsole().println("Importing from shapefile " + shp);
            ProgressListener progressListener = cli.getProgressListener();
            ImportOp command = cli.getGeogig().command(ImportOp.class).setAll(true).setTable(null).setAlter(alter).setOverwrite(!add).setDestinationPath(destTable).setDataStore(dataStore).setFidAttribute(fidAttribute).setAdaptToDefaultFeatureType(!forceFeatureType);
            // force the import not to use paging due to a bug in the shapefile datastore
            command.setUsePaging(false);
            command.setProgressListener(progressListener).call();
            cli.getConsole().println(shp + " imported successfully.");
        } catch (GeoToolsOpException e) {
            switch(e.statusCode) {
                case NO_FEATURES_FOUND:
                    throw new CommandFailedException("No features were found in the shapefile.", e);
                case UNABLE_TO_GET_NAMES:
                    throw new CommandFailedException("Unable to get feature types from the shapefile.", e);
                case UNABLE_TO_GET_FEATURES:
                    throw new CommandFailedException("Unable to get features from the shapefile.", e);
                case UNABLE_TO_INSERT:
                    throw new CommandFailedException("Unable to insert features into the working tree.", e);
                case INCOMPATIBLE_FEATURE_TYPE:
                    throw new CommandFailedException("The feature type of the data to import does not match the feature type of the destination tree and cannot be imported\n" + "USe the --force-featuretype switch to import using the original featuretype and crete a mixed type tree", e);
                default:
                    throw new CommandFailedException("Import failed with exception: " + e.statusCode.name(), e);
            }
        } finally {
            dataStore.dispose();
            cli.getConsole().flush();
        }
    }
}
Also used : InvalidParameterException(org.locationtech.geogig.cli.InvalidParameterException) ProgressListener(org.locationtech.geogig.api.ProgressListener) DataStore(org.geotools.data.DataStore) AttributeDescriptor(org.opengis.feature.type.AttributeDescriptor) ImportOp(org.locationtech.geogig.geotools.plumbing.ImportOp) CommandFailedException(org.locationtech.geogig.cli.CommandFailedException) GeoToolsOpException(org.locationtech.geogig.geotools.plumbing.GeoToolsOpException)

Example 10 with AttributeDescriptor

use of org.opengis.feature.type.AttributeDescriptor 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)

Aggregations

AttributeDescriptor (org.opengis.feature.type.AttributeDescriptor)13 SimpleFeatureType (org.opengis.feature.simple.SimpleFeatureType)8 SimpleFeatureTypeBuilder (org.geotools.feature.simple.SimpleFeatureTypeBuilder)6 SimpleFeatureSource (org.geotools.data.simple.SimpleFeatureSource)3 SimpleFeatureStore (org.geotools.data.simple.SimpleFeatureStore)3 CommandFailedException (org.locationtech.geogig.cli.CommandFailedException)3 InvalidParameterException (org.locationtech.geogig.cli.InvalidParameterException)3 GeoToolsOpException (org.locationtech.geogig.geotools.plumbing.GeoToolsOpException)3 Feature (org.opengis.feature.Feature)3 Point (com.vividsolutions.jts.geom.Point)2 ArrayList (java.util.ArrayList)2 DataStore (org.geotools.data.DataStore)2 MemoryDataStore (org.geotools.data.memory.MemoryDataStore)2 SimpleFeatureCollection (org.geotools.data.simple.SimpleFeatureCollection)2 SimpleFeatureIterator (org.geotools.data.simple.SimpleFeatureIterator)2 Test (org.junit.Test)2 ProgressListener (org.locationtech.geogig.api.ProgressListener)2 RevCommit (org.locationtech.geogig.api.RevCommit)2 RevFeatureType (org.locationtech.geogig.api.RevFeatureType)2 RevFeatureTypeImpl (org.locationtech.geogig.api.RevFeatureTypeImpl)2