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));
}
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();
}
}
}
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;
}
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();
}
}
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();
}
}
}
Aggregations