Search in sources :

Example 6 with TYPE

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

the class BlameOp method _call.

@Override
protected BlameReport _call() {
    String fullPath = (commit != null ? commit.toString() : Ref.HEAD) + ":" + path;
    Optional<ObjectId> id = command(RevParse.class).setRefSpec(fullPath).call();
    if (!id.isPresent()) {
        throw new BlameException(StatusCode.FEATURE_NOT_FOUND);
    }
    TYPE type = command(ResolveObjectType.class).setObjectId(id.get()).call();
    if (!type.equals(TYPE.FEATURE)) {
        throw new BlameException(StatusCode.PATH_NOT_FEATURE);
    }
    Optional<RevFeatureType> featureType = command(ResolveFeatureType.class).setRefSpec(path).call();
    BlameReport report = new BlameReport(featureType.get());
    Iterator<RevCommit> log = command(LogOp.class).addPath(path).setUntil(commit).call();
    RevCommit commit = log.next();
    RevObjectParse revObjectParse = command(RevObjectParse.class);
    DiffOp diffOp = command(DiffOp.class);
    DiffFeature diffFeature = command(DiffFeature.class);
    while (!report.isComplete()) {
        if (!log.hasNext()) {
            String refSpec = commit.getId().toString() + ":" + path;
            RevFeature feature = revObjectParse.setRefSpec(refSpec).call(RevFeature.class).get();
            report.setFirstVersion(feature, commit);
            break;
        }
        RevCommit commitB = log.next();
        Iterator<DiffEntry> diffs = diffOp.setNewVersion(commit.getId()).setOldVersion(commitB.getId()).setReportTrees(false).call();
        while (diffs.hasNext()) {
            DiffEntry diff = diffs.next();
            if (path.equals(diff.newPath())) {
                if (diff.isAdd()) {
                    String refSpec = commit.getId().toString() + ":" + path;
                    RevFeature feature = revObjectParse.setRefSpec(refSpec).call(RevFeature.class).get();
                    report.setFirstVersion(feature, commit);
                    break;
                }
                FeatureDiff featureDiff = diffFeature.setNewVersion(Suppliers.ofInstance(diff.getNewObject())).setOldVersion(Suppliers.ofInstance(diff.getOldObject())).call();
                Map<PropertyDescriptor, AttributeDiff> attribDiffs = featureDiff.getDiffs();
                Iterator<PropertyDescriptor> iter = attribDiffs.keySet().iterator();
                while (iter.hasNext()) {
                    PropertyDescriptor key = iter.next();
                    Optional<?> value = attribDiffs.get(key).getNewValue();
                    String attribute = key.getName().toString();
                    report.addDiff(attribute, value, commit);
                }
            }
        }
        commit = commitB;
    }
    return report;
}
Also used : PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) ObjectId(org.locationtech.geogig.api.ObjectId) DiffFeature(org.locationtech.geogig.api.plumbing.DiffFeature) FeatureDiff(org.locationtech.geogig.api.plumbing.diff.FeatureDiff) RevFeature(org.locationtech.geogig.api.RevFeature) AttributeDiff(org.locationtech.geogig.api.plumbing.diff.AttributeDiff) RevObjectParse(org.locationtech.geogig.api.plumbing.RevObjectParse) TYPE(org.locationtech.geogig.api.RevObject.TYPE) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) RevCommit(org.locationtech.geogig.api.RevCommit) DiffEntry(org.locationtech.geogig.api.plumbing.diff.DiffEntry)

Example 7 with TYPE

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

the class BranchCreateOp method resolveOriginCommitId.

