3

I have had a Java issue trying to convert a CSV to a shapefile using Geotools 17. This works to the point where it creates the shapefile with an attribute table, but it is not physically showing the points on my mapping program and I can't figure out why. This is a slight modification of another users' code I found that was using GeoTools 8 (his version 8 worked for me, but I am trying to get it to work using GeoTools 17).

Here is my code:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import org.geotools.data.DataStoreFactorySpi;
import org.geotools.data.DataUtilities;
import org.geotools.data.DefaultTransaction;
import org.geotools.data.FeatureStore;
import org.geotools.data.Transaction;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.shapefile.ShapefileDataStoreFactory;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.geotools.geometry.jts.JTSFactoryFinder;
import org.geotools.referencing.crs.DefaultGeographicCRS;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.GeometryFactory;
import org.geotools.feature.DefaultFeatureCollection;

public class Csv2Shape {

    public static void main(String[] args) throws Exception {
        String fileString = "C:\\Users\\Ben\\Desktop\\Java\\inflasi3.txt";
        String shapefileString = "C:\\Users\\Ben\\Desktop\\Java\\Data\\diditWork.shp";
        File file = new File(fileString);

        DefaultFeatureCollection collection = new DefaultFeatureCollection();
        BufferedReader reader = new BufferedReader(new FileReader(file));
        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("").append(header).append(":String,");
            }

            TYPE = DataUtilities.createType("Location", stringBuilder.substring(0, stringBuilder.toString().length() - 1));
            GeometryFactory factory = JTSFactoryFinder.getGeometryFactory(null);

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

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

                Object[] o = new Object[split.length+1];
                for (int i = 2; i < o.length; i++) {
                    o[i] = split[i-1];
                }

                o[0] = factory.createPoint(new Coordinate(longitude, latitude));
                o[1] = name;

                SimpleFeature feature = SimpleFeatureBuilder.build(TYPE, o, null);
                collection.add(feature);
            }
        } finally {
            reader.close();
        }
        File newFile = new File(shapefileString);

        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);
        newDataStore.forceSchemaCRS(DefaultGeographicCRS.WGS84);

        Transaction transaction = new DefaultTransaction("create");

        String typeName = newDataStore.getTypeNames()[0];
        FeatureStore<SimpleFeatureType, SimpleFeature> featureStore;
        featureStore = (FeatureStore<SimpleFeatureType, SimpleFeature>) newDataStore.getFeatureSource(typeName);

        featureStore.setTransaction(transaction);
        try {
            featureStore.addFeatures(collection);
            transaction.commit();
        } catch (Exception ex) {
            ex.printStackTrace();
            transaction.rollback();
        } finally {
        transaction.close();
        }
    }
}


This is my textfile: 
KOTA,LAT,LON,JULIL2009,AGUS2009,9-Sep,OKT2009,9-Nov,DES2009,10-Jan,10-Feb,10-Mar,10-Apr,MEI2010,JUNI2010
BANDA ACEH,5.546181947,95.32366186,0.8,1.45,1.82,-1.3,0.45,-0.23,-0.3,-0.04,0.7,-0.47,0.63,0.63
TARAKAN,3.276090324,117.6193848,0.99,0.97,1.53,-0.74,0.64,1.76,0.24,0.2,0.08,0.09,-0.19,1.44
MANADO,1.493103951,124.8409503,0.46,0.65,-0.36,0.83,1.27,0.38,-1.35,1.25,1.29,-1.32,-0.64,-0.12

Any help?

4
  • I fixed it. This is the line I changed:
    – BenV
    Commented May 22, 2017 at 16:39
  • stringBuilder.append("the_geom:Point,");
    – BenV
    Commented May 22, 2017 at 16:39
  • Do you mean you replaced stringBuilder.append("location:Point,"); with stringBuilder.append("the_geom:Point,");
    – John Jesus
    Commented May 30, 2017 at 1:55
  • Which line was replaced? Or maybe post the successful code snippet as an answer?
    – John Jesus
    Commented May 30, 2017 at 1:59

2 Answers 2

2

I fixed it by changing what I appended to the StringBuilder, for anyone else who runs into this issue for new versions of GeoTools

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

Which coordinate axis order use your mapping program, by default.

enter image description here

If it is also Geotools based see

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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