package myapplication; import com.vividsolutions.jts.geom.Coordinate; import com.vividsolutions.jts.geom.Geometry; import com.vividsolutions.jts.geom.GeometryFactory; import com.vividsolutions.jts.geom.LinearRing; import jade.core.ProfileImpl;
import jade.wrapper.StaleProxyException; import java.io.File; import org.geotools.data.FeatureSource; import org.geotools.data.FileDataStore; import org.geotools.data.FileDataStoreFinder; import org.geotools.data.simple.SimpleFeatureCollection; import org.geotools.data.simple.SimpleFeatureIterator; import org.geotools.map.DefaultMapContext; import org.geotools.map.MapContext; import org.geotools.swing.JMapFrame; import org.geotools.swing.data.JFileDataStoreChooser; import org.opengis.feature.simple.SimpleFeature; import java.io.IOException; import java.io.Serializable; import java.net.MalformedURLException; 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.SimpleFeatureSource; import org.geotools.data.simple.SimpleFeatureStore; import org.geotools.feature.FeatureCollections; import org.geotools.feature.SchemaException; 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.opengis.feature.simple.SimpleFeatureType; public class ImportVecteur2 { public static int nbrobj = 2; public static int i =1; private static Coordinate[] CoordInit; public Coordinate[] CoordGen; public static AMSAgentDescription [] agents = null; private JMapFrame fenMap; private MapContext mapContext; ImportVecteur2(JMapFrame fenMap) { //this.mapContext = mapContext; this.fenMap = fenMap; } public void chercheAfficheVecteur() throws StaleProxyException, SchemaException, IOException //throws Exception { try { File file = JFileDataStoreChooser.showOpenFile("shp", null); if (file == null) { return; } FileDataStore store = FileDataStoreFinder.getDataStore(file); FeatureSource featureSource = store.getFeatureSource(); //get vertices of file // Create a map context and add our shapefile to it mapContext = new DefaultMapContext(); mapContext.addLayer(featureSource, null); // Now display the map fenMap.enableLayerTable(true); fenMap.setMapContext(mapContext); fenMap.setVisible(true); SimpleFeatureCollection collection=(SimpleFeatureCollection) featureSource.getFeatures(); SimpleFeatureIterator iterator = collection.features(); System.out.println("tong so:"+ collection.size()); nbrobj= collection.size(); int t=0; // File fileout; // fileout = new File("F:\\Disp.shp"); File newFile = getNewShapeFile(file); final SimpleFeatureType TYPE = DataUtilities.createType("the_geom", "location:Polygon:srid=4326," + // <- the geometry attribute: Point type "name:String," + // <- a String attribute "number:Integer" // a number attribute ); //final SimpleFeatureType TYPE = createFeatureType(); SimpleFeatureCollection collection1 = FeatureCollections.newCollection(); while (iterator.hasNext()) { //System.out.println("le num de l'objet " + k); SimpleFeature feature = iterator.next(); //System.out.println( feature.getIdentifier()); Geometry geom = (Geometry) feature.getDefaultGeometry(); CoordInit = geom.getCoordinates(); //final SimpleFeatureType TYPE = createFeatureType(); //Afficher aff = new Afficher(); SimpleFeature feature1 = CreateFeature(CoordInit,TYPE); collection1.add(feature1); } CreateFile(TYPE,collection1,newFile); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void CreateFile(SimpleFeatureType TYPE,SimpleFeatureCollection collection1, File newFile) throws MalformedURLException, IOException { 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); /* * You can comment out this line if you are using the createFeatureType method (at end of * class file) rather than DataUtilities.createType */ newDataStore.forceSchemaCRS(DefaultGeographicCRS.WGS84); Transaction transaction = new DefaultTransaction("create"); String typeName = newDataStore.getTypeNames()[0]; SimpleFeatureSource featureSource1 = newDataStore.getFeatureSource(typeName); if (featureSource1 instanceof SimpleFeatureStore) { SimpleFeatureStore featureStore1 = (SimpleFeatureStore) featureSource1; featureStore1.setTransaction(transaction); try { featureStore1.addFeatures(collection1); 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("the_geom"); 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; } public SimpleFeature CreateFeature(Coordinate[] Coord,SimpleFeatureType TYPE) { //Create de SHP Type System.out.println("je suis dans l'afficheur"); SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(TYPE); //GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(null); String name = "thing" + Math.random(); int number = (int) Math.round(Math.random() * 10.0); GeometryFactory geometryFactory = new GeometryFactory(); /* Longitude (= x coord) first ! */ LinearRing ring = geometryFactory.createLinearRing(Coord); com.vividsolutions.jts.geom.Polygon polygon = geometryFactory.createPolygon(ring,null); featureBuilder.add(polygon); featureBuilder.add(name); featureBuilder.add(number ); final SimpleFeature feature = featureBuilder.buildFeature(null); return feature; } 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/Write-feature-in-shapefile-tp5196083p5196121.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