Search in sources :

Example 16 with DataStore

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

the class OSMShapeFileReader method processJunctions.

@Override
void processJunctions() {
    DataStore dataStore = null;
    FeatureIterator<SimpleFeature> roads = null;
    try {
        dataStore = openShapefileDataStore(roadsFile, encoding);
        roads = getFeatureIterator(dataStore);
        HashSet<Coordinate> tmpSet = new HashSet<>();
        while (roads.hasNext()) {
            SimpleFeature road = roads.next();
            for (Coordinate[] points : getCoords(road.getDefaultGeometry())) {
                tmpSet.clear();
                for (int i = 0; i < points.length; i++) {
                    Coordinate c = points[i];
                    // duplicate coords or a road which forms a circle (e.g. roundabout)
                    if (tmpSet.contains(c))
                        continue;
                    tmpSet.add(c);
                    // skip if its already a node
                    int state = coordState.get(c);
                    if (state >= FIRST_NODE_ID) {
                        continue;
                    }
                    if (i == 0 || i == points.length - 1 || state == COORD_STATE_PILLAR) {
                        // turn into a node if its the first or last
                        // point, or already appeared in another edge
                        int nodeId = nextNodeId++;
                        coordState.put(c, nodeId);
                        saveTowerPosition(nodeId, c);
                    } else if (state == COORD_STATE_UNKNOWN) {
                        // mark it as a pillar (which may get upgraded
                        // to an edge later)
                        coordState.put(c, COORD_STATE_PILLAR);
                    }
                }
            }
        }
    } finally {
        if (roads != null) {
            roads.close();
        }
        if (dataStore != null) {
            dataStore.dispose();
        }
    }
    if (nextNodeId == FIRST_NODE_ID)
        throw new IllegalArgumentException("No data found for roads file " + roadsFile);
    LOGGER.info("Number of junction points : " + (nextNodeId - FIRST_NODE_ID));
}
Also used : Coordinate(com.vividsolutions.jts.geom.Coordinate) DataStore(org.geotools.data.DataStore) SimpleFeature(org.opengis.feature.simple.SimpleFeature) GHPoint(com.graphhopper.util.shapes.GHPoint) HashSet(java.util.HashSet)

Example 17 with DataStore

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

the class ShpImport method runInternal.

/**
     * Executes the import command using the provided options.
     */
@Override
protected void runInternal(GeogigCLI cli) throws IOException {
    checkParameter(shapeFile != null && !shapeFile.isEmpty(), "No shapefile specified");
    for (String shp : shapeFile) {
        DataStore dataStore = null;
        try {
            dataStore = getDataStore(shp);
        } catch (InvalidParameterException e) {
            cli.getConsole().println("The shapefile '" + shp + "' could not be found, skipping...");
            continue;
        }
        if (fidAttribute != null) {
            AttributeDescriptor attrib = dataStore.getSchema(dataStore.getNames().get(0)).getDescriptor(fidAttribute);
            if (attrib == null) {
                throw new InvalidParameterException("The specified attribute does not exist in the selected shapefile");
            }
        }
        try {
            cli.getConsole().println("Importing from shapefile " + shp);
            ProgressListener progressListener = cli.getProgressListener();
            ImportOp command = cli.getGeogig().command(ImportOp.class).setAll(true).setTable(null).setAlter(alter).setOverwrite(!add).setDestinationPath(destTable).setDataStore(dataStore).setFidAttribute(fidAttribute).setAdaptToDefaultFeatureType(!forceFeatureType);
            // force the import not to use paging due to a bug in the shapefile datastore
            command.setUsePaging(false);
            command.setProgressListener(progressListener).call();
            cli.getConsole().println(shp + " imported successfully.");
        } catch (GeoToolsOpException e) {
            switch(e.statusCode) {
                case NO_FEATURES_FOUND:
                    throw new CommandFailedException("No features were found in the shapefile.", e);
                case UNABLE_TO_GET_NAMES:
                    throw new CommandFailedException("Unable to get feature types from the shapefile.", e);
                case UNABLE_TO_GET_FEATURES:
                    throw new CommandFailedException("Unable to get features from the shapefile.", e);
                case UNABLE_TO_INSERT:
                    throw new CommandFailedException("Unable to insert features into the working tree.", e);
                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:
                    throw new CommandFailedException("Import failed with exception: " + e.statusCode.name(), e);
            }
        } finally {
            dataStore.dispose();
            cli.getConsole().flush();
        }
    }
}
Also used : InvalidParameterException(org.locationtech.geogig.cli.InvalidParameterException) ProgressListener(org.locationtech.geogig.api.ProgressListener) DataStore(org.geotools.data.DataStore) AttributeDescriptor(org.opengis.feature.type.AttributeDescriptor) ImportOp(org.locationtech.geogig.geotools.plumbing.ImportOp) CommandFailedException(org.locationtech.geogig.cli.CommandFailedException) GeoToolsOpException(org.locationtech.geogig.geotools.plumbing.GeoToolsOpException)

Example 18 with DataStore

use of org.geotools.data.DataStore in project sldeditor by robward-scisys.

the class DatabaseClient method connect.

/*
     * (non-Javadoc)
     * 
     * @see com.sldeditor.extension.filesystem.database.client.DatabaseClientInterface#connect()
     */
