Search in sources :

Example 1 with FeatureJSON

use of org.geotools.geojson.feature.FeatureJSON in project GeoGig by boundlessgeo.

the class AbstractGeoJsonCommand method getDataStore.

protected DataStore getDataStore(String geoJSON) throws FileNotFoundException, IOException {
    try {
        File geoJsonfile = new File(geoJSON);
        checkParameter(geoJsonfile.exists(), "File does not exist '%s'", geoJsonfile);
        InputStream in = new FileInputStream(geoJsonfile);
        FeatureJSON fjson = new FeatureJSON();
        @SuppressWarnings("rawtypes") FeatureCollection fc = fjson.readFeatureCollection(in);
        @SuppressWarnings("unchecked") DataStore dataStore = new MemoryDataStore(fc);
        return dataStore;
    } catch (IOException ioe) {
        throw new CommandFailedException("Error opening GeoJSON: " + ioe.getMessage(), ioe);
    }
}
Also used : FeatureJSON(org.geotools.geojson.feature.FeatureJSON) FeatureCollection(org.geotools.feature.FeatureCollection) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) DataStore(org.geotools.data.DataStore) MemoryDataStore(org.geotools.data.memory.MemoryDataStore) MemoryDataStore(org.geotools.data.memory.MemoryDataStore) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream) CommandFailedException(org.locationtech.geogig.cli.CommandFailedException)

Example 2 with FeatureJSON

use of org.geotools.geojson.feature.FeatureJSON 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)

Aggregations

File (java.io.File)2 DataStore (org.geotools.data.DataStore)2 MemoryDataStore (org.geotools.data.memory.MemoryDataStore)2 FeatureCollection (org.geotools.feature.FeatureCollection)2 FeatureJSON (org.geotools.geojson.feature.FeatureJSON)2 CommandFailedException (org.locationtech.geogig.cli.CommandFailedException)2 FileInputStream (java.io.FileInputStream)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 SimpleFeatureSource (org.geotools.data.simple.SimpleFeatureSource)1 SimpleFeatureStore (org.geotools.data.simple.SimpleFeatureStore)1 ObjectId (org.locationtech.geogig.api.ObjectId)1 TYPE (org.locationtech.geogig.api.RevObject.TYPE)1 InvalidParameterException (org.locationtech.geogig.cli.InvalidParameterException)1 ExportOp (org.locationtech.geogig.geotools.plumbing.ExportOp)1 GeoToolsOpException (org.locationtech.geogig.geotools.plumbing.GeoToolsOpException)1 SimpleFeatureType (org.opengis.feature.simple.SimpleFeatureType)1