I am attempting to build a simple java program that loads shapefiles into a Postgis 2.0 database. Everything is working out great except that I would like to be able to modify the FeatureType in the following ways:
1) change the geometry column name from "the_geom" to "geom" 2) set all columns to be lower case. 3) change the primary key from serial to bigserial.
The following is my current code which basically is just transferring the shpSchema to the pgSchema. I am fairly certain that what I am trying to do is fairly simple and common, but I can find no obvious way to do it or any documentation --or even any examples of someone trying to do this.
Map shpConnectParams = new HashMap();
shpConnectParams.put("url", shapefile.toURI().toURL());
DataStore shpStore = DataStoreFinder.getDataStore(shpConnectParams);
String shpTypeName = shpStore.getTypeNames()[0];
FeatureSource shpFS = shpStore.getFeatureSource(shpTypeName);
SimpleFeatureType shpSchema = (SimpleFeatureType) shpFS.getSchema();
CoordinateReferenceSystem crs = shpSchema.getCoordinateReferenceSystem();
Map pgParams = new HashMap();
out.println(shpSchema.getGeometryDescriptor().getType());
for (AttributeDescriptor attrDesc : shpSchema.getAttributeDescriptors()) {
out.println(attrDesc.getName());
}
pgParams.put("dbtype", "postgis"); //must be postgis
pgParams.put("host", "localhost"); //the name or ip address of the machine running PostGIS
pgParams.put("port", new Integer(5432)); //the port that PostGIS is running on (generally 5432)
pgParams.put("database", "test"); //the name of the database to connect to.
pgParams.put("user", "postgres"); //the user to connect with
pgParams.put("passwd", "test!"); //the password of the user.
pgParams.put("schema", "scratch"); //the schema of the database
pgParams.put("create spatial index", Boolean.TRUE);
SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
builder.addAll(shpSchema.getAttributeDescriptors());
builder.setCRS(crs);
builder.setName(shpTypeName);
DataStore pgDatastore = DataStoreFinder.getDataStore(pgParams);
pgDatastore.createSchema(builder.buildFeatureType());
SimpleFeatureSource pgFS = pgDatastore.getFeatureSource(shpTypeName);
SimpleFeatureType pgSchema = pgFS.getSchema();
Transaction transaction = new DefaultTransaction("create");
if (pgFS instanceof SimpleFeatureStore) {
SimpleFeatureStore pgFeatureStore = (SimpleFeatureStore) pgFS;
SimpleFeatureCollection features = (SimpleFeatureCollection) shpFS.getFeatures();
pgFeatureStore.setTransaction(transaction);
try {
pgFeatureStore.addFeatures(features);
transaction.commit();
} catch (Exception problem) {
problem.printStackTrace();
transaction.rollback();
} finally {
transaction.close();
}
System.exit(0); // success!
} else {
System.out.println(shpTypeName + " does not support read/write access");
System.exit(1);
}
pgDatastore.dispose();