Search in sources :

Example 16 with InvalidParameterException

use of org.locationtech.geogig.cli.InvalidParameterException in project GeoGig by boundlessgeo.

the class SQLServerExport method getFeatureType.

private SimpleFeatureType getFeatureType(String path, GeogigCLI cli) {
    checkParameter(path != null, "No path specified.");
    String refspec;
    if (path.contains(":")) {
        refspec = path;
    } else {
        refspec = "WORK_HEAD:" + path;
    }
    checkParameter(!refspec.endsWith(":"), "No path specified.");
    final GeoGIG geogig = cli.getGeogig();
    Optional<ObjectId> rootTreeId = geogig.command(ResolveTreeish.class).setTreeish(refspec.split(":")[0]).call();
    checkParameter(rootTreeId.isPresent(), "Couldn't resolve '" + refspec + "' to a treeish object");
    RevTree rootTree = geogig.getRepository().getTree(rootTreeId.get());
    Optional<NodeRef> featureTypeTree = geogig.command(FindTreeChild.class).setChildPath(refspec.split(":")[1]).setParent(rootTree).setIndex(true).call();
    checkParameter(featureTypeTree.isPresent(), "pathspec '" + refspec.split(":")[1] + "' did not match any valid path");
    Optional<RevObject> revObject = cli.getGeogig().command(RevObjectParse.class).setObjectId(featureTypeTree.get().getMetadataId()).call();
    if (revObject.isPresent() && revObject.get() instanceof RevFeatureType) {
        RevFeatureType revFeatureType = (RevFeatureType) revObject.get();
        if (revFeatureType.type() instanceof SimpleFeatureType) {
            return (SimpleFeatureType) revFeatureType.type();
        } else {
            throw new InvalidParameterException("Cannot find feature type for the specified path");
        }
    } else {
        throw new InvalidParameterException("Cannot find feature type for the specified path");
    }
}
Also used : NodeRef(org.locationtech.geogig.api.NodeRef) InvalidParameterException(org.locationtech.geogig.cli.InvalidParameterException) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) ObjectId(org.locationtech.geogig.api.ObjectId) RevObject(org.locationtech.geogig.api.RevObject) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) GeoGIG(org.locationtech.geogig.api.GeoGIG) RevTree(org.locationtech.geogig.api.RevTree)

Example 17 with InvalidParameterException

use of org.locationtech.geogig.cli.InvalidParameterException in project GeoGig by boundlessgeo.

the class OracleExport method getFeatureType.

private SimpleFeatureType getFeatureType(String path, GeogigCLI cli) {
    checkParameter(path != null, "No path specified.");
    String refspec;
    if (path.contains(":")) {
        refspec = path;
    } else {
        refspec = "WORK_HEAD:" + path;
    }
    checkParameter(!refspec.endsWith(":"), "No path specified.");
    final GeoGIG geogig = cli.getGeogig();
    Optional<ObjectId> rootTreeId = geogig.command(ResolveTreeish.class).setTreeish(refspec.split(":")[0]).call();
    checkParameter(rootTreeId.isPresent(), "Couldn't resolve '" + refspec + "' to a treeish object");
    RevTree rootTree = geogig.getRepository().getTree(rootTreeId.get());
    Optional<NodeRef> featureTypeTree = geogig.command(FindTreeChild.class).setChildPath(refspec.split(":")[1]).setParent(rootTree).setIndex(true).call();
    checkParameter(featureTypeTree.isPresent(), "pathspec '" + refspec.split(":")[1] + "' did not match any valid path");
    Optional<RevObject> revObject = cli.getGeogig().command(RevObjectParse.class).setObjectId(featureTypeTree.get().getMetadataId()).call();
    if (revObject.isPresent() && revObject.get() instanceof RevFeatureType) {
        RevFeatureType revFeatureType = (RevFeatureType) revObject.get();
        if (revFeatureType.type() instanceof SimpleFeatureType) {
            return (SimpleFeatureType) revFeatureType.type();
        } else {
            throw new InvalidParameterException("Cannot find feature type for the specified path");
        }
    } else {
        throw new InvalidParameterException("Cannot find feature type for the specified path");
    }
}
Also used : NodeRef(org.locationtech.geogig.api.NodeRef) InvalidParameterException(org.locationtech.geogig.cli.InvalidParameterException) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) ObjectId(org.locationtech.geogig.api.ObjectId) RevObject(org.locationtech.geogig.api.RevObject) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) GeoGIG(org.locationtech.geogig.api.GeoGIG) RevTree(org.locationtech.geogig.api.RevTree)

Example 18 with InvalidParameterException

use of org.locationtech.geogig.cli.InvalidParameterException in project GeoGig by boundlessgeo.

the class GeoJsonExport method runInternal.

/**
     * Executes the export command using the provided options.
     */
