Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private static File getNewShapeFile(File csvFile) {
- String path = csvFile.getAbsolutePath();
- String newPath = path.substring(0, path.length() - 4) + ".shp";
- JFileDataStoreChooser chooser = new JFileDataStoreChooser("shp");
- chooser.setDialogTitle("Save shapefile");
- chooser.setSelectedFile(new File(newPath));
- int returnVal = chooser.showSaveDialog(null);
- if (returnVal != JFileDataStoreChooser.APPROVE_OPTION) {
- // the user cancelled the dialog
- System.exit(0);
- }
- File newFile = chooser.getSelectedFile();
- if (newFile.equals(csvFile)) {
- System.out.println("Error: cannot replace " + csvFile);
- System.exit(0);
- }
- return newFile;
- }
- public static void collectionToShapeFile(SimpleFeatureCollection collection, File file, SimpleFeatureType TYPE){
- try {
- File newFile = getNewShapeFile(file);
- ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
- Map<String, Serializable> params = new HashMap<String, Serializable>();
- params.put("url", newFile.toURI().toURL());
- params.put("create spatial index", Boolean.TRUE);
- ShapefileDataStore newDataStore = (ShapefileDataStore) dataStoreFactory.createNewDataStore(params);
- newDataStore.createSchema(TYPE);
- /* zdefiniowanie układu przestrzennej */ newDataStore.forceSchemaCRS(DefaultGeographicCRS.WGS84);
- Transaction transaction = new DefaultTransaction("create");
- String typeName = newDataStore.getTypeNames()[0];
- SimpleFeatureSource featureSource = newDataStore.getFeatureSource(typeName);
- if (featureSource instanceof SimpleFeatureStore) {
- SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
- featureStore.setTransaction(transaction);
- try {
- featureStore.addFeatures(collection);
- transaction.commit();
- } catch (Exception problem) {
- problem.printStackTrace();
- }
- }
- } catch (Exception ex) {
- Logger.getLogger(CSV2SHP.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement