Search in sources :

Example 31 with PropertyDescriptor

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

the class DescribeOp method _call.

/**
     * Describes a table from the data store that has been assigned.
     * 
     * @return a map that contains all properties and their types from the provided table
     */
@Override
protected Optional<Map<String, String>> _call() {
    if (dataStore == null) {
        throw new GeoToolsOpException(StatusCode.DATASTORE_NOT_DEFINED);
    }
    if (table == null || table.isEmpty()) {
        throw new GeoToolsOpException(StatusCode.TABLE_NOT_DEFINED);
    }
    Map<String, String> propertyMap = new HashMap<String, String>();
    boolean foundTable = false;
    List<Name> typeNames;
    try {
        typeNames = dataStore.getNames();
    } catch (Exception e) {
        throw new GeoToolsOpException(StatusCode.UNABLE_TO_GET_NAMES);
    }
    for (Name typeName : typeNames) {
        if (!table.equals(typeName.toString()))
            continue;
        foundTable = true;
        SimpleFeatureSource featureSource;
        try {
            featureSource = dataStore.getFeatureSource(typeName);
        } catch (Exception e) {
            throw new GeoToolsOpException(StatusCode.UNABLE_TO_GET_FEATURES);
        }
        SimpleFeatureType featureType = featureSource.getSchema();
        Collection<PropertyDescriptor> descriptors = featureType.getDescriptors();
        for (PropertyDescriptor descriptor : descriptors) {
            propertyMap.put(descriptor.getName().toString(), descriptor.getType().getBinding().getSimpleName());
        }
    }
    if (!foundTable) {
        return Optional.absent();
    }
    return Optional.of(propertyMap);
}
Also used : SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) HashMap(java.util.HashMap) SimpleFeatureSource(org.geotools.data.simple.SimpleFeatureSource) Name(org.opengis.feature.type.Name)

Example 32 with PropertyDescriptor

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

the class ImportOp method alter.

/**
     * Translates a feature pointed by a node from its original feature type to a given one, using
     * values from those attributes that exist in both original and destination feature type. New
     * attributes are populated with null values
     * 
     * @param node The node that points to the feature. No checking is performed to ensure the node
     *        points to a feature instead of other type
     * @param featureType the destination feature type
     * @return a feature with the passed feature type and data taken from the input feature
     */
private Feature alter(NodeRef node, RevFeatureType featureType) {
    RevFeature oldFeature = command(RevObjectParse.class).setObjectId(node.objectId()).call(RevFeature.class).get();
    RevFeatureType oldFeatureType;
    oldFeatureType = command(RevObjectParse.class).setObjectId(node.getMetadataId()).call(RevFeatureType.class).get();
    ImmutableList<PropertyDescriptor> oldAttributes = oldFeatureType.sortedDescriptors();
    ImmutableList<PropertyDescriptor> newAttributes = featureType.sortedDescriptors();
    ImmutableList<Optional<Object>> oldValues = oldFeature.getValues();
    List<Optional<Object>> newValues = Lists.newArrayList();
    for (int i = 0; i < newAttributes.size(); i++) {
        int idx = oldAttributes.indexOf(newAttributes.get(i));
        if (idx != -1) {
            Optional<Object> oldValue = oldValues.get(idx);
            newValues.add(oldValue);
        } else {
            newValues.add(Optional.absent());
        }
    }
    RevFeature newFeature = RevFeatureImpl.build(ImmutableList.copyOf(newValues));
    FeatureBuilder featureBuilder = new FeatureBuilder(featureType);
    Feature feature = featureBuilder.build(node.name(), newFeature);
    return feature;
}
Also used : FeatureBuilder(org.locationtech.geogig.api.FeatureBuilder) PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) Optional(com.google.common.base.Optional) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Feature(org.opengis.feature.Feature) RevFeature(org.locationtech.geogig.api.RevFeature) DecoratingFeature(org.geotools.feature.DecoratingFeature) RevFeature(org.locationtech.geogig.api.RevFeature) RevObjectParse(org.locationtech.geogig.api.plumbing.RevObjectParse) RevFeatureType(org.locationtech.geogig.api.RevFeatureType)

