Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main_package;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileReader;
- import java.io.Serializable;
- import java.util.HashMap;
- import java.util.Map;
- import org.geotools.data.DataUtilities;
- import org.geotools.data.DefaultTransaction;
- import org.geotools.data.Transaction;
- import org.geotools.data.shapefile.ShapefileDataStore;
- import org.geotools.data.shapefile.ShapefileDataStoreFactory;
- import org.geotools.data.simple.SimpleFeatureCollection;
- import org.geotools.data.simple.SimpleFeatureSource;
- import org.geotools.data.simple.SimpleFeatureStore;
- import org.geotools.feature.FeatureCollections;
- import org.geotools.feature.simple.SimpleFeatureBuilder;
- import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
- import org.geotools.geometry.jts.JTSFactoryFinder;
- import org.geotools.referencing.crs.DefaultGeographicCRS;
- import org.geotools.swing.data.JFileDataStoreChooser;
- import org.opengis.feature.simple.SimpleFeature;
- import org.opengis.feature.simple.SimpleFeatureType;
- import com.vividsolutions.jts.geom.Coordinate;
- import com.vividsolutions.jts.geom.GeometryFactory;
- import com.vividsolutions.jts.geom.LineString;
- import com.vividsolutions.jts.geom.Point;
- public class CSV2SHP {
- private static double long;
- private static double lat;
- static File file;
- static SimpleFeatureCollection collection;
- static SimpleFeatureType TYPE;
- static SimpleFeatureType TYPE_DROGA;
- static SimpleFeatureType TYPE_DOM;
- public static void main(String[] args) throws Exception {
- file = JFileDataStoreChooser.showOpenFile("csv", null);
- if (file == null) {
- return;
- }
- System.out.print(file.getName() + "\n");
- TYPE = DataUtilities.createType("Location",
- "location:Point:srid=4326," + // definiuje geometrię odpowiadającą przestrzennemu charakterowi danej cechy – w tym przypadku punkt
- "name:String," + // <- atrybut nazwa
- "number:Integer" // atrybut numer
- );
- // TYPE_DROGA = DataUtilities.createType("LINE", "centerline:LineString,name:\"\",id:0");
- // Kolekcja o której mowiliśmy
- collection = FeatureCollections.newCollection();
- // Fabryka GeometryFactory tworzy atrybut geometrii dla danej cechy – wtym przyadku geometrią jest Point
- GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(null);
- SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(TYPE);
- BufferedReader bufferedReader = new BufferedReader(new FileReader(file.getAbsolutePath()));
- String line = bufferedReader.readLine(); //head line
- while( (line = bufferedReader.readLine()) !=null){
- String[] strings = line.split(", ");
- lat = Double.valueOf(strings[0]);
- long = Double.valueOf(strings[1]);
- Point point = geometryFactory.createPoint(new Coordinate(long, lat));
- featureBuilder.add(point);
- featureBuilder.add(strings[2]);
- featureBuilder.add(Integer.valueOf(strings[3]));
- SimpleFeature feature = featureBuilder.buildFeature(null);
- collection.add(feature);
- }
- bufferedReader.close();
- export();
- }
- public static void export() throws Exception{
- /* nazwa pliku shapefile */
- 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();
- transaction.rollback();
- } finally {
- transaction.close();
- }
- System.exit(0); // success!
- } else {
- System.out.println(typeName + " does not support read/write access");
- System.exit(1);
- }
- }
- 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 shp file");
- chooser.setSelectedFile(new File(newPath));
- int retV = chooser.showSaveDialog(null);
- if (retV != 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 + " file");
- System.exit(0);
- }
- return newFile;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement