use of org.opengis.feature.simple.SimpleFeature in project GeoGig by boundlessgeo.
the class OSMHistoryImport method insertChanges.
/**
* @param cli
* @param changes
* @param featureFilter
* @throws IOException
*/
private long insertChanges(GeogigCLI cli, final Iterator<Change> changes, @Nullable Envelope featureFilter) throws IOException {
final GeoGIG geogig = cli.getGeogig();
final Repository repository = geogig.getRepository();
final WorkingTree workTree = repository.workingTree();
Map<Long, Coordinate> thisChangePointCache = new LinkedHashMap<Long, Coordinate>() {
/** serialVersionUID */
private static final long serialVersionUID = 1277795218777240552L;
@Override
protected boolean removeEldestEntry(Map.Entry<Long, Coordinate> eldest) {
return size() == 10000;
}
};
long cnt = 0;
Set<String> deletes = Sets.newHashSet();
Multimap<String, SimpleFeature> insertsByParent = HashMultimap.create();
while (changes.hasNext()) {
Change change = changes.next();
final String featurePath = featurePath(change);
if (featurePath == null) {
// ignores relations
continue;
}
final String parentPath = NodeRef.parentPath(featurePath);
if (Change.Type.delete.equals(change.getType())) {
cnt++;
deletes.add(featurePath);
} else {
final Primitive primitive = change.getNode().isPresent() ? change.getNode().get() : change.getWay().get();
final Geometry geom = parseGeometry(geogig, primitive, thisChangePointCache);
if (geom instanceof Point) {
thisChangePointCache.put(Long.valueOf(primitive.getId()), ((Point) geom).getCoordinate());
}
SimpleFeature feature = toFeature(primitive, geom);
if (featureFilter == null || featureFilter.intersects((Envelope) feature.getBounds())) {
insertsByParent.put(parentPath, feature);
cnt++;
}
}
}
for (String parentPath : insertsByParent.keySet()) {
Collection<SimpleFeature> features = insertsByParent.get(parentPath);
if (features.isEmpty()) {
continue;
}
Iterator<? extends Feature> iterator = features.iterator();
ProgressListener listener = new DefaultProgressListener();
List<org.locationtech.geogig.api.Node> insertedTarget = null;
Integer collectionSize = Integer.valueOf(features.size());
workTree.insert(parentPath, iterator, listener, insertedTarget, collectionSize);
}
if (!deletes.isEmpty()) {
workTree.delete(deletes.iterator());
}
return cnt;
}
use of org.opengis.feature.simple.SimpleFeature in project GeoGig by boundlessgeo.
the class OSMHistoryImport method toFeature.
private static SimpleFeature toFeature(Primitive feature, Geometry geom) {
SimpleFeatureType ft = feature instanceof Node ? nodeType() : wayType();
SimpleFeatureBuilder builder = new SimpleFeatureBuilder(ft);
// "visible:Boolean,version:Int,timestamp:long,[location:Point | way:LineString];
builder.set("visible", Boolean.valueOf(feature.isVisible()));
builder.set("version", Integer.valueOf(feature.getVersion()));
builder.set("timestamp", Long.valueOf(feature.getTimestamp()));
builder.set("changeset", Long.valueOf(feature.getChangesetId()));
String tags = buildTagsString(feature.getTags());
builder.set("tags", tags);
String user = feature.getUserName() + ":" + feature.getUserId();
builder.set("user", user);
if (feature instanceof Node) {
builder.set("location", geom);
} else if (feature instanceof Way) {
builder.set("way", geom);
String nodes = buildNodesString(((Way) feature).getNodes());
builder.set("nodes", nodes);
} else {
throw new IllegalArgumentException();
}
String fid = String.valueOf(feature.getId());
SimpleFeature simpleFeature = builder.buildFeature(fid);
return simpleFeature;
}
use of org.opengis.feature.simple.SimpleFeature 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()]));
}
use of org.opengis.feature.simple.SimpleFeature in project GeoGig by boundlessgeo.
the class EntityConverter method toFeature.
public SimpleFeature toFeature(Entity entity, Geometry geom) {
SimpleFeatureType ft = entity instanceof Node ? OSMUtils.nodeType() : OSMUtils.wayType();
SimpleFeatureBuilder builder = new SimpleFeatureBuilder(ft, FEATURE_FACTORY);
// TODO: Check this!
builder.set("visible", Boolean.TRUE);
builder.set("version", Integer.valueOf(entity.getVersion()));
builder.set("timestamp", Long.valueOf(entity.getTimestamp().getTime()));
builder.set("changeset", Long.valueOf(entity.getChangesetId()));
String tags = OSMUtils.buildTagsString(entity.getTags());
builder.set("tags", tags);
String user = entity.getUser().getName() + ":" + Integer.toString(entity.getUser().getId());
builder.set("user", user);
if (entity instanceof Node) {
builder.set("location", geom);
} else if (entity instanceof Way) {
builder.set("way", geom);
String nodes = buildNodesString(((Way) entity).getWayNodes());
builder.set("nodes", nodes);
} else {
throw new IllegalArgumentException();
}
String fid = String.valueOf(entity.getId());
SimpleFeature simpleFeature = builder.buildFeature(fid);
return simpleFeature;
}
use of org.opengis.feature.simple.SimpleFeature in project GeoGig by boundlessgeo.
the class FeatureMapFlusher method flush.
private void flush(String path) {
Set<SimpleFeature> features = map.get(path);
if (!features.isEmpty()) {
Iterator<? extends Feature> iterator = features.iterator();
ProgressListener listener = new DefaultProgressListener();
List<org.locationtech.geogig.api.Node> insertedTarget = null;
Integer collectionSize = Integer.valueOf(features.size());
workTree.insert(path, iterator, listener, insertedTarget, collectionSize);
}
}
Aggregations