org.geotools.data.FileDataStoreFinder Java Examples

The following examples show how to use org.geotools.data.FileDataStoreFinder. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: OmsShapefileFeatureReader.java    From hortonmachine with GNU General Public License v3.0 6 votes vote down vote up
@Execute
public void readFeatureCollection() throws IOException {
    if (!concatOr(geodata == null, doReset)) {
        return;
    }

    try {
        File shapeFile = new File(file);
        pm.beginTask("Reading features from shapefile: " + shapeFile.getName(), -1);
        FileDataStore store = FileDataStoreFinder.getDataStore(shapeFile);
        if (store instanceof ShapefileDataStore) {
            ShapefileDataStore shpStore = (ShapefileDataStore) store;
            String shpCharset = PreferencesHandler.getShpCharset();
            if (shpCharset != null) {
                shpStore.setCharset(Charset.forName(shpCharset));
            }
        }
        SimpleFeatureSource featureSource = store.getFeatureSource();
        geodata = featureSource.getFeatures();
        store.dispose();
    } finally {
        pm.done();
    }
}
 
Example #2
Source File: OmsVectorReader.java    From hortonmachine with GNU General Public License v3.0 4 votes vote down vote up
public static ReferencedEnvelope readEnvelope( String filePath ) throws Exception {
    File shapeFile = new File(filePath);
    FileDataStore store = FileDataStoreFinder.getDataStore(shapeFile);
    SimpleFeatureSource featureSource = store.getFeatureSource();
    return featureSource.getBounds();
}
 
Example #3
Source File: OmsShapefileFeatureWriter.java    From hortonmachine with GNU General Public License v3.0 4 votes vote down vote up
@Execute
public void writeFeatureCollection() throws IOException {
    if (!concatOr(!hasWritten, doReset)) {
        return;
    }

    pm.beginTask("Writing features to shapefile...", -1);

    if (!file.endsWith(".shp")) {
        file = file + ".shp";
    }
    if (geodata != null && geodata.size() != 0) {
        pType = geodata.getSchema();
    }
    File shapeFile = new File(file);
    FileDataStoreFactorySpi factory = FileDataStoreFinder.getDataStoreFactory("shp");

    Map<String, Serializable> map = new HashMap<>();
    map.put("url", shapeFile.toURI().toURL());
    String shpDoIndex = PreferencesHandler.getShpDoIndex();
    if (shpDoIndex != null) {
        map.put("create spatial index", new Boolean(shpDoIndex));
    }

    String shpCharset = PreferencesHandler.getShpCharset();
    if (shpCharset != null) {
        map.put("charset", shpCharset);
    }

    DataStore newDataStore = factory.createNewDataStore(map);
    newDataStore.createSchema(pType);

    Transaction transaction = new DefaultTransaction("create");
    String typeName = newDataStore.getTypeNames()[0];
    SimpleFeatureStore featureStore = (SimpleFeatureStore) newDataStore.getFeatureSource(typeName);

    featureStore.setTransaction(transaction);
    try {
        if (geodata == null) {
            featureStore.addFeatures(new DefaultFeatureCollection());
        } else {
            featureStore.addFeatures(geodata);
        }
        transaction.commit();
    } catch (Exception problem) {
        transaction.rollback();
        throw new IOException(problem.getLocalizedMessage());
    } finally {
        transaction.close();
        pm.done();
    }

    hasWritten = true;
}
 
