2

I'm fairly new to Java and GeoTools and I'm trying to plot this grib of meteorological data; I have the code for shapefiles, but I don't know where to go from here:

File file = JFileDataStoreChooser.showOpenFile("shp", null);
        if (file == null) {
            return;
        }

        FileDataStore store = FileDataStoreFinder.getDataStore(file);
        SimpleFeatureSource featureSource = store.getFeatureSource();
        CachingFeatureSource cache = new CachingFeatureSource(featureSource);
        // Create a map content and add our shapefile to it
        MapContent map = new MapContent();
        map.setTitle("Using cached features");
        Style style = SLD.createSimpleStyle(featureSource.getSchema());
        FeatureLayer layer = new FeatureLayer(cache, style);
        map.addLayer(layer);
        // Now display the map
        JMapFrame.showMap(map);
2

In the GeoTools world (as with most of GIS) we consider vector and raster data as fundamentally different things. So a shapefile is classed as a vector datasource and returns you a collection of features that represent a geometry and associated attributes. A raster (or coverage) is an N dimensional grid of data (where N is normally 2 but can be more), they are read in using a GridCoverageReader rather than a DataStore.

To read a grib file you need the gt-grib plugin covered on the Coverage MultiDimensional page

<dependency>
        <groupId>org.geotools</groupId>
        <artifactId>gt-grib</artifactId>
        <version>${geotools.version}</version>
</dependency>

and some code like:

    // Selection of the NetCDF file
    File file = new File("path/to/file.grb2");

    // Creation of the NetCDF reader
    final NetCDFReader reader = new NetCDFReader(file, null);

    // It is better to surround this part of code with a try-finally construct
    // in order to avoid to leave the reader unclosed.
    try {
            // Getting the coverage names
            String[] names = reader.getGridCoverageNames();

            // Selection of the first coverage name
            String first = names[0];

            // Selection of the coverage associated to the name
            GridCoverage2D grid = reader.read(first, null);

            // Example: Get the value for the following position.
            float[] value = grid.evaluate((DirectPosition) new
                    DirectPosition2D(grid.getCoordinateReferenceSystem(), 5, 45 ), new float[1]);

    } finally {
            // Closure of the Reader
            if (reader != null) {
                    try {
                            reader.dispose();
                    } catch (Throwable t) {
                            // Log the exception
                    }
            }
    }

There are more examples of its usage in the GribTest.java file.

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.