Search in sources :

Example 1 with OSMMappingLogEntry

use of org.locationtech.geogig.osm.internal.log.OSMMappingLogEntry in project GeoGig by boundlessgeo.

the class OSMUnmapOp method _call.

@Override
protected RevTree _call() {
    Optional<OSMMappingLogEntry> entry = command(ReadOSMMappingLogEntry.class).setPath(path).call();
    if (entry.isPresent()) {
        Optional<Mapping> opt = command(ReadOSMMapping.class).setEntry(entry.get()).call();
        if (opt.isPresent()) {
            mapping = opt.get();
        }
    }
    Iterator<NodeRef> iter = command(LsTreeOp.class).setReference(path).setStrategy(Strategy.FEATURES_ONLY).call();
    FeatureMapFlusher flusher = new FeatureMapFlusher(workingTree());
    while (iter.hasNext()) {
        NodeRef node = iter.next();
        RevFeature revFeature = command(RevObjectParse.class).setObjectId(node.objectId()).call(RevFeature.class).get();
        RevFeatureType revFeatureType = command(RevObjectParse.class).setObjectId(node.getMetadataId()).call(RevFeatureType.class).get();
        List<PropertyDescriptor> descriptors = revFeatureType.sortedDescriptors();
        ImmutableList<Optional<Object>> values = revFeature.getValues();
        SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder((SimpleFeatureType) revFeatureType.type());
        String id = null;
        for (int i = 0; i < descriptors.size(); i++) {
            PropertyDescriptor descriptor = descriptors.get(i);
            if (descriptor.getName().getLocalPart().equals("id")) {
                id = values.get(i).get().toString();
            }
            Optional<Object> value = values.get(i);
            featureBuilder.set(descriptor.getName(), value.orNull());
        }
        Preconditions.checkNotNull(id, "No 'id' attribute found");
        SimpleFeature feature = featureBuilder.buildFeature(id);
        unmapFeature(feature, flusher);
    }
    flusher.flushAll();
    if (entry.isPresent()) {
        Iterator<DiffEntry> diffs = command(DiffTree.class).setPathFilter(path).setNewTree(workingTree().getTree().getId()).setOldTree(entry.get().getPostMappingId()).call();
        while (diffs.hasNext()) {
            DiffEntry diff = diffs.next();
            if (diff.changeType().equals(DiffEntry.ChangeType.REMOVED)) {
                ObjectId featureId = diff.getOldObject().getNode().getObjectId();
                RevFeature revFeature = command(RevObjectParse.class).setObjectId(featureId).call(RevFeature.class).get();
                RevFeatureType revFeatureType = command(RevObjectParse.class).setObjectId(diff.getOldObject().getMetadataId()).call(RevFeatureType.class).get();
                List<PropertyDescriptor> descriptors = revFeatureType.sortedDescriptors();
                ImmutableList<Optional<Object>> values = revFeature.getValues();
                SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder((SimpleFeatureType) revFeatureType.type());
                String id = null;
                for (int i = 0; i < descriptors.size(); i++) {
                    PropertyDescriptor descriptor = descriptors.get(i);
                    if (descriptor.getName().getLocalPart().equals("id")) {
                        id = values.get(i).get().toString();
                    }
                    Optional<Object> value = values.get(i);
                    featureBuilder.set(descriptor.getName(), value.orNull());
                }
                Preconditions.checkNotNull(id, "No 'id' attribute found");
                SimpleFeature feature = featureBuilder.buildFeature(id);
                Class<?> clazz = feature.getDefaultGeometryProperty().getType().getBinding();
                String deletePath = clazz.equals(Point.class) ? OSMUtils.NODE_TYPE_NAME : OSMUtils.WAY_TYPE_NAME;
                workingTree().delete(deletePath, id);
            }
        }
    }
    return workingTree().getTree();
}
Also used : ReadOSMMapping(org.locationtech.geogig.osm.internal.log.ReadOSMMapping) LineString(com.vividsolutions.jts.geom.LineString) ReadOSMMappingLogEntry(org.locationtech.geogig.osm.internal.log.ReadOSMMappingLogEntry) OSMMappingLogEntry(org.locationtech.geogig.osm.internal.log.OSMMappingLogEntry) NodeRef(org.locationtech.geogig.api.NodeRef) RevFeature(org.locationtech.geogig.api.RevFeature) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) DiffEntry(org.locationtech.geogig.api.plumbing.diff.DiffEntry) PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) Optional(com.google.common.base.Optional) ObjectId(org.locationtech.geogig.api.ObjectId) Point(com.vividsolutions.jts.geom.Point) Point(com.vividsolutions.jts.geom.Point) SimpleFeature(org.opengis.feature.simple.SimpleFeature) LsTreeOp(org.locationtech.geogig.api.plumbing.LsTreeOp) RevObjectParse(org.locationtech.geogig.api.plumbing.RevObjectParse) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder)

