This tutorial is for those who prefer the pleasant company of a text editor and a trusty command prompt. Even if you routinely use an IDE, you will find that it’s often quicker and easier to compile, test and install your applications from the command line. We’ll be using Maven (http://maven.apache.org/) to manage the large number of jars that a GeoTools projects depend on. Don’t worry if you’re not familiar with Maven because we will explain everything step by step.
The example application is the same one used for the NetBeans and Eclipse Quickstart tutorials: a simple program to load and display a shapefile.
We would like thank members of the GeoTools User mailing list for their feedback while we were preparing the course material, with special thanks to Tom Williamson for reviewing early drafts. If you have any questions or comments about this tutorial, please post them to the user list.
We are going to be making use of Java so if you don’t have a Java Development Kit (JDK) installed now is the time to do so.
Download the latest JDK from the the java.sun.com website:
At the time of writing the latest JDK was:
jdk-7u1-windows-i586.exe
Click through the installer you will need to set an acceptance a license agreement and so forth. By default this will install to:
C:\Program Files\Java\jdk1.7.0\
Note
In this tutorial we refer to file and directory paths as used by Windows. If you are fortunate enough to be using another operating system such as Linux or OSX all of the commands and source code below will work, just modify the paths to suit.
Maven is a widely-used build tool which works by describing the contents of a project. This is a different approach than that used by the Make or Ant tools which list the steps required to build.
It takes a while to get used to Maven and, for some, it remains a love-hate relationship, but it definitely makes working with GeoTools much easier:
Download Maven from http://maven.apache.org/download.html
In this tutorial we refer to Maven version 2.2.1 which is the one used by many GeoTools developers and users, at the time of writing. You can also use Maven version 3 but we don’t have as much experience using that version with GeoTools yet.
Unzip the file apache-maven-2.2.1-bin.zip to C:javaapache-maven-2.2.1
You need to have a couple of environmental variables set for maven to work. Use Control Panel ‣ System ‣ Advanced ‣ Environmental Variables to set the following.
Open up a commands prompt Accessories ‣ Command Prompt
Type the following command to confirm you are set up correctly:
C:java> mvn --version
This should produce something similar to the following output
We can now create our project with:
C:>cd C:\java
C:java> mvn archetype:create -DgroupId=org.geotools -DartifactId=tutorial
The above command creates the following files and directories:
tutorial
tutorial\pom.xml
tutorial\src
tutorial\src\main
tutorial\src\main\java
tutorial\src\main\java\org
tutorial\src\main\java\org\geotools
tutorial\src\main\java\org\geotools\App.java
tutorial\src\test
tutorial\src\test\java
tutorial\src\test\java\org
tutorial\src\test\java\org\geotools
tutorial\src\test\java\org\geotools\AppTest.java
App.java and AppTest.java are just placeholder files not used in this tutorial.
During the build process your local maven repository will be used to store both downloaded jars, and those you build locally.
Your local Maven repository is located in your home folder.
PLATFORM
LOCAL REPOSITORY
Windows XP:
C:\Documents and Settings\Jody\.m2\repository
Windows:
C:\Users\Jody.m2\repository
Linux and Mac:
~/.m2/repository
Open the pom.xml file in your favourite text editor. If your editor has an XML syntax mode switch into that now because it will make it a lot easier to find errors such as mis-matched brackets. Some editors, such as vim, will do this automatically on loading the file.
We are going to start by defining the version number of GeoTools we wish to use. This workbook was written for 10-SNAPSHOT although you may wish to try a newer version, or make use of a nightly build by using 8-SNAPSHOT.
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<geotools.version>10-SNAPSHOT</geotools.version>
</properties>
We specify the following dependencies (GeoTools modules which your application will need):
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-shapefile</artifactId>
<version>${geotools.version}</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-swing</artifactId>
<version>${geotools.version}</version>
</dependency>
</dependencies>
Finally, we tell maven which repositories to download jars from:
<repositories>
<repository>
<id>maven2-repository.dev.java.net</id>
<name>Java.net repository</name>
<url>http://download.java.net/maven/2</url>
</repository>
<repository>
<id>osgeo</id>
<name>Open Source Geospatial Foundation Repository</name>
<url>http://download.osgeo.org/webdav/geotools/</url>
</repository>
</repositories>
Return to the command line and get maven to download the required jars for your project with this command:
C:\java\example> mvn install
If maven has trouble downloading any jar, you can try again by selecting Project ‣ Update All Maven Dependencies.
If it really cannot connect you can switch to edit the geotools.version property in your pom.xml to 10-SNAPSHOT (GeoTools development version) and then add a reference to the snapshot repository as shown below:
<repositories>
<repository>
<id>maven2-repository.dev.java.net</id>
<name>Java.net repository</name>
<url>http://download.java.net/maven/2</url>
</repository>
<repository>
<id>osgeo</id>
<name>Open Source Geospatial Foundation Repository</name>
<url>http://download.osgeo.org/webdav/geotools/</url>
</repository>
<repository>
<snapshots>
<enabled>true</enabled>
</snapshots>
<id>opengeo</id>
<name>OpenGeo Maven Repository</name>
<url>http://repo.opengeo.org</url>
</repository>
</repositories>
Now we are ready to create the application.
package org.geotools.tutorial.quickstart; import java.io.File; import org.geotools.data.FileDataStore; import org.geotools.data.FileDataStoreFinder; import org.geotools.data.simple.SimpleFeatureSource; 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; /** * Prompts the user for a shapefile and displays the contents on the screen in a map frame. * <p> * This is the GeoTools Quickstart application used in documentationa and tutorials. * */ public class Quickstart { /** * GeoTools Quickstart demo application. Prompts the user for a shapefile and displays its * contents on the screen in a map frame */ public static void main(String[] args) throws Exception { // display a data store file chooser dialog for shapefiles File file = JFileDataStoreChooser.showOpenFile("shp", null); if (file == null) { return; } FileDataStore store = FileDataStoreFinder.getDataStore(file); SimpleFeatureSource featureSource = store.getFeatureSource(); // Create a map content and add our shapefile to it MapContent map = new MapContent(); map.setTitle("Quickstart"); Style style = SLD.createSimpleStyle(featureSource.getSchema()); Layer layer = new FeatureLayer(featureSource, style); map.addLayer(layer); // Now display the map JMapFrame.showMap(map); } }
Go back to the top project directory (the one that contains your pom.xml file) and build the application with the command:
mvn clean install
If you need some shapefiles to work with you will find a selection of data at the http://www.naturalearthdata.com/ project which is supported by the North American Cartographic Information Society.
Unzip the above data into a location you can find easily such as the desktop.
You can run the application using Maven on the command line:
mvn exec:java -Dexec.mainClass=org.geotools.tutorial.Quickstart
The application will connect to your shapefile, produce a map context, and display the shapefile.
A couple of things to note about the code example:
Try out the different sample data sets.
You can zoom in, zoom out and show the full extent and use the info tool to examine individual countries in the sample countries.shp file.
Download the largest shapefile you can find and see how quickly it can be rendered. You should find that the very first time it will take a while as a spatial index is generated. After that rendering will become much faster.
Fast: We know that one of the ways people select a spatial library is based on speed. By design GeoTools does not load the above shapefile into memory (instead it streams it off of disk each time it is drawn using a spatial index to only bring the content required for display).
If you would like to ask GeoTools to cache the shapefile in memory try the following code:
/**
* This method demonstrates using a memory-based cache to speed up the display (e.g. when
* zooming in and out).
*
* There is just one line extra compared to the main method, where we create an instance of
* CachingFeatureStore.
*/
public static void main(String[] args) throws Exception {
// display a data store file chooser dialog for shapefiles
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());
Layer layer = new FeatureLayer(cache, style);
map.addLayer(layer);
// Now display the map
JMapFrame.showMap(map);
}
You will also need to add this import statement:
import org.geotools.data.CachingFeatureSource;
Hint
When working in a text editor instead of an IDE use the GeoTools javadocs to work out what import statements are required in your source. The javadocs also list the GeoTools module in which each class is found.
File file = JFileDataStoreChooser.showOpenFile("shp", null);
Map<String,Object> params = new HashMap<String,Object>();
params.put( "url", file.toURI().toURL() );
params.put( "create spatial index", false );
params.put( "memory mapped buffer", false );
params.put( "charset", "ISO-8859-1" );
DataStore store = DataStoreFinder.getDataStore( params );
SimpleFeatureSource featureSource = store.getFeatureSource( store.getTypeNames()[0] );
Important: GeoTools is an active open source project - you can quickly use maven to try out the latest nightly build by changing your pom.xml file to use a “SNAPSHOT” release.
At the time of writing 10-SNAPSHOT is under active development.
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- use the latest snapshot -->
<geotools.version>10-SNAPSHOT</geotools.version>
</properties>
You will also need to change your pom.xml file to include the following snapshot repository:
<repositories>
<repository>
<id>maven2-repository.dev.java.net</id>
<name>Java.net repository</name>
<url>http://download.java.net/maven/2</url>
</repository>
<repository>
<id>osgeo</id>
<name>Open Source Geospatial Foundation Repository</name>
<url>http://download.osgeo.org/webdav/geotools/</url>
</repository>
<repository>
<snapshots>
<enabled>true</enabled>
</snapshots>
<id>opengeo</id>
<name>OpenGeo Maven Repository</name>
<url>http://repo.opengeo.org</url>
</repository>
</repositories>
So what jars did maven actually use for the Quickstart application? Try the following on the command line:
mvn dependency:tree
We will be making use of some of the project in greater depth in the remaining tutorials.