Example #4
Source File: ShapefileQueryOutputFormat.java    From geowave with Apache License 2.0 4 votes vote down vote up
@Override
public void output(final ResultSet results) {
  int geometryColumn = -1;
  for (int i = 0; i < results.columnCount(); i++) {
    if (Geometry.class.isAssignableFrom(results.columnType(i))) {
      geometryColumn = i;
      break;
    }
  }
  if (geometryColumn < 0) {
    throw new RuntimeException(
        "Unable to output results to a shapefile without a geometry column.");
  }

  final SimpleFeatureTypeBuilder ftb = new SimpleFeatureTypeBuilder();
  ftb.setCRS(results.getCRS());
  ftb.setName(typeName);
  for (int i = 0; i < results.columnCount(); i++) {
    final AttributeTypeBuilder atb = new AttributeTypeBuilder();
    atb.setBinding(results.columnType(i));
    atb.nillable(true);
    if (i == geometryColumn) {
      ftb.add(atb.buildDescriptor("the_geom"));
    } else {
      ftb.add(atb.buildDescriptor(results.columnName(i)));
    }
  }
  final SimpleFeatureType sft = ftb.buildFeatureType();

  final SimpleFeatureBuilder sfb = new SimpleFeatureBuilder(sft);
  final AtomicLong nextId = new AtomicLong(0L);
  final Iterator<SimpleFeature> features = Iterators.transform(results, r -> {
    sfb.reset();
    for (int i = 0; i < results.columnCount(); i++) {
      sfb.add(r.columnValue(i));
    }
    final SimpleFeature feature = sfb.buildFeature(Long.toString(nextId.incrementAndGet()));
    return feature;
  });

  final FileDataStoreFactorySpi factory = FileDataStoreFinder.getDataStoreFactory("shp");
  final File file = new File(outputFile);
  final Map<String, Serializable> params = Maps.newHashMap();
  final Transaction transaction = new DefaultTransaction("Write Results");
  try {
    params.put("url", file.toURI().toURL());
    final DataStore dataStore = factory.createNewDataStore(params);
    dataStore.createSchema(sft);
    final SimpleFeatureStore store =
        (SimpleFeatureStore) dataStore.getFeatureSource(dataStore.getTypeNames()[0]);
    store.setTransaction(transaction);
    final SimpleFeatureCollection featureCollection =
        DataUtilities.collection(new SimpleFeatureIterator() {
          @Override
          public boolean hasNext() {
            return features.hasNext();
          }

          @Override
          public SimpleFeature next() throws NoSuchElementException {
            return features.next();
          }

          @Override
          public void close() {}
        });
    store.addFeatures(featureCollection);
    transaction.commit();
  } catch (final Exception e) {
    try {
      transaction.rollback();
    } catch (final IOException ioe) {
      throw new RuntimeException("Encountered an error when rolling back transaction", ioe);
    }
    throw new RuntimeException(
        "Encountered an error when writing the features to the file: " + e.getMessage(),
        e);
  }
}
 
Example #5
Source File: SpatialDbsImportUtils.java    From hortonmachine with GNU General Public License v3.0 3 votes vote down vote up
/**
 * Create a spatial table using a shapefile as schema.
 * 
 * @param db the database to use.
 * @param shapeFile the shapefile to use.
 * @param newTableName the new name of the table. If null, the shp name is used.
 * @param forceSrid an optional srid to force the table to.
 * @return the name of the created table.
 * @param avoidSpatialIndex if <code>true</code>, no spatial index will be created. This is useful if many records 
 *          have to be inserted and the index will be created later manually.
 * @return the name of the created table.
 * @throws Exception
 */
public static String createTableFromShp( ASpatialDb db, File shapeFile, String newTableName, String forceSrid,
        boolean avoidSpatialIndex ) throws Exception {
    FileDataStore store = FileDataStoreFinder.getDataStore(shapeFile);
    SimpleFeatureSource featureSource = store.getFeatureSource();
    SimpleFeatureType schema = featureSource.getSchema();
    if (newTableName == null) {
        newTableName = FileUtilities.getNameWithoutExtention(shapeFile);
    }
    return createTableFromSchema(db, schema, newTableName, forceSrid, avoidSpatialIndex);
}
 
Example #6
Source File: SpatialDbsImportUtils.java    From hortonmachine with GNU General Public License v3.0 3 votes vote down vote up
/**
 * Import a shapefile into a table.
 * 
 * @param db the database to use.
 * @param shapeFile the shapefile to import.
 * @param tableName the name of the table to import to.
 * @param limit if > 0, a limit to the imported features is applied.
 * @param pm the progress monitor.
 * @return <code>false</code>, is an error occurred. 
 * @throws Exception
 */
public static boolean importShapefile( ASpatialDb db, File shapeFile, String tableName, int limit, IHMProgressMonitor pm )
        throws Exception {
    FileDataStore store = FileDataStoreFinder.getDataStore(shapeFile);
    SimpleFeatureSource featureSource = store.getFeatureSource();
    SimpleFeatureCollection features = featureSource.getFeatures();

    return importFeatureCollection(db, features, tableName, limit, pm);

}
 
Example #7
Source File: NwwUtilities.java    From hortonmachine with GNU General Public License v3.0 3 votes vote down vote up
/**
 * Get the feature source from a file.
 * 
 * @param path
 *            the path to the shapefile.
 * @return the feature source.
 * @throws Exception
 */
public static SimpleFeatureSource readFeatureSource( String path ) throws Exception {
    File shapeFile = new File(path);
    FileDataStore store = FileDataStoreFinder.getDataStore(shapeFile);
    SimpleFeatureSource featureSource = store.getFeatureSource();
    return featureSource;
}