Example 2 with OSMMappingLogEntry

use of org.locationtech.geogig.osm.internal.log.OSMMappingLogEntry in project GeoGig by boundlessgeo.

the class OSMImportOp method _call.

@Override
protected Optional<OSMReport> _call() {
    checkNotNull(urlOrFilepath);
    ObjectId oldTreeId = workingTree().getTree().getId();
    File osmDataFile = null;
    final InputStream osmDataStream;
    if (urlOrFilepath.startsWith("http")) {
        osmDataStream = downloadFile();
    } else {
        osmDataFile = new File(urlOrFilepath);
        Preconditions.checkArgument(osmDataFile.exists(), "File does not exist: " + urlOrFilepath);
        try {
            osmDataStream = new BufferedInputStream(new FileInputStream(osmDataFile), 1024 * 1024);
        } catch (FileNotFoundException e) {
            throw Throwables.propagate(e);
        }
    }
    ProgressListener progressListener = getProgressListener();
    progressListener.setDescription("Importing into GeoGig repo...");
    EntityConverter converter = new EntityConverter();
    OSMReport report;
    try {
        report = parseDataFileAndInsert(osmDataFile, osmDataStream, converter);
    } finally {
        Closeables.closeQuietly(osmDataStream);
    }
    if (!progressListener.isCanceled() && report != null) {
        ObjectId newTreeId = workingTree().getTree().getId();
        if (!noRaw) {
            if (mapping != null || filter != null) {
                progressListener.setDescription("Staging features...");
                command(AddOp.class).setProgressListener(progressListener).call();
                progressListener.setDescription("Committing features...");
                command(CommitOp.class).setMessage(message).setProgressListener(progressListener).call();
                OSMLogEntry entry = new OSMLogEntry(newTreeId, report.getLatestChangeset(), report.getLatestTimestamp());
                command(AddOSMLogEntry.class).setEntry(entry).call();
                if (filter != null) {
                    command(WriteOSMFilterFile.class).setEntry(entry).setFilterCode(filter).call();
                }
                if (mapping != null) {
                    command(WriteOSMMappingEntries.class).setMapping(mapping).setMappingLogEntry(new OSMMappingLogEntry(oldTreeId, newTreeId)).call();
                }
            }
        }
    }
    return Optional.fromNullable(report);
}
Also used : ObjectId(org.locationtech.geogig.api.ObjectId) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) WriteOSMMappingEntries(org.locationtech.geogig.osm.internal.log.WriteOSMMappingEntries) CommitOp(org.locationtech.geogig.api.porcelain.CommitOp) OSMMappingLogEntry(org.locationtech.geogig.osm.internal.log.OSMMappingLogEntry) FileInputStream(java.io.FileInputStream) ProgressListener(org.locationtech.geogig.api.ProgressListener) SubProgressListener(org.locationtech.geogig.api.SubProgressListener) BufferedInputStream(java.io.BufferedInputStream) WriteOSMFilterFile(org.locationtech.geogig.osm.internal.log.WriteOSMFilterFile) WriteOSMFilterFile(org.locationtech.geogig.osm.internal.log.WriteOSMFilterFile) File(java.io.File) AddOSMLogEntry(org.locationtech.geogig.osm.internal.log.AddOSMLogEntry) OSMLogEntry(org.locationtech.geogig.osm.internal.log.OSMLogEntry)

