Advertisement
Guest User

Untitled

a guest
Apr 19th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.69 KB | None | 0 0
  1. package main_package;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.File;
  5. import java.io.FileReader;
  6. import java.io.Serializable;
  7. import java.util.HashMap;
  8. import java.util.Map;
  9.  
  10. import org.geotools.data.DataUtilities;
  11. import org.geotools.data.DefaultTransaction;
  12. import org.geotools.data.Transaction;
  13. import org.geotools.data.shapefile.ShapefileDataStore;
  14. import org.geotools.data.shapefile.ShapefileDataStoreFactory;
  15. import org.geotools.data.simple.SimpleFeatureCollection;
  16. import org.geotools.data.simple.SimpleFeatureSource;
  17. import org.geotools.data.simple.SimpleFeatureStore;
  18. import org.geotools.feature.FeatureCollections;
  19. import org.geotools.feature.simple.SimpleFeatureBuilder;
  20. import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
  21. import org.geotools.geometry.jts.JTSFactoryFinder;
  22. import org.geotools.referencing.crs.DefaultGeographicCRS;
  23. import org.geotools.swing.data.JFileDataStoreChooser;
  24. import org.opengis.feature.simple.SimpleFeature;
  25. import org.opengis.feature.simple.SimpleFeatureType;
  26.  
  27. import com.vividsolutions.jts.geom.Coordinate;
  28. import com.vividsolutions.jts.geom.GeometryFactory;
  29. import com.vividsolutions.jts.geom.LineString;
  30. import com.vividsolutions.jts.geom.Point;
  31.  
  32. public class CSV2SHP {
  33.  
  34. private static double long;
  35. private static double lat;
  36. static File file;
  37. static SimpleFeatureCollection collection;
  38. static SimpleFeatureType TYPE;
  39. static SimpleFeatureType TYPE_DROGA;
  40. static SimpleFeatureType TYPE_DOM;
  41.  
  42. public static void main(String[] args) throws Exception {
  43. file = JFileDataStoreChooser.showOpenFile("csv", null);
  44. if (file == null) {
  45. return;
  46. }
  47. System.out.print(file.getName() + "\n");
  48.  
  49. TYPE = DataUtilities.createType("Location",
  50. "location:Point:srid=4326," + // definiuje geometrię odpowiadającą przestrzennemu charakterowi danej cechy – w tym przypadku punkt
  51. "name:String," + // <- atrybut nazwa
  52. "number:Integer" // atrybut numer
  53. );
  54.  
  55. // TYPE_DROGA = DataUtilities.createType("LINE", "centerline:LineString,name:\"\",id:0");
  56.  
  57.  
  58. // Kolekcja o której mowiliśmy
  59. collection = FeatureCollections.newCollection();
  60. // Fabryka GeometryFactory tworzy atrybut geometrii dla danej cechy – wtym przyadku geometrią jest Point
  61. GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(null);
  62. SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(TYPE);
  63.  
  64.  
  65. BufferedReader bufferedReader = new BufferedReader(new FileReader(file.getAbsolutePath()));
  66. String line = bufferedReader.readLine(); //head line
  67.  
  68. while( (line = bufferedReader.readLine()) !=null){
  69. String[] strings = line.split(", ");
  70. lat = Double.valueOf(strings[0]);
  71. long = Double.valueOf(strings[1]);
  72. Point point = geometryFactory.createPoint(new Coordinate(long, lat));
  73. featureBuilder.add(point);
  74. featureBuilder.add(strings[2]);
  75. featureBuilder.add(Integer.valueOf(strings[3]));
  76.  
  77. SimpleFeature feature = featureBuilder.buildFeature(null);
  78. collection.add(feature);
  79.  
  80. }
  81. bufferedReader.close();
  82. export();
  83.  
  84.  
  85. }
  86.  
  87. public static void export() throws Exception{
  88. /* nazwa pliku shapefile */
  89. File newFile = getNewShapeFile(file);
  90. ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
  91. Map<String, Serializable> params = new HashMap<String, Serializable>();
  92. params.put("url", newFile.toURI().toURL());
  93. params.put("create spatial index", Boolean.TRUE);
  94. ShapefileDataStore newDataStore = (ShapefileDataStore) dataStoreFactory.createNewDataStore(params);
  95. newDataStore.createSchema(TYPE);
  96. /* zdefiniowanie układu przestrzennej */
  97. newDataStore.forceSchemaCRS(DefaultGeographicCRS.WGS84);
  98. Transaction transaction = new DefaultTransaction("create");
  99. String typeName = newDataStore.getTypeNames()[0];
  100. SimpleFeatureSource featureSource = newDataStore.getFeatureSource(typeName);
  101. if (featureSource instanceof SimpleFeatureStore) {
  102. SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
  103. featureStore.setTransaction(transaction);
  104. try {
  105. featureStore.addFeatures(collection);
  106. transaction.commit();
  107. } catch (Exception problem) {
  108. problem.printStackTrace();
  109. transaction.rollback();
  110. } finally {
  111. transaction.close();
  112. }
  113. System.exit(0); // success!
  114. } else {
  115. System.out.println(typeName + " does not support read/write access");
  116. System.exit(1);
  117. }
  118. }
  119.  
  120. private static File getNewShapeFile(File csvFile) {
  121.  
  122. String path = csvFile.getAbsolutePath();
  123. String newPath = path.substring(0, path.length() - 4) + ".shp";
  124.  
  125. JFileDataStoreChooser chooser = new JFileDataStoreChooser("shp");
  126. chooser.setDialogTitle("Save shp file");
  127. chooser.setSelectedFile(new File(newPath));
  128. int retV = chooser.showSaveDialog(null);
  129. if (retV != JFileDataStoreChooser.APPROVE_OPTION) {
  130. // the user cancelled the dialog
  131. System.exit(0);
  132. }
  133.  
  134. File newFile = chooser.getSelectedFile();
  135. if (newFile.equals(csvFile)) {
  136. System.out.println("error: cannot replace " + csvFile + " file");
  137. System.exit(0);
  138. }
  139.  
  140. return newFile;
  141. }
  142.  
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement