Search in sources :

Example 1 with OSMLogEntry

use of org.locationtech.geogig.osm.internal.log.OSMLogEntry 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 2 with OSMLogEntry

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

the class OSMUpdateOp method _call.

/**
     * Executes the {@code OSMUpdateOp} operation.
     * 
     * @return a {@link OSMDownloadReport} of the operation
     */
@Override
protected Optional<OSMReport> _call() {
    final Optional<Ref> currHead = command(RefParse.class).setName(Ref.HEAD).call();
    Preconditions.checkState(currHead.isPresent(), "Repository has no HEAD, can't update.");
    Preconditions.checkState(currHead.get() instanceof SymRef, "Can't update from detached HEAD");
    List<OSMLogEntry> entries = command(ReadOSMLogEntries.class).call();
    checkArgument(!entries.isEmpty(), "Not in a geogig repository with OSM data");
    Iterator<RevCommit> log = command(LogOp.class).setFirstParentOnly(false).setTopoOrder(false).call();
    RevCommit lastCommit = null;
    OSMLogEntry lastEntry = null;
    while (log.hasNext()) {
        RevCommit commit = log.next();
        for (OSMLogEntry entry : entries) {
            if (entry.getId().equals(commit.getTreeId())) {
                lastCommit = commit;
                lastEntry = entry;
                break;
            }
        }
        if (lastCommit != null) {
            break;
        }
    }
    checkNotNull(lastCommit, "The current branch does not contain OSM data");
    command(BranchCreateOp.class).setSource(lastCommit.getId().toString()).setName(OSMUtils.OSM_FETCH_BRANCH).setAutoCheckout(true).setForce(true).call();
    Optional<String> filter = command(ReadOSMFilterFile.class).setEntry(lastEntry).call();
    Preconditions.checkState(filter.isPresent(), "Filter file not found");
    message = message == null ? "Updated OSM data" : message;
    Optional<OSMReport> report = command(OSMImportOp.class).setMessage(message).setFilter(filter.get()).setDataSource(apiUrl).setProgressListener(getProgressListener()).call();
    if (!report.isPresent()) {
        return report;
    }
    if (!getProgressListener().isCanceled()) {
        command(CheckoutOp.class).setSource(((SymRef) currHead.get()).getTarget()).call();
        Optional<Ref> upstreamRef = command(RefParse.class).setName(OSMUtils.OSM_FETCH_BRANCH).call();
        Supplier<ObjectId> commitSupplier = Suppliers.ofInstance(upstreamRef.get().getObjectId());
        if (rebase) {
            getProgressListener().setDescription("Rebasing updated features...");
            command(RebaseOp.class).setUpstream(commitSupplier).setProgressListener(getProgressListener()).call();
        } else {
            getProgressListener().setDescription("Merging updated features...");
            command(MergeOp.class).addCommit(commitSupplier).setProgressListener(getProgressListener()).call();
        }
    }
    return report;
}
Also used : ReadOSMLogEntries(org.locationtech.geogig.osm.internal.log.ReadOSMLogEntries) ObjectId(org.locationtech.geogig.api.ObjectId) LogOp(org.locationtech.geogig.api.porcelain.LogOp) MergeOp(org.locationtech.geogig.api.porcelain.MergeOp) Ref(org.locationtech.geogig.api.Ref) SymRef(org.locationtech.geogig.api.SymRef) SymRef(org.locationtech.geogig.api.SymRef) BranchCreateOp(org.locationtech.geogig.api.porcelain.BranchCreateOp) RebaseOp(org.locationtech.geogig.api.porcelain.RebaseOp) OSMLogEntry(org.locationtech.geogig.osm.internal.log.OSMLogEntry) RevCommit(org.locationtech.geogig.api.RevCommit)

Example 3 with OSMLogEntry

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

the class OSMDownloadOpTest method testDownloadNodes.

@Ignore
@Test
public void testDownloadNodes() throws Exception {
    String filename = OSMImportOp.class.getResource("nodes_overpass_filter.txt").getFile();
    File filterFile = new File(filename);
    OSMDownloadOp download = geogig.command(OSMDownloadOp.class);
    download.setFilterFile(filterFile).setOsmAPIUrl(OSMUtils.DEFAULT_API_ENDPOINT).call();
    Optional<Node> tree = geogig.getRepository().getRootTreeChild("node");
    assertTrue(tree.isPresent());
    List<OSMLogEntry> entries = geogig.command(ReadOSMLogEntries.class).call();
    assertFalse(entries.isEmpty());
    Iterator<RevCommit> log = geogig.command(LogOp.class).call();
    assertTrue(log.hasNext());
}
Also used : ReadOSMLogEntries(org.locationtech.geogig.osm.internal.log.ReadOSMLogEntries) Node(org.locationtech.geogig.api.Node) LogOp(org.locationtech.geogig.api.porcelain.LogOp) File(java.io.File) OSMLogEntry(org.locationtech.geogig.osm.internal.log.OSMLogEntry) RevCommit(org.locationtech.geogig.api.RevCommit) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 4 with OSMLogEntry

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