Example 33 with PropertyDescriptor

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

the class ExportOp method alter.

private Iterator<SimpleFeature> alter(Iterator<SimpleFeature> plainFeatures, final ObjectId targetFeatureTypeId) {
    final RevFeatureType targetType = stagingDatabase().getFeatureType(targetFeatureTypeId);
    Function<SimpleFeature, SimpleFeature> alterFunction = new Function<SimpleFeature, SimpleFeature>() {

        @Override
        public SimpleFeature apply(SimpleFeature input) {
            final RevFeatureType oldFeatureType;
            oldFeatureType = (RevFeatureType) input.getUserData().get(RevFeatureType.class);
            final ObjectId metadataId = oldFeatureType.getId();
            if (targetType.getId().equals(metadataId)) {
                return input;
            }
            final RevFeature oldFeature;
            oldFeature = (RevFeature) input.getUserData().get(RevFeature.class);
            ImmutableList<PropertyDescriptor> oldAttributes = oldFeatureType.sortedDescriptors();
            ImmutableList<PropertyDescriptor> newAttributes = targetType.sortedDescriptors();
            ImmutableList<Optional<Object>> oldValues = oldFeature.getValues();
            List<Optional<Object>> newValues = Lists.newArrayList();
            for (int i = 0; i < newAttributes.size(); i++) {
                int idx = oldAttributes.indexOf(newAttributes.get(i));
                if (idx != -1) {
                    Optional<Object> oldValue = oldValues.get(idx);
                    newValues.add(oldValue);
                } else {
                    newValues.add(Optional.absent());
                }
            }
            RevFeature newFeature = RevFeatureImpl.build(ImmutableList.copyOf(newValues));
            FeatureBuilder featureBuilder = new FeatureBuilder(targetType);
            SimpleFeature feature = (SimpleFeature) featureBuilder.build(input.getID(), newFeature);
            return feature;
        }
    };
    return Iterators.transform(plainFeatures, alterFunction);
}
Also used : FeatureBuilder(org.locationtech.geogig.api.FeatureBuilder) PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) Optional(com.google.common.base.Optional) ObjectId(org.locationtech.geogig.api.ObjectId) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Function(com.google.common.base.Function) RevFeature(org.locationtech.geogig.api.RevFeature) RevObject(org.locationtech.geogig.api.RevObject) RevFeatureType(org.locationtech.geogig.api.RevFeatureType)

Example 34 with PropertyDescriptor

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

the class FormatCommonV2 method writeFeatureType.

public static void writeFeatureType(RevFeatureType object, DataOutput data) throws IOException {
    writeName(object.getName(), data);
    ImmutableList<PropertyDescriptor> descriptors = object.sortedDescriptors();
    writeUnsignedVarInt(descriptors.size(), data);
    for (PropertyDescriptor desc : object.type().getDescriptors()) {
        writeProperty(desc, data);
    }
}
Also used : PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor)

Example 35 with PropertyDescriptor

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

the class MergeFeatureResource method post.

