Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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.LinearRing;
- import com.vividsolutions.jts.geom.MultiPoint;
- import com.vividsolutions.jts.geom.Point;
- import com.vividsolutions.jts.geom.Polygon;
- public class CSV2SHP {
- static File file;
- ;//= DataUtilities.createType("Location","location:Point:srid=4326," + "name:String," +"number:Integer");
- public static void main(String[] args) throws Exception {
- file = JFileDataStoreChooser.showOpenFile("csv", null);
- if (file == null) {
- return;
- }
- final SimpleFeatureType TYPE = DataUtilities.createType("EDGE", "edge:Polygon:srid=4326," + "name:String," +"number:Integer");
- //final SimpleFeatureType TYPEPol = DataUtilities.createType("Location","location:Polygon:srid=4326," + "name:String," +"number:Integer");
- /* Kolekcja o której mowiliśmy */
- SimpleFeatureCollection 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(", ");
- Coordinate[] coordinates = new Coordinate[strings.length/2];
- for (int i=0;i<coordinates.length;i++){
- coordinates[i] = new Coordinate(Double.valueOf(strings[i*2]),Double.valueOf(strings[i*2+1]));
- }
- Double lat = Double.valueOf(strings[0]);
- Double lon = Double.valueOf(strings[1]);
- LinearRing shell = new LinearRing(coordinates, geometryFactory);
- LinearRing[] holes = null;
- Polygon point = geometryFactory.createPolygon(shell, holes);// .createMultiPoint(coordinates);// .createPoint(new Coordinate(lon, lat));
- featureBuilder.add(point);
- line = bufferedReader.readLine();
- strings = line.split(", ");
- featureBuilder.add(strings[0]);
- featureBuilder.add(Integer.valueOf(strings[1]));
- SimpleFeature feature = featureBuilder.buildFeature(null);
- collection.add(feature);
- //System.out.println(point.getX() + " " + point.getY());
- }
- bufferedReader.close();
- export(TYPE,collection);
- }
- public static void export(SimpleFeatureType TYPE,SimpleFeatureCollection collection) 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