GeoTools : A HowTo for beginners

A HowTo for beginners

Note: Work in progress.

This section is thought to contain task-oriented code snippets useful for beginners. Explainations should be kept simple and without too many details on specs and implementation internals. Each snippet shall have a version tag to tell for which GeoTools version it was developed (e.g. GT2.0, GT2.1, ...). Information for the advanced user shall be geiven below each how-to in form of links to advanced tutorial/snippet pages or relevant specifications.

How to build a map?

As known from nearly every GIS a map consists of map layers. In GeoTools this is the same:

Creating a map (GeoTools2.1)
  MapContext map = new DefaultMapContext();

The exact method to add data to the map depends on the data type, e.g. raster or vector data. Some examples are given in the snippets below.

See also:
Tutorials: Putting everything together

How to load a shapefile into the map?

In order to load a shapefile its URL must be known. This can be a local or remote URL.

Loading shapefiles (GeoTools2.1)
  URL roadsURL = getResource("C:/temp/myShapefile.shp");//Windows
  URL roadsURL = getResource("/home/abcde/myShapefile.shp");/Linux etc.
  //Load the data

  ShapefileDataStore dsRoads = new ShapefileDataStore(roadsURL);
  FeatureSource fsRoads = dsRoads.getFeatureSource("roads");

  //Determine the data's display style (some examples):

  StyleBuilder sb = new StyleBuilder(); For the style convenience methods below
  // 3 pixel wide blue line, e.g. for rivers
  LineSymbolizer lsStream = sb.createLineSymbolizer(Color.BLUE, 3);
  Style streamsStyle = sb.createStyle(lsStream);
  // Line with black border and yellow inner color, e.g. for roads
  LineSymbolizer ls1 = sb.createLineSymbolizer(Color.YELLOW, 1);
  LineSymbolizer ls2 = sb.createLineSymbolizer(Color.BLACK, 3);
  Style roadsStyle = sb.createStyle();
  roadsStyle.addFeatureTypeStyle(sb.createFeatureTypeStyle(null, sb.createRule(ls2)));
  roadsStyle.addFeatureTypeStyle(sb.createFeatureTypeStyle(null, sb.createRule(ls1)));
  // Polygons, e.g. half-transparent grey polygons with black border
  PolygonSymbolizer restrictedSymb = sb.createPolygonSymbolizer(Color.LIGHT_GRAY, Color.BLACK, 0);
  Style restrictedStyle = sb.createStyle(restrictedSymb);

  //Put the data into a map:

  map.addLayer(fsRoads, roadsStyle);

See also:
Tutorials: Putting everything together

How to load a GeoTiff image into the map?

GeoTiff is an open and wellknown format for georeferenced images. Images can be viewed with almost any viewer supporting plain TIFF images, but usually only GIS programs can utilize the geographical metadata stored in GeoTiff. Loading such images in GeoTools requires the "geotiff" plugin, to be found in ".../plugin/geotiff/gt2-geotiff.jar" (in GeoTools2.1).
The GeoTiff is, in the GeoTools language, a "GridCoverage". A "coverage" delivers a value for every coordinate (within its bounds of course), and a grid coverage uses a grid (a raster image) to achieve that.

Loading shapefiles (GeoTools2.1)
  File tiffFile = new File("C:/temp/myDEM.tif");//Windows
  File tiffFile = new File("/home/abcde/myDEM.tif");//Linux etc.

  //Load the image

  GeoTiffReader rdr = (GeoTiffReader) ((new GeoTiffFormat()).getReader(tiffFile));
  GridCoverage tiffCov =; //We do not use any parametery here.

  //Determine the data's display style. We style it as a digital elevation model
  //using interpolated color ramps for the different heights. The example below
  //assumes a 16 bit image, since the standard 8 bit tiff images only have values
  //between 0 and 255.
  StyleBuilder sb = new StyleBuilder(); For the style convenience methods below
  ColorMap cm = sb.createColorMap(
        new double[] { 1000, 1200, 1400, 1600, 2000 },
        new Color[] {
            new Color(0, 255, 0),
            new Color(255, 255, 0),
            new Color(255, 127, 0),
            new Color(191, 127, 63),
            new Color(255, 255, 255)},
  RasterSymbolizer rsDem = sb.createRasterSymbolizer(cm, 1);
  Style demStyle = sb.createStyle(rsDem);

  //Put the data into a map:

  map.addLayer(tiffCov, demStyle );

See also:
Tutorials: Putting everything together
For styles and symbolizers:
Snippets: Use of external graphicsas symbols