the class OSMDownloadOpTest method testDowloadNodesWithDestinationFile.

@Ignore
@Test
public void testDowloadNodesWithDestinationFile() throws Exception {
    String filename = OSMImportOp.class.getResource("nodes_overpass_filter.txt").getFile();
    File filterFile = new File(filename);
    File downloadFile = File.createTempFile("osm-geogig", ".xml");
    OSMDownloadOp download = geogig.command(OSMDownloadOp.class);
    download.setFilterFile(filterFile).setSaveFile(downloadFile).setOsmAPIUrl(OSMUtils.DEFAULT_API_ENDPOINT).call();
    Optional<Node> tree = geogig.getRepository().getRootTreeChild("node");
    assertTrue(tree.isPresent());
    List<OSMLogEntry> entries = geogig.command(ReadOSMLogEntries.class).call();
    assertFalse(entries.isEmpty());
    Iterator<RevCommit> log = geogig.command(LogOp.class).call();
    assertTrue(log.hasNext());
}
Also used : ReadOSMLogEntries(org.locationtech.geogig.osm.internal.log.ReadOSMLogEntries) Node(org.locationtech.geogig.api.Node) LogOp(org.locationtech.geogig.api.porcelain.LogOp) File(java.io.File) OSMLogEntry(org.locationtech.geogig.osm.internal.log.OSMLogEntry) RevCommit(org.locationtech.geogig.api.RevCommit) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 5 with OSMLogEntry

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

the class OSMDownloadOpTest method testUpdate.

@Ignore
@Test
public void testUpdate() throws Exception {
    String filename = OSMImportOp.class.getResource("fire_station_filter.txt").getFile();
    File filterFile = new File(filename);
    OSMDownloadOp download = geogig.command(OSMDownloadOp.class);
    download.setFilterFile(filterFile).setOsmAPIUrl(OSMUtils.DEFAULT_API_ENDPOINT).call();
    Optional<Node> tree = geogig.getRepository().getRootTreeChild("node");
    assertTrue(tree.isPresent());
    tree = geogig.getRepository().getRootTreeChild("way");
    assertTrue(tree.isPresent());
    List<OSMLogEntry> entries = geogig.command(ReadOSMLogEntries.class).call();
    assertFalse(entries.isEmpty());
    OSMUpdateOp update = geogig.command(OSMUpdateOp.class);
    try {
        update.setAPIUrl(OSMUtils.DEFAULT_API_ENDPOINT).call();
    } catch (NothingToCommitException e) {
    // No new data
    }
}
Also used : ReadOSMLogEntries(org.locationtech.geogig.osm.internal.log.ReadOSMLogEntries) Node(org.locationtech.geogig.api.Node) NothingToCommitException(org.locationtech.geogig.api.porcelain.NothingToCommitException) File(java.io.File) OSMLogEntry(org.locationtech.geogig.osm.internal.log.OSMLogEntry) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

OSMLogEntry (org.locationtech.geogig.osm.internal.log.OSMLogEntry)5 File (java.io.File)4 ReadOSMLogEntries (org.locationtech.geogig.osm.internal.log.ReadOSMLogEntries)4 Ignore (org.junit.Ignore)3 Test (org.junit.Test)3 Node (org.locationtech.geogig.api.Node)3 RevCommit (org.locationtech.geogig.api.RevCommit)3 LogOp (org.locationtech.geogig.api.porcelain.LogOp)3 ObjectId (org.locationtech.geogig.api.ObjectId)2 BufferedInputStream (java.io.BufferedInputStream)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 InputStream (java.io.InputStream)1 ProgressListener (org.locationtech.geogig.api.ProgressListener)1 Ref (org.locationtech.geogig.api.Ref)1 SubProgressListener (org.locationtech.geogig.api.SubProgressListener)1 SymRef (org.locationtech.geogig.api.SymRef)1 BranchCreateOp (org.locationtech.geogig.api.porcelain.BranchCreateOp)1 CommitOp (org.locationtech.geogig.api.porcelain.CommitOp)1 MergeOp (org.locationtech.geogig.api.porcelain.MergeOp)1