public void post(Representation entity) {
    InputStream input = null;
    try {
        input = getRequest().getEntity().getStream();
        final GeoGIG ggit = getGeogig(getRequest()).get();
        final Reader body = new InputStreamReader(input);
        final JsonParser parser = new JsonParser();
        final JsonElement conflictJson = parser.parse(body);
        if (conflictJson.isJsonObject()) {
            final JsonObject conflict = conflictJson.getAsJsonObject();
            String featureId = null;
            RevFeature ourFeature = null;
            RevFeatureType ourFeatureType = null;
            RevFeature theirFeature = null;
            RevFeatureType theirFeatureType = null;
            JsonObject merges = null;
            if (conflict.has("path") && conflict.get("path").isJsonPrimitive()) {
                featureId = conflict.get("path").getAsJsonPrimitive().getAsString();
            }
            Preconditions.checkState(featureId != null);
            if (conflict.has("ours") && conflict.get("ours").isJsonPrimitive()) {
                String ourCommit = conflict.get("ours").getAsJsonPrimitive().getAsString();
                Optional<NodeRef> ourNode = parseID(ObjectId.valueOf(ourCommit), featureId, ggit);
                if (ourNode.isPresent()) {
                    Optional<RevObject> object = ggit.command(RevObjectParse.class).setObjectId(ourNode.get().objectId()).call();
                    Preconditions.checkState(object.isPresent() && object.get() instanceof RevFeature);
                    ourFeature = (RevFeature) object.get();
                    object = ggit.command(RevObjectParse.class).setObjectId(ourNode.get().getMetadataId()).call();
                    Preconditions.checkState(object.isPresent() && object.get() instanceof RevFeatureType);
                    ourFeatureType = (RevFeatureType) object.get();
                }
            }
            if (conflict.has("theirs") && conflict.get("theirs").isJsonPrimitive()) {
                String theirCommit = conflict.get("theirs").getAsJsonPrimitive().getAsString();
                Optional<NodeRef> theirNode = parseID(ObjectId.valueOf(theirCommit), featureId, ggit);
                if (theirNode.isPresent()) {
                    Optional<RevObject> object = ggit.command(RevObjectParse.class).setObjectId(theirNode.get().objectId()).call();
                    Preconditions.checkState(object.isPresent() && object.get() instanceof RevFeature);
                    theirFeature = (RevFeature) object.get();
                    object = ggit.command(RevObjectParse.class).setObjectId(theirNode.get().getMetadataId()).call();
                    Preconditions.checkState(object.isPresent() && object.get() instanceof RevFeatureType);
                    theirFeatureType = (RevFeatureType) object.get();
                }
            }
            if (conflict.has("merges") && conflict.get("merges").isJsonObject()) {
                merges = conflict.get("merges").getAsJsonObject();
            }
            Preconditions.checkState(merges != null);
            Preconditions.checkState(ourFeatureType != null || theirFeatureType != null);
            SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder((SimpleFeatureType) (ourFeatureType != null ? ourFeatureType.type() : theirFeatureType.type()));
            ImmutableList<PropertyDescriptor> descriptors = (ourFeatureType == null ? theirFeatureType : ourFeatureType).sortedDescriptors();
            for (Entry<String, JsonElement> entry : merges.entrySet()) {
                int descriptorIndex = getDescriptorIndex(entry.getKey(), descriptors);
                if (descriptorIndex != -1 && entry.getValue().isJsonObject()) {
                    PropertyDescriptor descriptor = descriptors.get(descriptorIndex);
                    JsonObject attributeObject = entry.getValue().getAsJsonObject();
                    if (attributeObject.has("ours") && attributeObject.get("ours").isJsonPrimitive() && attributeObject.get("ours").getAsBoolean()) {
                        featureBuilder.set(descriptor.getName(), ourFeature == null ? null : ourFeature.getValues().get(descriptorIndex).orNull());
                    } else if (attributeObject.has("theirs") && attributeObject.get("theirs").isJsonPrimitive() && attributeObject.get("theirs").getAsBoolean()) {
                        featureBuilder.set(descriptor.getName(), theirFeature == null ? null : theirFeature.getValues().get(descriptorIndex).orNull());
                    } else if (attributeObject.has("value") && attributeObject.get("value").isJsonPrimitive()) {
                        JsonPrimitive primitive = attributeObject.get("value").getAsJsonPrimitive();
                        if (primitive.isString()) {
                            try {
                                Object object = valueFromString(FieldType.forBinding(descriptor.getType().getBinding()), primitive.getAsString());
                                featureBuilder.set(descriptor.getName(), object);
                            } catch (Exception e) {
                                throw new Exception("Unable to convert attribute (" + entry.getKey() + ") to required type: " + descriptor.getType().getBinding().toString());
                            }
                        } else if (primitive.isNumber()) {
                            try {
                                Object value = valueFromNumber(FieldType.forBinding(descriptor.getType().getBinding()), primitive.getAsNumber());
                                featureBuilder.set(descriptor.getName(), value);
                            } catch (Exception e) {
                                throw new Exception("Unable to convert attribute (" + entry.getKey() + ") to required type: " + descriptor.getType().getBinding().toString());
                            }
                        } else if (primitive.isBoolean()) {
                            try {
                                Object value = valueFromBoolean(FieldType.forBinding(descriptor.getType().getBinding()), primitive.getAsBoolean());
                                featureBuilder.set(descriptor.getName(), value);
                            } catch (Exception e) {
                                throw new Exception("Unable to convert attribute (" + entry.getKey() + ") to required type: " + descriptor.getType().getBinding().toString());
                            }
                        } else if (primitive.isJsonNull()) {
                            featureBuilder.set(descriptor.getName(), null);
                        } else {
                            throw new Exception("Unsupported JSON type for attribute value (" + entry.getKey() + ")");
                        }
                    }
                }
            }
            SimpleFeature feature = featureBuilder.buildFeature(NodeRef.nodeFromPath(featureId));
            RevFeature revFeature = RevFeatureBuilder.build(feature);
            ggit.getRepository().stagingDatabase().put(revFeature);
            getResponse().setEntity(new StringRepresentation(revFeature.getId().toString(), MediaType.TEXT_PLAIN));
        }
    } catch (Exception e) {
        throw new RestletException(e.getMessage(), Status.SERVER_ERROR_INTERNAL, e);
    } finally {
        if (input != null)
            Closeables.closeQuietly(input);
    }
}
Also used : JsonPrimitive(com.google.gson.JsonPrimitive) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) JsonObject(com.google.gson.JsonObject) LineString(com.vividsolutions.jts.geom.LineString) MultiLineString(com.vividsolutions.jts.geom.MultiLineString) NodeRef(org.locationtech.geogig.api.NodeRef) StringRepresentation(org.restlet.resource.StringRepresentation) RestletException(org.locationtech.geogig.rest.RestletException) RevFeature(org.locationtech.geogig.api.RevFeature) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) JsonParser(com.google.gson.JsonParser) InputStreamReader(java.io.InputStreamReader) PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) RevObject(org.locationtech.geogig.api.RevObject) InputStream(java.io.InputStream) Point(com.vividsolutions.jts.geom.Point) MultiPoint(com.vividsolutions.jts.geom.MultiPoint) CommandSpecException(org.locationtech.geogig.web.api.CommandSpecException) RestletException(org.locationtech.geogig.rest.RestletException) IOException(java.io.IOException) SimpleFeature(org.opengis.feature.simple.SimpleFeature) JsonElement(com.google.gson.JsonElement) RevObjectParse(org.locationtech.geogig.api.plumbing.RevObjectParse) JsonObject(com.google.gson.JsonObject) RevObject(org.locationtech.geogig.api.RevObject) GeoGIG(org.locationtech.geogig.api.GeoGIG) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder)

