2

The following code works fine on my Eclipse IDE.

private void getLayersAndDisplay() throws Exception {
        URL imageURL = ImageLab.class.getResource("earthlights.jpg");
        File imageFile = new File(imageURL.toURI());
        URL shapeFileURL = ImageLab.class.getResource("countries.shp");
        File shapeFile = new File(shapeFileURL.toURI());
        URL shapeFileURL2 = ImageLab.class.getResource("Brasil.shp");
        File shapeFile2 = new File(shapeFileURL2.toURI());
        displayLayers(imageFile, shapeFile,shapeFile2);
    }

However, I need to compile this to a jar File. A jar doesn't accept the creation of a new File on its content. The problem is, that in the function displayLayers, the FileDataStoreFinder.getDataStore() only accepts File types to work on. Also, I think this part of the code would give me some troubles too:

AbstractGridFormat format = GridFormatFinder.findFormat( rasterFile ); 
Hints hints = new Hints();
if (format instanceof GeoTiffFormat) {
    hints = new Hints(Hints.FORCE_LONGITUDE_FIRST_AXIS_ORDER, Boolean.TRUE);
}
reader = format.getReader(rasterFile, hints);

Is there any possible solution to that?

3
  • Use URL and DatastoreFinder? What exactly are you trying to do?
    – Ian Turton
    Feb 25 '17 at 16:40
  • I am trying to read a shapefile directly from my program (without showing a dialog box to the user select a shapefile, but instead, loading it directly on my class). I need to generate a runnable jar for testing purposes, to execute my app outside my IDE. However, a jar file doesn't allow me to use File file = new File("shape.shp") but instead an getResourceStream(). DataStoreFInder.getDataStore however, only receives a File object as a parameter.
    – R. Natalie
    Feb 25 '17 at 18:00
  • This question does not appear to be GIS-centric. You might find a faster result by asking in Stackoverflow.
    – Vince
    Feb 26 '17 at 0:52
1

If you are not sure if you will have a file or an URL as input then you should use the standard DataStoreFinder.getDataStore(...) method which takes a Map of parameters and use the Java SPI mechanism to load all the available DataStores and checks to see which of them can handle that set of parameters.

Map<String,Object> params = new HashMap<>();
params.put("URL",url);
DataStore ds = DataStoreFinder.getDataStore(params);

However if you are specifically writing a test case then you would probably be better off using the GeoTools TestData class which gives you access to File and URL references to objects in the test resources directory. There is a longer discussion of its uses in the developers' guide.

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.