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?