Java Code Examples for org.geotools.data.simple.SimpleFeatureSource#getFeatures()

The following examples show how to use org.geotools.data.simple.SimpleFeatureSource#getFeatures() . 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: DataSourceInfo.java    From sldeditor with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Gets the feature collection.
 *
 * @return the feature collection
 */
public SimpleFeatureCollection getFeatureCollection() {
    SimpleFeatureCollection featureCollection = null;
    try {
        if (dataStore != null) {
            SimpleFeatureSource source = dataStore.getFeatureSource(typeName);
            featureCollection = source.getFeatures();
        }
    } catch (IOException e) {
        ConsoleManager.getInstance().exception(this, e);
    }
    return featureCollection;
}
 
Example 3
Source File: SimpleConfigurator.java    From gama with GNU General Public License v3.0 5 votes vote down vote up
public static Style showDialog(final Shell parent, final SimpleFeatureSource featureSource, final Style style)
		throws IOException {
	final SimpleFeatureCollection features = featureSource.getFeatures();
	final SimpleConfigurator tmp = new SimpleConfigurator(parent, features, style);
	tmp.setBlockOnOpen(true);
	tmp.open();
	return tmp.getStyle();
}
 
Example 4
Source File: DataSourceInfo.java    From sldeditor with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Gets the feature collection.
 *
 * @return the feature collection
 */
public SimpleFeatureCollection getFeatureCollection() {
    SimpleFeatureCollection featureCollection = null;
    try {
        if(dataStore != null)
        {
            SimpleFeatureSource source = dataStore.getFeatureSource(typeName);
            featureCollection = source.getFeatures();
        }
    } catch (IOException e) {
        ConsoleManager.getInstance().exception(this, e);
    }
    return featureCollection;
}
 
Example 5
Source File: InLineFeatureModel.java    From sldeditor with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Populate.
 *
 * @param userLayer the user layer
 */
public void populate(UserLayer userLayer) {
    this.userLayer = userLayer;
    featureCollection = null;
    geometryFieldIndex = -1;

    columnList.clear();

    if ((userLayer != null) && (userLayer.getInlineFeatureType() != null)) {
        String typeName = userLayer.getInlineFeatureType().getTypeName();
        try {
            SimpleFeatureSource featureSource =
                    userLayer.getInlineFeatureDatastore().getFeatureSource(typeName);
            if (featureSource != null) {
                featureCollection = featureSource.getFeatures();
            }
        } catch (IOException e) {
            ConsoleManager.getInstance().exception(this, e);
        }

        if (featureCollection != null) {
            // Populate field names
            List<AttributeDescriptor> descriptorList =
                    featureCollection.getSchema().getAttributeDescriptors();
            int index = 0;
            for (AttributeDescriptor descriptor : descriptorList) {
                if (descriptor instanceof GeometryDescriptorImpl) {
                    geometryFieldIndex = index;
                }
                columnList.add(descriptor.getLocalName());
                index++;
            }
        }
    }

    this.fireTableStructureChanged();
    this.fireTableDataChanged();

    // Set up the editor to handle editing the table cells
    InlineCellEditor editor = new InlineCellEditor(this);

    if ((featureTable != null) && (featureTable.getColumnModel().getColumnCount() > 0)) {
        TableColumn column = featureTable.getColumnModel().getColumn(0);
        column.setCellEditor(editor);
        featureTable.setCellEditor(editor);
    }
}
 
Example 6
Source File: NwwUtilities.java    From hortonmachine with GNU General Public License v3.0 4 votes vote down vote up
public static SimpleFeatureCollection readAndReproject( SimpleFeatureSource featureSource ) throws Exception {
    SimpleFeatureCollection fc = featureSource.getFeatures();
    return reprojectToWGS84(fc);
}
 
Example 7
Source File: WRS2GeometryStore.java    From geowave with Apache License 2.0 4 votes vote down vote up
private void init() throws MalformedURLException, IOException {
  if (!wrs2Shape.exists()) {
    if (!wrs2Directory.delete()) {
      LOGGER.warn("Unable to delete '" + wrs2Directory.getAbsolutePath() + "'");
    }
    final File wsDir = wrs2Directory.getParentFile();
    if (!wsDir.exists() && !wsDir.mkdirs()) {
      LOGGER.warn("Unable to create directory '" + wsDir.getAbsolutePath() + "'");
    }

    if (!wrs2Directory.mkdirs()) {
      LOGGER.warn("Unable to create directory '" + wrs2Directory.getAbsolutePath() + "'");
    }
    // download and unzip the shapefile
    final File targetFile = new File(wrs2Directory, WRS2_SHAPE_ZIP);
    if (targetFile.exists()) {
      if (!targetFile.delete()) {
        LOGGER.warn("Unable to delete file '" + targetFile.getAbsolutePath() + "'");
      }
    }
    FileUtils.copyURLToFile(new URL(WRS2_SHAPE_URL), targetFile);
    final ZipFile zipFile = new ZipFile(targetFile);
    try {
      final Enumeration<ZipArchiveEntry> entries = zipFile.getEntries();
      while (entries.hasMoreElements()) {
        final ZipArchiveEntry entry = entries.nextElement();
        if (!entry.isDirectory()) {
          FileUtils.copyInputStreamToFile(
              zipFile.getInputStream(entry),
              new File(wrs2Directory, entry.getName()));
          // HP Fortify "Path Traversal" false positive
          // What Fortify considers "user input" comes only
          // from users with OS-level access anyway
        }
      }
    } finally {
      zipFile.close();
    }
  }
  // read the shapefile and cache the features for quick lookup by path
  // and row
  try {
    final Map<String, Object> map = new HashMap<>();
    map.put("url", wrs2Shape.toURI().toURL());
    final DataStore dataStore = DataStoreFinder.getDataStore(map);
    if (dataStore == null) {
      LOGGER.error("Unable to get a datastore instance, getDataStore returned null");
      return;
    }
    final SimpleFeatureSource source = dataStore.getFeatureSource(WRS2_TYPE_NAME);

    final SimpleFeatureCollection featureCollection = source.getFeatures();
    wrs2Type = featureCollection.getSchema();
    final SimpleFeatureIterator iterator = featureCollection.features();
    while (iterator.hasNext()) {
      final SimpleFeature feature = iterator.next();
      final Number path = (Number) feature.getAttribute("PATH");
      final Number row = (Number) feature.getAttribute("ROW");
      featureCache.put(
          new WRS2Key(path.intValue(), row.intValue()),
          (MultiPolygon) feature.getDefaultGeometry());
    }
  } catch (final IOException e) {
    LOGGER.error(
        "Unable to read wrs2_asc_desc shapefile '" + wrs2Shape.getAbsolutePath() + "'",
        e);
    throw (e);
  }
}
 
Example 8
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);

}