// 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)
更新:
我尝试过
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()
}
但是我仍然得到
fs.addFeatures(reTypedSFC)java.lang.IllegalArgumentException:值fid--31eafd14_14a112b8f13_-6cbb对于类型java.lang.Integer是非法的在org.geotools.jdbc.JDBCDataStore.decodeFID(JDBCDataStore.java:1890)
问题是,对于shapefile,特征标识符(FID)是一个包含一些随机内容(文件名,日期,特征的哈希码等)的字符串。在数据库中时,如果已定义,则通常基于主键。
因此,您需要将功能的字段替换为NULL,以便JDBCStore在将其添加到表时可以创建一个新的字段。或更改表以接受ID列中的字符串。
这受伊恩(Ian)的回答和我自己的一点研究启发,似乎解决了这个问题。
val builder = new SimpleFeatureBuilder(simpleFeature.getFeatureType)
builder.init(simpleFeature)
builder.featureUserData(Hints.USE_PROVIDED_FID, false)
val featureNoId: SimpleFeature = builder.buildFeature(null)
尽管将id=null
传递给buildFeature
,您仍然必须将USE_PROVIDED_FID
提示设置为false。