0

I am loading a shapefile where .shx file is missing I am getting the following error. I am unable to catch this exception please help me.

error log:

Could not open the .shx file, continuing assuming the .shp file is not sparse java.io.FileNotFoundException: D:\shapes\123.shx (The system cannot find the file specified) at java.io.RandomAccessFile.open(Native Method) at java.io.RandomAccessFile.(RandomAccessFile.java:216) at org.geotools.data.shapefile.ShpFiles.getReadChannel(ShpFiles.java:831) at org.geotools.data.shapefile.shp.IndexFile.(IndexFile.java:79) at org.geotools.data.shapefile.shp.ShapefileReader.(ShapefileReader.java:268) at org.geotools.data.shapefile.shp.ShapefileReader.(ShapefileReader.java:240) at org.geotools.data.shapefile.ShapefileDataStore.openShapeReader(ShapefileDataStore.java:599) at org.geotools.data.shapefile.ShapefileDataStore.readAttributes(ShapefileDataStore.java:826) at org.geotools.data.shapefile.ShapefileDataStore.getSchema(ShapefileDataStore.java:784) at org.geotools.data.AbstractFileDataStore.getFeatureSource(AbstractFileDataStore.java:77)

code:

public boolean openShapeFile() throws Exception {
     File dataFile = new File(fileName);
     dataStore = new ShapefileDataStore(dataFile.toURL());
     SimpleFeatureSource source = dataStore.getFeatureSource();
     final SimpleFeatureCollection featureCollection = source.getFeatures();
     shapeFeatureIterator = featureCollection.features();
     return true;
 }
 try {
     openShapeFile();
 } catch (Exception exep) {
     return false;
 }

complete code

public class test 
{    
    public static void main(String[] args)
    {    
        try
        {
            File dataFile = new File("D:\\shapes\\123.shp");
            ShapefileDataStore dataStore = new ShapefileDataStore(dataFile.toURL());
            SimpleFeatureSource source = dataStore.getFeatureSource();
            final SimpleFeatureCollection featureCollection = source.getFeatures();
            SimpleFeatureIterator shapeFeatureIterator = featureCollection.features();
        }
        catch(Exception e)
        {
            System.out.println("ERROR" + e.getMessage());
        }
    }
}

I tried this code after deleting the 123.shx file but I was not able to catch the above exception because the control not at all coming to catch block.

1

In general it's better to use the return type you've declared rather than rely on exceptions, so something like this may work better:

public boolean openShapeFile() {
    try {
        File dataFile = new File(fileName);
        dataStore = new ShapefileDataStore(dataFile.toURL());
        SimpleFeatureSource source = dataStore.getFeatureSource();
        final SimpleFeatureCollection featureCollection = source.getFeatures();
        shapeFeatureIterator = featureCollection.features();
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}
  • hello sir here control is not coming to catch block but error is getting displayed on the console in my case (.shx file not exist on shapepath). In catch block i need to display a message (Incorrect shapefile ...etc) to the user without printing error message on console. – Suri Mar 13 '15 at 4:35
  • I think in that case it is a warning and you can continue to use the shapefile. Do you have features in the iterator? – Ian Turton Mar 13 '15 at 8:45
  • yes i have a polygon features – Suri Mar 13 '15 at 10:08
  • then there is no problem – Ian Turton Mar 13 '15 at 10:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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