@Override
protected void runInternal(GeogigCLI cli) throws InvalidParameterException, CommandFailedException, IOException {
    if (args.isEmpty()) {
        printUsage(cli);
        throw new CommandFailedException();
    }
    String path = args.get(0);
    String geojson = args.get(1);
    File file = new File(geojson);
    if (file.exists() && !overwrite) {
        throw new CommandFailedException("The selected GeoJSON file already exists. Use -o to overwrite");
    }
    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 dataStore = new MemoryDataStore(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.");
    }
    final SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
    ExportOp op = cli.getGeogig().command(ExportOp.class).setFeatureStore(featureStore).setPath(path).setFilterFeatureTypeId(featureTypeId).setAlter(alter);
    op.setTransactional(false);
    if (defaultType) {
        op.exportDefaultFeatureType();
    }
    FileWriter writer = null;
    try {
        op.setProgressListener(cli.getProgressListener()).call();
        FeatureJSON fjson = new FeatureJSON();
        @SuppressWarnings("rawtypes") FeatureCollection fc = featureSource.getFeatures();
        writer = new FileWriter(file);
        fjson.writeFeatureCollection(fc, writer);
    } catch (IllegalArgumentException iae) {
        throw new org.locationtech.geogig.cli.InvalidParameterException(iae.getMessage(), iae);
    } catch (GeoToolsOpException e) {
        file.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);
        }
    } finally {
        writer.flush();
        writer.close();
    }
    cli.getConsole().println(path + " exported successfully to " + geojson);
}
Also used : ObjectId(org.locationtech.geogig.api.ObjectId) SimpleFeatureSource(org.geotools.data.simple.SimpleFeatureSource) FileWriter(java.io.FileWriter) MemoryDataStore(org.geotools.data.memory.MemoryDataStore) InvalidParameterException(org.locationtech.geogig.cli.InvalidParameterException) CommandFailedException(org.locationtech.geogig.cli.CommandFailedException) GeoToolsOpException(org.locationtech.geogig.geotools.plumbing.GeoToolsOpException) FeatureJSON(org.geotools.geojson.feature.FeatureJSON) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) FeatureCollection(org.geotools.feature.FeatureCollection) MemoryDataStore(org.geotools.data.memory.MemoryDataStore) DataStore(org.geotools.data.DataStore) SimpleFeatureStore(org.geotools.data.simple.SimpleFeatureStore) ExportOp(org.locationtech.geogig.geotools.plumbing.ExportOp) File(java.io.File) TYPE(org.locationtech.geogig.api.RevObject.TYPE)

Example 19 with InvalidParameterException

use of org.locationtech.geogig.cli.InvalidParameterException in project GeoGig by boundlessgeo.

the class OSMHistoryImport method parseBbox.

private Envelope parseBbox() {
    final String bbox = args.bbox;
    if (bbox != null) {
        String[] split = bbox.split(",");
        checkParameter(split.length == 4, String.format("Invalid bbox format: '%s'. Expected minx,miny,maxx,maxy", bbox));
        try {
            double x1 = Double.parseDouble(split[0]);
            double y1 = Double.parseDouble(split[1]);
            double x2 = Double.parseDouble(split[2]);
            double y2 = Double.parseDouble(split[3]);
            Envelope envelope = new Envelope(x1, x2, y1, y2);
            checkParameter(!envelope.isNull(), "Provided envelope is nil");
            return envelope;
        } catch (NumberFormatException e) {
            String message = String.format("One or more bbox coordinate can't be parsed to double: '%s'", bbox);
            throw new InvalidParameterException(message, e);
        }
    }
    return null;
}
Also used : InvalidParameterException(org.locationtech.geogig.cli.InvalidParameterException) Envelope(com.vividsolutions.jts.geom.Envelope)

Aggregations

InvalidParameterException (org.locationtech.geogig.cli.InvalidParameterException)19 GeoGIG (org.locationtech.geogig.api.GeoGIG)11 ObjectId (org.locationtech.geogig.api.ObjectId)10 SimpleFeatureType (org.opengis.feature.simple.SimpleFeatureType)10 RevFeatureType (org.locationtech.geogig.api.RevFeatureType)8 NodeRef (org.locationtech.geogig.api.NodeRef)7 RevObject (org.locationtech.geogig.api.RevObject)7 RevTree (org.locationtech.geogig.api.RevTree)7 CommandFailedException (org.locationtech.geogig.cli.CommandFailedException)7 DataStore (org.geotools.data.DataStore)5 GeoToolsOpException (org.locationtech.geogig.geotools.plumbing.GeoToolsOpException)5 IOException (java.io.IOException)3 SimpleFeatureSource (org.geotools.data.simple.SimpleFeatureSource)3 SimpleFeatureStore (org.geotools.data.simple.SimpleFeatureStore)3 ExportOp (org.locationtech.geogig.geotools.plumbing.ExportOp)3 Optional (com.google.common.base.Optional)2 File (java.io.File)2 Date (java.util.Date)2 ConsoleReader (jline.console.ConsoleReader)2 ProgressListener (org.locationtech.geogig.api.ProgressListener)2