use of org.locationtech.geogig.api.RevFeature 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));
}
}
use of org.locationtech.geogig.api.RevFeature in project GeoGig by boundlessgeo.
the class OSMUnmapOp method getNodeStringFromWay.
/**
* This method takes a way and generates the corresponding string with ids of nodes than compose
* that line. If those nodes are declared in the "nodes" attribute of the feature, they will be
* referenced and no new node will be added. If a coordinate in the linestring doesn't have a
* corresponding node declared in the "nodes" attribute, a new node at that coordinate will be
* added to the "node" tree. This way, the returned linestring is guaranteed to refer to nodes
* that already exist in the repository, and as such can be safely used to add a new way that
* uses those nodes.
*
* @param line
* @return
*/
private String getNodeStringFromWay(SimpleFeature way, FeatureMapFlusher flusher) {
Map<Coordinate, Long> nodeCoords = Maps.newHashMap();
Object nodesAttribute = way.getAttribute("nodes");
if (nodesAttribute != null) {
String[] nodeIds = nodesAttribute.toString().split(";");
for (String nodeId : nodeIds) {
Optional<RevFeature> revFeature = command(RevObjectParse.class).setRefSpec("WORK_HEAD:" + OSMUtils.NODE_TYPE_NAME + "/" + nodeId).call(RevFeature.class);
if (revFeature.isPresent()) {
Optional<Object> location = revFeature.get().getValues().get(NODE_LOCATION_FIELD_INDEX);
if (location.isPresent()) {
Coordinate coord = ((Geometry) location.get()).getCoordinate();
nodeCoords.put(coord, Long.parseLong(nodeId));
}
}
}
}
List<Long> nodes = Lists.newArrayList();
Coordinate[] coords = ((Geometry) way.getDefaultGeometryProperty().getValue()).getCoordinates();
for (Coordinate coord : coords) {
if (nodeCoords.containsKey(coord)) {
nodes.add(nodeCoords.get(coord));
} else {
nodes.add(createNodeForCoord(coord, flusher));
}
}
return Joiner.on(';').join(nodes);
}
use of org.locationtech.geogig.api.RevFeature in project GeoGig by boundlessgeo.
the class CreateOSMChangesetOp method _call.
/**
* Executes the diff operation.
*
* @return an iterator to a set of differences between the two trees
* @see DiffEntry
*/
@Override
protected Iterator<ChangeContainer> _call() {
Iterator<DiffEntry> nodeIterator = command(DiffOp.class).setFilter(OSMUtils.NODE_TYPE_NAME).setNewVersion(newRefSpec).setOldVersion(oldRefSpec).setReportTrees(false).call();
Iterator<DiffEntry> wayIterator = command(DiffOp.class).setFilter(OSMUtils.WAY_TYPE_NAME).setNewVersion(newRefSpec).setOldVersion(oldRefSpec).setReportTrees(false).call();
Iterator<DiffEntry> iterator = Iterators.concat(nodeIterator, wayIterator);
final EntityConverter converter = new EntityConverter();
Function<DiffEntry, ChangeContainer> function = new Function<DiffEntry, ChangeContainer>() {
@Override
@Nullable
public ChangeContainer apply(@Nullable DiffEntry diff) {
NodeRef ref = diff.changeType().equals(ChangeType.REMOVED) ? diff.getOldObject() : diff.getNewObject();
RevFeature revFeature = command(RevObjectParse.class).setObjectId(ref.objectId()).call(RevFeature.class).get();
RevFeatureType revFeatureType = command(RevObjectParse.class).setObjectId(ref.getMetadataId()).call(RevFeatureType.class).get();
SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder((SimpleFeatureType) revFeatureType.type());
ImmutableList<PropertyDescriptor> descriptors = revFeatureType.sortedDescriptors();
ImmutableList<Optional<Object>> values = revFeature.getValues();
for (int i = 0; i < descriptors.size(); i++) {
PropertyDescriptor descriptor = descriptors.get(i);
Optional<Object> value = values.get(i);
featureBuilder.set(descriptor.getName(), value.orNull());
}
SimpleFeature feature = featureBuilder.buildFeature(ref.name());
Entity entity = converter.toEntity(feature, id);
EntityContainer container;
if (entity instanceof Node) {
container = new NodeContainer((Node) entity);
} else {
container = new WayContainer((Way) entity);
}
ChangeAction action = diff.changeType().equals(ChangeType.ADDED) ? ChangeAction.Create : diff.changeType().equals(ChangeType.MODIFIED) ? ChangeAction.Modify : ChangeAction.Delete;
return new ChangeContainer(container, action);
}
};
return Iterators.transform(iterator, function);
}
use of org.locationtech.geogig.api.RevFeature in project GeoGig by boundlessgeo.
the class OSMHistoryImport method parseGeometry.
/**
* @param primitive
* @param thisChangePointCache
* @return
*/
private Geometry parseGeometry(GeoGIG geogig, Primitive primitive, Map<Long, Coordinate> thisChangePointCache) {
if (primitive instanceof Relation) {
return null;
}
if (primitive instanceof Node) {
Optional<Point> location = ((Node) primitive).getLocation();
return location.orNull();
}
final Way way = (Way) primitive;
final ImmutableList<Long> nodes = way.getNodes();
StagingArea index = geogig.getRepository().index();
FeatureBuilder featureBuilder = new FeatureBuilder(NODE_REV_TYPE);
List<Coordinate> coordinates = Lists.newArrayList(nodes.size());
FindTreeChild findTreeChild = geogig.command(FindTreeChild.class);
findTreeChild.setIndex(true);
ObjectId rootTreeId = geogig.command(ResolveTreeish.class).setTreeish(Ref.HEAD).call().get();
if (!rootTreeId.isNull()) {
RevTree headTree = geogig.command(RevObjectParse.class).setObjectId(rootTreeId).call(RevTree.class).get();
findTreeChild.setParent(headTree);
}
for (Long nodeId : nodes) {
Coordinate coord = thisChangePointCache.get(nodeId);
if (coord == null) {
String fid = String.valueOf(nodeId);
String path = NodeRef.appendChild(NODE_TYPE_NAME, fid);
Optional<org.locationtech.geogig.api.Node> ref = index.findStaged(path);
if (!ref.isPresent()) {
Optional<NodeRef> nodeRef = findTreeChild.setChildPath(path).call();
if (nodeRef.isPresent()) {
ref = Optional.of(nodeRef.get().getNode());
} else {
ref = Optional.absent();
}
}
if (ref.isPresent()) {
org.locationtech.geogig.api.Node nodeRef = ref.get();
RevFeature revFeature = index.getDatabase().getFeature(nodeRef.getObjectId());
String id = NodeRef.nodeFromPath(nodeRef.getName());
Feature feature = featureBuilder.build(id, revFeature);
Point p = (Point) ((SimpleFeature) feature).getAttribute("location");
if (p != null) {
coord = p.getCoordinate();
thisChangePointCache.put(Long.valueOf(nodeId), coord);
}
}
}
if (coord != null) {
coordinates.add(coord);
}
}
if (coordinates.size() < 2) {
return null;
}
return GEOMF.createLineString(coordinates.toArray(new Coordinate[coordinates.size()]));
}
Aggregations