/** * 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 JDataStorePage(DataStoreFactorySpi format, Map<String, Object> params) { super(ID); setTitle(format.getDisplayName()); setDescription(format.getDescription()); this.format = format; if (params == null) { params = new HashMap<String, Object>(); if (format != null) { for (Param param : format.getParametersInfo()) { params.put(param.key, (Serializable) param.sample); } } } this.connectionParameters = params; }
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); }
public synchronized DataStore toDataAccess() throws IOException { if (dataStore == null) { // connect! try { dataStore = factory.createDataStore(connectionParams); } catch (IOException e) { message = e; throw e; } } return dataStore; }
public void createControl(Composite parent) { Composite mainComposite = new Composite(parent, SWT.NONE); GridLayout gridLayout = new GridLayout(2, false); mainComposite.setLayout(gridLayout); for (Param param : format.getParametersInfo()) { if (level != null) { String check = param.metadata == null ? "user" : (String) param.metadata.get(Parameter.LEVEL); if (check == null) { check = "user"; } if (level.equals(check)) { // we are good this is the one we want } else { continue; // skip since it is not the one we want } } String txt = param.title.toString(); if (param.required) { txt += "*"; } Label label = new Label(mainComposite, SWT.NONE); label.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false)); label.setText(txt); ParamField field = ParamField.create(mainComposite, param); field.doLayout(); fields.put(param, field); // if (param.description != null) { // JLabel info = new JLabel(formatDescription(param.description.toString())); // page.add(info, "skip, span, wrap"); // } } setControl(mainComposite); }