use of org.locationtech.geogig.api.FeatureBuilder in project GeoGig by boundlessgeo.
the class GeoGigAPI method getFeatureFromWorkingTree.
/**
* Returns a feature from the working tree of the repository, given its full path
*
* Returns null if the given path doesn't resolve to a feature
*
* @param path the path to the feature to return
*/
public Feature getFeatureFromWorkingTree(String path) {
String name = NodeRef.nodeFromPath(path);
String refSpec = "WORK_HEAD:" + path;
Optional<RevFeature> revFeature = repository.command(RevObjectParse.class).setRefSpec(refSpec).call(RevFeature.class);
if (revFeature.isPresent()) {
RevFeatureType revFeatureType = repository.command(ResolveFeatureType.class).setRefSpec(refSpec).call().get();
FeatureBuilder builder = new FeatureBuilder(revFeatureType);
return builder.build(name, revFeature.get());
} else {
return null;
}
}
use of org.locationtech.geogig.api.FeatureBuilder in project GeoGig by boundlessgeo.
the class GeoGigAPI method getUnstagedFeatures.
public Feature[] getUnstagedFeatures(String path, boolean noDeletions) {
Iterator<DiffEntry> diffs = repository.workingTree().getUnstaged(path);
List<Feature> list = Lists.newArrayList();
while (diffs.hasNext()) {
DiffEntry diff = diffs.next();
if (!diff.changeType().equals(ChangeType.REMOVED) || !noDeletions) {
RevFeature revFeature = repository.command(RevObjectParse.class).setObjectId(diff.newObjectId()).call(RevFeature.class).get();
RevFeatureType revFeatureType = repository.command(RevObjectParse.class).setObjectId(diff.getNewObject().getMetadataId()).call(RevFeatureType.class).get();
FeatureBuilder builder = new FeatureBuilder(revFeatureType);
list.add(builder.build(diff.getNewObject().name(), revFeature));
}
}
return list.toArray(new Feature[0]);
}
use of org.locationtech.geogig.api.FeatureBuilder in project GeoGig by boundlessgeo.
the class ResponseWriter method writeMerged.
/**
* Writes the response for a set of merged features while also supplying the geometry.
*
* @param geogig - a CommandLocator to call commands from
* @param features - a FeatureInfo iterator to build the response from
* @throws XMLStreamException
*/
public void writeMerged(final Context geogig, Iterator<FeatureInfo> features) throws XMLStreamException {
Iterator<GeometryChange> changeIterator = Iterators.transform(features, new Function<FeatureInfo, GeometryChange>() {
@Override
public GeometryChange apply(FeatureInfo input) {
GeometryChange change = null;
RevFeature revFeature = RevFeatureBuilder.build(input.getFeature());
RevFeatureType featureType = input.getFeatureType();
Collection<PropertyDescriptor> attribs = featureType.type().getDescriptors();
String crsCode = null;
for (PropertyDescriptor attrib : attribs) {
PropertyType attrType = attrib.getType();
if (attrType instanceof GeometryType) {
GeometryType gt = (GeometryType) attrType;
CoordinateReferenceSystem crs = gt.getCoordinateReferenceSystem();
if (crs != null) {
try {
crsCode = CRS.lookupIdentifier(Citations.EPSG, crs, false);
} catch (FactoryException e) {
crsCode = null;
}
if (crsCode != null) {
crsCode = "EPSG:" + crsCode;
}
}
break;
}
}
FeatureBuilder builder = new FeatureBuilder(featureType);
GeogigSimpleFeature simpleFeature = (GeogigSimpleFeature) builder.build(revFeature.getId().toString(), revFeature);
change = new GeometryChange(simpleFeature, ChangeType.MODIFIED, input.getPath(), crsCode);
return change;
}
});
while (changeIterator.hasNext()) {
GeometryChange next = changeIterator.next();
if (next != null) {
GeogigSimpleFeature feature = next.getFeature();
out.writeStartElement("Feature");
writeElement("change", "MERGED");
writeElement("id", next.getPath());
List<Object> attributes = feature.getAttributes();
for (Object attribute : attributes) {
if (attribute instanceof Geometry) {
writeElement("geometry", ((Geometry) attribute).toText());
break;
}
}
if (next.getCRS() != null) {
writeElement("crs", next.getCRS());
}
out.writeEndElement();
}
}
}
use of org.locationtech.geogig.api.FeatureBuilder in project GeoGig by boundlessgeo.
the class OSMMapOp method getFeatures.
private Iterator<Feature> getFeatures(String ref) {
Optional<ObjectId> id = command(RevParse.class).setRefSpec(ref).call();
if (!id.isPresent()) {
return Iterators.emptyIterator();
}
LsTreeOp op = command(LsTreeOp.class).setStrategy(Strategy.DEPTHFIRST_ONLY_FEATURES).setReference(ref);
Iterator<NodeRef> iterator = op.call();
Function<NodeRef, Feature> nodeRefToFeature = new Function<NodeRef, Feature>() {
private final //
Map<String, FeatureBuilder> builders = //
ImmutableMap.<//
String, //
FeatureBuilder>of(//
OSMUtils.NODE_TYPE_NAME, //
new FeatureBuilder(RevFeatureTypeImpl.build(OSMUtils.nodeType())), //
OSMUtils.WAY_TYPE_NAME, new FeatureBuilder(RevFeatureTypeImpl.build(OSMUtils.wayType())));
private final RevObjectParse parseCommand = command(RevObjectParse.class);
@Override
@Nullable
public Feature apply(@Nullable NodeRef ref) {
RevFeature revFeature = parseCommand.setObjectId(ref.objectId()).call(RevFeature.class).get();
final String parentPath = ref.getParentPath();
FeatureBuilder featureBuilder = builders.get(parentPath);
String fid = ref.name();
Feature feature = featureBuilder.build(fid, revFeature);
return feature;
}
};
return Iterators.transform(iterator, nodeRefToFeature);
}
use of org.locationtech.geogig.api.FeatureBuilder 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));
}
}
Aggregations