0

Using geotools to store shape files in a PostgreSQL database. Encountering an exception on the insert. Any suggestions?

 DataStore shapeDataStore = DataStoreFinder.getDataStore(file);
 String typeName = shapeDataStore.getTypeNames()[0];
 SimpleFeatureType inputType = shapeDataStore.getSchema(typeName);
 FeatureSource<SimpleFeatureType, SimpleFeature> 
    source = shapeDataStore.getFeatureSource(typeName);

 FeatureCollection<SimpleFeatureType, SimpleFeature>
    inputFeatureCollection = source.getFeatures();

 Map<Object, Serializable> postGISparams = new HashMap();

 postGISparams.put("dbtype", "postgis");
 postGISparams.put("host", "localhost");
 postGISparams.put("port", 5437);
 postGISparams.put("database", "db");
 postGISparams.put("schema", "new_schema");
 postGISparams.put("user", "user");
 postGISparams.put("passwd", "password");

 // storage in PostGIS
 DataStore dataStore = DataStoreFinder.getDataStore(postGISparams);
   dataStore.createSchema(inputType);

 SimpleFeatureStore featureStore = (SimpleFeatureStore)   
   dataStore.getFeatureSource(typeName);
        
 // write results
 featureStore.addFeatures(source.getFeatures());

 String typeName1 = dataStore.getTypeNames()[0];

 SimpleFeatureStore featureStore1 = (SimpleFeatureStore)      
    dataStore.getFeatureSource(typeName1);

 featureStore1.addFeatures(inputFeatureCollection);

The exception:

java.io.IOException: Cannot set a NULL value on the not null column applcnt_type_status
 at org.geotools.jdbc.JDBCDataStore.insertSQL(JDBCDataStore.java:4165)
 at org.geotools.jdbc.JDBCDataStore.insertNonPS(JDBCDataStore.java:1903)
 at org.geotools.jdbc.JDBCDataStore.insert(JDBCDataStore.java:1731)
 at org.geotools.jdbc.JDBCInsertFeatureWriter.flush(JDBCInsertFeatureWriter.java:126)
 at org.geotools.jdbc.JDBCInsertFeatureWriter.write(JDBCInsertFeatureWriter.java:98)
 at org.geotools.data.InProcessLockingManager$1.write(InProcessLockingManager.java:296)
 at org.geotools.data.store.ContentFeatureStore.addFeature(ContentFeatureStore.java:302)
 at org.geotools.data.store.ContentFeatureStore.addFeatures(ContentFeatureStore.java:254)
  • Can you add the value of inputType – Ian Turton Jan 13 at 19:18
  • inputType is SimpleFeatureTypeImpl shapefile_testname identified extends polygonFeature(the_geom:MultiPolygon,type:type) – user840930 Jan 13 at 19:32
  • it looks like I am calling addFeatures twice in my code. – user840930 Jan 13 at 19:33
  • yes, I removed the second call to addFeatures and that eliminated the error. And the shape file is still in the database. – user840930 Jan 13 at 19:40
1

I am calling addFeatures twice in my code.

this is the first call

 featureStore.addFeatures(source.getFeatures());

this is the second call

 featureStore1.addFeatures(inputFeatureCollection);

I commented out the second call and the code works without errors!

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