I've been going through the GeoTools tutorials and have read the Vector Grids page http://docs.geotools.org/latest/userguide/extension/grid.html
I am trying to replicate the maps shown on this page with the following code, however I'm unable to generate any output (the program doesn't open the map window or output any errors). Where am I going wrong?
import org.geotools.data.FileDataStore;
import org.geotools.data.FileDataStoreFinder;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
import org.geotools.geometry.jts.ReferencedEnvelope;
import org.geotools.grid.Envelopes;
import org.geotools.grid.GridFeatureBuilder;
import org.geotools.grid.Grids;
import org.geotools.helpers.IntersectionBuilder;
import org.geotools.map.FeatureLayer;
import org.geotools.map.Layer;
import org.geotools.map.MapContent;
import org.geotools.styling.SLD;
import org.geotools.styling.Style;
import org.geotools.swing.JMapFrame;
import org.geotools.swing.data.JFileDataStoreChooser;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.geometry.coordinate.Polygon;
import java.io.File;
import java.net.URL;
public class Grid1 {
public static void main(String[] args) throws Exception {
// Load the outline of Australia from a shapefile
// display a data store file chooser dialog for shapefiles
File file = JFileDataStoreChooser.showOpenFile("shp", null);
if (file == null) {
return;
}
FileDataStore dataStore = FileDataStoreFinder.getDataStore(file);
SimpleFeatureSource ozMapSource = dataStore.getFeatureSource();
// Set the grid size (1 degree) and create a bounding envelope
// that is neatly aligned with the grid size
double sideLen = 1.0;
ReferencedEnvelope gridBounds = Envelopes.expandToInclude(ozMapSource.getBounds(), sideLen);
// Create a feature type
SimpleFeatureTypeBuilder tb = new SimpleFeatureTypeBuilder();
tb.setName("grid");
tb.add(
GridFeatureBuilder.DEFAULT_GEOMETRY_ATTRIBUTE_NAME,
Polygon.class,
gridBounds.getCoordinateReferenceSystem());
tb.add("id", Integer.class);
SimpleFeatureType TYPE = tb.buildFeatureType();
// Build the grid the custom feature builder class
GridFeatureBuilder builder = new IntersectionBuilder(TYPE, ozMapSource);
SimpleFeatureSource grid = Grids.createHexagonalGrid(gridBounds, sideLen, -1, builder);
// Create a map content and add our shapefile to it
MapContent map = new MapContent();
map.setTitle("Quickstart");
Style style = SLD.createSimpleStyle(ozMapSource.getSchema());
Layer layer = new FeatureLayer(ozMapSource, style);
map.addLayer(layer);
Style layerStyle = SLD.createSimpleStyle(grid.getSchema());
Layer layerGrid = new FeatureLayer(grid, layerStyle);
map.addLayer(layerGrid);
// Now display the map
JMapFrame.showMap(map);
}
}