Search in sources :

Example 11 with DataStore

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

the class OracleImport method runInternal.

/**
     * Executes the import command using the provided options.
     * 
     * @param cli
     * @see org.locationtech.geogig.cli.AbstractOracleCommand#runInternal(org.locationtech.geogig.cli.GeogigCLI)
     */
@Override
protected void runInternal(GeogigCLI cli) throws IOException {
    DataStore dataStore = getDataStore();
    try {
        cli.getConsole().println("Importing from database " + commonArgs.database);
        ProgressListener progressListener = cli.getProgressListener();
        cli.getGeogig().command(ImportOp.class).setAll(all).setTable(table).setAlter(alter).setDestinationPath(destTable).setOverwrite(!add).setDataStore(dataStore).setAdaptToDefaultFeatureType(!forceFeatureType).setProgressListener(progressListener).call();
        cli.getConsole().println("Import successful.");
    } catch (GeoToolsOpException e) {
        switch(e.statusCode) {
            case TABLE_NOT_DEFINED:
                cli.getConsole().println("No tables specified for import. Specify --all or --table <table>.");
                throw new CommandFailedException();
            case ALL_AND_TABLE_DEFINED:
                cli.getConsole().println("Specify --all or --table <table>, both cannot be set.");
                throw new CommandFailedException();
            case NO_FEATURES_FOUND:
                cli.getConsole().println("No features were found in the database.");
                break;
            case TABLE_NOT_FOUND:
                cli.getConsole().println("Could not find the specified table.");
                throw new CommandFailedException();
            case UNABLE_TO_GET_NAMES:
                cli.getConsole().println("Unable to get feature types from the database.");
                throw new CommandFailedException();
            case UNABLE_TO_GET_FEATURES:
                cli.getConsole().println("Unable to get features from the database.");
                break;
            case UNABLE_TO_INSERT:
                cli.getConsole().println("Unable to insert features into the working tree.");
                throw new CommandFailedException();
            case ALTER_AND_ALL_DEFINED:
                cli.getConsole().println("Alter cannot be used with --all option and more than one table.");
                throw new CommandFailedException();
            case INCOMPATIBLE_FEATURE_TYPE:
                throw new CommandFailedException("The feature type of the data to import does not match the feature type of the destination tree and cannot be imported\n" + "USe the --force-featuretype switch to import using the original featuretype and crete a mixed type tree", e);
            default:
                cli.getConsole().println("Import failed with exception: " + e.statusCode.name());
                throw new CommandFailedException();
        }
    } finally {
        dataStore.dispose();
        cli.getConsole().flush();
    }
}
Also used : ProgressListener(org.locationtech.geogig.api.ProgressListener) DataStore(org.geotools.data.DataStore) ImportOp(org.locationtech.geogig.geotools.plumbing.ImportOp) CommandFailedException(org.locationtech.geogig.cli.CommandFailedException) GeoToolsOpException(org.locationtech.geogig.geotools.plumbing.GeoToolsOpException)

Example 12 with DataStore

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

the class SLExport 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 truncating 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 13 with DataStore

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

the class OSMExportPG method runInternal.

/**
     * Executes the export command using the provided options.
     */
@Override
protected void runInternal(GeogigCLI cli) {
    Preconditions.checkNotNull(mappingFile != null, "A data mapping file must be specified");
    final Mapping mapping = Mapping.fromFile(mappingFile);
    List<MappingRule> rules = mapping.getRules();
    checkParameter(!rules.isEmpty(), "No rules are defined in the specified mapping");
    for (final MappingRule rule : mapping.getRules()) {
        Function<Feature, Optional<Feature>> function = new Function<Feature, Optional<Feature>>() {

            @Override
            @Nullable
            public Optional<Feature> apply(@Nullable Feature feature) {
                Optional<Feature> mapped = rule.apply(feature);
                if (mapped.isPresent()) {
                    return Optional.of(mapped.get());
                }
                return Optional.absent();
            }
        };
        SimpleFeatureType outputFeatureType = rule.getFeatureType();
        String path = getOriginTreesFromOutputFeatureType(outputFeatureType);
        DataStore dataStore = null;
        try {
            dataStore = getDataStore();
            String tableName = outputFeatureType.getName().getLocalPart();
            if (Arrays.asList(dataStore.getTypeNames()).contains(tableName)) {
                if (!overwrite) {
                    throw new CommandFailedException("A table named '" + tableName + "'already exists. Use -o to overwrite");
                }
            } else {
                try {
                    dataStore.createSchema(outputFeatureType);
                } catch (IOException e) {
                    throw new CommandFailedException("Cannot create new table in database", e);
                }
            }
            final SimpleFeatureSource featureSource = dataStore.getFeatureSource(tableName);
            if (!(featureSource instanceof SimpleFeatureStore)) {
                throw new CommandFailedException("Could not create feature store. Data source is read only.");
            }
            final SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
            if (overwrite) {
                featureStore.removeFeatures(Filter.INCLUDE);
            }
            ExportOp op = cli.getGeogig().command(ExportOp.class).setFeatureStore(featureStore).setPath(path).setFeatureTypeConversionFunction(function);
            try {
                op.setProgressListener(cli.getProgressListener()).call();
                cli.getConsole().println("OSM data exported successfully to " + tableName);
            } catch (IllegalArgumentException iae) {
                throw new org.locationtech.geogig.cli.InvalidParameterException(iae.getMessage(), iae);
            } catch (GeoToolsOpException e) {
                throw new CommandFailedException("Could not export. Error:" + e.statusCode.name(), e);
            }
        } catch (IOException e) {
            throw new IllegalStateException("Cannot connect to database: " + e.getMessage(), e);
        } finally {
            if (dataStore != null) {
                dataStore.dispose();
            }
        }
    }
}
Also used : Optional(com.google.common.base.Optional) SimpleFeatureSource(org.geotools.data.simple.SimpleFeatureSource) Mapping(org.locationtech.geogig.osm.internal.Mapping) IOException(java.io.IOException) MappingRule(org.locationtech.geogig.osm.internal.MappingRule) Feature(org.opengis.feature.Feature) CommandFailedException(org.locationtech.geogig.cli.CommandFailedException) GeoToolsOpException(org.locationtech.geogig.geotools.plumbing.GeoToolsOpException) Function(com.google.common.base.Function) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) DataStore(org.geotools.data.DataStore) SimpleFeatureStore(org.geotools.data.simple.SimpleFeatureStore) ExportOp(org.locationtech.geogig.geotools.plumbing.ExportOp) Nullable(javax.annotation.Nullable)

