Please, I have the coordinates of polygons, I want to save them in a
shapefile
I wrote this program but it does not work:
public class Afficher {
    
    
    public void chercheAfficher(Coordinate[] coord) throws IOException 
//throws Exception
{
    
   /**for ( int i =0;i< coord.length;i++){
           System.out.println(coord[i].x + "      " + coord[i].y );}**/
    
    final SimpleFeatureType TYPE = createFeatureType();
                
    
     SimpleFeatureCollection collection =
FeatureCollections.newCollection();
     GeometryFactory geometryFactory =
JTSFactoryFinder.getGeometryFactory(null);
     
     
     LinearRing ring = geometryFactory.createLinearRing( coord);

     LinearRing holes[] = null; // use LinearRing[] to represent holes

      LinearRing polygon = geometryFactory.createLinearRing(coord);
      
      SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(TYPE);
      featureBuilder.add(polygon);
     // featureBuilder.add("name");
     // featureBuilder.add("15");
      SimpleFeature feature = featureBuilder.buildFeature(null);
      collection.add(feature);
      
      
       String  file = FileUtils.getFileName("D:\\location.csv");
       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);

        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 SimpleFeatureType createFeatureType() {

        SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
        builder.setName("Location");
        builder.setCRS(DefaultGeographicCRS.WGS84); // <- Coordinate
reference system
         
        // add attributes in order
        builder.add("Location", Polygon.class);
        builder.length(15).add("Name", String.class); // <- 15 chars width
for name field

        // build the type
        final SimpleFeatureType LOCATION = builder.buildFeatureType();

        return LOCATION;
    
}
        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 shapefile");
        chooser.setSelectedFile(new File(newPath));

        int returnVal = chooser.showSaveDialog(null);

        if (returnVal != 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);
            System.exit(0);
        }

        return newFile;
    }

    

}



--
View this message in context: 
http://osgeo-org.1560.x6.nabble.com/Polygon-to-Shapefile-tp5195100.html
Sent from the geotools-gt2-users mailing list archive at Nabble.com.

------------------------------------------------------------------------------
Dive into the World of Parallel Programming The Go Parallel Website, sponsored
by Intel and developed in partnership with Slashdot Media, is your hub for all
things parallel software development, from weekly thought leadership blogs to
news, videos, case studies, tutorials and more. Take a look and join the 
conversation now. http://goparallel.sourceforge.net/
_______________________________________________
GeoTools-GT2-Users mailing list
GeoTools-GT2-Users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users

Reply via email to