【问题标题】:Rename Shape File which saves as TableName using Geotools - JAVA使用 Geotools - JAVA 重命名保存为 TableName 的形状文件
【发布时间】:2018-10-22 17:36:12
【问题描述】:

我正在尝试保存一个可以正常工作的 shapefile,下面是这个问题链接:

Add new column attribute to the shapefile and save it to database using Geotools Java

我面临的问题是 shapefile 的 fileName,它包含一个空格,结果保存为 %20 相当于空间 n db。

例如: New File abc.shp ==> New%20File%20abc (TableName)

我知道我们可以重命名文件路径,但在我的情况下,我想重命名选择的文件,而不是重命名文件系统上的文件。

编辑

使用 Geotools 我使用以下代码来存储 tableName:

  File FilePath = new File("/users/New File abc.shp");
 FileDataStore ds = FileDataStoreFinder.getDataStore(new File(FilePath.toString()));
    SimpleFeatureType schema = ds.getSchema();
    // create new schema
    SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
    builder.setName(schema.getName());
    builder.setSuperType((SimpleFeatureType) schema.getSuper());
    builder.addAll(schema.getAttributeDescriptors());

    // build new schema
    SimpleFeatureType nSchema = builder.buildFeatureType();
    System.out.println("Shapefile table info : " + nSchema);

我有什么方法可以使用 Java 或使用 Geotools 的任何解决方案。我使用的数据库是 PostGIS。

【问题讨论】:

  • 这可能是 URL 到文件转换过程的产物,您究竟是如何创建数据存储的
  • @IanTurton 我已经更新了我的问题。我以为我需要更改 nSchema 中的表名,但我看到了架构链接。所以就是做不好
  • 对不起,我不清楚。我们需要了解您是如何创建 Shapefile 数据存储的,尽管您仍需要将空格替换为 _ 以使表名有效
  • @IanTurton 我已经更新了完整的代码。包括 shapefile ds。
  • @IanTurton 感谢您的指导,我在 builder.setName(schema.getName().toString().replaceAll("%20", "") 这一行更改了我的代码。全部替换(" ", ""));

标签: java geotools


【解决方案1】:

答案似乎很简单,因为我只关注更改文件路径。但是根据 Ian 的评论,一个简单的解决方案给了我这个想法。我只需要在加载文件后重命名架构名称。

sn-p代码如下:

 File FilePath = new File("/users/New File abc.shp");
 FileDataStore ds = FileDataStoreFinder.getDataStore(new File(FilePath.toString()));
    SimpleFeatureType schema = ds.getSchema();
    // create new schema
    SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
    builder.setName(schema.getName().toString().replaceAll("%20", "_").replaceAll(" ", "_"));
    builder.setSuperType((SimpleFeatureType) schema.getSuper());
    builder.addAll(schema.getAttributeDescriptors());

    // build new schema
    SimpleFeatureType nSchema = builder.buildFeatureType();
    System.out.println("Shapefile table info : " + nSchema);

请注意,我们也可以在 java 中使用 renameTo 更改文件名,但这会导致更改 shapefile 中的其他文件,如果您像这样扩展应用程序,这不是一个好主意。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-04
    • 1970-01-01
    • 2010-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-02
    • 1970-01-01
    相关资源
    最近更新 更多