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);
}
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;
}
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);
}
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);
}
}
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);
}
}
Aggregations