Reading ESRI shapefiles from the InputStream in Java

不问归期 提交于 2021-02-11 08:39:33

问题


I have a web application that is supposed to read a shapefile from a user's disk. I use a MultipartFile class (https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/multipart/MultipartFile.html) to upload it. As far as I understand, it is not possible to recover a file path from the MultipartFile. It causes a problem for me because I use the following code to interpret the shapefile content (I attach the constructor which is crucial):

    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);
    }
}

The shapefile needs the File in the constructor and the File is strictly associated with an absolute path of a file. Therefore, I cannot use the MultipartFile and create the File in a memory (i.e. not in a file system) based on that. What I can do, is getting the InputStream from the MultipartFile, getInputStream() or getBytes().

Is there a way to modify the Shapefile constructor such that it accepts the InputStream or a table of bytes? I would like to avoid creating a temporary file.

Thank you for suggestions.


回答1:


A MultiPartFile is a single file sent in multiple parts, a ShapeFile is a collection of at least 3 and possibly up to 12 files that share a common basename and have various extensions, such as .shp, .shx, .dbf, .prj etc.

So it is impossible to construct a ShapeFile object from an InputStream or byte collection as the constructor needs to read from 3 files at once to tie the geometries (.shp) to the attributes (.dbf) using the index and other information scattered across the remaining files.



来源:https://stackoverflow.com/questions/59499090/reading-esri-shapefiles-from-the-inputstream-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!