@Override
public boolean connect() {
    connected = false;
    Map<String, Object> params = getDBConnectionParams();
    try {
        DataStore dataStore = DataStoreFinder.getDataStore(params);
        if (dataStore != null) {
            try {
                List<Name> nameList = dataStore.getNames();
                if (nameList != null) {
                    for (Name name : nameList) {
                        if (hasGeometryField(dataStore, name)) {
                            featureClassList.add(name.getLocalPart());
                        }
                    }
                }
                dataStore.dispose();
                dataStore = null;
                connected = true;
            } catch (Exception e) {
                ConsoleManager.getInstance().exception(this, e);
            }
        } else {
            String message = String.format("%s : %s", Localisation.getString(DatabaseClient.class, "DatabaseClient.noDriver"), connection.getConnectionName());
            ConsoleManager.getInstance().error(this, message);
        }
    } catch (IOException e) {
        ConsoleManager.getInstance().exception(this, e);
    }
    return connected;
}
Also used : DataStore(org.geotools.data.DataStore) IOException(java.io.IOException) IOException(java.io.IOException) Name(org.opengis.feature.type.Name)

Example 19 with DataStore

use of org.geotools.data.DataStore in project sldeditor by robward-scisys.

the class InLineFeatureModel method removeFeature.

/**
 * Removes the feature.
 *
 * @param selectedRow the selected row
 */
public void removeFeature(int selectedRow) {
    if ((selectedRow < 0) || (selectedRow >= getRowCount())) {
        return;
    }
    SimpleFeatureType featureType = userLayer.getInlineFeatureType();
    String typeName = userLayer.getInlineFeatureType().getTypeName();
    try {
        SimpleFeatureSource featureSource = userLayer.getInlineFeatureDatastore().getFeatureSource(typeName);
        SimpleFeatureBuilder sfb = new SimpleFeatureBuilder(featureType);
        ArrayList<SimpleFeature> featureList = new ArrayList<SimpleFeature>();
        SimpleFeatureIterator it = featureSource.getFeatures().features();
        try {
            int index = 0;
            while (it.hasNext()) {
                SimpleFeature sf = it.next();
                if (index != selectedRow) {
                    List<Object> attributeValueList = sf.getAttributes();
                    sfb.addAll(attributeValueList);
                    featureList.add(sfb.buildFeature(null));
                }
                index++;
            }
        } finally {
            it.close();
        }
        SimpleFeatureCollection collection = new ListFeatureCollection(featureType, featureList);
        featureCollection = collection;
        cachedFeature = null;
        lastRow = -1;
        DataStore dataStore = DataUtilities.dataStore(collection);
        userLayer.setInlineFeatureDatastore(dataStore);
    } catch (IOException e) {
        ConsoleManager.getInstance().exception(this, e);
    }
    this.fireTableStructureChanged();
    this.fireTableDataChanged();
    if (parentObj != null) {
        parentObj.inlineFeatureUpdated();
    }
}
Also used : SimpleFeatureSource(org.geotools.data.simple.SimpleFeatureSource) ArrayList(java.util.ArrayList) IOException(java.io.IOException) SimpleFeature(org.opengis.feature.simple.SimpleFeature) SimpleFeatureCollection(org.geotools.data.simple.SimpleFeatureCollection) SimpleFeatureIterator(org.geotools.data.simple.SimpleFeatureIterator) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) DataStore(org.geotools.data.DataStore) ListFeatureCollection(org.geotools.data.collection.ListFeatureCollection) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder)

Example 20 with DataStore

use of org.geotools.data.DataStore in project sldeditor by robward-scisys.

the class InLineFeatureModel method updateCRS.

/**
 * Update CRS.
 *
 * @param selectedValue the selected value
 */
public void updateCRS(ValueComboBoxData selectedValue) {
    if (selectedValue != null) {
        String crsCode = selectedValue.getKey();
        CoordinateReferenceSystem newCRS = CoordManager.getInstance().getCRS(crsCode);
        SimpleFeatureType newFeatureType = SimpleFeatureTypeBuilder.retype(featureCollection.getSchema(), newCRS);
        String typeName = userLayer.getInlineFeatureType().getTypeName();
        try {
            SimpleFeatureSource featureSource = userLayer.getInlineFeatureDatastore().getFeatureSource(typeName);
            SimpleFeatureBuilder sfb = new SimpleFeatureBuilder(newFeatureType);
            ArrayList<SimpleFeature> featureList = new ArrayList<SimpleFeature>();
            SimpleFeatureIterator it = featureSource.getFeatures().features();
            try {
                while (it.hasNext()) {
                    SimpleFeature sf = it.next();
                    List<Object> attributeValueList = sf.getAttributes();
                    sfb.addAll(attributeValueList);
                    featureList.add(sfb.buildFeature(null));
                }
            } finally {
                it.close();
            }
            SimpleFeatureCollection collection = new ListFeatureCollection(newFeatureType, featureList);
            featureCollection = collection;
            cachedFeature = null;
            lastRow = -1;
            DataStore dataStore = DataUtilities.dataStore(collection);
            userLayer.setInlineFeatureDatastore(dataStore);
            userLayer.setInlineFeatureType(newFeatureType);
        } catch (IOException e) {
            ConsoleManager.getInstance().exception(this, e);
        }
        this.fireTableStructureChanged();
        this.fireTableDataChanged();
        if (parentObj != null) {
            parentObj.inlineFeatureUpdated();
        }
    }
}
Also used : SimpleFeatureSource(org.geotools.data.simple.SimpleFeatureSource) ArrayList(java.util.ArrayList) IOException(java.io.IOException) SimpleFeature(org.opengis.feature.simple.SimpleFeature) SimpleFeatureCollection(org.geotools.data.simple.SimpleFeatureCollection) SimpleFeatureIterator(org.geotools.data.simple.SimpleFeatureIterator) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) DataStore(org.geotools.data.DataStore) ListFeatureCollection(org.geotools.data.collection.ListFeatureCollection) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder)

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