Advertisement
Guest User

Untitled

a guest
May 25th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. private static File getNewShapeFile(File csvFile) {
  2. String path = csvFile.getAbsolutePath();
  3. String newPath = path.substring(0, path.length() - 4) + ".shp";
  4.  
  5. JFileDataStoreChooser chooser = new JFileDataStoreChooser("shp");
  6. chooser.setDialogTitle("Save shapefile");
  7. chooser.setSelectedFile(new File(newPath));
  8.  
  9. int returnVal = chooser.showSaveDialog(null);
  10.  
  11. if (returnVal != JFileDataStoreChooser.APPROVE_OPTION) {
  12. // the user cancelled the dialog
  13. System.exit(0);
  14. }
  15.  
  16. File newFile = chooser.getSelectedFile();
  17. if (newFile.equals(csvFile)) {
  18. System.out.println("Error: cannot replace " + csvFile);
  19. System.exit(0);
  20. }
  21.  
  22. return newFile;
  23. }
  24.  
  25. public static void collectionToShapeFile(SimpleFeatureCollection collection, File file, SimpleFeatureType TYPE){
  26. try {
  27. File newFile = getNewShapeFile(file);
  28. ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
  29. Map<String, Serializable> params = new HashMap<String, Serializable>();
  30. params.put("url", newFile.toURI().toURL());
  31. params.put("create spatial index", Boolean.TRUE);
  32. ShapefileDataStore newDataStore = (ShapefileDataStore) dataStoreFactory.createNewDataStore(params);
  33. newDataStore.createSchema(TYPE);
  34. /* zdefiniowanie układu przestrzennej */ newDataStore.forceSchemaCRS(DefaultGeographicCRS.WGS84);
  35. Transaction transaction = new DefaultTransaction("create");
  36. String typeName = newDataStore.getTypeNames()[0];
  37. SimpleFeatureSource featureSource = newDataStore.getFeatureSource(typeName);
  38. if (featureSource instanceof SimpleFeatureStore) {
  39. SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
  40. featureStore.setTransaction(transaction);
  41. try {
  42. featureStore.addFeatures(collection);
  43. transaction.commit();
  44. } catch (Exception problem) {
  45. problem.printStackTrace();
  46. }
  47. }
  48. } catch (Exception ex) {
  49. Logger.getLogger(CSV2SHP.class.getName()).log(Level.SEVERE, null, ex);
  50. }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement