Search in sources :

Example 31 with DataStore

use of org.geotools.data.DataStore in project GeoGig by boundlessgeo.

the class SQLServerDescribe method runInternal.

/**
     * Executes the describe command using the provided options.
     */
@Override
protected void runInternal(GeogigCLI cli) throws IOException {
    DataStore dataStore = getDataStore();
    try {
        cli.getConsole().println("Fetching table...");
        Optional<Map<String, String>> propertyMap = cli.getGeogig().command(DescribeOp.class).setTable(table).setDataStore(dataStore).call();
        if (propertyMap.isPresent()) {
            cli.getConsole().println("Table : " + table);
            cli.getConsole().println("----------------------------------------");
            for (Entry<String, String> entry : propertyMap.get().entrySet()) {
                cli.getConsole().println("\tProperty  : " + entry.getKey());
                cli.getConsole().println("\tType      : " + entry.getValue());
                cli.getConsole().println("----------------------------------------");
            }
        } else {
            throw new CommandFailedException("Could not find the specified table.");
        }
    } catch (GeoToolsOpException e) {
        switch(e.statusCode) {
            case TABLE_NOT_DEFINED:
                throw new CommandFailedException("No table supplied.", e);
            case UNABLE_TO_GET_FEATURES:
                throw new CommandFailedException("Unable to read the feature source.", e);
            case UNABLE_TO_GET_NAMES:
                throw new CommandFailedException("Unable to read feature types.", e);
            default:
                throw new CommandFailedException("Exception: " + e.statusCode.name(), e);
        }
    } finally {
        dataStore.dispose();
        cli.getConsole().flush();
    }
}
Also used : DataStore(org.geotools.data.DataStore) DescribeOp(org.locationtech.geogig.geotools.plumbing.DescribeOp) Map(java.util.Map) CommandFailedException(org.locationtech.geogig.cli.CommandFailedException) GeoToolsOpException(org.locationtech.geogig.geotools.plumbing.GeoToolsOpException)

Example 32 with DataStore

use of org.geotools.data.DataStore in project GeoGig by boundlessgeo.

the class OracleDescribe method runInternal.

/**
     * Executes the describe command using the provided options.
     * 
     * @param cli
     * @see org.locationtech.geogig.geotools.cli.porcelain.AbstractOracleCommand#runInternal(org.locationtech.geogig.cli.GeogigCLI)
     */
@Override
protected void runInternal(GeogigCLI cli) throws IOException {
    DataStore dataStore = getDataStore();
    try {
        cli.getConsole().println("Fetching table...");
        Optional<Map<String, String>> propertyMap = cli.getGeogig().command(DescribeOp.class).setTable(table).setDataStore(dataStore).call();
        if (propertyMap.isPresent()) {
            cli.getConsole().println("Table : " + table);
            cli.getConsole().println("----------------------------------------");
            for (Entry<String, String> entry : propertyMap.get().entrySet()) {
                cli.getConsole().println("\tProperty  : " + entry.getKey());
                cli.getConsole().println("\tType      : " + entry.getValue());
                cli.getConsole().println("----------------------------------------");
            }
        } else {
            throw new CommandFailedException("Could not find the specified table.");
        }
    } catch (GeoToolsOpException e) {
        switch(e.statusCode) {
            case TABLE_NOT_DEFINED:
                throw new CommandFailedException("No table supplied.", e);
            case UNABLE_TO_GET_FEATURES:
                throw new CommandFailedException("Unable to read the feature source.", e);
            case UNABLE_TO_GET_NAMES:
                throw new CommandFailedException("Unable to read feature types.", e);
            default:
                throw new CommandFailedException("Exception: " + e.statusCode.name(), e);
        }
    } finally {
        dataStore.dispose();
        cli.getConsole().flush();
    }
}
Also used : DataStore(org.geotools.data.DataStore) DescribeOp(org.locationtech.geogig.geotools.plumbing.DescribeOp) Map(java.util.Map) CommandFailedException(org.locationtech.geogig.cli.CommandFailedException) GeoToolsOpException(org.locationtech.geogig.geotools.plumbing.GeoToolsOpException)

Example 33 with DataStore

use of org.geotools.data.DataStore in project GeoGig by boundlessgeo.

the class OracleExport method runInternal.

/**
     * Executes the export command using the provided options.
     * 
     * @param cli
     */
@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 34 with DataStore

use of org.geotools.data.DataStore in project GeoGig by boundlessgeo.

the class OracleList method runInternal.

/**
     * Executes the list command using the provided options.
     * 
     * @param cli
     * @see org.locationtech.geogig.geotools.cli.porcelain.AbstractOracleCommand#runInternal(org.locationtech.geogig.cli.GeogigCLI)
     */
@Override
protected void runInternal(GeogigCLI cli) throws IOException {
    DataStore dataStore = getDataStore();
    try {
        cli.getConsole().println("Fetching feature types...");
        Optional<List<String>> features = cli.getGeogig().command(ListOp.class).setDataStore(dataStore).call();
        if (features.isPresent()) {
            for (String featureType : features.get()) {
                cli.getConsole().println(" - " + featureType);
            }
        } else {
            throw new CommandFailedException("No features types were found in the specified database.");
        }
    } catch (GeoToolsOpException e) {
        throw new CommandFailedException("Unable to get feature types from the database.");
    } finally {
        dataStore.dispose();
        cli.getConsole().flush();
    }
}
Also used : DataStore(org.geotools.data.DataStore) List(java.util.List) CommandFailedException(org.locationtech.geogig.cli.CommandFailedException) GeoToolsOpException(org.locationtech.geogig.geotools.plumbing.GeoToolsOpException)

Example 35 with DataStore

use of org.geotools.data.DataStore in project GeoGig by boundlessgeo.

the class AbstractShpCommand method getDataStore.

/**
     * Constructs a new shapefile data store using the specified shapefile.
     * 
     * @param shapefile the filepath of the shapefile to use in creating the data store
     * @return the constructed data store
     * @throws IllegalArgumentException if the datastore cannot be acquired
     * @see DataStore
     */
protected DataStore getDataStore(String shapefile) {
    File file = new File(shapefile);
    checkParameter(file.exists(), "File does not exist '%s'", shapefile);
    try {
        Map<String, Serializable> params = Maps.newHashMap();
        params.put(ShapefileDataStoreFactory.URLP.key, new File(shapefile).toURI().toURL());
        params.put(ShapefileDataStoreFactory.NAMESPACEP.key, "http://www.opengis.net/gml");
        params.put(ShapefileDataStoreFactory.CREATE_SPATIAL_INDEX.key, Boolean.FALSE);
        params.put(ShapefileDataStoreFactory.ENABLE_SPATIAL_INDEX.key, Boolean.FALSE);
        params.put(ShapefileDataStoreFactory.MEMORY_MAPPED.key, Boolean.FALSE);
        DataStore dataStore = dataStoreFactory.createDataStore(params);
        checkParameter(dataStore != null, "Unable to open '%s' as a shapefile", shapefile);
        return dataStore;
    } catch (IOException e) {
        throw new CommandFailedException("Error opening shapefile: " + e.getMessage(), e);
    }
}
Also used : Serializable(java.io.Serializable) DataStore(org.geotools.data.DataStore) IOException(java.io.IOException) File(java.io.File) CommandFailedException(org.locationtech.geogig.cli.CommandFailedException)

Aggregations

DataStore (org.geotools.data.DataStore)52 IOException (java.io.IOException)28 CommandFailedException (org.locationtech.geogig.cli.CommandFailedException)28 SimpleFeatureType (org.opengis.feature.simple.SimpleFeatureType)23 SimpleFeatureSource (org.geotools.data.simple.SimpleFeatureSource)22 GeoToolsOpException (org.locationtech.geogig.geotools.plumbing.GeoToolsOpException)22 HashMap (java.util.HashMap)10 Map (java.util.Map)10 SimpleFeature (org.opengis.feature.simple.SimpleFeature)9 SimpleFeatureStore (org.geotools.data.simple.SimpleFeatureStore)8 Test (org.junit.Test)8 InvalidParameterException (org.locationtech.geogig.cli.InvalidParameterException)8 ExportOp (org.locationtech.geogig.geotools.plumbing.ExportOp)8 Serializable (java.io.Serializable)7 ArrayList (java.util.ArrayList)7 SLDEditorFile (com.sldeditor.datasource.SLDEditorFile)6 DataSourceInfo (com.sldeditor.datasource.impl.DataSourceInfo)6 URL (java.net.URL)6 ProgressListener (org.locationtech.geogig.api.ProgressListener)6 ListFeatureCollection (org.geotools.data.collection.ListFeatureCollection)5