EsriShapeFileWriter { static void write(List<SimpleFeature> features, File file) throws IOException { String basename = file.getName(); if (basename.endsWith(FILE_EXTENSION_SHAPEFILE)) { basename = basename.substring(0, basename.length() - 4); } File shapefile = new File(file.getParentFile(), basename + FILE_EXTENSION_SHAPEFILE); ShapefileDataStoreFactory factory = new ShapefileDataStoreFactory(); Map map = Collections.singletonMap("url", shapefile.toURI().toURL()); DataStore dataStore = factory.createNewDataStore(map); SimpleFeature simpleFeature = features.get(0); SimpleFeatureType simpleFeatureType = simpleFeature.getType(); final String typeName = simpleFeatureType.getName().getLocalPart(); dataStore.createSchema(simpleFeatureType); FeatureStore<SimpleFeatureType, SimpleFeature> featureStore = (FeatureStore<SimpleFeatureType, SimpleFeature>) dataStore.getFeatureSource(typeName); DefaultTransaction transaction = new DefaultTransaction("X"); featureStore.setTransaction(transaction); final FeatureCollection<SimpleFeatureType, SimpleFeature> featureCollection = DataUtilities.collection(features); featureStore.addFeatures(featureCollection); try { transaction.commit(); } catch (Exception e) { transaction.rollback(); throw new IOException("Cannot write shapefile. Error:\n" + e.getMessage(), e); } finally { dataStore.dispose(); transaction.close(); } }    }
@Test public void testSingleShape() throws Exception { testdataDir = new File("target/statistics-test-io_1"); final File targetShapefile = getTestFile("4_pixels_output.shp").getAbsoluteFile(); final List<SimpleFeature> featureWithAdaptedStatistic = get4PixelsFeaturesWithAdaptedStatistic(); EsriShapeFileWriter.write(featureWithAdaptedStatistic, targetShapefile); final FeatureSource<SimpleFeatureType, SimpleFeature> featureSource = FeatureUtils.getFeatureSource(targetShapefile.toURI().toURL()); final FeatureCollection<SimpleFeatureType, SimpleFeature> features = featureSource.getFeatures(); assertEquals(1, features.size()); final SimpleFeature simpleFeature = features.features().next(); assertNotNull(simpleFeature.getProperty("p90_lgl2")); assertNotNull(simpleFeature.getProperty("p95_lgl2")); assertEquals(0.1, (Double) simpleFeature.getProperty("p90_lgl2").getValue(), 1E-6); assertEquals(0.195, (Double) simpleFeature.getProperty("p95_lgl2").getValue(), 1E-6); }