Advertisement
Guest User

Untitled

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