Search in sources :

Example 1 with ProgressListener

use of org.locationtech.geogig.api.ProgressListener in project GeoGig by boundlessgeo.

the class OSMApplyDiffOp method _call.

@Override
protected Optional<OSMReport> _call() {
    checkNotNull(file);
    Preconditions.checkArgument(file.exists(), "File does not exist: " + file);
    ProgressListener progressListener = getProgressListener();
    progressListener.setDescription("Applying OSM diff file to GeoGig repo...");
    OSMReport report = parseDiffFileAndInsert();
    return Optional.fromNullable(report);
}
Also used : ProgressListener(org.locationtech.geogig.api.ProgressListener) SubProgressListener(org.locationtech.geogig.api.SubProgressListener)

Example 2 with ProgressListener

use of org.locationtech.geogig.api.ProgressListener 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 ProgressListener

use of org.locationtech.geogig.api.ProgressListener in project GeoGig by boundlessgeo.

the class OSMImportOp method downloadFile.

private InputStream downloadFile() {
    ProgressListener listener = getProgressListener();
    checkNotNull(filter);
    OSMDownloader downloader = new OSMDownloader(urlOrFilepath, listener);
    listener.setDescription("Connecting to " + urlOrFilepath + "...");
    File destination = null;
    if (keepFile) {
        destination = this.downloadFile;
        if (destination == null) {
            try {
                destination = File.createTempFile("osm-geogig", ".xml");
            } catch (IOException e) {
                Throwables.propagate(e);
            }
        } else {
            destination = destination.getAbsoluteFile();
        }
    }
    try {
        InputStream dataStream = downloader.download(filter, destination);
        if (keepFile) {
            listener.setDescription("Downloaded data will be kept in " + destination.getAbsolutePath());
        }
        return dataStream;
    } catch (Exception e) {
        throw Throwables.propagate(Throwables.getRootCause(e));
    }
}
Also used : ProgressListener(org.locationtech.geogig.api.ProgressListener) SubProgressListener(org.locationtech.geogig.api.SubProgressListener) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) WriteOSMFilterFile(org.locationtech.geogig.osm.internal.log.WriteOSMFilterFile) File(java.io.File) OsmosisRuntimeException(org.openstreetmap.osmosis.core.OsmosisRuntimeException) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException)

Example 4 with ProgressListener

use of org.locationtech.geogig.api.ProgressListener in project GeoGig by boundlessgeo.

the class WriteTree method _call.

/**
     * Executes the write tree operation.
     * 
     * @return the new root tree id, the current HEAD tree id if there are no differences between
     *         the index and the HEAD, or {@code null} if the operation has been cancelled (as
     *         indicated by the {@link #getProgressListener() progress listener}.
     */
