Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala 将ShapefileDataStore转换为JDBCDataStore失败,名称更改_Scala_Gis_Geotools - Fatal编程技术网

Scala 将ShapefileDataStore转换为JDBCDataStore失败,名称更改

Scala 将ShapefileDataStore转换为JDBCDataStore失败,名称更改,scala,gis,geotools,Scala,Gis,Geotools,更新: 我试过了 // 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], memoryMappe

更新:

我试过了

// 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)
但我还是会

fs.addFeatures(重新键入DSFC) java.lang.IllegalArgumentException:值fid--31eafd14_14a112b8f13_6cbb对于java.lang.Integer类型非法
在org.geotools.jdbc.JDBCDataStore.decodeFID(JDBCDataStore.java:1890)

上,问题在于,对于shapefile,特征标识符(FID)是一个包含一些随机内容(文件名、日期、特征的哈希代码等)的字符串。而在数据库中,它通常基于主键(如果已定义)


因此,您需要将特性的fid替换为NULL,以便JDBCStore可以在您将其添加到表中时创建一个新特性。或者将您的表更改为接受ID列中的字符串。

受Ian的回答和我自己的一些研究启发,这似乎解决了这个问题

val builder=新的SimpleFeatureBuilder(simpleFeature.getFeatureType)
builder.init(simpleFeature)
builder.featureUserData(Hints.USE\u提供的\u FID,false)
val featureNoId:SimpleFeature=builder.buildFeature(null)
尽管将
id=null
传递给
buildFeature
,您仍然必须将
USE\u-PROVIDED\u-FID
提示设置为false

我相信这是有效的,因为签入:

//如果用户要求,请通过fid
布尔值=
Boolean.TRUE.equals(feature.getUserData().get(Hints.USE_-PROVIDED_-FID));
if(getQueryCapabilities().isUseProvidedFIDSupported()&&useExisting){
((FeatureIdImpl)toWrite.getIdentifier()).setID(feature.getID());
}

但这确实意味着您不能使用功能id来防止重复。

我更新了设置为null的尝试。我现在完全可以确定如何更改数据库模式,因为这似乎是geotools(又称jdbcDataStore.createSchema(featureType))的内部内容
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()
}