Search in sources :

Example 1 with AttributeDescriptor

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

the class GeogigSimpleFeature method validate.

@Override
public void validate() throws IllegalAttributeException {
    for (int i = 0; i < getAttributeCount(); i++) {
        AttributeDescriptor descriptor = getType().getDescriptor(i);
        Types.validate(descriptor, getAttribute(i));
    }
}
Also used : AttributeDescriptor(org.opengis.feature.type.AttributeDescriptor) Point(com.vividsolutions.jts.geom.Point)

Example 2 with AttributeDescriptor

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

the class GeoJsonImport method runInternal.

@Override
protected void runInternal(GeogigCLI cli) throws InvalidParameterException, CommandFailedException, IOException {
    checkParameter(geoJSONList != null && !geoJSONList.isEmpty(), "No GeoJSON specified");
    checkParameter(geomName == null || !geomNameAuto, "Cannot use --geom-name and --geom-name-auto at the same time");
    for (String geoJSON : geoJSONList) {
        DataStore dataStore = null;
        try {
            dataStore = getDataStore(geoJSON);
        } catch (InvalidParameterException e) {
            cli.getConsole().println("The GeoJSON file '" + geoJSON + "' 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 GeoJSON file");
            }
        }
        if (geomNameAuto) {
            String destPath = destTable;
            if (destPath == null) {
                destPath = dataStore.getSchema(dataStore.getNames().get(0)).getTypeName();
            }
            Optional<RevFeatureType> ft = cli.getGeogig().command(RevObjectParse.class).setRefSpec("WORK_HEAD:" + destPath).call(RevFeatureType.class);
            // attribute
            if (ft.isPresent()) {
                GeometryDescriptor geomDescriptor = ft.get().type().getGeometryDescriptor();
                if (geomDescriptor != null) {
                    geomName = geomDescriptor.getLocalName();
                }
            }
        }
        try {
            cli.getConsole().println("Importing from GeoJSON " + geoJSON);
            ProgressListener progressListener = cli.getProgressListener();
            cli.getGeogig().command(ImportOp.class).setAll(true).setTable(null).setAlter(alter).setOverwrite(!add).setDestinationPath(destTable).setDataStore(dataStore).setFidAttribute(fidAttribute).setGeometryNameOverride(geomName).setAdaptToDefaultFeatureType(!forceFeatureType).setProgressListener(progressListener).call();
            cli.getConsole().println(geoJSON + " imported successfully.");
        } catch (GeoToolsOpException e) {
            switch(e.statusCode) {
                case NO_FEATURES_FOUND:
                    throw new CommandFailedException("No features were found in the GeoJSON file.", e);
                case UNABLE_TO_GET_NAMES:
                    throw new CommandFailedException("Unable to get feature types from the GeoJSON file.", e);
                case UNABLE_TO_GET_FEATURES:
                    throw new CommandFailedException("Unable to get features from the GeoJSON file.", 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);
            }
        }
    }
}
Also used : InvalidParameterException(org.locationtech.geogig.cli.InvalidParameterException) GeometryDescriptor(org.opengis.feature.type.GeometryDescriptor) ProgressListener(org.locationtech.geogig.api.ProgressListener) DataStore(org.geotools.data.DataStore) AttributeDescriptor(org.opengis.feature.type.AttributeDescriptor) ImportOp(org.locationtech.geogig.geotools.plumbing.ImportOp) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) CommandFailedException(org.locationtech.geogig.cli.CommandFailedException) GeoToolsOpException(org.locationtech.geogig.geotools.plumbing.GeoToolsOpException)

Example 3 with AttributeDescriptor

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

the class Insert method createFeature.

