Hi all, I posted yetserday something but no reply...maybe not exact enough, so I'll try again after several further tests... I tried to work the feature-tutorial http://docs.geotools.org/latest/userguide/tutorial/feature/csv2shp.html when just copiing & paste all the code...the sample read a csv file correct and before it is written to the shapefile everything seems to be correct... but after "calculating" the coordinates in any gis-software the result is long=1 lat=1 for all Points. I tried to check the values at any detectable place but can't figure out why this happened... I swiched the CRS to ETRS Coordinates to get an other coordinate space instead of wgs84...but the same result.... It seems to me at the moment that: a) theres anything lost to do in the Tutorial (setting of something...) or b) theres a bug behind (?) that code.... or c) I got huge tomatos on my eyes.... anyway.... I use eclipse on win7-64bit using jdk7_u40
Here's the code, which I feed with some system.out.controlpoints at some places, also I'd used here etrs89 crs... is anybody able to help here? the CSV for Import: ######################## r, h, CITY, NUMBER 32545646.06, 6034311.67, Trento, 140 32553244.94, 6053693.02, St Paul, 125 32554313.75, 6023100.49, Bangkok, 150 32583145.42, 6124975.69, Ottawa, 200 32594344.98, 6025393.27, Minneapolis, 350 32553846.53, 6023526.65, Lausanne, 560 ######################### sorry for the huge code-snippet, but so it should work with copy & paste package org.geotools.FeatureTest; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.Serializable; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.geotools.data.DefaultTransaction; import org.geotools.data.Transaction; import org.geotools.data.collection.ListFeatureCollection; import org.geotools.data.shapefile.ShapefileDataStore; import org.geotools.data.shapefile.ShapefileDataStoreFactory; import org.geotools.data.simple.SimpleFeatureCollection; import org.geotools.data.simple.SimpleFeatureIterator; import org.geotools.data.simple.SimpleFeatureSource; import org.geotools.data.simple.SimpleFeatureStore; import org.geotools.feature.simple.SimpleFeatureBuilder; import org.geotools.feature.simple.SimpleFeatureTypeBuilder; import org.geotools.swing.data.JFileDataStoreChooser; import org.opengis.feature.simple.SimpleFeature; import org.opengis.feature.simple.SimpleFeatureType; import org.geotools.referencing.CRS; import com.vividsolutions.jts.geom.Coordinate; import com.vividsolutions.jts.geom.GeometryFactory; import com.vividsolutions.jts.geom.Point; public class Csv2Shape { public static void main(String[] args) throws Exception { File file = JFileDataStoreChooser.showOpenFile("csv", null); if (file == null) { return; } final SimpleFeatureType TYPE=createFeatureType(); List<SimpleFeature> features = new ArrayList<SimpleFeature>(); // GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(); GeometryFactory geometryFactory = new GeometryFactory() ; SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(TYPE); BufferedReader reader = new BufferedReader(new FileReader(file)); try { for (line = reader.readLine(); line != null; line = reader.readLine()) { if (line.trim().length() > 0) { // skip blank lines String tokens[] = line.split("\\,"); double latitude = Double.parseDouble(tokens[0]); double longitude = Double.parseDouble(tokens[1]); String name = tokens[2].trim(); int number = Integer.parseInt(tokens[3].trim()); // OK =>!! System.out.println(latitude+" LONG:"+longitude+" Name="+name); Point point = geometryFactory.createPoint(new Coordinate(longitude, latitude)); // OK =>!! System.out.println("Point="+point); featureBuilder.add(point); featureBuilder.add(name); featureBuilder.add(number); SimpleFeature feature = featureBuilder.buildFeature(null); features.add(feature); //Point g = (Point) features.get(0).getAttribute( 0 ); //OK=>!! System.out.println("p0="+g); //OK=> System.out.println("bnd X "+feature.getBounds().getMinX()+" Y="+feature.getBounds().getMinY()); } } } finally { reader.close(); } /* * Get an output file name and create the new 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); newDataStore.forceSchemaCRS(CRS.decode("EPSG:25832 ")); /* * Write the features to the shapefile */ Transaction transaction = new DefaultTransaction("create"); String typeName = newDataStore.getTypeNames()[0]; SimpleFeatureSource featureSource = newDataStore.getFeatureSource(typeName); if (featureSource instanceof SimpleFeatureStore) { SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource; SimpleFeatureCollection collection = new ListFeatureCollection(TYPE, features); //again for checking SimpleFeatureIterator iterator = collection.features(); try { while( iterator.hasNext() ){ SimpleFeature feature = iterator.next(); // OK =>!! System.out.println( feature.getAttribute("Location") ); } } finally { iterator.close(); } //result: gaves correct Coordinates... 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 shapefile"); chooser.setSelectedFile(new File(newPath)); int returnVal = chooser.showSaveDialog(null); if (returnVal != JFileDataStoreChooser.APPROVE_OPTION) { System.exit(0); } File newFile = chooser.getSelectedFile(); if (newFile.equals(csvFile)) { System.out.println("Error: cannot replace " + csvFile); System.exit(0); } return newFile; } private static SimpleFeatureType createFeatureType() { SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder(); builder.setName("Location"); builder.setSRS("EPSG:25832"); // add attributes in order builder.add("Location", Point.class); builder.length(15).add("Name", String.class); // <- 15 chars width for name field builder.add("id", Integer.class); // build the type final SimpleFeatureType LOCATION = builder.buildFeatureType(); return LOCATION; } } The Result FAT with calculated Coordinates is: FID_;Name;id;east;north ;Trento;140;1.000000000000000;1.000000000000000 ;St Paul;125;1.000000000000000;1.000000000000000 ;Bangkok;150;1.000000000000000;1.000000000000000 ;Ottawa;200;1.000000000000000;1.000000000000000 ;Minneapolis;350;1.000000000000000;1.000000000000000 ;Lausanne;560;1.000000000000000;1.000000000000000 :( seem to be that something in the shapefilewriter behind the above code doesnt work as it has to do... also the Coordinates are not viewable at Point (1,1) - so it looks for me that there's anything wrong in the definition - and the Tutorial for that is anybody able to figure out which Problem in the code is? hopefully better described and hopefully any hints, answers and maybe a solution includin explanation... best wishes rolf ------------------------------------------------------------------------------ October Webinars: Code for Performance Free Intel webinars can help you accelerate application performance. Explore tips for MPI, OpenMP, advanced profiling, and more. Get the most from the latest Intel processors and coprocessors. See abstracts and register > http://pubads.g.doubleclick.net/gampad/clk?id=60134071&iu=/4140/ostg.clktrk _______________________________________________ GeoTools-GT2-Users mailing list GeoTools-GT2-Users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users