@Override
protected ObjectId _call() {
    final ProgressListener progress = getProgressListener();
    final RevTree oldRootTree = resolveRootTree();
    final ObjectDatabase repositoryDatabase = objectDatabase();
    Iterator<DiffEntry> diffs = null;
    long numChanges = 0;
    if (diffSupplier == null) {
        diffs = index().getStaged(pathFilters);
        numChanges = index().countStaged(pathFilters).count();
    } else {
        diffs = diffSupplier.get();
    }
    if (!diffs.hasNext()) {
        return oldRootTree.getId();
    }
    if (progress.isCanceled()) {
        return null;
    }
    Map<String, RevTreeBuilder> repositoryChangedTrees = Maps.newHashMap();
    Map<String, NodeRef> indexChangedTrees = Maps.newHashMap();
    Map<String, ObjectId> changedTreesMetadataId = Maps.newHashMap();
    Set<String> deletedTrees = Sets.newHashSet();
    final boolean moveObjects = this.moveObjects;
    NodeRef ref;
    int i = 0;
    RevTree stageHead = index().getTree();
    while (diffs.hasNext()) {
        if (numChanges != 0) {
            progress.setProgress((float) (++i * 100) / numChanges);
        }
        if (progress.isCanceled()) {
            return null;
        }
        DiffEntry diff = diffs.next();
        // ignore the root entry
        if (NodeRef.ROOT.equals(diff.newName()) || NodeRef.ROOT.equals(diff.oldName())) {
            continue;
        }
        ref = diff.getNewObject();
        if (ref == null) {
            ref = diff.getOldObject();
        }
        final String parentPath = ref.getParentPath();
        final boolean isDelete = ChangeType.REMOVED.equals(diff.changeType());
        final TYPE type = ref.getType();
        if (isDelete && deletedTrees.contains(parentPath)) {
            // tree delete entry was processed
            continue;
        }
        RevTreeBuilder parentTree = resolveTargetTree(oldRootTree, parentPath, repositoryChangedTrees, changedTreesMetadataId, ObjectId.NULL, repositoryDatabase);
        if (type == TYPE.TREE && !isDelete) {
            // cache the tree
            resolveTargetTree(oldRootTree, ref.name(), repositoryChangedTrees, changedTreesMetadataId, ref.getMetadataId(), repositoryDatabase);
        }
        resolveSourceTreeRef(parentPath, indexChangedTrees, changedTreesMetadataId, stageHead);
        Preconditions.checkState(parentTree != null);
        if (isDelete) {
            String oldName = diff.getOldObject().getNode().getName();
            parentTree.remove(oldName);
            if (TYPE.TREE.equals(type)) {
                deletedTrees.add(ref.path());
            }
        } else {
            if (moveObjects && ref.getType().equals(TYPE.TREE)) {
                RevTree tree = stagingDatabase().getTree(ref.objectId());
                if (!ref.getMetadataId().isNull()) {
                    repositoryDatabase.put(stagingDatabase().getFeatureType(ref.getMetadataId()));
                }
                if (tree.isEmpty()) {
                    repositoryDatabase.put(tree);
                } else {
                    continue;
                }
            } else if (moveObjects) {
                deepMove(ref.getNode());
            }
            parentTree.put(ref.getNode());
        }
    }
    if (progress.isCanceled()) {
        return null;
    }
    // now write back all changed trees
    ObjectId newTargetRootId = oldRootTree.getId();
    RevTreeBuilder directRootEntries = repositoryChangedTrees.remove(NodeRef.ROOT);
    if (directRootEntries != null) {
        RevTree newRoot = directRootEntries.build();
        repositoryDatabase.put(newRoot);
        newTargetRootId = newRoot.getId();
    }
    for (Map.Entry<String, RevTreeBuilder> e : repositoryChangedTrees.entrySet()) {
        String treePath = e.getKey();
        ObjectId metadataId = changedTreesMetadataId.get(treePath);
        RevTreeBuilder treeBuilder = e.getValue();
        RevTree newRoot = getTree(newTargetRootId);
        RevTree tree = treeBuilder.build();
        newTargetRootId = writeBack(newRoot.builder(repositoryDatabase), tree, treePath, metadataId);
    }
    progress.complete();
    return newTargetRootId;
}
Also used : ObjectId(org.locationtech.geogig.api.ObjectId) RevTreeBuilder(org.locationtech.geogig.api.RevTreeBuilder) NodeRef(org.locationtech.geogig.api.NodeRef) ProgressListener(org.locationtech.geogig.api.ProgressListener) ObjectDatabase(org.locationtech.geogig.storage.ObjectDatabase) TYPE(org.locationtech.geogig.api.RevObject.TYPE) Map(java.util.Map) RevTree(org.locationtech.geogig.api.RevTree) DiffEntry(org.locationtech.geogig.api.plumbing.diff.DiffEntry)

Example 5 with ProgressListener

use of org.locationtech.geogig.api.ProgressListener in project GeoGig by boundlessgeo.

the class CloneOp method _call.

/**
     * Executes the clone operation.
     * 
     * @return {@code null}
     * @see org.locationtech.geogig.api.AbstractGeoGigOp#call()
     */
