/** * This method takes the input {@link SimpleFeatureCollection} and transforms it into a shapefile * using the provided file. * * <p>Make sure the provided files ends with .shp. * * @param fc the {@link SimpleFeatureCollection} to be encoded as a shapefile. * @param destination the {@link File} where we want to write the shapefile. * @throws IOException in case an {@link IOException} is thrown by the underlying code. */ protected static void featureCollectionToShapeFile( final SimpleFeatureCollection fc, final File destination) throws IOException { // // checks // org.geotools.util.Utilities.ensureNonNull("fc", fc); Utilities.ensureNonNull("destination", destination); // checks on the file if (destination.exists()) { if (destination.isDirectory()) throw new IOException("The provided destination maps to a directory:" + destination); if (!destination.canWrite()) throw new IOException( "The provided destination maps to an existing file that cannot be deleted:" + destination); if (!destination.delete()) throw new IOException( "The provided destination maps to an existing file that cannot be deleted:" + destination); } // real work final DataStoreFactorySpi dataStoreFactory = new ShapefileDataStoreFactory(); Map<String, Serializable> params = new HashMap<String, Serializable>(); params.put("url", destination.toURI().toURL()); params.put("create spatial index", Boolean.TRUE); ShapefileDataStore store = null; Transaction transaction = null; try { store = (ShapefileDataStore) dataStoreFactory.createNewDataStore(params); store.createSchema(fc.getSchema()); final SimpleFeatureStore featureStore = (SimpleFeatureStore) store.getFeatureSource(fc.getSchema().getName()); transaction = featureStore.getTransaction(); featureStore.addFeatures(fc); } catch (IOException e) { e.printStackTrace(); } finally { if (transaction != null) { transaction.commit(); transaction.close(); } if (store != null) { store.dispose(); } } }
public void testRandomProcess() throws Exception { File f = TestData.file(this, "all_data.shp"); // System.out.println(f + " " + f.exists()); URL url = DataUtilities.fileToURL(f); DataStoreFactorySpi dataStoreFactory = new ShapefileDataStoreFactory(); Map<String, Serializable> params2 = new HashMap<String, Serializable>(); params2.put("url", url); params2.put("create spatial index", Boolean.TRUE); ShapefileDataStore store = (ShapefileDataStore) dataStoreFactory.createNewDataStore(params2); // ShapefileDataStore store = new ShapefileDataStore(url); assertNotNull(store); FeatureSource featureSource = store.getFeatureSource(); final FeatureCollection features = featureSource.getFeatures(); Map<String, Object> params = new HashMap<String, Object>(); params.put(ClusterMethodFactory.NAME.key, "random"); params.put(ClusterMethodFactory.POPULATION.key, features); params.put(ClusterMethodFactory.POPATTRIBUTE.key, "pop"); params.put(ClusterMethodFactory.CANCER.key, features); params.put(ClusterMethodFactory.CANATTRIBUTE.key, "cases"); params.put(ClusterMethodFactory.MINRAD.key, 1000.0); params.put(ClusterMethodFactory.MAXRAD.key, 5000.0); params.put(ClusterMethodFactory.NCIRCLES.key, 500.0); params.put(ClusterMethodFactory.TESTNAME.key, "poisson"); ClusterMethodFactory factory = new ClusterMethodFactory(); Process process = factory.create(params); assertNotNull(process); long start = System.currentTimeMillis(); Map<String, Object> results = process.execute(params, new ClusterMonitor()); long end = System.currentTimeMillis(); System.out.println("process took " + ((end - start) / 1000) + " seconds"); GridCoverage2D grid = (GridCoverage2D) results.get(ClusterMethodFactory.RESULT.key); String basename = f.toString(); basename = basename.substring(0, basename.length() - 4); String filename = basename + "_rand.tiff"; Utilities.writeGrid(filename, grid); FeatureCollection outfeatures = (FeatureCollection) results.get(ClusterMethodFactory.CIRCLES.key); Utilities.writeCircles(basename + "_rand.shp", outfeatures); }