use of org.locationtech.geogig.api.RevFeature in project GeoGig by boundlessgeo.
the class PatchSerializer method addElement.
private static void addElement(List<String> lines, Patch patch, Map<String, RevFeatureType> featureTypes) {
String[] headerTokens = lines.get(0).split("\t");
if (headerTokens.length == 4 || headerTokens.length == 3) {
// modified // modification
if (lines.size() == 1) {
// feature type
FeatureTypeDiff diff = new FeatureTypeDiff(headerTokens[0], ObjectId.valueOf(headerTokens[1]), ObjectId.valueOf(headerTokens[2]));
patch.addAlteredTree(diff);
} else {
// feature
String element = Joiner.on("\n").join(lines.subList(1, lines.size()));
ByteArrayInputStream stream;
stream = new ByteArrayInputStream(element.getBytes(Charsets.UTF_8));
String operation = headerTokens[0].trim();
if (operation.equals("M")) {
String fullPath = headerTokens[1].trim();
String oldMetadataId = headerTokens[2].trim();
String newMetadataId = headerTokens[3].trim();
RevFeatureType newRevFeatureType = featureTypes.get(newMetadataId);
RevFeatureType oldRevFeatureType = featureTypes.get(oldMetadataId);
Map<PropertyDescriptor, AttributeDiff> map = Maps.newHashMap();
for (int i = 1; i < lines.size(); i++) {
addDifference(lines.get(i), map, oldRevFeatureType, newRevFeatureType);
}
FeatureDiff featureDiff = new FeatureDiff(fullPath, map, oldRevFeatureType, newRevFeatureType);
patch.addModifiedFeature(featureDiff);
} else if (operation.equals("A") || operation.equals("R")) {
String fullPath = headerTokens[1].trim();
String featureTypeId = headerTokens[2].trim();
RevFeatureType revFeatureType;
revFeatureType = featureTypes.get(featureTypeId);
FeatureBuilder featureBuilder = new FeatureBuilder(revFeatureType);
ObjectReader<RevFeature> reader = factory.createFeatureReader();
RevFeature revFeature = reader.read(null, stream);
Feature feature = featureBuilder.build(NodeRef.nodeFromPath(fullPath), revFeature);
if (operation.equals("R")) {
patch.addRemovedFeature(fullPath, feature, revFeatureType);
} else {
patch.addAddedFeature(fullPath, feature, revFeatureType);
}
} else {
throw new IllegalArgumentException("Wrong patch content: " + lines.get(0));
}
}
} else if (headerTokens.length == 1) {
// feature type definition
String element = Joiner.on("\n").join(lines);
ByteArrayInputStream stream = new ByteArrayInputStream(element.getBytes(Charsets.UTF_8));
String[] tokens = lines.get(1).split("\t");
ObjectReader<RevFeatureType> reader = factory.createFeatureTypeReader();
RevFeatureType featureType = reader.read(null, stream);
featureTypes.put(featureType.getId().toString(), featureType);
} else {
throw new IllegalArgumentException("Wrong patch content: " + lines.get(0));
}
}
use of org.locationtech.geogig.api.RevFeature in project GeoGig by boundlessgeo.
the class CommitOpTest method testAmend.
@Test
public void testAmend() throws Exception {
final ObjectId id = insertAndAdd(points1);
final RevCommit commit1 = geogig.command(CommitOp.class).setMessage("Message").call();
{
assertCommit(commit1, null, null, null);
assertEquals(id, repo.getRootTreeChild(appendChild(pointsName, idP1)).get().getObjectId());
assertNotNull(repo.objectDatabase().get(id));
}
final ObjectId id2 = insertAndAdd(points2);
final RevCommit commit2 = geogig.command(CommitOp.class).setAmend(true).call();
{
assertCommit(commit2, null, "groldan", "Message");
Optional<RevFeature> p2 = geogig.command(RevObjectParse.class).setRefSpec("HEAD:" + appendChild(pointsName, idP2)).call(RevFeature.class);
assertTrue(p2.isPresent());
assertEquals(id2, p2.get().getId());
Optional<RevFeature> p1 = geogig.command(RevObjectParse.class).setRefSpec("HEAD:" + appendChild(pointsName, idP1)).call(RevFeature.class);
assertTrue(p1.isPresent());
assertEquals(id, p1.get().getId());
}
Iterator<RevCommit> log = geogig.command(LogOp.class).call();
assertTrue(log.hasNext());
log.next();
assertFalse(log.hasNext());
}
use of org.locationtech.geogig.api.RevFeature in project GeoGig by boundlessgeo.
the class ExportOp method getFeatures.
private static Iterator<SimpleFeature> getFeatures(final RevTree typeTree, final ObjectDatabase database, final ObjectId defaultMetadataId, final ProgressListener progressListener) {
Iterator<NodeRef> nodes = new DepthTreeIterator("", defaultMetadataId, typeTree, database, Strategy.FEATURES_ONLY);
// progress reporting
nodes = Iterators.transform(nodes, new Function<NodeRef, NodeRef>() {
private AtomicInteger count = new AtomicInteger();
@Override
public NodeRef apply(NodeRef input) {
progressListener.setProgress((count.incrementAndGet() * 100.f) / typeTree.size());
return input;
}
});
Function<NodeRef, SimpleFeature> asFeature = new Function<NodeRef, SimpleFeature>() {
private Map<ObjectId, FeatureBuilder> ftCache = Maps.newHashMap();
@Override
@Nullable
public SimpleFeature apply(final NodeRef input) {
final ObjectId metadataId = input.getMetadataId();
final RevFeature revFeature = database.getFeature(input.objectId());
FeatureBuilder featureBuilder = getBuilderFor(metadataId);
Feature feature = featureBuilder.build(input.name(), revFeature);
feature.getUserData().put(Hints.USE_PROVIDED_FID, true);
feature.getUserData().put(RevFeature.class, revFeature);
feature.getUserData().put(RevFeatureType.class, featureBuilder.getType());
if (feature instanceof SimpleFeature) {
return (SimpleFeature) feature;
}
return null;
}
private FeatureBuilder getBuilderFor(final ObjectId metadataId) {
FeatureBuilder featureBuilder = ftCache.get(metadataId);
if (featureBuilder == null) {
RevFeatureType revFtype = database.getFeatureType(metadataId);
featureBuilder = new FeatureBuilder(revFtype);
ftCache.put(metadataId, featureBuilder);
}
return featureBuilder;
}
};
Iterator<SimpleFeature> asFeatures = Iterators.transform(nodes, asFeature);
UnmodifiableIterator<SimpleFeature> filterNulls = Iterators.filter(asFeatures, Predicates.notNull());
return filterNulls;
}
use of org.locationtech.geogig.api.RevFeature in project GeoGig by boundlessgeo.
the class Show method printFormatted.
public void printFormatted(GeogigCLI cli) throws IOException {
ConsoleReader console = cli.getConsole();
GeoGIG geogig = cli.getGeogig();
for (String ref : refs) {
Optional<RevObject> obj = geogig.command(RevObjectParse.class).setRefSpec(ref).call();
if (!obj.isPresent()) {
ref = getFullRef(ref);
obj = geogig.command(RevObjectParse.class).setRefSpec(ref).call();
}
checkParameter(obj.isPresent(), "refspec did not resolve to any object.");
RevObject revObject = obj.get();
if (revObject instanceof RevFeature) {
Optional<RevFeatureType> opt = geogig.command(ResolveFeatureType.class).setRefSpec(ref).call();
if (opt.isPresent()) {
RevFeatureType ft = opt.get();
ImmutableList<PropertyDescriptor> attribs = ft.sortedDescriptors();
RevFeature feature = (RevFeature) revObject;
Ansi ansi = super.newAnsi(console.getTerminal());
ansi.newline().fg(Color.YELLOW).a("ID: ").reset().a(feature.getId().toString()).newline();
ansi.fg(Color.YELLOW).a("FEATURE TYPE ID: ").reset().a(ft.getId().toString()).newline().newline();
ansi.a("ATTRIBUTES ").newline();
ansi.a("---------- ").newline();
ImmutableList<Optional<Object>> values = feature.getValues();
int i = 0;
for (Optional<Object> value : values) {
ansi.fg(Color.YELLOW).a(attribs.get(i).getName() + ": ").reset();
ansi.a(value.or("[NULL]").toString()).newline();
i++;
}
console.println(ansi.toString());
} else {
CharSequence s = geogig.command(CatObject.class).setObject(Suppliers.ofInstance(revObject)).call();
console.println(s);
}
} else if (revObject instanceof RevTree) {
RevTree tree = (RevTree) revObject;
Optional<RevFeatureType> opt = geogig.command(ResolveFeatureType.class).setRefSpec(ref).call();
checkParameter(opt.isPresent(), "Refspec must resolve to a commit, tree, feature or feature type");
RevFeatureType ft = opt.get();
Ansi ansi = super.newAnsi(console.getTerminal());
ansi.fg(Color.YELLOW).a("TREE ID: ").reset().a(tree.getId().toString()).newline();
ansi.fg(Color.YELLOW).a("SIZE: ").reset().a(Long.toString(tree.size())).newline();
ansi.fg(Color.YELLOW).a("NUMBER Of SUBTREES: ").reset().a(Integer.toString(tree.numTrees()).toString()).newline();
printFeatureType(ansi, ft, true);
console.println(ansi.toString());
} else if (revObject instanceof RevCommit) {
RevCommit commit = (RevCommit) revObject;
Ansi ansi = super.newAnsi(console.getTerminal());
ansi.a(Strings.padEnd("Commit:", 15, ' ')).fg(Color.YELLOW).a(commit.getId().toString()).reset().newline();
ansi.a(Strings.padEnd("Author:", 15, ' ')).fg(Color.GREEN).a(formatPerson(commit.getAuthor())).reset().newline();
ansi.a(Strings.padEnd("Committer:", 15, ' ')).fg(Color.GREEN).a(formatPerson(commit.getAuthor())).reset().newline();
ansi.a(Strings.padEnd("Author date:", 15, ' ')).a("(").fg(Color.RED).a(estimateSince(geogig.getPlatform(), commit.getAuthor().getTimestamp())).reset().a(") ").a(new Date(commit.getAuthor().getTimestamp())).newline();
ansi.a(Strings.padEnd("Committer date:", 15, ' ')).a("(").fg(Color.RED).a(estimateSince(geogig.getPlatform(), commit.getCommitter().getTimestamp())).reset().a(") ").a(new Date(commit.getCommitter().getTimestamp())).newline();
ansi.a(Strings.padEnd("Subject:", 15, ' ')).a(commit.getMessage()).newline();
console.println(ansi.toString());
} else if (revObject instanceof RevFeatureType) {
Ansi ansi = super.newAnsi(console.getTerminal());
printFeatureType(ansi, (RevFeatureType) revObject, false);
console.println(ansi.toString());
} else {
throw new InvalidParameterException("Refspec must resolve to a commit, tree, feature or feature type");
}
console.println();
}
}
use of org.locationtech.geogig.api.RevFeature in project GeoGig by boundlessgeo.
the class Show method printRaw.
private void printRaw(GeogigCLI cli) throws IOException {
ConsoleReader console = cli.getConsole();
GeoGIG geogig = cli.getGeogig();
for (String ref : refs) {
Optional<RevObject> obj = geogig.command(RevObjectParse.class).setRefSpec(ref).call();
if (!obj.isPresent()) {
ref = getFullRef(ref);
obj = geogig.command(RevObjectParse.class).setRefSpec(ref).call();
}
checkParameter(obj.isPresent(), "refspec did not resolve to any object.");
RevObject revObject = obj.get();
if (revObject instanceof RevFeature) {
Optional<RevFeatureType> opt = geogig.command(ResolveFeatureType.class).setRefSpec(ref).call();
if (opt.isPresent()) {
RevFeatureType ft = opt.get();
ImmutableList<PropertyDescriptor> attribs = ft.sortedDescriptors();
RevFeature feature = (RevFeature) revObject;
Ansi ansi = super.newAnsi(console.getTerminal());
ansi.a(ref).newline();
ansi.a(feature.getId().toString()).newline();
ImmutableList<Optional<Object>> values = feature.getValues();
int i = 0;
for (Optional<Object> value : values) {
PropertyDescriptor attrib = attribs.get(i);
ansi.a(attrib.getName()).newline();
PropertyType attrType = attrib.getType();
String typeName = FieldType.forBinding(attrType.getBinding()).name();
if (attrType instanceof GeometryType) {
GeometryType gt = (GeometryType) attrType;
CoordinateReferenceSystem crs = gt.getCoordinateReferenceSystem();
String crsText = CrsTextSerializer.serialize(crs);
ansi.a(typeName).a(" ").a(crsText).newline();
} else {
ansi.a(typeName).newline();
}
ansi.a(value.or("[NULL]").toString()).newline();
i++;
}
console.println(ansi.toString());
} else {
CharSequence s = geogig.command(CatObject.class).setObject(Suppliers.ofInstance(revObject)).call();
console.println(s);
}
} else {
CharSequence s = geogig.command(CatObject.class).setObject(Suppliers.ofInstance(revObject)).call();
console.println(s);
}
}
}
Aggregations