I have some code where the relevant part looks like this:
private static List<GisShape> shapes(String shapefile) {
DataStore dataStore;
try {
dataStore = getDataStore(GIS_BOUNDARIES + shapefile);
} catch (IOException e) {
throw new RuntimeException(e);
}
List<GisShape> shapes = new ArrayList<>();
try {
String typeName = dataStore.getTypeNames()[0];
SimpleFeatureSource featureSource = dataStore.getFeatureSource(typeName);
SimpleFeatureCollection features = featureSource.getFeatures();
SimpleFeatureIterator iterator = features.features();
while (iterator.hasNext()) {
Here's the code for getDataStore
:
private static DataStore getDataStore(final String filename) throws IOException {
Map<String, Object> params = new HashMap<String, Object>();
final URL resource = GisBoundariesController.class.getClassLoader().getResource(filename);
params.put("url", resource);
logger.info(resource.toString());
final DataStore dataStore = DataStoreFinder.getDataStore(params);
if(dataStore == null) {
throw new IOException(filename);
}
return dataStore;
}
Now here's the weird part... This code has worked fine for years, and still works in my master repo, but as soon as I try to use a file modified by QGIS 3.12, then the code fails with:
Caused by: java.lang.UnsupportedOperationException: Random Access not enabled
at org.geotools.data.shapefile.shp.ShapefileReader.positionBufferForOffset(ShapefileReader.java:518)
at org.geotools.data.shapefile.shp.ShapefileReader.hasNext(ShapefileReader.java:424)
at org.geotools.data.shapefile.shp.ShapefileReader.hasNext(ShapefileReader.java:406)
at org.geotools.data.shapefile.ShapefileFeatureReader.filesHaveMore(ShapefileFeatureReader.java:170)
at org.geotools.data.shapefile.ShapefileFeatureReader.hasNext(ShapefileFeatureReader.java:183)
at org.geotools.data.store.ContentFeatureCollection$WrappingFeatureIterator.hasNext(ContentFeatureCollection.java:138)
What could cause this? With the original shapefiles the code still works fine, but with the slightly modified shapefile, I get the above exception.
Java Geotools version is 22.0.
UPDATE
Other clues and things tried:
- The log shows this just before the failure
SEVERE: The following locker still has a lock: read on jar:file:/C:/Users/.../target/boundaries.jar!/gis_boundaries/schools/Schools.shp by org.geotools.data.shapefile.shp.ShapefileReader
Other files which were edited by QGIS 3.12 open just fine; I've narrowed the issue to just one shape file. I tried editing and re-saving the shape file, no go. Reverting to the original file works, but I need the updates.
Adding
params.put("CREATE_SPATIAL_INDEX", "true");
had no effect
getDataStore()
to be sure but I suspect you don't have an index - try setting CREATE_SPATIAL_INDEX=true in the creation paramsgetDateStore()
. I will tryCREATE_SPATIAL_INDEX
as soon as repo.boundlessgeo.com comes back up (I did a local repo purge and upgrade to latest version just to remove any old library weirdness, then found repo.boundlessgeo.com is down).