@Override
protected Void _call() {
    Preconditions.checkArgument(repositoryURL != null && !repositoryURL.isEmpty(), "No repository specified to clone from.");
    Repository repository = repository();
    if (repository.isSparse()) {
        Preconditions.checkArgument(branch.isPresent(), "No branch specified for sparse clone.");
    }
    ProgressListener progressListener = getProgressListener();
    progressListener.started();
    // Set up origin
    Remote remote = command(RemoteAddOp.class).setName("origin").setURL(repositoryURL).setMapped(repository.isSparse()).setUserName(username).setPassword(password).setBranch(repository.isSparse() ? branch.get() : null).call();
    if (!depth.isPresent()) {
        // See if we are cloning a shallow clone. If so, a depth must be specified.
        Optional<IRemoteRepo> remoteRepo = RemoteUtils.newRemote(GlobalContextBuilder.builder.build(Hints.readOnly()), remote, repository, repository.deduplicationService());
        Preconditions.checkState(remoteRepo.isPresent(), "Failed to connect to the remote.");
        IRemoteRepo remoteRepoInstance = remoteRepo.get();
        try {
            remoteRepoInstance.open();
        } catch (IOException e) {
            Throwables.propagate(e);
        }
        try {
            depth = remoteRepoInstance.getDepth();
        } finally {
            try {
                remoteRepoInstance.close();
            } catch (IOException e) {
                Throwables.propagate(e);
            }
        }
    }
    if (depth.isPresent()) {
        command(ConfigOp.class).setAction(ConfigAction.CONFIG_SET).setScope(ConfigScope.LOCAL).setName(Repository.DEPTH_CONFIG_KEY).setValue(depth.get().toString()).call();
    }
    // Fetch remote data
    command(FetchOp.class).setDepth(depth.or(0)).setProgressListener(progressListener).call();
    // Set up remote tracking branches
    final ImmutableSet<Ref> remoteRefs = command(LsRemote.class).retrieveTags(false).setRemote(Suppliers.ofInstance(Optional.of(remote))).call();
    boolean emptyRepo = true;
    for (Ref remoteRef : remoteRefs) {
        if (emptyRepo && !remoteRef.getObjectId().isNull()) {
            emptyRepo = false;
        }
        String branchName = remoteRef.localName();
        if (remoteRef instanceof SymRef) {
            continue;
        }
        if (!command(RefParse.class).setName(Ref.HEADS_PREFIX + branchName).call().isPresent()) {
            command(BranchCreateOp.class).setName(branchName).setSource(remoteRef.getObjectId().toString()).call();
        } else {
            command(UpdateRef.class).setName(Ref.HEADS_PREFIX + branchName).setNewValue(remoteRef.getObjectId()).call();
        }
        command(ConfigOp.class).setAction(ConfigAction.CONFIG_SET).setScope(ConfigScope.LOCAL).setName("branches." + branchName + ".remote").setValue(remote.getName()).call();
        command(ConfigOp.class).setAction(ConfigAction.CONFIG_SET).setScope(ConfigScope.LOCAL).setName("branches." + branchName + ".merge").setValue(Ref.HEADS_PREFIX + remoteRef.localName()).call();
    }
    if (!emptyRepo) {
        // checkout branch
        if (branch.isPresent()) {
            command(CheckoutOp.class).setForce(true).setSource(branch.get()).call();
        } else {
            // checkout the head
            final Optional<Ref> currRemoteHead = command(RefParse.class).setName(Ref.REMOTES_PREFIX + remote.getName() + "/" + Ref.HEAD).call();
            if (currRemoteHead.isPresent() && currRemoteHead.get() instanceof SymRef) {
                final SymRef remoteHeadRef = (SymRef) currRemoteHead.get();
                final String currentBranch = Ref.localName(remoteHeadRef.getTarget());
                command(CheckoutOp.class).setForce(true).setSource(currentBranch).call();
            } else {
            // just leave at default; should be master since we just initialized the repo.
            }
        }
    }
    progressListener.complete();
    return null;
}
Also used : Remote(org.locationtech.geogig.api.Remote) LsRemote(org.locationtech.geogig.api.plumbing.LsRemote) UpdateRef(org.locationtech.geogig.api.plumbing.UpdateRef) IOException(java.io.IOException) Repository(org.locationtech.geogig.repository.Repository) Ref(org.locationtech.geogig.api.Ref) UpdateRef(org.locationtech.geogig.api.plumbing.UpdateRef) SymRef(org.locationtech.geogig.api.SymRef) SymRef(org.locationtech.geogig.api.SymRef) ProgressListener(org.locationtech.geogig.api.ProgressListener) IRemoteRepo(org.locationtech.geogig.remote.IRemoteRepo) LsRemote(org.locationtech.geogig.api.plumbing.LsRemote)

Aggregations

ProgressListener (org.locationtech.geogig.api.ProgressListener)25 IOException (java.io.IOException)9 CommandFailedException (org.locationtech.geogig.cli.CommandFailedException)7 DataStore (org.geotools.data.DataStore)6 ObjectId (org.locationtech.geogig.api.ObjectId)6 SubProgressListener (org.locationtech.geogig.api.SubProgressListener)6 GeoToolsOpException (org.locationtech.geogig.geotools.plumbing.GeoToolsOpException)6 WorkingTree (org.locationtech.geogig.repository.WorkingTree)6 SimpleFeature (org.opengis.feature.simple.SimpleFeature)6 DefaultProgressListener (org.locationtech.geogig.api.DefaultProgressListener)5 NodeRef (org.locationtech.geogig.api.NodeRef)5 Function (com.google.common.base.Function)4 Ref (org.locationtech.geogig.api.Ref)4 RevTree (org.locationtech.geogig.api.RevTree)4 SymRef (org.locationtech.geogig.api.SymRef)4 Feature (org.opengis.feature.Feature)4 SimpleFeatureType (org.opengis.feature.simple.SimpleFeatureType)4 Point (com.vividsolutions.jts.geom.Point)3 Node (org.locationtech.geogig.api.Node)3 RevFeature (org.locationtech.geogig.api.RevFeature)3