// Given
println(shpDataStore)
//ShapefileDataStore [file=file:/opt/c_1970-01-01T00-00-00.shp, charset=ISO-8859-1, timeZone=sun.util.calendar.ZoneInf [id="UTC",offset=0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null], memoryMapped=false, bufferCachingEnabled=true, indexed=true, fidIndexed=true]
println(jdbcDataStore)
// org.geotools.jdbc.JDBCDataStore@4371a490
// Works
val features = shpDataStore.getFeatureSource(sourceTypeName).getFeatures
val featureType = features.getSchema
val featureTypeName = featureType.getName.getLocalPart
jdbcDataStore.createSchema(featureType)
val newType = jdbcDataStore.getSchema(featureTypeName)
val reTypedSFC = new TypeUpdatingFeatureCollection(features, newType)
val fs: FeatureStore[SimpleFeatureType, SimpleFeature] = jdbcDataStore.getFeatureSource(featureTypeName).asInstanceOf[FeatureStore[SimpleFeatureType, SimpleFeature]]
fs.addFeatures(reTypedSFC)
// Doesn't work
val features = shpDataStore.getFeatureSource(sourceTypeName).getFeatures
val featureName = "newName"
val originalFeatureType = features.getSchema
val sftBuilder = new SimpleFeatureTypeBuilder()
sftBuilder.init(originalFeatureType)
sftBuilder.setName(featureName)
val featureType = sftBuilder.buildFeatureType()
val featureTypeName = featureType.getName.getLocalPart
jdbcDataStore.createSchema(featureType)
val newType = jdbcDataStore.getSchema(featureTypeName)
val reTypedSFC = new TypeUpdatingFeatureCollection(features, newType)
val fs: FeatureStore[SimpleFeatureType, SimpleFeature] =
jdbcDataStore.getFeatureSource(featureTypeName).asInstanceOf[FeatureStore[SimpleFeatureType, SimpleFeature]]
fs.addFeatures(reTypedSFC)
// Output is
//java.lang.IllegalArgumentException: Value c_1970-01-01T00-00-00.1 illegal for type java.lang.Integer
// at org.geotools.jdbc.JDBCDataStore.decodeFID(JDBCDataStore.java:1890)
// at org.geotools.jdbc.JDBCDataStore.insert(JDBCDataStore.java:1523)
UPDATE:
I tried
val f = shpDataStore.getFeatureSource(sourceTypeName).getFeatures
val ty = f.getSchema
val builder = new SimpleFeatureBuilder(ty)
val features = new DefaultFeatureCollection()
f.features().foreach { j =>
builder.init(j)
features.add(builder.buildFeature(null))
builder.reset()
}
but I still get
fs.addFeatures(reTypedSFC) java.lang.IllegalArgumentException: Value fid--31eafd14_14a112b8f13_-6cbb illegal for type java.lang.Integer at org.geotools.jdbc.JDBCDataStore.decodeFID(JDBCDataStore.java:1890)
The problem is that with a shapefile the feature identifier (FID) is a string containing some random stuff (filename, date, hashcode of feature etc). While in a database it is usually based on the primary key if defined.
So you need to replace the fid of your feature with NULL so that the JDBCStore can create a new one when you add it to the table. Or change your table to accept strings in the ID column.