private ObjectId resolveOriginCommitId(String branchOrigin) {
    Optional<Ref> ref = command(RefParse.class).setName(branchOrigin).call();
    if (ref.isPresent()) {
        ObjectId commitId = ref.get().getObjectId();
        checkArgument(!commitId.isNull(), branchOrigin + " has no commits yet, branch cannot be created.");
        return commitId;
    }
    Optional<ObjectId> objectId = command(RevParse.class).setRefSpec(branchOrigin).call();
    checkArgument(objectId.isPresent(), branchOrigin + " does not resolve to a repository object");
    ObjectId commitId = objectId.get();
    TYPE objectType = command(ResolveObjectType.class).setObjectId(commitId).call();
    checkArgument(TYPE.COMMIT.equals(objectType), branchOrigin + " does not resolve to a commit: " + objectType);
    return commitId;
}
Also used : Ref(org.locationtech.geogig.api.Ref) UpdateRef(org.locationtech.geogig.api.plumbing.UpdateRef) ObjectId(org.locationtech.geogig.api.ObjectId) TYPE(org.locationtech.geogig.api.RevObject.TYPE)

Example 8 with TYPE

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

the class Commit method runInternal.

/**
     * Executes the commit command using the provided options.
     * 
     * @param cli
     * @see org.locationtech.geogig.cli.AbstractCommand#runInternal(org.locationtech.geogig.cli.GeogigCLI)
     */
@Override
public void runInternal(GeogigCLI cli) throws IOException {
    final GeoGIG geogig = cli.getGeogig();
    if (message == null || Strings.isNullOrEmpty(message)) {
        message = geogig.command(ReadMergeCommitMessageOp.class).call();
    }
    checkParameter(!Strings.isNullOrEmpty(message) || commitToReuse != null || amend, "No commit message provided");
    ConsoleReader console = cli.getConsole();
    Ansi ansi = newAnsi(console.getTerminal());
    RevCommit commit;
    try {
        CommitOp commitOp = geogig.command(CommitOp.class).setMessage(message).setAmend(amend);
        if (commitTimestamp != null && !Strings.isNullOrEmpty(commitTimestamp)) {
            Long millis = geogig.command(ParseTimestamp.class).setString(commitTimestamp).call();
            commitOp.setCommitterTimestamp(millis.longValue());
        }
        if (commitToReuse != null) {
            Optional<ObjectId> commitId = geogig.command(RevParse.class).setRefSpec(commitToReuse).call();
            checkParameter(commitId.isPresent(), "Provided reference does not exist");
            TYPE type = geogig.command(ResolveObjectType.class).setObjectId(commitId.get()).call();
            checkParameter(TYPE.COMMIT.equals(type), "Provided reference does not resolve to a commit");
            commitOp.setCommit(geogig.getRepository().getCommit(commitId.get()));
        }
        commit = commitOp.setPathFilters(pathFilters).setProgressListener(cli.getProgressListener()).call();
    } catch (NothingToCommitException noChanges) {
        throw new CommandFailedException(noChanges.getMessage(), noChanges);
    }
    final ObjectId parentId = commit.parentN(0).or(ObjectId.NULL);
    console.println("[" + commit.getId() + "] " + commit.getMessage());
    console.print("Committed, counting objects...");
    Iterator<DiffEntry> diff = geogig.command(DiffOp.class).setOldVersion(parentId).setNewVersion(commit.getId()).call();
    int adds = 0, deletes = 0, changes = 0;
    DiffEntry diffEntry;
    while (diff.hasNext()) {
        diffEntry = diff.next();
        switch(diffEntry.changeType()) {
            case ADDED:
                ++adds;
                break;
            case REMOVED:
                ++deletes;
                break;
            case MODIFIED:
                ++changes;
                break;
        }
    }
    ansi.fg(Color.GREEN).a(adds).reset().a(" features added, ").fg(Color.YELLOW).a(changes).reset().a(" changed, ").fg(Color.RED).a(deletes).reset().a(" deleted.").reset().newline();
    console.print(ansi.toString());
}
Also used : ConsoleReader(jline.console.ConsoleReader) ObjectId(org.locationtech.geogig.api.ObjectId) NothingToCommitException(org.locationtech.geogig.api.porcelain.NothingToCommitException) DiffOp(org.locationtech.geogig.api.porcelain.DiffOp) CommitOp(org.locationtech.geogig.api.porcelain.CommitOp) CommandFailedException(org.locationtech.geogig.cli.CommandFailedException) Ansi(org.fusesource.jansi.Ansi) TYPE(org.locationtech.geogig.api.RevObject.TYPE) GeoGIG(org.locationtech.geogig.api.GeoGIG) RevCommit(org.locationtech.geogig.api.RevCommit) DiffEntry(org.locationtech.geogig.api.plumbing.diff.DiffEntry)

