use of org.geotools.feature.simple.SimpleFeatureBuilder in project GeoGig by boundlessgeo.
the class FeatureTypeAdapterFeatureSource method getFeatures.
@Override
public FeatureCollection<T, F> getFeatures(Query query) throws IOException {
final FeatureCollection<T, F> features = super.getFeatures(query);
return new ForwardingFeatureCollection<T, F>(features) {
@Override
public FeatureIterator<F> features() {
if (delegate.getSchema().getDescriptors().size() != featureType.getDescriptors().size()) {
throw new GeoToolsOpException(GeoToolsOpException.StatusCode.INCOMPATIBLE_FEATURE_TYPE);
}
GeometryDescriptor geomDescriptorOrg = delegate.getSchema().getGeometryDescriptor();
GeometryDescriptor geomDescriptorDest = featureType.getGeometryDescriptor();
if (!geomDescriptorOrg.getType().getBinding().equals(geomDescriptorDest.getType().getBinding()) || !geomDescriptorOrg.getType().getCoordinateReferenceSystem().equals(geomDescriptorDest.getType().getCoordinateReferenceSystem())) {
throw new GeoToolsOpException(GeoToolsOpException.StatusCode.INCOMPATIBLE_FEATURE_TYPE);
}
FeatureIterator<F> iterator = delegate.features();
SimpleFeatureBuilder builder = new SimpleFeatureBuilder((SimpleFeatureType) featureType);
return new FeatureTypeConverterIterator<F>(iterator, (SimpleFeatureBuilder) builder);
}
@Override
public T getSchema() {
return featureType;
}
};
}
use of org.geotools.feature.simple.SimpleFeatureBuilder in project GeoGig by boundlessgeo.
the class HashObjectTest method feature.
protected Feature feature(SimpleFeatureType type, String id, Object... values) throws ParseException {
SimpleFeatureBuilder builder = new SimpleFeatureBuilder(type);
for (int i = 0; i < values.length; i++) {
Object value = values[i];
if (type.getDescriptor(i) instanceof GeometryDescriptor) {
if (value instanceof String) {
value = new WKTReader2().read((String) value);
}
}
builder.set(i, value);
}
return builder.buildFeature(id);
}
use of org.geotools.feature.simple.SimpleFeatureBuilder 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);
}
}
use of org.geotools.feature.simple.SimpleFeatureBuilder in project GeoGig by boundlessgeo.
the class MergeFeaturesOp method merge.
@SuppressWarnings("unchecked")
private Feature merge(RevFeature featureA, RevFeature featureB, RevFeature ancestor, RevFeatureType featureType) {
SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder((SimpleFeatureType) featureType.type());
ImmutableList<Optional<Object>> valuesA = featureA.getValues();
ImmutableList<Optional<Object>> valuesB = featureB.getValues();
ImmutableList<Optional<Object>> valuesAncestor = ancestor.getValues();
ImmutableList<PropertyDescriptor> descriptors = featureType.sortedDescriptors();
for (int i = 0; i < descriptors.size(); i++) {
PropertyDescriptor descriptor = descriptors.get(i);
boolean isGeom = Geometry.class.isAssignableFrom(descriptor.getType().getBinding());
Name name = descriptor.getName();
Optional<Object> valueAncestor = valuesAncestor.get(i);
Optional<Object> valueA = valuesA.get(i);
Optional<Object> valueB = valuesB.get(i);
if (!valueA.equals(valueAncestor)) {
Optional<Object> merged = valueA;
if (isGeom && !valueB.equals(valueAncestor)) {
// true merge is only done with
// geometries
GeometryAttributeDiff diffB = new GeometryAttributeDiff(Optional.fromNullable((Geometry) valueAncestor.orNull()), Optional.fromNullable((Geometry) valueB.orNull()));
merged = (Optional<Object>) diffB.applyOn(valueA);
}
featureBuilder.set(name, merged.orNull());
} else {
featureBuilder.set(name, valueB.orNull());
}
}
return featureBuilder.buildFeature(nodeRefA.name());
}
use of org.geotools.feature.simple.SimpleFeatureBuilder in project GeoGig by boundlessgeo.
the class OSMUnmapOp method unmapWay.
private void unmapWay(SimpleFeature feature, FeatureMapFlusher flusher) {
boolean modified = false;
String id = feature.getID();
SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(OSMUtils.wayType());
Optional<RevFeature> rawFeature = command(RevObjectParse.class).setRefSpec("WORK_HEAD:" + OSMUtils.WAY_TYPE_NAME + "/" + id).call(RevFeature.class);
Map<String, String> tagsMap = Maps.newHashMap();
long timestamp = System.currentTimeMillis();
int version = 1;
long changeset = -1;
String user = UNKNOWN_USER;
Collection<Tag> tags = Lists.newArrayList();
if (rawFeature.isPresent()) {
ImmutableList<Optional<Object>> values = rawFeature.get().getValues();
tags = OSMUtils.buildTagsCollectionFromString(values.get(WAY_TAGS_FIELD_INDEX).get().toString());
for (Tag tag : tags) {
tagsMap.put(tag.getKey(), tag.getValue());
}
Optional<Object> timestampOpt = values.get(WAY_TIMESTAMP_FIELD_INDEX);
if (timestampOpt.isPresent()) {
timestamp = ((Long) timestampOpt.get()).longValue();
}
Optional<Object> versionOpt = values.get(WAY_VERSION_FIELD_INDEX);
if (versionOpt.isPresent()) {
version = ((Integer) versionOpt.get()).intValue();
}
Optional<Object> changesetOpt = values.get(WAY_CHANGESET_FIELD_INDEX);
if (changesetOpt.isPresent()) {
changeset = ((Long) changesetOpt.get()).longValue();
}
Optional<Object> userOpt = values.get(WAY_USER_FIELD_INDEX);
if (userOpt.isPresent()) {
user = (String) userOpt.get();
}
}
Map<String, String> unaliased = Maps.newHashMap();
Collection<Property> properties = feature.getProperties();
for (Property property : properties) {
String name = property.getName().getLocalPart();
if (name.equals("id") || name.equals("nodes") || Geometry.class.isAssignableFrom(property.getDescriptor().getType().getBinding())) {
continue;
}
Object value = property.getValue();
if (value != null) {
String tagName = name;
if (mapping != null) {
if (unaliased.containsKey(name)) {
tagName = unaliased.get(name);
} else {
tagName = mapping.getTagNameFromAlias(path, tagName);
unaliased.put(name, tagName);
}
}
if (!DefaultField.isDefaultField(tagName)) {
if (tagsMap.containsKey(tagName)) {
if (!modified) {
String oldValue = tagsMap.get(tagName);
modified = !value.equals(oldValue);
}
} else {
modified = true;
}
tagsMap.put(tagName, value.toString());
}
}
}
if (!modified && rawFeature.isPresent()) {
// no changes after unmapping tags, so there's nothing else to do
return;
}
tags.clear();
Set<Entry<String, String>> entries = tagsMap.entrySet();
for (Entry<String, String> entry : entries) {
tags.add(new Tag(entry.getKey(), entry.getValue()));
}
Geometry geom = (Geometry) feature.getDefaultGeometry();
LineString line;
if (geom instanceof LineString) {
line = (LineString) geom;
} else {
line = gf.createLineString(geom.getCoordinates());
}
featureBuilder.set("visible", true);
featureBuilder.set("tags", OSMUtils.buildTagsString(tags));
featureBuilder.set("way", line);
featureBuilder.set("changeset", changeset);
featureBuilder.set("timestamp", timestamp);
featureBuilder.set("version", version);
featureBuilder.set("user", user);
featureBuilder.set("nodes", getNodeStringFromWay(feature, flusher));
if (rawFeature.isPresent()) {
// the feature has changed, so we cannot reuse some attributes
featureBuilder.set("timestamp", System.currentTimeMillis());
// temporary negative changeset ID
featureBuilder.set("changeset", -changeset);
// featureBuilder.set("version", version);
flusher.put("way", featureBuilder.buildFeature(id));
} else {
flusher.put("way", featureBuilder.buildFeature(id));
}
}
Aggregations