I have an array of JTS Geometry objects that I need to put in a shape file. I also have some other attributes that needs to go in a DBase file. I need to index the spatial objects as well and if required create the projection file. Is there a way to do this using JTS/GeoTools. I tried ShapeFileWriter but that doesn't seem enough (no dbf support for instance).
public Shape(String shpFileName, String shxFileName) throws FileNotFoundException {
RandomAccessFile shpFile = new RandomAccessFile(shpFileName, "rw");
this.shpChannel = shpFile.getChannel();
RandomAccessFile shxFile = new RandomAccessFile(shxFileName, "rw");
this.shxChannel = shxFile.getChannel();
}
public void createShapeFile(GeometryCollection geometries, ShapeType shapeType) throws IOException {
ShapefileWriter writer = new ShapefileWriter(this.shpChannel, this.shxChannel);
writer.write(geometries, shapeType);
writer.close();
}
You can't write out a set of Geometries and get a DBF file (as there are no attributes to put in it). You need to create a FeatureCollection and then pass that to a ShapeFileDatastore.
You will need something like:
There is a full working example at https://github.com/ianturton/geotools-cookbook/tree/master/modules/output/src/main/java/org/ianturton/cookbook/output (including the pom file to sort out the dependencies)