1

When I convert CSV to Shapefile in GeoTools, I have an error NULL store for first attribute in my shapefile. e.g. -

Want result:

POINT(3625.55 3659.25), 2.4, 3.9

Error result:

Null, 2.4, 3.9

This is my program:

public class Csv2ShapeNew {

    public static void main(String[] args) throws Exception {
        File file = getCSVFile(args);

        BufferedReader reader = new BufferedReader(new FileReader(file));
        List<SimpleFeature> features = new ArrayList<SimpleFeature>();

        SimpleFeatureCollection collection = null ;//= new ListFeatureCollection(TYPE, features);
        SimpleFeatureType TYPE = null;
        try {
            String line = reader.readLine();

            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("location:Point,");

            String[] headers = line.split("\\,");

            for (String header : headers) {
                stringBuilder.append(header).append(":String,");
            }
            //stringBuilder.append("location:Point,");

            TYPE = DataUtilities.createType("Location", stringBuilder.substring(0,stringBuilder.toString().length() - 1));
            //System.out.println(stringBuilder.toString());
            //System.out.println(TYPE);
            GeometryFactory factory = JTSFactoryFinder.getGeometryFactory(GeoTools.getDefaultHints());

            for (line = reader.readLine(); line != null; line = reader.readLine()) {
                String split[] = line.split("\\,");

                //String name = split[0];
                double latitude = Double.parseDouble(split[0]);
                double longitude = Double.parseDouble(split[1]);

                Object[] o = new Object[split.length+1];
                //o[0]="";
                o[0] = (factory.createPoint(new Coordinate(longitude, latitude)));
                    for (int i = 1; i < o.length; i++) {
                        o[i] = split[i-1];
                    }

               /* for (int i = 0; i < o.length; i++) {
                    System.out.println(o[i]);// = split[i-2];
                }*/


                    //o[1] = name;

                System.out.println(o[0]);
                SimpleFeature feature = SimpleFeatureBuilder.build(TYPE, o, "");
                features.add(feature);
                collection = new ListFeatureCollection(TYPE, features);
                System.out.println(features.get(0));
            }
        } finally {
            reader.close();
        }
        File newFile = getNewShapeFile( file );

        DataStoreFactorySpi factory = new ShapefileDataStoreFactory();

        Map<String, Serializable> create = new HashMap<String, Serializable>();
        create.put("url", newFile.toURI().toURL());
        create.put("create spatial index", Boolean.TRUE);

        ShapefileDataStore newDataStore = (ShapefileDataStore) factory.createNewDataStore(create);
        newDataStore.createSchema(TYPE);
        System.out.println(TYPE);
        newDataStore.forceSchemaCRS(DefaultGeographicCRS.WGS84);
        System.out.println(newDataStore.getSchema());
        //Transaction transaction = new DefaultTransaction("create");

        String typeName = newDataStore.getTypeNames()[0];

        FeatureStore<SimpleFeatureType, SimpleFeature> featureStore;
        featureStore = (FeatureStore<SimpleFeatureType, SimpleFeature>) newDataStore.getFeatureSource(typeName);
        //SimpleFeatureCollection collection = new ListFeatureCollection(TYPE, features);
         System.out.println(collection.getSchema());
        //featureStore.setTransaction(transaction);
        try {
            featureStore.addFeatures(collection);
            //transaction.commit();
            System.out.println(featureStore.getDataStore());
        } catch (Exception ex) {
            ex.printStackTrace();
            //transaction.rollback();
        } finally {
            //transaction.close();
        }
    }

    private static File getCSVFile(String[] args) throws FileNotFoundException {
    File file;
    if (args.length == 0){
        JFileChooser chooser = new JFileChooser();
        chooser.setDialogTitle("Open CSV file");
        chooser.setFileFilter( new FileFilter(){
            public boolean accept( File f ) {
                return f.isDirectory() || f.getPath().endsWith("csv") || f.getPath().endsWith("CSV");
            }
            public String getDescription() {
                return "Comma Seperated Value";
            }
        });
        int returnVal = chooser.showOpenDialog( null );

        if(returnVal != JFileChooser.APPROVE_OPTION) {
            System.exit(0);
        }
        file = chooser.getSelectedFile();

        System.out.println("Opening CVS file: " + file.getName());
    }
    else {
        file = new File( args[0] );
    }
    if (!file.exists()){
        throw new FileNotFoundException( file.getAbsolutePath() );
    }
    return file;
    }
    private static File getNewShapeFile(File file) {
    String path = file.getAbsolutePath();
    String newPath = path.substring(0,path.length()-4)+".shp";

    JFileChooser chooser = new JFileChooser();
    chooser.setDialogTitle("Save shapefile");
    chooser.setSelectedFile( new File( newPath ));      
    chooser.setFileFilter( new FileFilter(){
        public boolean accept( File f ) {
            return f.isDirectory() || f.getPath().endsWith("shp") || f.getPath().endsWith("SHP");
        }
        public String getDescription() {
            return "Shapefiles";
        }
    });
    int returnVal = chooser.showSaveDialog(null);

    if(returnVal != JFileChooser.APPROVE_OPTION) {
        System.exit(0);
    }
    File newFile = chooser.getSelectedFile();
        System.out.println(newFile);
    if( newFile.equals( file )){
        System.out.println("Cannot replace "+file);
        System.exit(0);
    }
    return newFile;
}
}
  • did you call your geometry the_geom as the standard requires? – Ian Turton Aug 1 '15 at 10:36
  • 2
    The problem is likely in your code, but since you haven't shown a minimal, compilable, complete example, its fairly hard to guess. – BradHards Aug 2 '15 at 4:43
  • Thanks BradHards, attach my program please check and suggest me as soon as possible.. – Partha Aug 2 '15 at 16:33
1

shapefile expects geometry column name only "the_geom". Replace line:

stringBuilder.append("location:Point,");

with:

stringBuilder.append("the_geom:Point,");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.