Search code examples
javadatastoregeotools

How to recover features of a shape in memory using GeoTools?


After taking a look to geotools quickstart: https://docs.geotools.org/latest/userguide/tutorial/quickstart/intellij.html

It shows an example of recovering the features of a shape, using this code:

File file = new File("myfile.shp");

FileDataStore store = FileDataStoreFinder.getDataStore(file);
SimpleFeatureSource featureSource = store.getFeatureSource();
FeatureCollection collection = featureSource.getFeatures();

FeatureIterator iterator = collection.features();

However this code has a dependency on File. In my real project shape content is provided me as a byte array and I cannot create a temp file. So, how can I access features?

This is my code so far

public static Map<String,Vector<String>> getAllPropsValues(byte[] fileContent){
   //Some other code here

   DataStore store = DataStoreFinder.getDataStore(fileContent); //<-- how to replace this line
   SimpleFeatureSource featureSource = store.getFeatureSource();
   FeatureCollection collection = featureSource.getFeatures();

   FeatureIterator iterator = collection.features();

   //other things here
}

Solution

  • 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.