Example 14 with DataStore

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

the class OSMExportSL method exportRule.

private void exportRule(final MappingRule rule, final GeogigCLI cli) throws IOException {
    Function<Feature, Optional<Feature>> function = new Function<Feature, Optional<Feature>>() {

        @Override
        public Optional<Feature> apply(Feature feature) {
            Optional<Feature> mapped = rule.apply(feature);
            return mapped;
        }
    };
    SimpleFeatureType outputFeatureType = rule.getFeatureType();
    String path = getOriginTreesFromOutputFeatureType(outputFeatureType);
    DataStore dataStore = getDataStore();
    try {
        final String tableName = ensureTableExists(outputFeatureType, dataStore);
        final SimpleFeatureSource source = dataStore.getFeatureSource(tableName);
        if (!(source instanceof SimpleFeatureStore)) {
            throw new CommandFailedException("Could not create feature store.");
        }
        final SimpleFeatureStore store = (SimpleFeatureStore) source;
        if (overwrite) {
            try {
                store.removeFeatures(Filter.INCLUDE);
            } catch (IOException e) {
                throw new CommandFailedException("Error truncating table " + tableName, e);
            }
        }
        ExportOp op = cli.getGeogig().command(ExportOp.class).setFeatureStore(store).setPath(path).setFeatureTypeConversionFunction(function);
        try {
            op.setProgressListener(cli.getProgressListener()).call();
            cli.getConsole().println("OSM data exported successfully to " + tableName);
        } catch (IllegalArgumentException iae) {
            throw new InvalidParameterException(iae.getMessage(), iae);
        } catch (GeoToolsOpException e) {
            throw new CommandFailedException("Could not export. Error:" + e.statusCode.name(), e);
        }
    } finally {
        dataStore.dispose();
    }
}
Also used : Optional(com.google.common.base.Optional) SimpleFeatureSource(org.geotools.data.simple.SimpleFeatureSource) IOException(java.io.IOException) Feature(org.opengis.feature.Feature) CommandFailedException(org.locationtech.geogig.cli.CommandFailedException) GeoToolsOpException(org.locationtech.geogig.geotools.plumbing.GeoToolsOpException) Function(com.google.common.base.Function) InvalidParameterException(org.locationtech.geogig.cli.InvalidParameterException) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) DataStore(org.geotools.data.DataStore) SimpleFeatureStore(org.geotools.data.simple.SimpleFeatureStore) ExportOp(org.locationtech.geogig.geotools.plumbing.ExportOp)

Example 15 with DataStore

use of org.geotools.data.DataStore in project graphhopper by graphhopper.

the class OSMShapeFileReader method processRoads.

@Override
void processRoads() {
    DataStore dataStore = null;
    FeatureIterator<SimpleFeature> roads = null;
    try {
        dataStore = openShapefileDataStore(roadsFile, encoding);
        roads = getFeatureIterator(dataStore);
        while (roads.hasNext()) {
            SimpleFeature road = roads.next();
            for (Coordinate[] points : getCoords(road.getDefaultGeometry())) {
                // Parse all points in the geometry, splitting into
                // individual graphhopper edges
                // whenever we find a node in the list of points
                Coordinate startTowerPnt = null;
                List<Coordinate> pillars = new ArrayList<Coordinate>();
                for (Coordinate point : points) {
                    if (startTowerPnt == null) {
                        startTowerPnt = point;
                    } else {
                        int state = coordState.get(point);
                        if (state >= FIRST_NODE_ID) {
                            int fromTowerNodeId = coordState.get(startTowerPnt);
                            int toTowerNodeId = state;
                            // get distance and estimated centres
                            double distance = getWayLength(startTowerPnt, pillars, point);
                            GHPoint estmCentre = new GHPoint(0.5 * (lat(startTowerPnt) + lat(point)), 0.5 * (lng(startTowerPnt) + lng(point)));
                            PointList pillarNodes = new PointList(pillars.size(), false);
                            for (Coordinate pillar : pillars) {
                                pillarNodes.add(lat(pillar), lng(pillar));
                            }
                            addEdge(fromTowerNodeId, toTowerNodeId, road, distance, estmCentre, pillarNodes);
                            startTowerPnt = point;
                            pillars.clear();
                        } else {
                            pillars.add(point);
                        }
                    }
                }
            }
        }
    } finally {
        if (roads != null) {
            roads.close();
        }
        if (dataStore != null) {
            dataStore.dispose();
        }
    }
}
Also used : PointList(com.graphhopper.util.PointList) Coordinate(com.vividsolutions.jts.geom.Coordinate) DataStore(org.geotools.data.DataStore) ArrayList(java.util.ArrayList) GHPoint(com.graphhopper.util.shapes.GHPoint) SimpleFeature(org.opengis.feature.simple.SimpleFeature) GHPoint(com.graphhopper.util.shapes.GHPoint)

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