Aggregations

PropertyDescriptor (org.opengis.feature.type.PropertyDescriptor)47 RevFeature (org.locationtech.geogig.api.RevFeature)24 RevFeatureType (org.locationtech.geogig.api.RevFeatureType)23 Optional (com.google.common.base.Optional)18 AttributeDiff (org.locationtech.geogig.api.plumbing.diff.AttributeDiff)17 RevObjectParse (org.locationtech.geogig.api.plumbing.RevObjectParse)16 FeatureDiff (org.locationtech.geogig.api.plumbing.diff.FeatureDiff)15 Test (org.junit.Test)12 RevObject (org.locationtech.geogig.api.RevObject)12 GenericAttributeDiffImpl (org.locationtech.geogig.api.plumbing.diff.GenericAttributeDiffImpl)11 NodeRef (org.locationtech.geogig.api.NodeRef)10 Patch (org.locationtech.geogig.api.plumbing.diff.Patch)10 DiffEntry (org.locationtech.geogig.api.plumbing.diff.DiffEntry)9 ObjectId (org.locationtech.geogig.api.ObjectId)8 Entry (java.util.Map.Entry)7 SimpleFeature (org.opengis.feature.simple.SimpleFeature)7 SimpleFeatureBuilder (org.geotools.feature.simple.SimpleFeatureBuilder)6 FeatureBuilder (org.locationtech.geogig.api.FeatureBuilder)6 GeometryType (org.opengis.feature.type.GeometryType)6 PropertyType (org.opengis.feature.type.PropertyType)6