【问题标题】:Reading ESRI shapefiles from the InputStream in Java从 Java 中的 InputStream 读取 ESRI shapefile
【发布时间】:2019-12-27 10:07:58
【问题描述】:

我有一个应该从用户磁盘读取 shapefile 的 Web 应用程序。我使用MultipartFile 类(https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/multipart/MultipartFile.html)来上传它。据我了解,无法从MultipartFile 恢复文件路径。它给我带来了一个问题,因为我使用以下代码来解释 shapefile 内容(我附加了至关重要的构造函数):

    private ShapeFile(final File srcFile) {
    try {
        final Map<String, Serializable> params = ImmutableMap.of("url", srcFile.toURI().toURL());
        dataStore = new ShapefileDataStoreFactory().createDataStore(params);
        final String typeName = dataStore.getTypeNames()[0];
        featureType = dataStore.getSchema(typeName);
        featureSource = dataStore.getFeatureSource(typeName);
    } catch (IOException exception) {
        throw new RuntimeException(exception);
    }
}

shapefile 在构造函数中需要File,而File 与文件的绝对路径严格关联。因此,我不能使用MultipartFile 并基于此在内存中(即不在文件系统中)创建File。我能做的就是从MultipartFilegetInputStream()getBytes() 获取InputStream

有没有办法修改Shapefile 构造函数,使其接受InputStream 或字节表?我想避免创建临时文件。

感谢您的建议。

【问题讨论】:

    标签: java spring shapefile geotools


    【解决方案1】:

    MultiPartFile 是一个分多个部分发送的单个文件,ShapeFile 是至少 3 个可能最多 12 个文件的集合,这些文件共享一个共同的基本名称并具有各种扩展名,例如 .shp、@ 987654326@、.dbf.prj

    因此不可能从 InputStream 或字节集合构造 ShapeFile 对象,因为构造函数需要一次从 3 个文件中读取以将几何 (.shp) 绑定到属性 (.dbf)使用分散在其余文件中的索引和其他信息。

    【讨论】:

    • 你好,好点!如果我只需要 Shapefile 中的坐标怎么办?
    • 可能可以一起破解一些东西来做到这一点,但目前库中没有任何东西可以做到这一点。另见gis.stackexchange.com/questions/180515/…
    猜你喜欢
    • 1970-01-01
    • 2012-12-23
    • 2017-03-03
    • 2014-03-01
    • 2012-03-03
    • 2021-06-15
    • 2023-04-06
    • 1970-01-01
    相关资源
    最近更新 更多