Your program is very unclear, but I've attached a version that compiles
with the latest GeoTools. I can't work out what you are trying to do
exactly (it looks like a csv to shp file converter - in which case have a
look at csv2shp tutorial at
http://docs.geotools.org/stable/tutorials/feature/csv2shp.html ) .
Ian
package spike;
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 com.vividsolutions.jts.geom.Polygon;
import java.io.File;
import org.geotools.data.FeatureSource;
import org.geotools.data.FileDataStore;
import org.geotools.data.FileDataStoreFinder;
import org.geotools.data.collection.ListFeatureCollection;
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.ArrayList;
import java.util.HashMap;
import java.util.List;
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 Lejdel {
ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
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;
Lejdel(JMapFrame fenMap) {
// this.mapContext = mapContext;
this.fenMap = fenMap;
}
public void chercheAfficheVecteur() throws 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.setMapContent(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 = createFeatureType();
List<SimpleFeature> featureList = new ArrayList<SimpleFeature>();
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);
featureList.add(feature1);
}
SimpleFeatureCollection collection1 = new ListFeatureCollection(
TYPE, featureList);
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 {
Map<String, Serializable> params = new HashMap<String, Serializable>();
params.put("url", DataUtilities.fileToURL(newFile));
}
private static SimpleFeatureType createFeatureType() {
SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
builder.setName("MyFeature");
builder.setCRS(DefaultGeographicCRS.WGS84); // <- Coordinate reference
// system
// add attributes in order
builder.add("the_geom", Polygon.class);
builder.length(15).add("Name", String.class); // <- 15 chars width for
builder.add("Number", Double.class); // the number
// 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;
}
}
------------------------------------------------------------------------------
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