Example 3 with OSMMappingLogEntry

use of org.locationtech.geogig.osm.internal.log.OSMMappingLogEntry in project GeoGig by boundlessgeo.

the class OSMMapOp method _call.

@Override
protected RevTree _call() {
    checkNotNull(mapping);
    long staged = index().countStaged(null).count();
    long unstaged = workingTree().countUnstaged(null).count();
    Preconditions.checkState((staged == 0 && unstaged == 0), "You must have a clean working tree and index to perform a mapping.");
    ObjectId oldTreeId = workingTree().getTree().getId();
    Iterator<Feature> nodes;
    if (mapping.canUseNodes()) {
        nodes = getFeatures("WORK_HEAD:node");
    } else {
        nodes = Iterators.emptyIterator();
    }
    Iterator<Feature> ways;
    if (mapping.canUseWays()) {
        ways = getFeatures("WORK_HEAD:way");
    } else {
        ways = Iterators.emptyIterator();
    }
    Iterator<Feature> iterator = Iterators.concat(nodes, ways);
    if (iterator.hasNext()) {
        FeatureMapFlusher insertsByParent = new FeatureMapFlusher(workingTree());
        while (iterator.hasNext()) {
            Feature feature = iterator.next();
            List<MappedFeature> mappedFeatures = mapping.map(feature);
            if (!mappedFeatures.isEmpty()) {
                for (MappedFeature mapped : mappedFeatures) {
                    String path = mapped.getPath();
                    insertsByParent.put(path, mapped);
                }
            }
        }
        insertsByParent.flushAll();
        ObjectId newTreeId = workingTree().getTree().getId();
        // If the mapping generates the same mapped features that already exist, we do nothing
        if (!newTreeId.equals(oldTreeId)) {
            command(AddOp.class).call();
            command(CommitOp.class).setMessage(message).call();
            command(WriteOSMMappingEntries.class).setMapping(mapping).setMappingLogEntry(new OSMMappingLogEntry(oldTreeId, newTreeId)).call();
        }
    }
    return workingTree().getTree();
}
Also used : AddOp(org.locationtech.geogig.api.porcelain.AddOp) ObjectId(org.locationtech.geogig.api.ObjectId) WriteOSMMappingEntries(org.locationtech.geogig.osm.internal.log.WriteOSMMappingEntries) OSMMappingLogEntry(org.locationtech.geogig.osm.internal.log.OSMMappingLogEntry) RevFeature(org.locationtech.geogig.api.RevFeature) Feature(org.opengis.feature.Feature)

Aggregations

ObjectId (org.locationtech.geogig.api.ObjectId)3 OSMMappingLogEntry (org.locationtech.geogig.osm.internal.log.OSMMappingLogEntry)3 RevFeature (org.locationtech.geogig.api.RevFeature)2 WriteOSMMappingEntries (org.locationtech.geogig.osm.internal.log.WriteOSMMappingEntries)2 Optional (com.google.common.base.Optional)1 LineString (com.vividsolutions.jts.geom.LineString)1 Point (com.vividsolutions.jts.geom.Point)1 BufferedInputStream (java.io.BufferedInputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 InputStream (java.io.InputStream)1 SimpleFeatureBuilder (org.geotools.feature.simple.SimpleFeatureBuilder)1 NodeRef (org.locationtech.geogig.api.NodeRef)1 ProgressListener (org.locationtech.geogig.api.ProgressListener)1 RevFeatureType (org.locationtech.geogig.api.RevFeatureType)1 SubProgressListener (org.locationtech.geogig.api.SubProgressListener)1 LsTreeOp (org.locationtech.geogig.api.plumbing.LsTreeOp)1 RevObjectParse (org.locationtech.geogig.api.plumbing.RevObjectParse)1 DiffEntry (org.locationtech.geogig.api.plumbing.diff.DiffEntry)1