Java 使用GeoTools编写形状文件

Java 使用GeoTools编写形状文件,java,gis,shapefile,geotools,Java,Gis,Shapefile,Geotools,我正在尝试使用GeoTools库编写/编辑我的shapefile属性 File file = new File("U:/xyz.shp"); Map<String, Serializable> map = new HashMap<>(); map.put( "url", file.toURI().toURL() ); DataStore dataStore = DataStoreFinder.getDataStore( map ); String typeNam

我正在尝试使用GeoTools库编写/编辑我的shapefile属性

File file = new File("U:/xyz.shp");    
Map<String, Serializable> map = new HashMap<>();
map.put( "url", file.toURI().toURL() );

DataStore dataStore = DataStoreFinder.getDataStore( map );
String typeName = dataStore.getTypeNames()[0];
System.out.println( typeName ); 
FeatureSource source = dataStore.getFeatureSource( typeName );

FeatureCollection collection =  source.getFeatures();
FeatureIterator<SimpleFeature> results = collection.features();
try {
    while (results.hasNext()) {
        SimpleFeature feature = (SimpleFeature) results.next();
        feature.setAttribute("FIELD_NAME","NEW_NAME");
        //FIELD_NAME is the name of the attribute
        //NEW_NAME is the modified value oif the attribute          

    }
} finally {
    results.close();
}
File File=new文件(“U:/xyz.shp”);
Map Map=newhashmap();
map.put(“url”,file.toURI().toul());
DataStore DataStore=DataStoreFinder.getDataStore(map);
字符串typeName=dataStore.getTypeNames()[0];
System.out.println(typeName);
FeatureSource=dataStore.getFeatureSource(typeName);
FeatureCollection=source.getFeatures();
FeatureIterator结果=collection.features();
试一试{
while(results.hasNext()){
SimpleFeature功能=(SimpleFeature)结果。下一步();
setAttribute(“字段名称”、“新名称”);
//字段\ u NAME是属性的名称
//NEW_NAME是属性的修改值
}
}最后{
结果:关闭();
}
这会更改SimpleFeature类对象特征中属性的值,但不会将其更新为实际的shapefile。 谁能告诉我哪里出了问题。
谢谢。

查看标题
从功能集合创建一个shapefile
。在你的代码中,没有任何东西可以将数据写入文件,只有用于阅读的代码。谢谢,在阅读你的评论之前,我实际上意识到了我的错误。我使用FeatureWriter类解决了这个问题。看一下,标题
从FeatureCollection创建一个shapefile
。在你的代码中,没有任何东西可以将数据写入文件,只有用于阅读的代码。谢谢,在阅读你的评论之前,我实际上意识到了我的错误。我用FeatureWriter课程来解决问题。