Example 9 with TYPE

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

the class SQLServerExport method runInternal.

/**
     * Executes the export command using the provided options.
     */
@Override
protected void runInternal(GeogigCLI cli) throws IOException {
    if (args.isEmpty()) {
        printUsage(cli);
        throw new CommandFailedException();
    }
    String path = args.get(0);
    String tableName = args.get(1);
    checkParameter(tableName != null && !tableName.isEmpty(), "No table name specified");
    DataStore dataStore = getDataStore();
    ObjectId featureTypeId = null;
    if (!Arrays.asList(dataStore.getTypeNames()).contains(tableName)) {
        SimpleFeatureType outputFeatureType;
        if (sFeatureTypeId != null) {
            // Check the feature type id string is a correct id
            Optional<ObjectId> id = cli.getGeogig().command(RevParse.class).setRefSpec(sFeatureTypeId).call();
            checkParameter(id.isPresent(), "Invalid feature type reference", sFeatureTypeId);
            TYPE type = cli.getGeogig().command(ResolveObjectType.class).setObjectId(id.get()).call();
            checkParameter(type.equals(TYPE.FEATURETYPE), "Provided reference does not resolve to a feature type: ", sFeatureTypeId);
            outputFeatureType = (SimpleFeatureType) cli.getGeogig().command(RevObjectParse.class).setObjectId(id.get()).call(RevFeatureType.class).get().type();
            featureTypeId = id.get();
        } else {
            try {
                SimpleFeatureType sft = getFeatureType(path, cli);
                outputFeatureType = new SimpleFeatureTypeImpl(new NameImpl(tableName), sft.getAttributeDescriptors(), sft.getGeometryDescriptor(), sft.isAbstract(), sft.getRestrictions(), sft.getSuper(), sft.getDescription());
            } catch (GeoToolsOpException e) {
                throw new CommandFailedException("No features to export.", e);
            }
        }
        try {
            dataStore.createSchema(outputFeatureType);
        } catch (IOException e) {
            throw new CommandFailedException("Cannot create new table in database", e);
        }
    } else {
        if (!overwrite) {
            throw new CommandFailedException("The selected table already exists. Use -o to overwrite");
        }
    }
    SimpleFeatureSource featureSource = dataStore.getFeatureSource(tableName);
    if (!(featureSource instanceof SimpleFeatureStore)) {
        throw new CommandFailedException("Can't write to the selected table");
    }
    SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
    if (overwrite) {
        try {
            featureStore.removeFeatures(Filter.INCLUDE);
        } catch (IOException e) {
            throw new CommandFailedException("Error accessing table: " + e.getMessage(), e);
        }
    }
    ExportOp op = cli.getGeogig().command(ExportOp.class).setFeatureStore(featureStore).setPath(path).setFilterFeatureTypeId(featureTypeId).setAlter(alter);
    if (defaultType) {
        op.exportDefaultFeatureType();
    }
    try {
        op.setProgressListener(cli.getProgressListener()).call();
    } catch (IllegalArgumentException iae) {
        throw new org.locationtech.geogig.cli.InvalidParameterException(iae.getMessage(), iae);
    } catch (GeoToolsOpException e) {
        switch(e.statusCode) {
            case MIXED_FEATURE_TYPES:
                throw new CommandFailedException("The selected tree contains mixed feature types. Use --defaulttype or --featuretype <feature_type_ref> to export.", e);
            default:
                throw new CommandFailedException("Could not export. Error:" + e.statusCode.name(), e);
        }
    }
    cli.getConsole().println(path + " exported successfully to " + tableName);
}
Also used : NameImpl(org.geotools.feature.NameImpl) ObjectId(org.locationtech.geogig.api.ObjectId) SimpleFeatureSource(org.geotools.data.simple.SimpleFeatureSource) IOException(java.io.IOException) InvalidParameterException(org.locationtech.geogig.cli.InvalidParameterException) CommandFailedException(org.locationtech.geogig.cli.CommandFailedException) SimpleFeatureTypeImpl(org.geotools.feature.simple.SimpleFeatureTypeImpl) GeoToolsOpException(org.locationtech.geogig.geotools.plumbing.GeoToolsOpException) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) DataStore(org.geotools.data.DataStore) SimpleFeatureStore(org.geotools.data.simple.SimpleFeatureStore) ExportOp(org.locationtech.geogig.geotools.plumbing.ExportOp) TYPE(org.locationtech.geogig.api.RevObject.TYPE)

