1

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.

Ghostwriter
  • 2,461
  • 2
  • 16
  • 18

1 Answers1

0

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.

Ian Turton
  • 10,018
  • 1
  • 28
  • 47
  • Hello, good point! What if I only need coordinates from a Shapefile? – Ghostwriter Jan 02 '20 at 08:09
  • 1
    It might be possible to hack something together to do that but there is nothing in the library to do that currently. See also https://gis.stackexchange.com/questions/180515/geotools-create-shapefile-in-memory – Ian Turton Jan 02 '20 at 09:00