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();

            String[] headers = line.split("\\,"); 
            for (String header : headers) {

            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);
        } finally {
        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);

        Transaction transaction = new DefaultTransaction("create");

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

        try {
        } catch (Exception ex) {
        } finally {

This is my textfile: 
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

Any help?

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

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

| improve this answer | |

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

enter image description here

If it is also Geotools based see

| improve this answer | |

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.