1

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);

  }
}
2

Actually I got it working... I think the cause was two fold. 1) a bad shp file 2) not defining the Style for the grid layer correctly. Here is my updated code in case anyone else is curious

package org.geotools;

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.PolygonSymbolizer;
import org.geotools.styling.SLD;
import org.geotools.styling.Style;
import org.geotools.styling.StyleBuilder;
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.awt.*;
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;
    }*/

    // load a shp file I know is good
    File file = new File("/home/me//50m_cultural/ne_50m_admin_0_countries.shp");

    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);

    //Creates the map style
    StyleBuilder styleBuilder = new StyleBuilder();
    PolygonSymbolizer restrictedSymb = styleBuilder.createPolygonSymbolizer(Color.RED, Color.BLACK, 0);

    //Sets opacity
    restrictedSymb.getFill().setOpacity(styleBuilder.literalExpression(0.5));
    org.geotools.styling.Style myStyle = styleBuilder.createStyle(restrictedSymb);

    Layer layerGrid = new FeatureLayer(grid, myStyle);
    map.addLayer(layerGrid);

    // Now display the map
    JMapFrame.showMap(map);

  }
}

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.