Example 10 with TYPE

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

the class ShpExport method runInternal.

/**
     * Executes the export command using the provided options.
     */
@Override
protected void runInternal(GeogigCLI cli) throws IOException {
    if (args.isEmpty()) {
        printUsage(cli);
        throw new CommandFailedException();
    }
    String path = args.get(0);
    String shapefile = args.get(1);
    ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
    File targetShapefile = new File(shapefile);
    if (!targetShapefile.isAbsolute()) {
        File pwd = cli.getGeogig().getPlatform().pwd();
        String relativePath = targetShapefile.getPath();
        targetShapefile = new File(pwd, relativePath);
    }
    if (targetShapefile.exists() && !overwrite) {
        throw new CommandFailedException("The selected shapefile already exists. Use -o to overwrite");
    }
    Map<String, Serializable> params = new HashMap<String, Serializable>();
    URL targetShapefileAsUrl = targetShapefile.toURI().toURL();
    params.put(ShapefileDataStoreFactory.URLP.key, targetShapefileAsUrl);
    params.put(ShapefileDataStoreFactory.CREATE_SPATIAL_INDEX.key, Boolean.FALSE);
    params.put(ShapefileDataStoreFactory.ENABLE_SPATIAL_INDEX.key, Boolean.FALSE);
    ShapefileDataStore dataStore = (ShapefileDataStore) dataStoreFactory.createNewDataStore(params);
    SimpleFeatureType outputFeatureType;
    ObjectId featureTypeId;
    if (sFeatureTypeId != null) {
        // Check the feature type id string is a correct id
        Optional<ObjectId> id = cli.getGeogig().command(RevParse.class).setRefSpec(sFeatureTypeId).call();
        checkParameter(id.isPresent(), "Invalid feature type reference", sFeatureTypeId);
        TYPE type = cli.getGeogig().command(ResolveObjectType.class).setObjectId(id.get()).call();
        checkParameter(type.equals(TYPE.FEATURETYPE), "Provided reference does not resolve to a feature type: ", sFeatureTypeId);
        outputFeatureType = (SimpleFeatureType) cli.getGeogig().command(RevObjectParse.class).setObjectId(id.get()).call(RevFeatureType.class).get().type();
        featureTypeId = id.get();
    } else {
        try {
            outputFeatureType = getFeatureType(path, cli);
            featureTypeId = null;
        } catch (GeoToolsOpException e) {
            cli.getConsole().println("No features to export.");
            return;
        }
    }
    dataStore.createSchema(outputFeatureType);
    final String typeName = dataStore.getTypeNames()[0];
    final SimpleFeatureSource featureSource = dataStore.getFeatureSource(typeName);
    if (!(featureSource instanceof SimpleFeatureStore)) {
        throw new CommandFailedException("Could not create feature store.");
    }
    Function<Feature, Optional<Feature>> function = getTransformingFunction(dataStore.getSchema());
    final SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
    ExportOp op = cli.getGeogig().command(ExportOp.class).setFeatureStore(featureStore).setPath(path).setFilterFeatureTypeId(featureTypeId).setAlter(alter).setFeatureTypeConversionFunction(function);
    // shapefile transactions are memory bound, so avoid them
    op.setTransactional(false);
    if (defaultType) {
        op.exportDefaultFeatureType();
    }
    try {
        op.setProgressListener(cli.getProgressListener()).call();
    } catch (IllegalArgumentException iae) {
        throw new org.locationtech.geogig.cli.InvalidParameterException(iae.getMessage(), iae);
    } catch (GeoToolsOpException e) {
        targetShapefile.delete();
        switch(e.statusCode) {
            case MIXED_FEATURE_TYPES:
                throw new CommandFailedException("Error: The selected tree contains mixed feature types. Use --defaulttype or --featuretype <feature_type_ref> to export.", e);
            default:
                throw new CommandFailedException("Could not export. Error:" + e.statusCode.name(), e);
        }
    }
    cli.getConsole().println(path + " exported successfully to " + shapefile);
}
Also used : Serializable(java.io.Serializable) ShapefileDataStore(org.geotools.data.shapefile.ShapefileDataStore) Optional(com.google.common.base.Optional) HashMap(java.util.HashMap) ObjectId(org.locationtech.geogig.api.ObjectId) SimpleFeatureSource(org.geotools.data.simple.SimpleFeatureSource) InvalidParameterException(org.locationtech.geogig.cli.InvalidParameterException) Feature(org.opengis.feature.Feature) CommandFailedException(org.locationtech.geogig.cli.CommandFailedException) URL(java.net.URL) GeoToolsOpException(org.locationtech.geogig.geotools.plumbing.GeoToolsOpException) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) SimpleFeatureStore(org.geotools.data.simple.SimpleFeatureStore) ExportOp(org.locationtech.geogig.geotools.plumbing.ExportOp) ShapefileDataStoreFactory(org.geotools.data.shapefile.ShapefileDataStoreFactory) File(java.io.File) TYPE(org.locationtech.geogig.api.RevObject.TYPE)

