2

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:

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

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

  2. Adding params.put("CREATE_SPATIAL_INDEX", "true"); had no effect

4
  • 1
    I think we need to see the contents of getDataStore() to be sure but I suspect you don't have an index - try setting CREATE_SPATIAL_INDEX=true in the creation params
    – Ian Turton
    Commented May 2, 2020 at 8:38
  • Question updated to show getDateStore(). I will try CREATE_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).
    – Alex R
    Commented May 2, 2020 at 18:25
  • 2
    Have you seen this gis.stackexchange.com/questions/358183/…?
    – user30184
    Commented May 2, 2020 at 18:46
  • Thanks for the tip on the repo move. Please see updated question.
    – Alex R
    Commented May 2, 2020 at 19:52

1 Answer 1

0

The following steps in combination solved the issue:

  • Purge my local Maven repo, which leads to...
  • Update the Maven repo location for geotools
  • Forced upgrade from JDK 8 to JDK 11 (geotools repo SSL cert not in JDK8 CA!)
  • Update all geotools dependencies to 23.0 (from 22.0)
  • Fix all the missing JAXB dependencies caused by JDK upgrade
  • Take the offending shapefile's last good version from git and then re-apply my required edits using QGIS 2.14 instead of QGIS 3.12

It is interesting to note that the purge/upgrade stuff was not sufficient, I did also have to re-edit the shapefile. In conclusion, there's something wrong with QGIS 3.12 that causes it to write out a corrupt file. Unfortunately, I am unable to revert the environment to see if the cause was JUST the corrupt file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.