private Feature createFeature(List<String> featureChanges, Map<String, SimpleFeatureBuilder> featureTypes) {
    String path = featureChanges.get(0);
    String tree = NodeRef.parentPath(path);
    String featureId = NodeRef.nodeFromPath(path);
    if (!featureTypes.containsKey(tree)) {
        Optional<RevFeatureType> opt = geogig.command(ResolveFeatureType.class).setRefSpec("WORK_HEAD:" + tree).call();
        checkParameter(opt.isPresent(), "The parent tree does not exist: " + tree);
        SimpleFeatureBuilder builder = new SimpleFeatureBuilder((SimpleFeatureType) opt.get().type());
        featureTypes.put(tree, builder);
    }
    SimpleFeatureBuilder ftb = featureTypes.get(tree);
    SimpleFeatureType ft = ftb.getFeatureType();
    for (int i = 1; i < featureChanges.size(); i++) {
        String[] tokens = featureChanges.get(i).split("\t");
        Preconditions.checkArgument(tokens.length == 2, "Wrong attribute definition: " + featureChanges.get(i));
        String fieldName = tokens[0];
        AttributeDescriptor desc = ft.getDescriptor(fieldName);
        Preconditions.checkNotNull(desc, "Wrong attribute in feature description");
        FieldType type = FieldType.forBinding(desc.getType().getBinding());
        Object value = TextValueSerializer.fromString(type, tokens[1]);
        ftb.set(tokens[0], value);
    }
    return ftb.buildFeature(featureId);
}
Also used : SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) AttributeDescriptor(org.opengis.feature.type.AttributeDescriptor) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder) FieldType(org.locationtech.geogig.storage.FieldType)

Example 4 with AttributeDescriptor

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

the class FormatCommonV2 method readFeatureType.

public static RevFeatureType readFeatureType(ObjectId id, DataInput in, FeatureTypeFactory typeFactory) throws IOException {
    Name name = readName(in);
    int propertyCount = readUnsignedVarInt(in);
    List<AttributeDescriptor> attributes = new ArrayList<AttributeDescriptor>();
    for (int i = 0; i < propertyCount; i++) {
        attributes.add(readAttributeDescriptor(in, typeFactory));
    }
    SimpleFeatureType ftype = typeFactory.createSimpleFeatureType(name, attributes, null, false, Collections.<Filter>emptyList(), BasicFeatureTypes.FEATURE, null);
    return new RevFeatureTypeImpl(id, ftype);
}
Also used : RevFeatureTypeImpl(org.locationtech.geogig.api.RevFeatureTypeImpl) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) ArrayList(java.util.ArrayList) AttributeDescriptor(org.opengis.feature.type.AttributeDescriptor) Name(org.opengis.feature.type.Name)

Example 5 with AttributeDescriptor

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

Aggregations

AttributeDescriptor (org.opengis.feature.type.AttributeDescriptor)40 SimpleFeatureType (org.opengis.feature.simple.SimpleFeatureType)23 SimpleFeature (org.opengis.feature.simple.SimpleFeature)11 SimpleFeatureTypeBuilder (org.geotools.feature.simple.SimpleFeatureTypeBuilder)9 ArrayList (java.util.ArrayList)8 HashMap (java.util.HashMap)8 SimpleFeatureIterator (org.geotools.data.simple.SimpleFeatureIterator)8 SimpleFeatureStore (org.geotools.data.simple.SimpleFeatureStore)7 SimpleFeatureBuilder (org.geotools.feature.simple.SimpleFeatureBuilder)7 Test (org.junit.Test)7 IOException (java.io.IOException)6 ShapefileDataStore (org.geotools.data.shapefile.ShapefileDataStore)6 SimpleFeatureCollection (org.geotools.data.simple.SimpleFeatureCollection)6 SimpleFeatureSource (org.geotools.data.simple.SimpleFeatureSource)6 Map (java.util.Map)4 DefaultTransaction (org.geotools.data.DefaultTransaction)4 FileDataStoreFactorySpi (org.geotools.data.FileDataStoreFactorySpi)4 Transaction (org.geotools.data.Transaction)4 MemoryDataStore (org.geotools.data.memory.MemoryDataStore)4 GeometryDescriptor (org.opengis.feature.type.GeometryDescriptor)4