Aggregations

TYPE (org.locationtech.geogig.api.RevObject.TYPE)17 ObjectId (org.locationtech.geogig.api.ObjectId)14 CommandFailedException (org.locationtech.geogig.cli.CommandFailedException)7 SimpleFeatureSource (org.geotools.data.simple.SimpleFeatureSource)6 SimpleFeatureStore (org.geotools.data.simple.SimpleFeatureStore)6 InvalidParameterException (org.locationtech.geogig.cli.InvalidParameterException)6 ExportOp (org.locationtech.geogig.geotools.plumbing.ExportOp)6 GeoToolsOpException (org.locationtech.geogig.geotools.plumbing.GeoToolsOpException)6 SimpleFeatureType (org.opengis.feature.simple.SimpleFeatureType)6 DataStore (org.geotools.data.DataStore)5 RevCommit (org.locationtech.geogig.api.RevCommit)5 DiffEntry (org.locationtech.geogig.api.plumbing.diff.DiffEntry)5 IOException (java.io.IOException)4 NameImpl (org.geotools.feature.NameImpl)4 SimpleFeatureTypeImpl (org.geotools.feature.simple.SimpleFeatureTypeImpl)4 NodeRef (org.locationtech.geogig.api.NodeRef)4 RevTree (org.locationtech.geogig.api.RevTree)4 RevFeature (org.locationtech.geogig.api.RevFeature)3 RevFeatureType (org.locationtech.geogig.api.RevFeatureType)3 RevObject (org.locationtech.geogig.api.RevObject)3